symfony/ai-cohere-platform
Symfony AI bridge for Cohere Platform, providing integrations for Cohere Chat, Embeddings, Rerank, and audio transcription. Use Cohere models through Symfony AI with a dedicated platform connector and shared tooling from the main Symfony AI repository.
Install Dependencies
Add to composer.json:
"require": {
"symfony/ai": "^0.8.0",
"symfony/ai-cohere-platform": "^0.8.0",
"symfony/http-client": "^6.0"
}
Run composer install.
Publish Configuration
Create a service provider (e.g., app/Providers/CohereServiceProvider.php):
use Symfony\Component\AI\Client\CohereClient;
use Symfony\Component\AI\Cohere\CohereBridge;
public function register()
{
$this->app->singleton(CohereClient::class, function ($app) {
return new CohereClient(
new CohereBridge(),
$app['config']['services.cohere.api_key']
);
});
}
public function boot()
{
$this->publishes([
__DIR__.'/config/cohere.php' => config_path('services/cohere.php'),
], 'cohere-config');
}
Publish config:
php artisan vendor:publish --provider="App\Providers\CohereServiceProvider"
Add to .env:
COHERE_API_KEY=your_api_key_here
First Use Case: Chat API
Create a facade (e.g., app/Facades/Cohere.php):
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Cohere extends Facade
{
protected static function getFacadeAccessor()
{
return 'cohere.client';
}
}
Bind in CohereServiceProvider:
$this->app->bind('cohere.client', function ($app) {
return $app->make(CohereClient::class);
});
Test in Tinker:
php artisan tinker
Cohere::chat()->generate('Hello, how are you?');
Pattern: Use for conversational interfaces (chatbots, assistants).
// Synchronous call
$response = Cohere::chat()
->setModel('command-light')
->setMessage('Summarize this: ' . $longText)
->generate();
// Async with queues
GenerateChatResponse::dispatch($prompt, $userId)
->onQueue('cohere-chat');
Job Example:
class GenerateChatResponse implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
public function handle()
{
$response = Cohere::chat()
->setMessage($this->prompt)
->generate();
ChatResponse::create([
'user_id' => $this->userId,
'response' => $response->getContent(),
]);
}
}
Pattern: Generate vectors for search/recommendations.
// Batch embeddings with caching
$embeddings = Cache::remember(
"embeddings:{$documentId}",
now()->addHours(1),
fn() => Cohere::embed()
->setModel('embed-english-v3.0')
->generate($document->text)
);
Integration with Vector DB:
$client = new MeilisearchClient('http://localhost:7700');
$client->index('documents')->addDocuments(
array_map(fn($doc) => [
'id' => $doc->id,
'embedding' => $embeddings[$doc->id],
'content' => $doc->text,
], $documents)
);
Pattern: Process voice inputs (e.g., call centers, podcasts).
// Async transcription job
TranscribeAudio::dispatch($audioFilePath, $userId)
->onQueue('cohere-audio');
Job Example:
class TranscribeAudio implements ShouldQueue
{
public function handle()
{
$response = Cohere::transcribe()
->setFile($this->audioFilePath)
->generate();
Transcript::create([
'user_id' => $this->userId,
'text' => $response->getText(),
]);
}
}
Pattern: Improve search relevance by reranking results.
$reranked = Cohere::rerank()
->setQuery('best Laravel packages')
->setDocuments($searchResults)
->generate();
$topResults = array_slice($reranked->getResults(), 0, 5);
Extend Symfony’s client with Laravel-specific bindings:
$this->app->bind('cohere.client', function ($app) {
$client = new CohereClient(
new CohereBridge(),
$app['config']['services.cohere.api_key']
);
// Add Laravel-specific middleware
$client->setMiddleware(function ($next) use ($app) {
return function ($request) use ($next, $app) {
$request = $app['request']->merge([
'headers' => [
'X-Request-ID' => $app['request']->header('X-Request-ID'),
],
]);
return $next($request);
};
});
return $client;
});
Template for Async Jobs:
abstract class CohereJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
protected $retryAfter = 60; // Cohere rate limit
public function retryUntil()
{
return now()->addMinutes($this->retryAfter);
}
}
// Example: EmbeddingsJob
class GenerateEmbeddingsJob extends CohereJob
{
public function handle()
{
$embeddings = Cohere::embed()->generate($this->text);
// Store in DB/cache
}
}
Publish and Merge Config:
// config/cohere.php
return [
'api_key' => env('COHERE_API_KEY'),
'default_model' => [
'chat' => 'command-light',
'embed' => 'embed-english-v3.0',
'transcribe' => 'transcribe-base',
],
'rate_limits' => [
'chat' => 800, // requests/minute
'embed' => 1000,
],
];
Access in Code:
$model = config('services.cohere.default_model.chat');
$response = Cohere::chat()->setModel($model)->generate($prompt);
Log API Calls:
use Symfony\Component\AI\Event\AiRequestEvent;
use Symfony\Component\AI\Event\AiResponseEvent;
class LogCohereEvents
{
public function onRequest(AiRequestEvent $event)
{
Log::debug('Cohere API Request', [
'endpoint' => $event->getEndpoint(),
'payload' => $event->getPayload(),
]);
}
public function onResponse(AiResponseEvent $event)
{
Log::debug('Cohere API Response', [
'status' => $event->getStatusCode(),
'content' => $event->getContent(),
]);
}
}
Register in EventServiceProvider:
protected $listen = [
AiRequestEvent::class => [
LogCohereEvents::class,
],
AiResponseEvent::class => [
LogCohereEvents::class,
],
];
illuminate/http-client may conflict with Symfony’s http-client.$this->app->singleton(\Symfony\Contracts\HttpClient\HttpClientInterface::class, function () {
return \Symfony\Component\HttpClient\HttpClient::create();
});
retryAfter:
class CohereJob implements ShouldQueue {
protected $retryAfter = 60; // seconds
}
$client->setMiddleware(function ($next) {
return function ($request) use
How can I help you explore Laravel packages today?