跳转到内容
搜索文档

Cohere

最后更新 查看 MarkdownAgent 设置

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 Gateway Account ID。
  • 你的 AI Gateway gateway 名称。
  • 有效的 Cohere API 令牌。
  • 要使用的 Cohere 模型名称。

示例

cURL

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

使用 Cohere SDK 与 Python

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

Pythonjs

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 兼容端点

你也可以通过 REST API 使用 OpenAI API schema 访问 Cohere 模型。将请求发送至:

https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions

指定:


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

这篇文档对您有帮助吗?