跳至主要內容

Cohere

API 金鑰

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

使用方式

LiteLLM Python SDK

Cohere v2 API(預設)

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/chat API,請在您的模型名稱前加上 cohere_chat/v1/

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"}]
)

串流

Cohere v2 串流:

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 串流:

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 一起使用

以下說明如何使用 LiteLLM Proxy Server 呼叫 Cohere

1. 將金鑰儲存在您的環境中

export COHERE_API_KEY="your-api-key"

2. 啟動 proxy

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

適用於 Cohere v1 模型:

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 模型:

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"
litellm --config /path/to/config.yaml

3. 測試它

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"
}
]
}
'

支援的模型

模型名稱函式呼叫
command-a-03-2025litellm.completion('command-a-03-2025', messages)
command-r-plus-08-2024litellm.completion('command-r-plus-08-2024', messages)
command-r-08-2024litellm.completion('command-r-08-2024', messages)
command-r-pluslitellm.completion('command-r-plus', messages)
command-rlitellm.completion('command-r', messages)
command-lightlitellm.completion('command-light', messages)
command-nightlylitellm.completion('command-nightly', messages)

嵌入

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 模型的輸入類型

v3 模型有一個必要參數:input_type。LiteLLM 預設為 search_document。它可以是以下四個值之一:

  • input_type="search_document":(預設)用於您要儲存在向量資料庫中的文字(文件)
  • input_type="search_query":用於搜尋查詢,以在您的向量資料庫中找出最相關的文件
  • input_type="classification":當您將嵌入作為分類系統的輸入時使用
  • input_type="clustering":當您將嵌入用於文字分群時使用

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

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

支援的嵌入模型

模型名稱函式呼叫
embed-english-v3.0embedding(model="embed-english-v3.0", input=["good morning from litellm", "this is another item"])
embed-english-light-v3.0embedding(model="embed-english-light-v3.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-v3.0embedding(model="embed-multilingual-v3.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-light-v3.0embedding(model="embed-multilingual-light-v3.0", input=["good morning from litellm", "this is another item"])
embed-english-v2.0embedding(model="embed-english-v2.0", input=["good morning from litellm", "this is another item"])
embed-english-light-v2.0embedding(model="embed-english-light-v2.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-v2.0embedding(model="embed-multilingual-v2.0", input=["good morning from litellm", "this is another item"])

重新排序

使用方式

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

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)