跳轉到主要內容
POST
/
v1
/
chat
/
completions
curl -X POST https://api-llm.sunra.ai/v1/chat/completions \
  -H "Authorization: Bearer <SUNRA_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "What is the capital of France?"
      }
    ]
  }'
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop",
      "logprobs": null
    }
  ],
  "system_fingerprint": "fp_44709d6fcb",
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 8,
    "total_tokens": 33
  }
}
發送請求以獲取給定聊天對話的模型回應。支援串流和非串流模式。相容 OpenAI Chat Completions API 格式。

認證

Authorization
string
必填
Bearer 令牌。在 Authorization 請求標頭中使用您的 API 金鑰作為 Bearer 令牌。Format: Bearer <SUNRA_KEY>

請求

此端點接受一個 JSON 物件。
messages
object[]
必填
對話的訊息列表。
model
string
必填
用於補全的模型。在 sunra.ai/models 瀏覽可用模型。
stream
boolean
預設值:false
如果設為 true,將以伺服器傳送事件(SSE)的方式傳送部分訊息增量。
max_tokens
integer
補全中生成的最大令牌數。輸入令牌和生成令牌的總長度受模型上下文長度的限制。
temperature
number
預設值:1
取樣溫度,介於 0 到 2 之間。較高的值(如 0.8)使輸出更隨機,較低的值(如 0.2)使其更集中和確定性。
top_p
number
預設值:1
核取樣參數(0-1)。作為溫度取樣的替代方案,模型會考慮具有 top_p 機率質量的令牌。
frequency_penalty
number
預設值:0
介於 -2.0 和 2.0 之間的數字。正值會根據新令牌在文本中的現有頻率進行懲罰,降低模型逐字重複相同內容的可能性。
presence_penalty
number
預設值:0
介於 -2.0 和 2.0 之間的數字。正值會根據新令牌是否出現在文本中進行懲罰,增加模型談論新主題的可能性。
stop
string | string[]
最多 4 個序列,API 將在這些序列處停止生成更多令牌。
n
integer
預設值:1
每個輸入訊息要生成多少個聊天補全選擇。
logprobs
boolean
預設值:false
是否返回輸出令牌的對數機率。如果為 true,將在訊息內容中返回每個輸出令牌的對數機率。
top_logprobs
integer
介於 0 和 20 之間的整數,指定在每個令牌位置返回最可能令牌的數量。使用此參數時,logprobs 必須設為 true
response_format
object
指定模型必須輸出的格式的物件。
seed
integer
如果指定,系統將盡最大努力進行確定性取樣,使得具有相同種子和參數的重複請求應返回相同的結果。
tools
object[]
模型可以呼叫的工具列表。目前僅支援函式作為工具。
tool_choice
string | object
控制模型呼叫哪個(如果有的話)工具。none 表示模型不會呼叫任何工具。auto 表示模型可以選擇生成訊息或呼叫工具。required 表示模型必須呼叫一個或多個工具。也可以透過 {"type": "function", "function": {"name": "my_function"}} 指定特定函式。
parallel_tool_calls
boolean
預設值:true
是否在工具使用期間啟用並行函式呼叫。
user
string
代表您終端使用者的唯一識別碼,有助於監控和檢測濫用。

回應

成功的聊天補全回應。
id
string
聊天補全的唯一識別碼。
object
string
物件類型。始終為 chat.completion
created
integer
聊天補全建立時的 Unix 時間戳(以秒為單位)。
model
string
用於聊天補全的模型。
choices
object[]
聊天補全選擇列表。如果 n 大於 1,則可以有多個。
usage
object
補全請求的使用量統計。
system_fingerprint
string | null
此指紋代表模型運行的後端配置。可與 seed 參數一起使用,以了解何時進行了後端變更。
curl -X POST https://api-llm.sunra.ai/v1/chat/completions \
  -H "Authorization: Bearer <SUNRA_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "What is the capital of France?"
      }
    ]
  }'
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop",
      "logprobs": null
    }
  ],
  "system_fingerprint": "fp_44709d6fcb",
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 8,
    "total_tokens": 33
  }
}