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 Cohere Platform Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.

  2. 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
    
  3. 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?');
    

Implementation Patterns

Core Workflows

1. Chat API Integration

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(),
        ]);
    }
}

2. Embeddings for Semantic Search

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)
);

3. Audio Transcription

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(),
        ]);
    }
}

4. Reranking for Search Refinement

Pattern: Improve search relevance by reranking results.

$reranked = Cohere::rerank()
    ->setQuery('best Laravel packages')
    ->setDocuments($searchResults)
    ->generate();

$topResults = array_slice($reranked->getResults(), 0, 5);

Laravel-Specific Patterns

1. Service Container Binding

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;
});

2. Queue Integration

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
    }
}

3. Configuration Management

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);

4. Event Listeners

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,
    ],
];

Gotchas and Tips

Pitfalls

1. Symfony AI Dependency Conflicts

  • Issue: Laravel’s illuminate/http-client may conflict with Symfony’s http-client.
  • Fix: Explicitly bind Symfony’s client in Laravel’s container:
    $this->app->singleton(\Symfony\Contracts\HttpClient\HttpClientInterface::class, function () {
        return \Symfony\Component\HttpClient\HttpClient::create();
    });
    

2. Rate Limit Exhaustion

  • Issue: Cohere’s rate limits (e.g., 800 requests/minute for Chat) can be hit during bursts.
  • Fix:
    • Use Laravel Queues with retryAfter:
      class CohereJob implements ShouldQueue {
          protected $retryAfter = 60; // seconds
      }
      
    • Implement exponential backoff in middleware:
      $client->setMiddleware(function ($next) {
          return function ($request) use
      
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