Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Prompt Manager Laravel Package

prismaticoder/laravel-prompt-manager

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require prismaticoder/laravel-prompt-manager
    php artisan vendor:publish --provider="Prismaticoder\PromptManager\PromptManagerServiceProvider" --tag="migrations"
    php artisan migrate
    
  2. Define a Prompt Create a prompt template in resources/views/prompts/your-prompt.blade.php:

    {{ $userInput }}
    {{ $systemInstructions }}
    
  3. First Use Case Register and use the prompt in a controller:

    use Prismaticoder\PromptManager\Facades\PromptManager;
    
    public function generateResponse()
    {
        $prompt = PromptManager::prompt('your-prompt')
            ->withVariables([
                'userInput' => 'Hello AI',
                'systemInstructions' => 'Be concise.'
            ])
            ->get();
    
        return $prompt->execute();
    }
    

Where to Look First

  • Migrations: Check database/migrations/ for the prompt_versions table structure.
  • Config: config/prompt-manager.php for default settings (e.g., default_model, token_estimation).
  • Facade: app/Facades/PromptManager.php for quick usage examples.

Implementation Patterns

Core Workflows

  1. Prompt Definition & Versioning

    // Define a new prompt (auto-versions on save)
    PromptManager::prompt('greeting')
        ->setTemplate('resources/views/prompts/greeting.blade.php')
        ->setDescription('Handles user greetings')
        ->save();
    
    // Update a prompt (creates a new version)
    PromptManager::prompt('greeting')
        ->setTemplate('updated-template.blade.php')
        ->save();
    
  2. Dynamic Variable Injection

    $response = PromptManager::prompt('analysis')
        ->withVariables([
            'data' => $userData,
            'tone' => 'professional',
        ])
        ->execute(); // Uses configured LLM (e.g., OpenAI)
    
  3. Model-Specific Prompts

    // Tag a prompt for a specific model
    PromptManager::prompt('gpt4-only')
        ->addModelTag('gpt-4')
        ->save();
    
    // Select version based on model context
    $version = PromptManager::prompt('gpt4-only')
        ->forModel('gpt-4')
        ->get();
    
  4. A/B Testing

    // Register variants
    PromptManager::prompt('ab-test')
        ->addVariant('variant_a', 'template-a.blade.php')
        ->addVariant('variant_b', 'template-b.blade.php')
        ->save();
    
    // Serve randomly or by condition
    $variant = PromptManager::prompt('ab-test')
        ->selectVariant('random')
        ->get();
    

Integration Tips

  • Service Providers: Bind the PromptManager to your container for dependency injection:
    $this->app->bind('promptManager', function ($app) {
        return new \Prismaticoder\PromptManager\PromptManager(
            $app['config']['prompt-manager']
        );
    });
    
  • Event Listeners: Listen for PromptSaved or PromptVersionCreated to log/notify:
    public function handle(PromptVersionCreated $event)
    {
        Log::info("New prompt version created: {$event->version->name}");
    }
    
  • Testing: Use the PromptManager facade in tests to mock responses:
    PromptManager::shouldReceive('execute')
        ->once()
        ->andReturn('Mocked response');
    

Gotchas and Tips

Pitfalls

  1. Template Paths

    • Issue: Blade templates must be in resources/views/prompts/ or the view_path config will fail.
    • Fix: Verify config/prompt-manager.php has the correct view_path.
  2. Variable Validation

    • Issue: Undefined variables in templates cause silent failures (no error thrown).
    • Fix: Use ->withRequiredVariables(['key' => 'default']) to enforce presence.
  3. Model Context Overrides

    • Issue: Forgetting to specify a model for tagged prompts may fall back to default_model, which could be incorrect.
    • Fix: Always use ->forModel('model-name') when model-specific logic exists.
  4. Token Estimation

    • Issue: Custom token counting may misalign with LLM expectations (e.g., OpenAI vs. Anthropic).
    • Fix: Override the tokenEstimator in config or extend the TokenEstimator class.

Debugging

  • Prompt Rendering: Use ->debug() to dump the rendered template:
    $prompt = PromptManager::prompt('debug-me')->debug();
    
  • Version History: Check the prompt_versions table directly or use:
    PromptManager::prompt('history')->versions()->get();
    
  • LLM Errors: Wrap executions in try-catch to log LLM-specific errors (e.g., rate limits):
    try {
        $response = $prompt->execute();
    } catch (\Prismaticoder\PromptManager\Exceptions\LLMException $e) {
        Log::error($e->getMessage());
    }
    

Extension Points

  1. Custom Storage Override the PromptRepository to use a different storage backend (e.g., Redis):

    $promptRepo = new \Prismaticoder\PromptManager\Repositories\RedisPromptRepository();
    PromptManager::setRepository($promptRepo);
    
  2. Prompt Validators Add validation logic before execution:

    PromptManager::prompt('secure')
        ->addValidator(function ($prompt) {
            return strlen($prompt->template) < 1000; // Reject long prompts
        });
    
  3. Event Extensions Dispatch custom events for prompt lifecycle hooks:

    // In PromptManagerServiceProvider
    Event::listen('prompt.before_execute', function ($prompt) {
        // Pre-execution logic
    });
    
  4. Token Calculators Replace the default token estimator for custom models:

    $estimator = new \Prismaticoder\PromptManager\Services\CustomTokenEstimator();
    PromptManager::setTokenEstimator($estimator);
    

Config Quirks

  • Default Model: Ensure config/prompt-manager.php has default_model set to your primary LLM (e.g., 'gpt-3.5-turbo').
  • Caching: Disable cache_enabled in config if testing frequently (versions won’t persist).
  • Environment Variables: Use .env for sensitive LLM API keys (e.g., OPENAI_API_KEY).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium