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.
ChatCompletionClientInterface, EmbeddingClientInterface). This aligns perfectly with Symfony’s dependency injection (DI), configuration-driven architecture, and event-driven patterns. Ideal for:
config/packages/ system, enabling environment-specific configurations (e.g., dev/staging/prod API keys).parameter_bag for secure credential management.HttpClientMock to simulate OpenAI API responses in tests.HttpClient supports connection reuse).max_tokens can cause cost spikes or truncated responses if misconfigured.vault component.token_usage from responses).symfony/ai (v1.0+).ChatMessage entities).guzzlehttp/guzzle)./chat endpoint) using the package.config/test/ to validate configuration.// src/Controller/ChatController.php
use Symfony\AI\OpenAI\Client\ChatCompletionClientInterface;
class ChatController {
public function __construct(private ChatCompletionClientInterface $client) {}
public function __invoke(Request $request): Response {
$response = $this->client->createChatCompletion([
'model' => 'gpt-4',
'messages' => [['role' => 'user', 'content' => $request->request->get('prompt')]],
]);
return new Response($response->getChoices()[0]->getMessage()->getContent());
}
}
ChatCompletionClientInterface).config/packages/ai_open_ai.yaml:
ai_open_ai:
client:
api_key: '%env(AI_OPEN_AI_KEY)%'
base_uri: 'https://api.openai.com/v1'
timeout: 30
retries: 3
models:
default: gpt-4
fallback: gpt-3.5-turbo
symfony/ai (≥v1.0), symfony/http-client, symfony/flex.^ or ~).openai:^1.0) to avoid surprises./completions → /chat/completions).Embedding, AudioTranscription).#[ORM\Entity]
class ChatMessage {
#[ORM\Id, ORM\GeneratedValue]
private ?int $
How can I help you explore Laravel packages today?