---
title: "Cohere"
url: "/docs/providers/cohere"
canonical_url: "https://docs.litellm.ai/docs/providers/cohere"
type: "docs"
last_updated: "2026-10-08"
related:
  - "/docs/providers/cognition"
  - "/docs/providers/cometapi"
---
# Cohere

> Index of all LiteLLM docs: https://docs.litellm.ai/llms.txt


## API 金鑰 {#api-keys}

```python
import os 
os.environ["COHERE_API_KEY"] = ""
```

## 使用方式 {#usage}

### LiteLLM Python SDK {#litellm-python-sdk}

#### Cohere v2 API（預設） {#cohere-v2-api-default}

```python showLineNumbers
from litellm import completion

## set ENV variables
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere v2 call
response = completion(
    model="cohere_chat/command-a-03-2025", 
    messages = [{ "content": "Hello, how are you?","role": "user"}]
)
```

#### Cohere v1 API {#cohere-v1-api}

若要使用 Cohere v1/chat API，請在模型名稱前加上 `cohere_chat/v1/`：

```python showLineNumbers
from litellm import completion

## set ENV variables
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere v1 call
response = completion(
    model="cohere_chat/v1/command-a-03-2025", 
    messages = [{ "content": "Hello, how are you?","role": "user"}]
)
```

#### 串流 {#streaming}

**Cohere v2 串流：**

```python showLineNumbers
from litellm import completion

## set ENV variables
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere v2 streaming
response = completion(
    model="cohere_chat/command-a-03-2025", 
    messages = [{ "content": "Hello, how are you?","role": "user"}],
    stream=True
)

for chunk in response:
    print(chunk)
```

**Cohere v1 串流：**

```python showLineNumbers
from litellm import completion

## set ENV variables
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere v1 streaming
response = completion(
    model="cohere_chat/v1/command-a-03-2025", 
    messages = [{ "content": "Hello, how are you?","role": "user"}],
    stream=True
)

for chunk in response:
    print(chunk)
```

## 與 LiteLLM Proxy 搭配使用 {#usage-with-litellm-proxy}

以下是如何透過 LiteLLM Proxy Server 呼叫 Cohere

### 1. 將金鑰儲存在您的環境中 {#1-save-key-in-your-environment}

```bash
export COHERE_API_KEY="your-api-key"
```

### 2. 啟動 proxy {#2-start-the-proxy}

在 config.yaml 中定義您要使用的 cohere 模型

**適用於 Cohere v1 模型：**
```yaml showLineNumbers
model_list:
  - model_name: command-a-03-2025 
    litellm_params:
      model: cohere_chat/v1/command-a-03-2025
      api_key: "os.environ/COHERE_API_KEY"
```

**適用於 Cohere v2 模型：**
```yaml showLineNumbers
model_list:
  - model_name: command-a-03-2025-v2
    litellm_params:
      model: cohere_chat/command-a-03-2025
      api_key: "os.environ/COHERE_API_KEY"
```

```bash
litellm --config /path/to/config.yaml
```

### 3. 測試 {#3-test-it}

**Cohere v1 - Curl 請求**

```shell showLineNumbers
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <your-litellm-api-key>' \
--data ' {
      "model": "command-a-03-2025",
      "messages": [
        {
          "role": "user",
          "content": "what llm are you"
        }
      ]
    }
'
```
**Cohere v2 - Curl 請求**

```shell showLineNumbers
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <your-litellm-api-key>' \
--data ' {
      "model": "command-a-03-2025-v2",
      "messages": [
        {
          "role": "user",
          "content": "what llm are you"
        }
      ]
    }
'
```
**Cohere v1 - OpenAI SDK**

```python showLineNumbers
import openai
client = openai.OpenAI(
    api_key="anything",
    base_url="http://0.0.0.0:4000"
)

# request sent to cohere v1 model
response = client.chat.completions.create(model="command-a-03-2025", messages = [
    {
        "role": "user",
        "content": "this is a test request, write a short poem"
    }
])

print(response)
```
**Cohere v2 - OpenAI SDK**

```python showLineNumbers
import openai
client = openai.OpenAI(
    api_key="anything",
    base_url="http://0.0.0.0:4000"
)

# request sent to cohere v2 model
response = client.chat.completions.create(model="command-a-03-2025-v2", messages = [
    {
        "role": "user",
        "content": "this is a test request, write a short poem"
    }
])

print(response)
```

## 支援的模型 {#supported-models}
| 模型名稱 | 函式呼叫 |
|------------|----------------|
| command-a-03-2025 | `litellm.completion('command-a-03-2025', messages)` |
| command-r-plus-08-2024 | `litellm.completion('command-r-plus-08-2024', messages)` |  
| command-r-08-2024 | `litellm.completion('command-r-08-2024', messages)` |
| command-r-plus | `litellm.completion('command-r-plus', messages)` |  
| command-r | `litellm.completion('command-r', messages)` |
| command-light | `litellm.completion('command-light', messages)` |  
| command-nightly | `litellm.completion('command-nightly', messages)` |

## 嵌入 {#embedding}

```python
from litellm import embedding
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere call
response = embedding(
    model="embed-english-v3.0", 
    input=["good morning from litellm", "this is another item"], 
)
```

### 設定 - v3 模型的輸入類型 {#setting---input-type-for-v3-models}
v3 模型有一個必填參數：`input_type`。LiteLLM 預設為 `search_document`。它可以是以下四個值之一：

- `input_type="search_document"`： （預設）當您要將文字（文件）儲存在向量資料庫中時使用
- `input_type="search_query"`：當您要用搜尋查詢在向量資料庫中尋找最相關的文件時使用
- `input_type="classification"`：當您將 embeddings 作為分類系統的輸入時使用
- `input_type="clustering"`：當您將 embeddings 用於文字叢集時使用

https://txt.cohere.com/introducing-embed-v3/

```python
from litellm import embedding
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere call
response = embedding(
    model="embed-english-v3.0", 
    input=["good morning from litellm", "this is another item"], 
    input_type="search_document" 
)
```

### 支援的嵌入模型 {#supported-embedding-models}
| 模型名稱               | 函式呼叫                                                |
|--------------------------|--------------------------------------------------------------|
| embed-english-v3.0       | `embedding(model="embed-english-v3.0", input=["good morning from litellm", "this is another item"])` |
| embed-english-light-v3.0 | `embedding(model="embed-english-light-v3.0", input=["good morning from litellm", "this is another item"])` |
| embed-multilingual-v3.0  | `embedding(model="embed-multilingual-v3.0", input=["good morning from litellm", "this is another item"])` |
| embed-multilingual-light-v3.0 | `embedding(model="embed-multilingual-light-v3.0", input=["good morning from litellm", "this is another item"])` |
| embed-english-v2.0       | `embedding(model="embed-english-v2.0", input=["good morning from litellm", "this is another item"])` |
| embed-english-light-v2.0 | `embedding(model="embed-english-light-v2.0", input=["good morning from litellm", "this is another item"])` |
| embed-multilingual-v2.0  | `embedding(model="embed-multilingual-v2.0", input=["good morning from litellm", "this is another item"])` |

## 重新排序 {#rerank}

### 使用方式 {#usage-1}

LiteLLM 支援 Cohere rerank 的 v1 和 v2 用戶端。預設情況下，`rerank` 端點會使用 v2 用戶端，但您可以透過明確呼叫 `v1/rerank` 來指定 v1 用戶端

**LiteLLM SDK 使用方式**

```python
from litellm import rerank
import os

os.environ["COHERE_API_KEY"] = "sk-.."

query = "What is the capital of the United States?"
documents = [
    "Carson City is the capital city of the American state of Nevada.",
    "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
    "Washington, D.C. is the capital of the United States.",
    "Capital punishment has existed in the United States since before it was a country.",
]

response = rerank(
    model="cohere/rerank-english-v3.0",
    query=query,
    documents=documents,
    top_n=3,
)
print(response)
```

**LiteLLM Proxy 使用方式**

LiteLLM 提供一個相容 cohere api 的 `/rerank` 端點供 Rerank 請求使用。

**設定**

將以下內容加入您的 litellm proxy config.yaml

```yaml
model_list:
  - model_name: Salesforce/Llama-Rank-V1
    litellm_params:
      model: together_ai/Salesforce/Llama-Rank-V1
      api_key: os.environ/TOGETHERAI_API_KEY
  - model_name: rerank-english-v3.0
    litellm_params:
      model: cohere/rerank-english-v3.0
      api_key: os.environ/COHERE_API_KEY
```

啟動 litellm

```bash
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000
```

測試請求

```bash
curl http://0.0.0.0:4000/rerank \
  -H "Authorization: Bearer $LITELLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "rerank-english-v3.0",
    "query": "What is the capital of the United States?",
    "documents": [
        "Carson City is the capital city of the American state of Nevada.",
        "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
        "Washington, D.C. is the capital of the United States.",
        "Capital punishment has existed in the United States since before it was a country."
    ],
    "top_n": 3
  }'
```

## 解析（OCR） {#parse-ocr}

[Cohere Parse](https://docs.cohere.com/reference/parse) 會將文件影像轉換為 markdown。LiteLLM 透過 [`/ocr` 端點](../ocr) 提供此功能，因此請求與回應會使用與其他 OCR 提供者相同的格式，而且每次呼叫都會依據計費頁面進行成本追蹤。

Parse 僅接受 `image_url` 文件：影像 URL 或 base64 `data:image/...` URI。PDF 與 `document_url` 輸入會在送往 Cohere 之前先以 400 拒絕。

**LiteLLM SDK 使用方式**

```python showLineNumbers
import os
from litellm import ocr

os.environ["COHERE_API_KEY"] = ""

response = ocr(
    model="cohere/parse-v5.0",
    document={
        "type": "image_url",
        "image_url": "https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/receipt.png",
    },
)

for page in response.pages:
    print(page.markdown)
print(response.usage_info.pages_processed)
```

**LiteLLM Proxy 使用方式**

**設定**

將以下內容加入您的 litellm proxy config.yaml

```yaml
model_list:
  - model_name: cohere-parse
    litellm_params:
      model: cohere/parse-v5.0
      api_key: os.environ/COHERE_API_KEY
```

啟動 litellm

```bash
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000
```

測試請求

```bash
curl http://0.0.0.0:4000/v1/ocr \
  -H "Authorization: Bearer $LITELLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "cohere-parse",
    "document": {
      "type": "image_url",
      "image_url": "https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/receipt.png"
    }
  }'
```

### 支援的參數 {#supported-parameters}

| 參數 | 值 | 說明 |
|-----------|--------|-------------|
| `output_format` | `markdown`（預設）、`blocks` | Cohere 的輸出版面配置。使用 `blocks` 時，每一頁都會包含 Cohere 的 `blocks` 陣列，且 `markdown` 會是空的 |
| `req_format` | `litellm`（預設）、`native` | `native` 會回傳 Cohere 自己的回應主體，而不是 LiteLLM OCR 格式 |

部署於 Azure AI Foundry 的 Cohere Parse 已涵蓋於 [Azure AI OCR](./azure_ocr#cohere-parse)。

## Related pages

- [Cognition](https://docs.litellm.ai/docs/providers/cognition.md)
- [CometAPI](https://docs.litellm.ai/docs/providers/cometapi.md)
