saqqal/llm-integration-bundle

Symfony 扩展包,用于通过API提供商将大型语言模型(LLM)集成到应用程序中,支持API Together和OpenAI。

安装: 6

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.0 2024-09-22 20:08 UTC

This package is auto-updated.

Last update: 2024-09-22 21:51:12 UTC


README

License: MIT PHP Version Symfony Version

LLMIntegrationBundle 是一个强大的 Symfony 扩展包,可以无缝地将大型语言模型(LLM)集成到您的 Symfony 应用程序中。它支持多个 AI 提供商和灵活的架构,易于扩展和定制。

📚 目录

✨ 特性

  • 🌐 支持多个 AI 提供商
  • ⚙️ 灵活配置
  • 🛡️ 使用自定义异常进行异常处理
  • 🖥️ CLI 集成用于生成新的 AI 服务类
  • 🧩 可扩展的架构
  • 🧪 全面单元测试

📦 安装

使用 Composer 安装扩展包

composer require saqqal/llm-integration-bundle

🛠️ 配置

  1. config/bundles.php 中注册扩展包
<?php
return [
    // ...
    Saqqal\LlmIntegrationBundle\LlmIntegrationBundle::class => ['all' => true],
];
  1. 创建 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'
  1. 在您的 .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 客户端

  1. API Together (ApiTogetherClient)
  2. OpenAI (OpenAiClient)
  3. Anthropic (AnthropicClient)
  4. Arliai (ArliaiClient)
  5. Deepinfra (DeepinfraClient)
  6. Groq (GroqClient)
  7. HuggingFace (HuggingFaceClient)
  8. Mistral (MistralClient)
  9. OpenRouter (OpenRouterClient)
  10. 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 提供商

  1. 创建一个新的客户端类,该类扩展了 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
        ];
    }
}
  1. 更新您的配置以使用新的提供者
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 - 作品集

🤝 贡献

欢迎贡献!请复制存储库并提交包含您的更改的拉取请求。

📚 文档

有关更详细的文档,请访问我们的维基