talleu/cohere-php-client

PHP client to interact with Cohere API

v0.2 2025-06-30 20:24 UTC

This package is auto-updated.

Last update: 2025-06-30 20:25:35 UTC


README

Build Status PHPStan Packagist Version GitHub

⚠️ Do not install yet, not tested

Cohere PHP Client

A PHP client to interact with the Cohere® API, designed to be framework-agnostic, simple to use, and fully compatible with PSR-18.

This package provides an easy and structured way to use Cohere's powerful language models — for embeddings, **chat **, classification, tokenization, and more — in any PHP project.

Features 🛠️

  • ✅ Support for Cohere API v1/v2 endpoints (chat, embed, classify, tokenize, etc.)
  • ✅ Compatible with any PSR-18 HTTP Client
  • ✅ Support for Symfony integration (optional bundle)
  • ✅ File upload and multipart support
  • ✅ Developer-friendly DTOs and response builders
  • ✅ Easily extendable with your own endpoints

Requirements ⚙️

You must also install the following PSR-17 factories (required by discovery):

composer require nyholm/psr7
composer require php-http/discovery

Installation 📝

Install the library via Composer:

composer require talleu/cohere-php-client

Then install your preferred HTTP client

Using Symfony HttpClient:

composer require symfony/http-client

Using Guzzle:

composer require guzzlehttp/guzzle

Basic Usage 🎯

Minimal example

use Talleu\CohereClient\Cohere;

$client = Cohere::client('your-api-key'); 

// Call the embed endpoint
$response = $client->embed()->create([
    'texts' => [
        'Cohere is amazing!',
        'Let’s try embedding some text.'
    ]
]);

print_r($response);

Or to simple chats with LLM

    $chat = $client->chat()->create([
        [
            'role' => 'user',
            'content' => 'how are you ?'
        ]
    ]);

Authentication 🔐

You can pass the API key directly in the http client:

Cohere::client('your-api-key');

Or use an environment variable (recommended):

#.env
COHERE_API_KEY=your-api-key

Available Endpoints 📚

The following endpoints are supported:

Endpoint Class Description
v2/embed Embed Generate embeddings from input text
v2/chat Chat Perform conversational chat with a LLM
v1/classify Classify Text classification based on custom labels
v1/tokenize Tokenize Token-level breakdown of input text
v1/detokenize Detokenize De-tokenify tokens to text
v1/connectors Connector Cohere connectors
v1/embed-jobs EmbedJob Async embed jobs
v2/rerank Rerank Retrieve Cohere available models
v1/models Model Produces an ordered array with text

You can access them via:

$client->embed();
$client->chat();
$client->tokenize();
$client->classify();
$client->rerank();
// etc.

Then you can use it simple :

$connector = $client->connector()->create($name, $url, ['model' => 'command-a-03-2025']);

$connector = $client->connector()->get($id);

$connector = $client->connector()->list();

Each endpoint returns a strongly typed DTO with the result of the API call.

Documentation 📚

Contributing 🤝

PRs are welcome! If you’d like to add support for more endpoints, improve tests or add features, feel free to open an issue or submit a PR.