Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ai Open Ai Platform Laravel Package

symfony/ai-open-ai-platform

Symfony integration for OpenAI Platform APIs, providing ready-to-use clients and tooling for text and chat generation, embeddings, and related AI features. Designed to fit Symfony apps with clean configuration and predictable HTTP handling.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package:
    composer require symfony/ai-open-ai-platform
    
  2. Configure API key in .env:
    AI_OPEN_AI_KEY="your-openai-api-key"
    
  3. Basic usage in a controller:
    use Symfony\AI\OpenAI\Client\ChatCompletionClientInterface;
    
    class ChatController extends AbstractController
    {
        public function __construct(
            private ChatCompletionClientInterface $chatClient
        ) {}
    
        public function ask(ChatMessage $message): Response
        {
            $response = $this->chatClient->complete(
                'gpt-4',
                [$message]
            );
            return $this->json($response->getContent());
        }
    }
    

First Use Case: Chatbot Endpoint

// src/Controller/AiChatController.php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AiChatController extends AbstractController
{
    public function __invoke(
        Request $request,
        ChatCompletionClientInterface $chatClient
    ): Response {
        $prompt = $request->request->get('prompt');
        $response = $chatClient->complete(
            'gpt-3.5-turbo',
            [new ChatMessage(ChatRole::User, $prompt)]
        );
        return $this->json(['reply' => $response->getContent()]);
    }
}

Route it:

# config/routes.yaml
ai_chat:
    path: /ai/chat
    controller: Symfony\Component\HttpKernel\HttpCache\AiChatController
    methods: POST

Implementation Patterns

Core Workflows

  1. Chat Completions

    // Single turn
    $response = $chatClient->complete(
        'gpt-4',
        [new ChatMessage(ChatRole::User, 'Explain quantum computing')]
    );
    
    // Multi-turn conversation
    $messages = [
        new ChatMessage(ChatRole::System, 'You are a helpful assistant'),
        new ChatMessage(ChatRole::User, 'What is PHP?'),
        new ChatMessage(ChatRole::Assistant, 'PHP is a server-side scripting language...'),
        new ChatMessage(ChatRole::User, 'Give me an example')
    ];
    $response = $chatClient->complete('gpt-3.5-turbo', $messages);
    
  2. Streaming Responses

    use Symfony\AI\OpenAI\Client\StreamingChatCompletionClientInterface;
    
    $stream = $streamingChatClient->stream(
        'gpt-3.5-turbo',
        [new ChatMessage(ChatRole::User, 'Tell me a joke')]
    );
    
    foreach ($stream as $delta) {
        echo $delta->getContent();
    }
    
  3. Embeddings

    $embeddingClient = $container->get(EmbeddingClientInterface::class);
    $embeddings = $embeddingClient->create('text-embedding-ada-002', ['Your text here']);
    

Integration Patterns

  1. Service Layer Abstraction

    // src/Service/AiChatService.php
    class AiChatService
    {
        public function __construct(
            private ChatCompletionClientInterface $chatClient
        ) {}
    
        public function generateResponse(string $prompt): string
        {
            $response = $this->chatClient->complete(
                'gpt-3.5-turbo',
                [new ChatMessage(ChatRole::User, $prompt)]
            );
            return $response->getContent();
        }
    }
    
  2. Event-Driven Architecture

    // Listen to chat completion events
    $eventDispatcher->addListener(
        ChatCompletionEvent::class,
        function (ChatCompletionEvent $event) {
            // Log or process the response
            $this->logger->info('AI Response:', ['content' => $event->getContent()]);
        }
    );
    
  3. Command Bus for Async Tasks

    // src/Command/GenerateEmbeddingsCommand.php
    class GenerateEmbeddingsCommand implements CommandInterface
    {
        public function __construct(
            private EmbeddingClientInterface $embeddingClient
        ) {}
    
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $texts = ['Text 1', 'Text 2'];
            $embeddings = $this->embeddingClient->create('text-embedding-ada-002', $texts);
            $output->writeln('Generated embeddings');
            return Command::SUCCESS;
        }
    }
    

Configuration Patterns

  1. Environment-Specific Config

    # config/packages/ai_open_ai.yaml
    ai_open_ai:
        client:
            api_key: '%env(AI_OPEN_AI_KEY)%'
            base_uri: '%env(AI_OPEN_AI_BASE_URI)%'
            timeout: 30
            retries: 3
            model_routing:
                default: 'gpt-3.5-turbo'
                premium: 'gpt-4'
    
  2. Model Routing

    // Dynamically route models based on user tier
    $model = $user->isPremium() ? 'gpt-4' : 'gpt-3.5-turbo';
    $response = $chatClient->complete($model, $messages);
    

Gotchas and Tips

Common Pitfalls

  1. API Key Management

    • ❌ Hardcoding keys in config files
    • ✅ Use Symfony's parameter_bag or environment variables
    # config/packages/ai_open_ai.yaml
    ai_open_ai:
        client:
            api_key: '%env(AI_OPEN_AI_KEY)%'
    
  2. Rate Limiting

    • OpenAI enforces rate limits
    • Implement exponential backoff:
    use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
    use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
    
    try {
        $response = $httpClient->request('POST', $url);
    } catch (TransportExceptionInterface | ClientExceptionInterface $e) {
        if ($e->getCode() === 429) {
            sleep(2); // Simple backoff
            retry();
        }
    }
    
  3. Token Limits

    • GPT-4 has a 8192 token limit (4097 for chat)
    • Validate input length:
    $tokenCount = $this->countTokens($prompt);
    if ($tokenCount > 4000) {
        throw new \RuntimeException('Prompt too long');
    }
    
  4. Streaming Quirks

    • Streams emit DeltaInterface objects, not raw strings
    • Handle partial content:
    $stream = $streamingChatClient->stream('gpt-3.5-turbo', [$message]);
    $fullResponse = '';
    foreach ($stream as $delta) {
        $fullResponse .= $delta->getContent();
        // Process partial content if needed
    }
    

Debugging Tips

  1. Enable Debug Mode

    ai_open_ai:
        client:
            debug: true
    
    • Logs raw API requests/responses to var/log/dev.log
  2. Inspect API Errors

    try {
        $response = $chatClient->complete('invalid-model', [$message]);
    } catch (ApiErrorException $e) {
        $error = $e->getError();
        // Handle specific OpenAI errors
        if ($error->getType() === 'invalid_request_error') {
            // Model not found, etc.
        }
    }
    
  3. Token Counting

    • Use OpenAI's tokenizer:
    use Symfony\AI\OpenAI\Tokenizer\TokenizerInterface;
    
    $tokenizer = $container->get(TokenizerInterface::class);
    $tokenCount = $tokenizer->countTokens($prompt);
    

Extension Points

  1. Custom Model Clients

    // src/OpenAI/CustomChatClient.php
    class CustomChatClient implements ChatCompletionClientInterface
    {
        private ChatCompletionClientInterface $decorated;
    
        public function __construct(ChatCompletionClientInterface $decorated)
        {
            $this->decorated = $decorated;
        }
    
        public function complete(string $model, array $messages, ?array $options = null): ChatCompletionResponseInterface
        {
            // Add custom logic (e.g., prompt validation)
            if (str_contains($messages[0]->getContent(), 'hack')) {
                throw new \RuntimeException('Blocked prompt');
            }
            return $this->decorated->complete($model, $messages, $options);
        }
    }
    

    Register as service:

    services:
        Symfony\AI\OpenAI\Client\ChatCompletionClientInterface: '@App\OpenAI\CustomChatClient'
    
  2. Response Transformers

    // src/OpenAI/ResponseTransformer.php
    class ResponseTransformer implements Response
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky