prismaticoder/laravel-prompt-manager
Installation
composer require prismaticoder/laravel-prompt-manager
php artisan vendor:publish --provider="Prismaticoder\PromptManager\PromptManagerServiceProvider" --tag="migrations"
php artisan migrate
Define a Prompt
Create a prompt template in resources/views/prompts/your-prompt.blade.php:
{{ $userInput }}
{{ $systemInstructions }}
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();
}
database/migrations/ for the prompt_versions table structure.config/prompt-manager.php for default settings (e.g., default_model, token_estimation).app/Facades/PromptManager.php for quick usage examples.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();
Dynamic Variable Injection
$response = PromptManager::prompt('analysis')
->withVariables([
'data' => $userData,
'tone' => 'professional',
])
->execute(); // Uses configured LLM (e.g., OpenAI)
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();
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();
PromptManager to your container for dependency injection:
$this->app->bind('promptManager', function ($app) {
return new \Prismaticoder\PromptManager\PromptManager(
$app['config']['prompt-manager']
);
});
PromptSaved or PromptVersionCreated to log/notify:
public function handle(PromptVersionCreated $event)
{
Log::info("New prompt version created: {$event->version->name}");
}
PromptManager facade in tests to mock responses:
PromptManager::shouldReceive('execute')
->once()
->andReturn('Mocked response');
Template Paths
resources/views/prompts/ or the view_path config will fail.config/prompt-manager.php has the correct view_path.Variable Validation
->withRequiredVariables(['key' => 'default']) to enforce presence.Model Context Overrides
default_model, which could be incorrect.->forModel('model-name') when model-specific logic exists.Token Estimation
tokenEstimator in config or extend the TokenEstimator class.->debug() to dump the rendered template:
$prompt = PromptManager::prompt('debug-me')->debug();
prompt_versions table directly or use:
PromptManager::prompt('history')->versions()->get();
try {
$response = $prompt->execute();
} catch (\Prismaticoder\PromptManager\Exceptions\LLMException $e) {
Log::error($e->getMessage());
}
Custom Storage
Override the PromptRepository to use a different storage backend (e.g., Redis):
$promptRepo = new \Prismaticoder\PromptManager\Repositories\RedisPromptRepository();
PromptManager::setRepository($promptRepo);
Prompt Validators Add validation logic before execution:
PromptManager::prompt('secure')
->addValidator(function ($prompt) {
return strlen($prompt->template) < 1000; // Reject long prompts
});
Event Extensions Dispatch custom events for prompt lifecycle hooks:
// In PromptManagerServiceProvider
Event::listen('prompt.before_execute', function ($prompt) {
// Pre-execution logic
});
Token Calculators Replace the default token estimator for custom models:
$estimator = new \Prismaticoder\PromptManager\Services\CustomTokenEstimator();
PromptManager::setTokenEstimator($estimator);
config/prompt-manager.php has default_model set to your primary LLM (e.g., 'gpt-3.5-turbo').cache_enabled in config if testing frequently (versions won’t persist)..env for sensitive LLM API keys (e.g., OPENAI_API_KEY).How can I help you explore Laravel packages today?