talleu/cohere-php-client
Composer 安装命令:
composer require talleu/cohere-php-client
包简介
PHP client to interact with Cohere API
README 文档
README
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
- ✅ File upload and multipart support
- ✅ Developer-friendly DTOs and response builders
- ✅ Easily extendable with your own endpoints
Requirements ⚙️
- PHP 8.2 or higher
- Composer
- A PSR-18 compatible HTTP client ( e.g. Guzzle, Symfony HttpClient, HTTPlug clients)
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
Or another client implementing PSR18
Basic Usage 🎯
Minimal example
use Talleu\CohereClient\Cohere; $client = Cohere::client('your-api-key'); // Call the embed endpoint $embeds = $client->embed()->create([ 'Cohere is amazing!', 'Let’s try embedding some text.' ]); var_dump($embeds);
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 |
v1/datasets |
Dataset |
Create a dataset by uploading a file, retrieve datasets |
v1/finetuning |
FineTuning |
Trains, deploy, list datasets |
You can access them via:
$client->embed(); $client->chat(); $client->tokenize(); $client->detokenize(); $client->classify(); $client->rerank(); $client->dataset(); $client->fineTuning(); $client->model(); $client->embedJob(); $client->connector(); // 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.
To send a request without using provided resources, you can use directly the CohereClient sendRequesst() method
$chat = Cohere::client()->sendRequest('GET', '/v2/chat', $body);
📚 Documentation by endpoints
- Embedding
- Chat Completion
- Rerank
- Model
- Connector
- Classify
- Fine Tuning
- Dataset
- Embed jobs
- Tokenize
- Detokenize
- A tutorial for Fine-tune and Classify a dataset
Documentation 📚
- 🧠 Cohere official docs: https://docs.cohere.com
- 📘 API Reference: https://docs.cohere.com/reference
- 📦 This PHP client wraps the API endpoints in a friendly OO API with typed DTOs.
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.
talleu/cohere-php-client 适用场景与选型建议
talleu/cohere-php-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 talleu/cohere-php-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 talleu/cohere-php-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 77
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-27