prismaticoder/laravel-prompt-manager
Composer 安装命令:
composer require prismaticoder/laravel-prompt-manager
包简介
Laravel package for seamlessly managing, versioning and testing AI prompts when integrating with LLMs.
关键字:
README 文档
README
A Laravel package built for the next generation of LLM engineers — seamlessly manage, version, and test your AI prompts with the same rigor you bring to your code. Designed for developers who want to apply real software engineering principles to prompt engineering and build AI features that scale with confidence.
Table of Contents
Features
- 🔄 Prompt versioning
- 📊 Easy A/B testing with random selection
- 📏 Built-in token counting
- 🎯 Support for model-specific prompt variants
- 🧪 Simple testing and maintenance
- ⚡️ Fluent and intuitive API
Installation
composer require prismaticoder/laravel-prompt-manager
Basic Usage
Generate a new prompt class:
php artisan make:prompt ProductDescriptionPrompt
This will create a new ProductDescriptionPrompt class in the App/Prompts directory
namespace App\Prompts; use Prismaticoder\LaravelPromptManager\BaseLLMPrompt; use Prismaticoder\LaravelPromptManager\PromptVersionManager; class ProductDescriptionPrompt extends BaseLLMPrompt { private string $productName; private array $features; public function __construct(string $productName, array $features) { $this->productName = $productName; $this->features = $features; } protected function versions(): PromptVersionManager { return new PromptVersionManager([ 'v1' => fn() => $this->generateBasicPrompt(), 'v1-creative' => fn() => $this->generateCreativePrompt(), 'v1-technical' => fn() => $this->generateTechnicalPrompt(), ]); } protected function defaultVersion(): string { return 'v1'; } private function generateBasicPrompt(): string { return <<<PROMPT Write a product description for {$this->productName}. Key features: - " . implode("\n- ", $this->features) PROMPT; } }
Generating and Using Prompts
$prompt = ProductDescriptionPrompt::make('Ergonomic Chair', [ 'Adjustable height', 'Lumbar support', 'Memory foam' ]); // Default version $result = $prompt->generate(); echo $result->prompt; // The generated prompt text echo "Version used: {$result->version}" // v1 echo "Token count: {$result->token_count}"; // Specific version $result = $prompt->generate('v1-creative');
Advanced Usage
Model-Specific Versions
Create model-specific prompts and dynamically select versions:
class CustomerSupportPrompt extends BaseLLMPrompt { protected function defaultVersion(): string { return 'v1-gpt3.5'; // Only defaults to this version when no version selector is defined. } protected function versions(): PromptVersionManager { return new PromptVersionManager([ 'v1-gpt3.5' => fn() => $this->basePromptWithConstraints(2000), 'v1-gpt4' => fn() => $this->basePromptWithConstraints(4000), 'v1-claude' => fn() => $this->generateClaudePrompt(), ]); } protected function versionSelector(): Closure { return function(array $versions) { $model = config('services.llm.default_model'); return match($model) { 'gpt-4' => 'v1-gpt4', 'claude' => 'v1-claude', default => 'v1-gpt3.5' }; }; } }
Working with Existing Template Storage
If you already store prompt templates in a database or registry, you can easily integrate them:
class TransactionFraudAnalysisPrompt extends BaseLLMPrompt { private Transaction $transaction; private array $riskMetrics; public function __construct(Transaction $transaction, array $riskMetrics) { $this->transaction = $transaction; $this->riskMetrics = $riskMetrics; } protected function versions(): PromptVersionManager { return new PromptVersionManager([ 'v1' => fn() => $this->getTemplateAndCompile('standard'), 'v2-high-risk' => fn() => $this->getTemplateAndCompile('high_risk'), ]); } private function getTemplateAndCompile(string $templateKey): string { // Fetch template from your existing storage $template = PromptTemplate::findByKey($templateKey); return collect([ 'amount' => $this->transaction->amount, 'merchant' => $this->transaction->merchant_name, 'risk_score' => $this->riskMetrics['score'], ])->reduce( fn(string $prompt, $value, $key) => str_replace("{{$key}}", $value, $prompt), $template ); } protected function versionSelector(): Closure { return fn() => $this->riskMetrics['score'] > 0.7 ? 'v2-high-risk' : 'v1'; } } // Usage example: $prompt = TransactionFraudAnalysisPrompt::make($transaction, $riskMetrics); $result = $prompt->generate(); // Version selected based on risk score
A/B Testing Made Easy
Easily run A/B tests on your prompts by enabling random version selection:
use Prismaticoder\LaravelPromptManager\Enums\VersionSelector; class ProductCopyPrompt extends BaseLLMPrompt { protected function versionSelector(): VersionSelector { return VersionSelector::RANDOM; } protected function defaultVersion(): string { return 'v1-formal'; // Only defaults to this version when no version selector is defined. } protected function versions(): PromptVersionManager { return new PromptVersionManager([ 'v1-formal' => fn() => $this->formalTone(), 'v1-casual' => fn() => $this->casualTone(), 'v1-persuasive' => fn() => $this->persuasiveTone(), ]); } }
Context-Aware Version Selection
You can override the default versionSelector() method to select prompt versions based on relevant context:
class TutorialPrompt extends BaseLLMPrompt { private User $user; public function __construct(User $user) { $this->user = $user; } protected function versionSelector(): Closure { return fn() => match($this->user->expertise_level) { 'beginner' => 'v1-basic', 'intermediate' => 'v1-detailed', 'expert' => 'v1-advanced', default => $this->defaultVersion() }; } }
Custom Token Counting
The default token counting strategy is a simple estimation (text length divided by 4, based on OpenAI's GPT tokenizer approximation). To improve accuracy, you can override it with a custom token counter like Tiktoken:
class ComplexPrompt extends BaseLLMPrompt { protected function tokenCounter(): Closure { return fn(string $prompt) => Tiktoken::count($prompt); } }
Best Practices
- Abstract Complex Prompts: Break down large prompts into smaller methods.
- Semantic Versioning: Use clear naming conventions (e.g.,
v1-gpt4,v1-formal). - Testing: Use built-in random selection for effective A/B testing.
License
The MIT License (MIT). See License File.
prismaticoder/laravel-prompt-manager 适用场景与选型建议
prismaticoder/laravel-prompt-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.4k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「prompt」 「nlp」 「ai」 「laravel-package」 「artificial-intelligence」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 prismaticoder/laravel-prompt-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prismaticoder/laravel-prompt-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 prismaticoder/laravel-prompt-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Reauthenticate users by letting them re-enter their passwords for specific parts of your app.
PHP Interface for Babel Street Text Analytics
Pest plugin to evaluate prompts
A modern PHP client for the Hugging Face Inference API with full type safety and comprehensive model support
Alibaba Cloud Nlp SDK for PHP
A collection of fast and easy to use attributed string classes for PHP. Attributed strings can have multiple attributes for each character of the string and are for example used for word processors and natural language processing.
统计信息
- 总下载量: 5.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-13