對話介面通用說明

使用前按存取基礎設定 SHANHAI_BASE_URL (本站根位址)及 SHANHAI_API_KEY (本站令牌)。下方指令適用於 Bash;素材範例位址須替換為可直接下載的真實位址。

介面格式與選擇

格式 請求位址 主輸入 最終文字位置
Chat Completions POST /v1/chat/completions messages choices[].message.content
留言 POST /v1/messages 頂層 system 和 messages content 中 type:text 的 text
回應 POST /v1/responses 指令和輸入 output 中 message 的 content[].text

已有應用程式優先沿用它使用的格式。下文相容格式範例使用 qwen3.8-flash;各機型可用輸入和工具仍以對應頁面為準。三種格式的欄位不能混在一個請求裡,介面相容不代表自動具有內建瀏覽、程式碼執行、檔案託管等能力。

請求參數

參數 型 必填 用法
model string 是 本頁準確模型名稱
messages 大批 是 依時間順序傳入完整對話,至少有一條
messages[].role string 是 system 设置任务, user 提问, assistant為歷史回答, tool 為工具結果
messages[].content string / array / null 视消息而定 文字通常为字符串;工具调用消息可为 null。圖片內容格式見下文,不能把普通檔案路徑當文字自動上傳
messages[].tool_calls 大批 否 原樣保留模型返回的工具調用,和對應工具結果一起回傳
messages[].tool_call_id 細繩 工具結果必填 與被回應的工具調用 id 相同
max_tokens 整數 否 限制輸出長度,範例 1024 不是模型上限;上下文還包括輸入與歷史訊息
stream 布林值 否 預設 false;true 按事件逐段返回
stream_options.include_usage 布林值 否 配合 stream:true 請求末尾用量;處理時允許空 choices 和空 usage
temperature 數位 否 調整生成隨機性;未設定時保留模型預設值,思考模式下可能固定或忽略
top_p 數位 否 調整候選詞範圍,通常與 temperature 只調一個
stop 字串/數組 否 遇到指定文字時停止;不要把結束條件寫成訊息角色
response_format object 否 {"type":"json_object"} 請求:
tools array 否 函式工具定義;
tool_choice string / object 否 autoD;
parallel_tool_calls 布林值 否 是否允許並行工具建議,客戶端仍應處理返回的每個調用

訊息、歷史與思考

每次請求傳入本輪所需的歷史,保留角色和順序。工具呼叫型 assistant 訊息可以沒有文字,但必須保留 tool_calls。某些模型返回 reasoning_content(思考內容),應與 content(最終答案)分開保存;多輪回傳時保持其原始字段位置。

不同模型使用不同思考開關,依模型頁填寫。不要同時傳 thinking、enable_thinking、reasoning_effort 來表達矛盾的設定。 max_tokens 的含義隨模型而異;支援 max_completion_tokens 的模型可用它限製完整輸出,但不要同時傳兩個上限。

請求範例

curl --fail-with-body "$SHANHAI_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "system",
      "content": "你是一位简明的中文助手。"
    },
    {
      "role": "user",
      "content": "用一句话介绍ShangHai Do。"
    }
  ],
  "max_tokens": 1024,
  "stream": false
}'

連續輸出與用量

設定 stream:true;可同時要求 stream_options.include_usage:true。服務傳回以空白行分隔的 data: 事件,不保證一次網路讀取恰好對應一個事件。

curl -N --fail-with-body "$SHANHAI_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "写一段产品介绍。"
    }
  ],
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'

讀取 choices[].delta.content 並依序拼接;reasoning_content 獨立保存。結尾可能只含 usage 而 choices 為空,也可能在最後一段內容附帶 usage。收到 data: [DONE] 後結束。網路斷開且沒有結束事件時,標記回答未完整接收,不要自動重複提交有外部副作用的工具請求。

工具定義、執行與結果回傳

第一步,向模型提供函數名稱、用途和參數結構。下面的函數由你的應用實現,本站不會替你訪問天氣服務。

{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "杭州现在天气如何?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询指定城市天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto",
  "max_tokens": 1024
}

第二步,模型可能會傳回這樣的 assistant 訊息:

{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_EXAMPLE",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"city\":\"杭州\"}"
      }
    }
  ]
}

第三步,由套用檢查函數名稱是否在允許清單內、解析並校驗 arguments,然後執行函數。只執行應用程式允許的操作,涉及發送、付款、刪除等操作時按應用權限確認。把真實結果和原 assistant 訊息一起追加回歷史。下例的天氣是假數據,用於展示結構,須替換為實際結果:

{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "杭州现在天气如何?"
    },
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_EXAMPLE",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\":\"杭州\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_EXAMPLE",
      "content": "{\"city\":\"杭州\",\"weather\":\"示例:晴\",\"temperature_c\":25}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询指定城市天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto",
  "max_tokens": 1024
}

隨後模型才能根據工具結果回答。一輪返回多個 tool_calls 時,分別回傳每個 id 的結果。連續輸出的 function.arguments 是片段,按 tool_calls[].index 合併後才可解析。不要把函數呼叫當最終文本,也不要遺漏原 assistant 訊息。

JSON 輸出

同時設定 response_format 與文字指令;接收後仍需做 JSON 解析與業務欄位校驗。輸出因 length 截斷時不能作為完整 JSON 使用。 json_schema 不是所有模型都支持,不能僅憑介面相容就啟用。

{
  "model": "qwen3.7-plus",
  "enable_thinking": false,
  "response_format": {
    "type": "json_object"
  },
  "messages": [
    {
      "role": "user",
      "content": "只输出 JSON 对象,含 title 字符串和 steps 字符串数组,主题为产品发布。"
    }
  ],
  "max_tokens": 1024
}

圖片理解

圖片理解不同於生成圖片。支援視覺輸入的模型可使用以下內容區塊;範例使用 qwen3.7-plus。位址必須能直接下載,不能填寫本機路徑。不要把「介面接收了圖片欄位」當作所有模型均支援視覺的證明。

{
  "model": "qwen3.7-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "描述图中的主体和颜色。"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/photo.jpg"
          }
        }
      ]
    }
  ],
  "max_tokens": 1024
}

Messages 格式

system 放在頂層;max_tokens 必填。請求仍使用本站令牌,建議帶 anthropic-version:2023-06-01。

curl --fail-with-body "$SHANHAI_BASE_URL/v1/messages" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
  "model": "qwen3.8-flash",
  "system": "用简明中文回答。",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "介绍一下工具调用。"
    }
  ]
}'

回傳 content 是內容區塊數組,文字區塊 type 為 text,思考區塊和工具區塊應分別處理。工具定義使用 name、description、input_schema;模型回傳 tool_use(id、name、input)後,在下一則 user 的 content 中加入 tool_result,tool_use_id 指向原 id,content 放工具結果。 stop_reason 為 tool_use 時執行工具,為 max_tokens 時表示截斷。

{
  "model": "qwen3.8-flash",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "杭州天气如何?"
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "tool_use",
          "id": "toolu_EXAMPLE",
          "name": "get_weather",
          "input": {
            "city": "杭州"
          }
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "tool_result",
          "tool_use_id": "toolu_EXAMPLE",
          "content": "{\"weather\":\"示例:晴\"}"
        }
      ]
    }
  ],
  "tools": [
    {
      "name": "get_weather",
      "description": "查询天气",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        },
        "required": ["city"],
        "additionalProperties": false
      }
    }
  ]
}

流式時解析 message_start、content_block_start、content_block_delta、content_block_stop、message_delta、message_stop;文字來自 text_delta.text,工具參數來自 input_json_delta.partial_json。不能用 Chat 的 choices 讀取 Messages 結果。

Responses 格式

簡單請求可把 input 寫成字串;instructions 用來設定任務。範例使用完整回應,客戶端從 output 的 message 內容區塊讀取文本,不要假定原始 JSON 一定有 output_text 快捷字段。

curl --fail-with-body "$SHANHAI_BASE_URL/v1/responses" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "instructions": "用简明中文回答。",
  "input": "介绍一下工具调用。",
  "max_output_tokens": 1024,
  "stream": false
}'

函數定義在 tools 中直接放 type:function、name、description、parameters,不再包 function 物件。傳回 output 中的 function_call 包含 call_id、name、arguments;解析參數並執行後,把原輸出項目和 function_call_output 加入下一輪 input,call_id 必須配對。

{
  "model": "qwen3.8-flash",
  "input": [
    {
      "role": "user",
      "content": "杭州天气如何?"
    },
    {
      "type": "function_call",
      "call_id": "call_EXAMPLE",
      "name": "get_weather",
      "arguments": "{\"city\":\"杭州\"}"
    },
    {
      "type": "function_call_output",
      "call_id": "call_EXAMPLE",
      "output": "{\"weather\":\"示例:晴\"}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "name": "get_weather",
      "description": "查询天气",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        },
        "required": ["city"],
        "additionalProperties": false
      }
    }
  ],
  "max_output_tokens": 1024
}

這是顯式傳歷史的用法。若返回思考輸出項,也一併保留。 previous_response_id、後台任務、內建工具等功能需要當前模型服務另行支援;本文不把它們當作所有對話模型的共同能力。串流讀取 response.output_text.delta,工具參數讀取 response.function_call_arguments.delta;以 response.completed、response.failed 或 response.incomplete 判斷最終狀態。

回傳與錯誤處理

狀況 處理
400 參數錯誤 檢查模型名稱、欄位位置、組合約束及模型專屬參數;修改後再請求
401 / 403 檢查令牌、權限、可見模型;不要在日誌中記錄完整令牌
429 按返回的重試提示等待,並降低請求頻率
5xx 或連線中斷 儲存要求編號及錯誤;已傳回部分內容或執行工具時,先確認狀態再重試
finish_reason:length 輸出截斷,調整上限或縮短歷史;JSON 可能不完整
工具呼叫返回 應用執行後繼續下一輪;只顯示工具參數不會得到最終答案

usage 記錄輸入、輸出和總 Token 數,快取明細可能出現在 prompt_tokens_details.cached_tokens。不同協議欄位名稱不同,金額以本站消費記錄為準。處理長任務時設定合理逾時,並儲存請求編號以進行排查。

模型明細

deepseek-v4-flash-0817、deepseek-v4-pro-0817、deepseek-v4.1-flash、deepseek-v4.1-flash-1017、glm-5.2、glm-5.3、glm-5.3-flash、kimi-k3、qwen3.7-plus、qwen3.7-max、qwen3.8-flash、qwen3.8-max

遇到問題

呼叫或配置遇到問題,請聯絡站內客服,提供模型名稱、任務編號、出錯時間及錯誤訊息,不要提供完整令牌。