saqqal / llm-integration-bundle
Symfony 扩展包,用于通过API提供商将大型语言模型(LLM)集成到应用程序中,支持API Together和OpenAI。
v1.0.0
2024-09-22 20:08 UTC
Requires
- php: ^8.0
- symfony/framework-bundle: ^6.0 || ^7.0
- symfony/http-client: ^6.0 || ^7.0
- symfony/monolog-bundle: ^3.10
- symfony/yaml: ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- phpunit/phpunit: ^9.5
- symfony/test-pack: ^1.1
This package is auto-updated.
Last update: 2024-09-22 21:51:12 UTC
README
LLMIntegrationBundle 是一个强大的 Symfony 扩展包,可以无缝地将大型语言模型(LLM)集成到您的 Symfony 应用程序中。它支持多个 AI 提供商和灵活的架构,易于扩展和定制。
📚 目录
✨ 特性
- 🌐 支持多个 AI 提供商
- ⚙️ 灵活配置
- 🛡️ 使用自定义异常进行异常处理
- 🖥️ CLI 集成用于生成新的 AI 服务类
- 🧩 可扩展的架构
- 🧪 全面单元测试
📦 安装
使用 Composer 安装扩展包
composer require saqqal/llm-integration-bundle
🛠️ 配置
- 在
config/bundles.php
中注册扩展包
<?php return [ // ... Saqqal\LlmIntegrationBundle\LlmIntegrationBundle::class => ['all' => true], ];
- 创建
config/packages/llm_integration.yaml
llm_integration: llm_provider: 'api_together' llm_api_key: '%env(LLM_PROVIDER_API_KEY)%' llm_model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'
- 在您的
.env
文件中设置 API 密钥
LLM_PROVIDER_API_KEY=your_api_key_here
🚀 使用
注入 AI 服务
将 AiServiceInterface
注入到您的服务或控制器中
use Saqqal\LlmIntegrationBundle\Interface\AiServiceInterface; class YourService { private AiServiceInterface $aiService; public function __construct(AiServiceInterface $aiService) { $this->aiService = $aiService; } // ... }
生成响应
使用 generate
方法发送提示并接收响应
public function generateResponse(string $prompt): string { $response = $this->aiService->generate($prompt); return $response->getData()['content']; }
更改输出类型
您可以将输出类型更改为 DynamicAiResponse
以获得对 API 响应的更灵活访问
public function generateDynamicResponse(string $prompt): mixed { $response = $this->aiService->generate($prompt, [], true); return $response->choices[0]->message->content; }
🤝 可用的 AI 客户端
LLMIntegrationBundle 支持以下 AI 客户端
- API Together (
ApiTogetherClient
) - OpenAI (
OpenAiClient
) - Anthropic (
AnthropicClient
) - Arliai (
ArliaiClient
) - Deepinfra (
DeepinfraClient
) - Groq (
GroqClient
) - HuggingFace (
HuggingFaceClient
) - Mistral (
MistralClient
) - OpenRouter (
OpenRouterClient
) - Tavily (
TavilyClient
)
要使用特定的客户端,请将您的配置中的 llm_provider
设置为相应的提供者名称。
💻 CLI 命令
生成新的 AI 服务类
php bin/console llm:create-ai-service
按照提示输入提供者名称和 API 端点。
列出可用的 AI 客户端
php bin/console llm:list-ai-services
此命令将列出所有标记为 @AiClient
的可用 AI 客户端。
🔧 扩展扩展包
要添加新的 AI 提供商
- 创建一个新的客户端类,该类扩展了
AbstractAiClient
use Saqqal\LlmIntegrationBundle\Attribute\AiClient; use Saqqal\LlmIntegrationBundle\Client\AbstractAiClient; #[AiClient('your_provider')] class YourProviderClient extends AbstractAiClient { protected function getApiUrl(): string { return 'https://api.yourprovider.com/v1/chat/completions'; } protected function getAdditionalRequestData(string $prompt, ?string $model): array { return [ // Add provider-specific options here ]; } }
- 更新您的配置以使用新的提供者
llm_integration: llm_provider: 'your_provider' llm_api_key: '%env(YOUR_PROVIDER_API_KEY)%' llm_model: 'your-default-model'
🚦 异常处理
创建一个事件订阅者来处理 LlmIntegrationExceptionEvent
use Saqqal\LlmIntegrationBundle\Event\LlmIntegrationExceptionEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class LlmIntegrationExceptionSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ LlmIntegrationExceptionEvent::class => 'onLlmIntegrationException', ]; } public function onLlmIntegrationException(LlmIntegrationExceptionEvent $event): void { $exception = $event->getException(); // Handle the exception } }
🧪 测试
运行测试套件
./vendor/bin/phpunit
📄 许可
此扩展包在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE 文件。
👨💻 作者
Abdelaziz Saqqal - LinkedIn - 作品集
🤝 贡献
欢迎贡献!请复制存储库并提交包含您的更改的拉取请求。
📚 文档
有关更详细的文档,请访问我们的维基。