|
| 1 | +# Cohere |
| 2 | + |
| 3 | +[Cohere](https://cohere.com) provides cutting-edge language models. These are accessible accessible through OpenAI's SDK via the Compatibility API. This allows easy and portable integration with the Strands Agents SDK using the familiar OpenAI interface. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The Strands Agents SDK provides access to Cohere models through the OpenAI compatiblity layer, configured as an optional dependency. To install, run: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install 'strands-agents[openai]' |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +After installing the `openai` package, you can import and initialize the Strands Agents' OpenAI-compatible provider for Cohere models as follows: |
| 16 | + |
| 17 | +```python |
| 18 | +from strands import Agent |
| 19 | +from strands.models.openai import OpenAIModel |
| 20 | +from strands_tools import calculator |
| 21 | + |
| 22 | +model = OpenAIModel( |
| 23 | + client_args={ |
| 24 | + "api_key": "<COHERE_API_KEY>", |
| 25 | + "base_url": "https://api.cohere.ai/compatibility/v1", # Cohere compatibility endpoint |
| 26 | + }, |
| 27 | + model_id="command-a-03-2025", # or see https://docs.cohere.com/docs/models |
| 28 | + params={ |
| 29 | + "stream_options": None |
| 30 | + } |
| 31 | +) |
| 32 | + |
| 33 | +agent = Agent(model=model, tools=[calculator]) |
| 34 | +agent("What is 2+2?") |
| 35 | +``` |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +### Client Configuration |
| 40 | + |
| 41 | +The `client_args` configure the underlying OpenAI-compatible client. When using Cohere, you must set: |
| 42 | + |
| 43 | +* `api_key`: Your Cohere API key. Get one from the [Cohere Dashboard](https://dashboard.cohere.com). |
| 44 | +* `base_url`: |
| 45 | + * `https://api.cohere.ai/compatibility/v1` |
| 46 | + |
| 47 | +Refer to [OpenAI Python SDK GitHub](https://github.com/openai/openai-python) for full client options. |
| 48 | + |
| 49 | +### Model Configuration |
| 50 | + |
| 51 | +The `model_config` specifies which Cohere model to use and any additional parameters. |
| 52 | + |
| 53 | +| Parameter | Description | Example | Options | |
| 54 | +| ---------- | ------------------------- | ------------------------------------------ | ------------------------------------------------------------------ | |
| 55 | +| `model_id` | Model name | `command-r-plus` | See [Cohere docs](https://docs.cohere.com/docs/models) | |
| 56 | +| `params` | Model-specific parameters | `{"max_tokens": 1000, "temperature": 0.7}` | [API reference](https://docs.cohere.com/docs/compatibility-api) | |
| 57 | + |
| 58 | +## Troubleshooting |
| 59 | + |
| 60 | +### `ModuleNotFoundError: No module named 'openai'` |
| 61 | + |
| 62 | +You must install the `openai` dependency to use this provider: |
| 63 | + |
| 64 | +```bash |
| 65 | +pip install 'strands-agents[openai]' |
| 66 | +``` |
| 67 | + |
| 68 | +### Unexpected model behavior? |
| 69 | + |
| 70 | +Ensure you're using a model ID compatible with Cohere’s Compatibility API (e.g., `command-r-plus`, `command-a-03-2025`, `embed-v4.0`), and your `base_url` is set to `https://api.cohere.ai/compatibility/v1`. |
| 71 | + |
| 72 | +## References |
| 73 | + |
| 74 | +* [Cohere Docs: Using the OpenAI SDK](https://docs.cohere.com/docs/compatibility-api) |
| 75 | +* [Cohere API Reference](https://docs.cohere.com/reference) |
| 76 | +* [OpenAI Python SDK](https://github.com/openai/openai-python) |
| 77 | +* [Strands Agents API](../../../api-reference/models.md) |
0 commit comments