按能力

對話

選模型、複製請求、讀回覆。所有對話模型都用同一種 OpenAI 格式的請求。

#1. 選模型

#3. 拿到結果

回傳 200 時,回答在 choices[0].message.content 裡。加上 "stream": true 可以邊生成邊接收。

#出錯了怎麼辦

  • 400 並提示某個欄位:這個模型不支援該選項,用第 2 步列出的取值。
  • 402:餘額不夠這次請求,到「錢包」儲值後重試。
  • 429:請求太快,按 Retry-After 給的秒數等一下再試。
  • 503 model_not_found:模型名寫錯了或模型暫時下線,從第 1 步重新選一個。

查詢其他錯誤 →

串流邊生成邊顯示回覆。

#工作原理

設 stream:true,閘道按 OpenAI 原樣轉發 Server-Sent Events:每個增量一個 data: 訊框,以 data: [DONE] 收尾。任何 SSE 客戶端或官方 SDK 都能直接使用。

#執行看看

terminal — live
curl -N https://www.aisplice.store/v1/chat/completions \
  -H "Authorization: Bearer sk-YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6-luna",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Write a haiku about the sea."
      }
    ]
  }'
streaming.py
from openai import OpenAI

client = OpenAI(base_url="https://www.aisplice.store/v1", api_key="sk-YOUR_KEY")

stream = client.chat.completions.create(
    model="gpt-5.6-luna",
    stream=True,
    messages=[{"role": "user", "content": "Write a haiku about the sea."}],
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

#按實際計費

只按已串流輸出的用量計費

客戶端中途斷開時,閘道只記錄實際發出的用量——你只為已交付的部分付費。
工具呼叫讓模型呼叫你自己的函式。

#宣告工具

傳送標準的 OpenAI tools 陣列。閘道會把它轉發給所有支援工具的模型——OpenAI、Claude、Gemini、DeepSeek——並把回覆統一成 tool_calls。支援串流工具呼叫增量。

tools.sh
curl https://www.aisplice.store/v1/chat/completions \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6-luna",
    "messages": [{"role":"user","content":"Weather in Tokyo?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }
    }]
  }'

#處理 tool_calls

模型決定呼叫工具時,choice 會帶著 finish_reason:"tool_calls" 回傳,arguments 是一個字串,由你的程式碼解析並執行。

response.json
{
  "choices": [{
    "finish_reason": "tool_calls",
    "message": {
      "role": "assistant",
      "tool_calls": [{
        "id": "call_abc123",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\": \"Tokyo\"}"
        }
      }]
    }
  }]
}

#回傳結果

把 assistant 輪次和每個呼叫對應的一條 role:"tool" 訊息(用 tool_call_id 對齊)追加進 messages 再請求一次——模型會把結果併入最終回答。

round-trip.sh
curl https://www.aisplice.store/v1/chat/completions \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6-luna",
    "messages": [
      {"role":"user","content":"Weather in Tokyo?"},
      {"role":"assistant","tool_calls":[{"id":"call_abc123","type":"function",
        "function":{"name":"get_weather","arguments":"{\"city\":\"Tokyo\"}"}}]},
      {"role":"tool","tool_call_id":"call_abc123","content":"{\"temp_c\": 22, \"sky\": \"clear\"}"}
    ]
  }'
圖片輸入在文字之外再傳送圖片。

#傳送圖片

支援視覺的對話模型接受混合 text 與 image_url 分段的 content 陣列。公開 URL 或內嵌 base64 data URL 都行——用的是同一個欄位。

vision.sh
curl https://www.aisplice.store/v1/chat/completions \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6-luna",
    "messages": [{
      "role": "user",
      "content": [
        {"type": "text", "text": "What is in this image?"},
        {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}}
      ]
    }]
  }'

#Base64 圖片

本機檔案用 data URL 內嵌:"url": "data:image/png;base64,<bytes>"。圖片 token 按輸入 token 計費;單一請求的圖片大小和數量上限取決於廠商——傳送前先縮小能省錢。

base64.py
import base64
from openai import OpenAI

client = OpenAI(base_url="https://www.aisplice.store/v1", api_key="sk-YOUR_KEY")
b64 = base64.b64encode(open("photo.png", "rb").read()).decode()

resp = client.chat.completions.create(
    model="gpt-5.6-luna",
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "Describe this photo."},
        {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}},
    ]}],
)
print(resp.choices[0].message.content)

還是沒解決?

把請求 ID 或錯誤訊息傳給我們,我們來查。