跳到主内容

Cohere SDK

Cohere 的透传端点 - 以原生格式调用特定提供商的端点(无须转换)。

功能支持备注
成本跟踪支持 /v1/chat/v2/chat
日志记录适用于所有集成
终端用户跟踪如果需要此功能,请告诉我们
流式传输

只需将 https://api.cohere.com 替换为 LITELLM_PROXY_BASE_URL/cohere 🚀

使用示例

curl --request POST \
--url http://0.0.0.0:4000/cohere/v1/chat \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-anything" \
--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 端点(包括流式传输)。

查看所有 Cohere 端点

快速入门

让我们调用 Cohere 的 /rerank 端点

  1. 将 Cohere API 密钥添加到您的环境变量中
export COHERE_API_KEY=""
  1. 启动 LiteLLM Proxy
litellm

# RUNNING on http://0.0.0.0:4000
  1. 测试一下!

让我们调用 Cohere 的 /rerank 端点

curl --request POST \
--url http://0.0.0.0:4000/cohere/v1/rerank \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-anything" \
--data '{
"model": "rerank-english-v3.0",
"query": "What is the capital of the United States?",
"top_n": 3,
"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. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."]
}'

示例

http://0.0.0.0:4000/cohere 之后的任何内容都将被视为特定提供商的路由,并进行相应处理。

主要变更

原始端点替换为
https://api.cohere.comhttp://0.0.0.0:4000/cohere (LITELLM_PROXY_BASE_URL="http://0.0.0.0:4000")
bearer $CO_API_KEYbearer anything (如果在 proxy 上设置了虚拟密钥,则使用 bearer LITELLM_VIRTUAL_KEY

示例 1: Rerank 端点

LiteLLM Proxy 调用

curl --request POST \
--url http://0.0.0.0:4000/cohere/v1/rerank \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-anything" \
--data '{
"model": "rerank-english-v3.0",
"query": "What is the capital of the United States?",
"top_n": 3,
"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. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."]
}'

直接 Cohere API 调用

curl --request POST \
--url https://api.cohere.com/v1/rerank \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer $CO_API_KEY" \
--data '{
"model": "rerank-english-v3.0",
"query": "What is the capital of the United States?",
"top_n": 3,
"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. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."]
}'

示例 2: Chat API

LiteLLM Proxy 调用

curl --request POST \
--url http://0.0.0.0:4000/cohere/v1/chat \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-anything" \
--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 API 调用

curl --request POST \
--url https://api.cohere.com/v1/chat \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer $CO_API_KEY" \
--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"}]
}'

示例 3: Embedding

curl --request POST \
--url https://api.cohere.com/v1/embed \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-anything" \
--data '{
"model": "embed-english-v3.0",
"texts": ["hello", "goodbye"],
"input_type": "classification"
}'

直接 Cohere API 调用

curl --request POST \
--url https://api.cohere.com/v1/embed \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer $CO_API_KEY" \
--data '{
"model": "embed-english-v3.0",
"texts": ["hello", "goodbye"],
"input_type": "classification"
}'

高级 - 与虚拟密钥一起使用

前提条件

使用此方法可避免向开发者提供原始的 Cohere API 密钥,同时仍允许他们使用 Cohere 端点。

用法

  1. 设置环境
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export COHERE_API_KEY=""
litellm

# RUNNING on http://0.0.0.0:4000
  1. 生成虚拟密钥
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'

预期响应

{
...
"key": "sk-1234ewknldferwedojwojw"
}
  1. 测试一下!
curl --request POST \
--url http://0.0.0.0:4000/cohere/v1/rerank \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer sk-1234ewknldferwedojwojw" \
--data '{
"model": "rerank-english-v3.0",
"query": "What is the capital of the United States?",
"top_n": 3,
"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. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."]
}'