聊天
聊天 API 涵蓋 Claude 相容對話、Gemini 原生多模態訊息、OpenAI 相容 Chat Completions,以及較新的 Responses API。
原生 Claude 格式
若您需要 Anthropic 相容載荷、工具調用或系統提示,請使用 Claude Messages API。
https://api.dgrid.ai
POST
/v1/messages請求體
| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型 ID,例如 claude-3-5-sonnet-20241022。 |
max_tokens | integer | 是 | 最大輸出 token 數。 |
messages | array | 是 | 對話訊息列表。 |
messages[].role | string | 是 | user 或 assistant。 |
messages[].content | string 或 array | 是 | 訊息內容或內容區塊。 |
system | string | 否 | 系統指令。 |
temperature | number | 否 | 採樣溫度。 |
top_p | number | 否 | Top-p 採樣。 |
top_k | integer | 否 | Top-k 採樣。 |
stop_sequences | array | 否 | 停止序列。 |
stream | boolean | 否 | 是否啟用串流。 |
tools | array | 否 | 工具定義。 |
tool_choice | object | 否 | 工具選擇策略。 |
回應體
| 欄位 | 型別 | 說明 |
|---|---|---|
id | string | 訊息識別碼。 |
type | string | 固定為 message。 |
role | string | 固定為 assistant。 |
content | array | 返回內容區塊。 |
content[].type | string | text 或 tool_use。 |
content[].text | string | 當型別為 text 時的文字內容。 |
model | string | 產生回應的模型。 |
stop_reason | string | end_turn、max_tokens、stop_sequence 或 tool_use。 |
usage | object | Token 使用統計。 |
Gemini 媒體識別
若您要在單一請求中分析圖像、音訊、影片或混合媒體,請使用 Gemini 原生多模態 parts。
https://api.dgrid.ai
POST
/v1/models/{model}:generateContent路徑參數
| 參數 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型 ID,例如 gemini-1.5-pro。 |
請求體
| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
body | object | 是 | 目前範例只傳送空的 JSON 物件 {}。 |
回應體
| 欄位 | 型別 | 說明 |
|---|---|---|
candidates | array | 模型返回的候選回應。 |
candidates[].content | object | 候選內容物件。 |
candidates[].content.role | string | 回應內容角色。 |
candidates[].content.parts | array | 返回內容片段陣列。 |
candidates[].finishReason | string | 生成結束原因。 |
candidates[].safetyRatings | array | 安全評分結果。 |
usageMetadata | object | Token 使用量中繼資料。 |
usageMetadata.promptTokenCount | integer | 輸入 token 數。 |
usageMetadata.candidatesTokenCount | integer | 輸出 token 數。 |
usageMetadata.totalTokenCount | integer | 總 token 數。 |
Gemini 文字聊天
若您只需要純文字對話,又希望保留 Gemini 原生格式,可使用此輕量聊天方式。
https://api.dgrid.ai
POST
/v1/models/{model}:generateContent請求體
| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
body | object | 是 | 目前範例只傳送空的 JSON 物件 {}。 |
回應體
| 欄位 | 型別 | 說明 |
|---|---|---|
candidates | array | 模型返回的候選回應。 |
candidates[].content | object | 候選內容物件。 |
candidates[].content.role | string | 回應內容角色。 |
candidates[].content.parts | array | 返回內容片段陣列。 |
candidates[].finishReason | string | 生成結束原因。 |
candidates[].safetyRatings | array | 安全評分結果。 |
usageMetadata | object | Token 使用量中繼資料。 |
usageMetadata.promptTokenCount | integer | 輸入 token 數。 |
usageMetadata.candidatesTokenCount | integer | 輸出 token 數。 |
usageMetadata.totalTokenCount | integer | 總 token 數。 |
Chat Completions
若您需要標準多輪對話、結構化輸出或工具調用,請使用 OpenAI 相容 Chat Completions 格式。
https://api.dgrid.ai
POST
/v1/chat/completions請求體
| 欄位 | 型別 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
model | string | 是 | - | 目標模型 ID。 |
messages | array | 是 | - | 目前範例會傳送只包含一個訊息物件的陣列。 |
messages[].role | string | 是 | - | 目前範例使用 system 角色。 |
messages[].content | string | 是 | - | 目前範例中的訊息文字。 |
回應體
| 欄位 | 型別 | 說明 |
|---|---|---|
id | string | Completion 識別碼。 |
object | string | 固定為 chat.completion。 |
created | integer | 建立時間戳。 |
model | string | 提供推理的模型。 |
choices | array | 返回的 choices。 |
choices[].message | object | 助手訊息物件。 |
choices[].message.role | string | 回應角色。 |
choices[].message.content | string | 回應文字。 |
choices[].message.tool_calls | array | 工具調用資料。 |
choices[].finish_reason | string | stop、length、content_filter 或 tool_calls。 |
usage | object | Token 使用統計。 |
Responses
若您需要狀態化流程、推理參數或新版 OpenAI 工具模式,建議使用 Responses API。
https://api.dgrid.ai
POST
/v1/responses請求體
| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 目標模型 ID。 |
body.model | string | 是 | 目前範例在請求體中只傳送 model 欄位。 |
回應體
| 欄位 | 型別 | 說明 |
|---|---|---|
id | string | 回應識別碼。 |
object | string | 固定為 response。 |
created_at | integer | 建立時間戳。 |
status | string | 回應狀態。 |
model | string | 使用的模型。 |
output | array | 輸出項目陣列。 |
output[].type | string | 一般為 message。 |
output[].role | string | 輸出角色。 |
output[].content | array | 輸出內容區塊。 |
output[].content[].type | string | 文字輸出時為 output_text。 |
output[].content[].text | string | 文字內容。 |
usage | object | Token 使用摘要。 |
