Skip to content
Cloudflare Docs
非官方翻译 - 此文档为非官方中文翻译版本,仅供参考。如有疑问请以 英文官方文档 为准。

Cohere

Cohere 构建旨在解决现实业务挑战的 AI 模型。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere

URL 结构

在向 Cohere 发出请求时,将您当前使用的 URL 中的 https://api.cohere.ai/v1 替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere

前提条件

在向 Cohere 发出请求时,确保您具有以下内容:

  • 您的 AI 网关账户 ID。
  • 您的 AI 网关网关名称。
  • 一个有效的 Cohere API 令牌。
  • 您要使用的 Cohere 模型的名称。

示例

cURL

请求
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere/v1/chat \
--header 'Authorization: Token {cohere_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"chat_history": [
{"role": "USER", "message": "Who discovered gravity?"},
{"role": "CHATBOT", "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton"}
],
"message": "What year was he born?",
"connectors": [{"id": "web-search"}]
}'

在 Python 中使用 Cohere SDK

如果使用 cohere-python-sdk,这样设置您的端点:

Python
import cohere
import os
api_key = os.getenv('API_KEY')
account_id = '{account_id}'
gateway_id = '{gateway_id}'
base_url = f"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere/v1"
co = cohere.Client(
api_key=api_key,
base_url=base_url,
)
message = "hello world!"
model = "command-r-plus"
chat = co.chat(
message=message,
model=model
)
print(chat)

OpenAI-Compatible Endpoint

You can also use the OpenAI-compatible endpoint (/ai-gateway/chat-completion/) to access Cohere models using the OpenAI API schema. To do so, send your requests to:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions

Specify:

{
"model": "cohere/{model}"
}