symfony/ai-anthropic-platform
Symfony AI integration for Anthropic’s Claude via the Anthropic Platform. Provides a PHP client and abstractions to send prompts, handle responses, and plug Claude into Symfony apps with a consistent AI interface for chat and text generation.
AI component, providing a standardized interface (ClientInterface) for interacting with Anthropic’s Claude API. This aligns with Symfony’s modular architecture, enabling consistent AI service orchestration across applications.
DeltaInterface) for chatbots or live updates.symfony/ai v1.x dependencies). Backward compatibility is unlikely for older versions.HttpClient). Resolve via Symfony’s HttpClientInterface or container aliases.# config/packages/ai.yaml
symfony_ai:
clients:
anthropic:
factory: ['Symfony\AI\Client\AnthropicClient', ['%env(ANTHROPIC_API_KEY)%']]
AsyncClientInterface (if Anthropic’s API supports it).['role' => 'user', 'content' => '...']).MultiPartResult for multi-turn conversations or DeltaInterface for streaming.Serializer component for payload normalization (e.g., AssistantMessageNormalizer).RateLimiter or RetryStrategy.prompt_caching: true in config).InvalidRequestError) may not map cleanly to Symfony’s AIException. Extend the exception hierarchy:
class AnthropicException extends \Symfony\AI\Exception\AIException {}
CacheInterface.DeltaInterface) require careful resource management to avoid leaks.AIClientInterface supports multi-provider routing.AIClientResolver to switch between Anthropic and OpenAI based on cost/availability.claude-3-haiku). Track usage via:
Monolog with custom handlers.ParameterBag with encrypted environment variables (e.g., symfony/var-exporter).AnthropicClient for isolated tests:
$mockClient = $this->createMock(AnthropicClient::class);
$mockClient->method('chat')->willReturn(new MultiPartResult([...]));
Symfony\Bundle\FrameworkBundle\Test\WebTestCase to test full request cycles.Profiler for request/response metrics.Symfony\Component\OptionsResolver\Exception\InvalidOptionsException for retries).ClientInterface abstraction for Anthropic./generate) with OpenAPI docs.
# config/api_platform/resources.yaml
resources:
App\Dto\GenerateContentDto:
collectionOperations:
generate:
method: 'POST'
controller: App\Controller\AIController::generate
HttpClient).config/services.yaml:
Symfony\AI\Client\AnthropicClient:
arguments:
$apiKey: '%env(ANTHROPIC_API_KEY)%'
$baseUri: '%env(ANTHROPIC_API_BASE_URI)%'
use Symfony\AI\Client\AnthropicClient;
class ContentGenerator {
public function __construct(private AnthropicClient $client) {}
public function generate(string $prompt): string {
$response = $this->client->chat([
'model' => 'claude-3-haiku',
'messages' => [['role' => 'user', 'content' => $prompt]],
]);
return $response->getContent();
}
}
Deprecation component to phase out old code:
How can I help you explore Laravel packages today?