Hugging Face Inference Providers 上的 Cohere 🔥

我們很高興地宣佈,**Cohere** 現已成為 HF Hub 上支援的推理提供商!這標誌著首個模型建立者直接在 Hub 上共享和提供其模型。
Cohere 致力於構建和提供專為企業用例設計的模型。他們全面的安全 AI 解決方案套件,從尖端生成式 AI 到強大的嵌入和排名模型,旨在解決實際業務挑戰。此外,Cohere Labs,Cohere 內部的研究實驗室,支援基礎研究並尋求改變研究發生的空間。
從現在開始,您可以透過 Cohere 和推理提供商對以下模型進行無伺服器推理:
- CohereLabs/c4ai-command-r-v01
- CohereLabs/c4ai-command-r-plus
- CohereLabs/c4ai-command-r-08-2024
- CohereLabs/c4ai-command-r7b-12-2024
- CohereLabs/c4ai-command-a-03-2025
- CohereLabs/aya-expanse-8b
- CohereLabs/aya-expanse-32b
- CohereLabs/aya-vision-8b
- CohereLabs/aya-vision-32b
立即使用 Cohere 和 Cohere Labs 點亮您的專案!
Cohere 模型
Cohere 和 Cohere Labs 將其大量模型引入 Inference Providers,這些模型在特定的業務應用中表現出色。讓我們詳細探討一些模型。
CohereLabs/c4ai-command-a-03-2025 🔗
針對需要快速、安全、高質量 AI 的企業進行最佳化。其 256k 的上下文長度(是大多數領先模型的兩倍)可以處理更長的企業文件。其他主要功能包括 Cohere 先進的檢索增強生成 (RAG) 和可驗證引用、代理工具使用、企業級安全以及強大的多語言效能(支援 23 種語言)。
CohereLabs/aya-expanse-32b 🔗
專注於最先進的多語言支援,應用最新的多語言預訓練研究。支援阿拉伯語、中文(簡體和繁體)、捷克語、荷蘭語、英語、法語、德語、希臘語、希伯來語、印地語、印度尼西亞語、義大利語、日語、韓語、波斯語、波蘭語、葡萄牙語、羅馬尼亞語、俄語、西班牙語、土耳其語、烏克蘭語和越南語,上下文長度為 128K。
CohereLabs/c4ai-command-r7b-12-2024 🔗
適用於低成本或低延遲用例,在其開放權重模型類別中,在實際任務中提供最先進的效能。該模型提供 128k 的上下文長度。它提供了多語言支援、引用驗證的檢索增強生成 (RAG)、推理、工具使用和代理行為的強大組合。也支援 23 種語言。
CohereLabs/aya-vision-32b 🔗
320 億引數模型,具有先進的功能,針對各種視覺語言用例進行了最佳化,包括 OCR、字幕、視覺推理、摘要、問答、程式碼等。它將多模態功能擴充套件到全球一半以上人口使用的 23 種語言。
工作原理
您可以在 Hub 網站 UI 或透過客戶端 SDK 直接使用 Cohere 模型。
您可以在Cohere 文件頁面上找到本節中提到的所有示例。
在網站 UI 中
您可以透過在模型中心中按推理提供商進行過濾來搜尋 Cohere 模型。
在模型卡片中,您可以選擇推理提供商並直接在 UI 中執行推理。
從客戶端 SDK
讓我們逐步瞭解如何使用客戶端 SDK 中的 Cohere 模型。我們還提供了一個包含這些程式碼片段的 colab notebook,以防您想立即嘗試。
從 Python 使用 huggingface_hub
以下示例展示瞭如何使用 Cohere 作為推理提供商來使用 Command A。您可以使用 Hugging Face token 進行 Hugging Face 的自動路由,或者如果您有自己的 cohere API 金鑰,也可以使用它。
安裝 huggingface_hub
v0.30.0 或更高版本
pip install -U "huggingface_hub>=0.30.0"
使用 huggingface_hub
python 庫透過定義 provider
引數來呼叫 Cohere 端點。
from huggingface_hub import InferenceClient
client = InferenceClient(
provider="cohere",
api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)
messages = [
{
"role": "user",
"content": "How to make extremely spicy Mayonnaise?"
}
]
completion = client.chat.completions.create(
model="CohereLabs/c4ai-command-r7b-12-2024",
messages=messages,
temperature=0.7,
max_tokens=512,
)
print(completion.choices[0].message)
Cohere Labs 的多語言多模態模型 Aya Vision 也受支援。您可以按以下方式包含 base64 編碼的影像
image_path = "img.jpg"
with open(image_path, "rb") as f:
base64_image = base64.b64encode(f.read()).decode("utf-8")
image_url = f"data:image/jpeg;base64,{base64_image}"
from huggingface_hub import InferenceClient
client = InferenceClient(
provider="cohere",
api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {"url": image_url},
},
]
}
]
completion = client.chat.completions.create(
model="CohereLabs/aya-vision-32b",
messages=messages,
temperature=0.7,
max_tokens=512,
)
print(completion.choices[0].message)
從 JS 使用 @huggingface/inference
import { HfInference } from "@huggingface/inference";
const client = new HfInference("xxxxxxxxxxxxxxxxxxxxxxxx");
const chatCompletion = await client.chatCompletion({
model: "CohereLabs/c4ai-command-a-03-2025",
messages: [
{
role: "user",
content: "How to make extremely spicy Mayonnaise?"
}
],
provider: "cohere",
max_tokens: 512
});
console.log(chatCompletion.choices[0].message);
從 OpenAI 客戶端
以下是如何透過 OpenAI 客戶端庫使用 Cohere 作為推理提供商呼叫 Command R7B。
from openai import OpenAI
client = OpenAI(
base_url="https://router.huggingface.co/cohere/compatibility/v1",
api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)
messages = [
{
"role": "user",
"content": "How to make extremely spicy Mayonnaise?"
}
]
completion = client.chat.completions.create(
model="command-a-03-2025",
messages=messages,
temperature=0.7,
)
print(completion.choices[0].message)
使用 Cohere 模型進行工具呼叫
Cohere 的模型為推理提供商帶來了最先進的代理工具使用功能,因此我們將詳細探討這一點。Hugging Face Hub 客戶端和 OpenAI 客戶端都相容透過推理提供商進行的工具呼叫,因此上述示例可以擴充套件。
首先,我們需要定義模型要使用的工具。下面我們定義了 get_flight_info
,它使用兩個位置呼叫 API 獲取最新航班資訊。此工具定義將由模型的聊天模板表示。我們也可以在模型卡片中(🎉開源)探討。
tools = [
{
"type": "function",
"function": {
"name": "get_flight_info",
"description": "Get flight information between two cities or airports",
"parameters": {
"type": "object",
"properties": {
"loc_origin": {
"type": "string",
"description": "The departure airport, e.g. MIA",
},
"loc_destination": {
"type": "string",
"description": "The destination airport, e.g. NYC",
},
},
"required": ["loc_origin", "loc_destination"],
},
},
}
]
接下來,我們需要將訊息傳遞給推理客戶端,以便模型在相關時使用工具。在下面的示例中,為了清晰起見,我們將助手的工具呼叫定義在 tool_calls
中。
messages = [
{"role": "developer", "content": "Today is April 30th"},
{
"role": "user",
"content": "When is the next flight from Miami to Seattle?",
},
{
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": '{ "loc_destination": "Seattle", "loc_origin": "Miami" }',
"name": "get_flight_info",
},
"id": "get_flight_info0",
"type": "function",
}
],
},
{
"role": "tool",
"name": "get_flight_info",
"tool_call_id": "get_flight_info0",
"content": "Miami to Seattle, May 1st, 10 AM.",
},
]
最後,工具和訊息被傳遞給建立方法。
from huggingface_hub import InferenceClient
client = InferenceClient(
provider="cohere",
api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)
completion = client.chat.completions.create(
model="CohereLabs/c4ai-command-r7b-12-2024",
messages=messages,
tools=tools,
temperature=0.7,
max_tokens=512,
)
print(completion.choices[0].message)
計費
對於直接請求,即當您使用 Cohere 金鑰時,將直接在您的 Cohere 賬戶上計費。
對於路由請求,即當您透過 Hub 進行身份驗證時,您只需支付標準的 Cohere API 費率。我們不收取額外費用,我們只是直接轉嫁提供商的成本。(將來,我們可能會與我們的提供商合作伙伴建立收入分成協議。)
重要提示‼️ PRO 使用者每月可獲得價值 2 美元的推理積分。您可以在所有提供商之間使用它們。🔥
訂閱 Hugging Face PRO 計劃,即可獲得推理額度、ZeroGPU、空間開發模式、20 倍更高的限制以及更多功能。