定制 katema/laravel-genai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Version License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 katema/laravel-genai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-27