katema/laravel-genai
Composer 安装命令:
composer require katema/laravel-genai
包简介
Drop-in Generative AI for Laravel applications - opinionated, extensible, and production-ready
README 文档
README
Drop-in Generative AI for Laravel applications - opinionated, extensible, and production-ready.
Features
- 🎯 Simple API - Clean, Laravel-native syntax
- 🔌 Provider Agnostic - OpenAI, Claude, Gemini, Ollama, or custom
- 📝 Prompt Management - Version control your prompts
- 💾 Context Management - Maintain conversation context
- 🎨 Model Integration - Add AI to Eloquent models with traits
- 📊 Cost Tracking - Monitor tokens and costs
- 🔒 Production Ready - Rate limiting, validation, error handling
- 🚀 Queue Support - Async AI operations
Installation
composer require katema/laravel-genai php artisan genai:install
Set your API key in .env:
GENAI_PROVIDER=openai OPENAI_API_KEY=your-api-key-here OPENAI_MODEL=gpt-4-turbo-preview
Quick Start
Basic Text Generation
use Katema\LaravelGenAI\Facades\AI; $response = AI::text('Write a product description for a coffee mug'); echo $response->content;
Chat Conversations
$messages = [ ['role' => 'user', 'content' => 'What is Laravel?'], ]; $response = AI::chat($messages); echo $response->content;
Structured JSON Output
$data = AI::json('Generate a user profile with name, email, and bio'); // Returns: ['name' => 'John Doe', 'email' => 'john@example.com', 'bio' => '...']
With Context
$response = AI::withContext([ 'user' => auth()->user()->name, 'company' => 'Acme Corp' ])->text('Write a welcome email');
Prompt Management
Create reusable prompts in resources/prompts/:
prompts/marketing/product_description.md:
Generate a compelling product description for: **Product:** {{product_name}} **Category:** {{category}} **Features:** {{features}} Make it persuasive and highlight benefits.
Use it in your code:
$response = AI::prompt('marketing.product_description', [ 'product_name' => 'Smart Coffee Maker', 'category' => 'Kitchen Appliances', 'features' => 'WiFi enabled, programmable, auto-shutoff' ]);
Model Integration
Add AI capabilities to your Eloquent models:
use Katema\LaravelGenAI\Traits\HasAI; class Product extends Model { use HasAI; }
Then use it:
$product = Product::find(1); // Generate a summary $summary = $product->summarize(); // Generate a description $description = $product->describe(); // Ask questions about the model $insights = $product->insights('What makes this product unique?');
Multiple Providers
Switch between providers easily:
// Use OpenAI $response = AI::driver('openai')->text('Hello'); // Use Claude $response = AI::driver('claude')->text('Hello');
Configure providers in config/genai.php:
'providers' => [ 'openai' => [ 'api_key' => env('OPENAI_API_KEY'), 'model' => env('OPENAI_MODEL', 'gpt-4-turbo-preview'), ], 'claude' => [ 'api_key' => env('CLAUDE_API_KEY'), 'model' => env('CLAUDE_MODEL', 'claude-3-5-sonnet-20241022'), ], ],
Advanced Usage
System Prompts
$response = AI::withSystemPrompt('You are a helpful marketing assistant') ->chat($messages);
Custom Options
$response = AI::text('Write a story', [ 'temperature' => 0.9, 'max_tokens' => 500, 'model' => 'gpt-4' ]);
Fresh Context
AI::withContext(['user' => 'Alice']) ->text('First request'); // Clear context for next request AI::fresh()->text('Second request');
Cost Tracking
All responses include cost and token information:
$response = AI::text('Hello'); echo $response->tokensUsed; // 150 echo $response->cost; // 0.0045 echo $response->model; // gpt-4-turbo-preview echo $response->provider; // openai
Configuration
Publish the config file:
php artisan vendor:publish --tag=genai-config
Key configuration options:
// Default provider 'default' => env('GENAI_PROVIDER', 'openai'), // Rate limiting 'rate_limits' => [ 'enabled' => true, 'max_requests_per_minute' => 60, 'max_tokens_per_day' => 100000, ], // Safety 'safety' => [ 'prompt_injection_detection' => true, 'output_validation' => true, 'max_prompt_length' => 10000, ],
Artisan Commands
# Install the package php artisan genai:install # Create a new prompt template php artisan genai:prompt marketing.email # Test a prompt php artisan genai:test marketing.email --var="product=Coffee"
Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
Security
If you discover any security issues, please email security@katema.dev instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
Credits
Built with ❤️ for the Laravel community
katema/laravel-genai 适用场景与选型建议
katema/laravel-genai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「ai」 「Gemini」 「openai」 「llm」 「claude」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 katema/laravel-genai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 katema/laravel-genai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 katema/laravel-genai 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests.
Estimate AI API costs before making expensive calls
A powerful package that seamlessly integrates OpenAI's advanced AI capabilities into your Laravel applications. This package offers quick setup and intuitive configuration to leverage AI models for chat, embeddings, and more.
AI Agent templates for Laravel development - Skills, Agents, and Workflows for enhanced coding assistance
AI discussion summaries for Flarum with real-time streaming.
Alfabank REST API integration
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-27