RunAi 相容於 OpenAI 介面規範。模型名稱在請求 body 的 model 欄位傳入,不使用 URL 路徑或自訂 Header 指定模型。
pip install openai
# or
npm install openaicurl https://ai.runmailx.com/v1/chat/completions \
-H "Authorization: Bearer $RUNAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek/deepseek-v4.1-flash",
"messages": [{"role": "user", "content": "Hello"}]
}'所有請求需在 Header 中攜帶 Bearer 金鑰:Authorization: Bearer sk-runai-xxxxxxxx。金鑰依專案建立,可個別設定預算、模型白名單與 IP 白名單。金鑰外洩後可在主控台立即刪除,刪除後立即失效。
請勿在瀏覽器前端或公開儲存庫中暴露金鑰。需要前端直連時,請透過自己的伺服器轉發。
POST /v1/chat/completions,請求 body 與 OpenAI 保持一致。回應中的 model 欄位會回顯請求時傳入的平台模型名稱,便於確認模型未被替換。
from openai import OpenAI
client = OpenAI(
api_key="sk-runai-xxxxxxxx",
base_url="https://ai.runmailx.com/v1",
)
resp = client.chat.completions.create(
model="qwen/qwen3.8-flash",
messages=[
{"role": "system", "content": "You are a concise technical assistant"},
{"role": "user", "content": "Explain how context caching is billed"},
],
temperature=0.7,
)
print(resp.model)
print(resp.usage)model:平台模型識別碼,必填。messages:對話訊息陣列,支援 system / user / assistant。temperature、top_p、max_tokens:依模型能力透傳。tools、response_format:僅對宣告支援的模型生效。stream:開啟後依 SSE 回傳增量內容。串流請求以 text/event-stream 回傳,格式與 OpenAI 一致。首個位元組回傳前若發生異常,會重試其他可用線路;已輸出的內容不重試、不重複計費。
stream = client.chat.completions.create(
model="deepseek/deepseek-v4.1-flash",
messages=[{"role": "user", "content": "Write a 200-word product description"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)呼叫 POST /v1/embeddings 產生文字向量。模型名稱同樣透過請求 body 傳入,回傳結構與 OpenAI 一致,可直接用於檢索、分群與語意去重。
resp = client.embeddings.create(
model="bge/bge-m3",
input=["第一段文本", "第二段文本"],
)
print(len(resp.data))
print(len(resp.data[0].embedding))呼叫 POST /v1/images/generations 依提示詞產生圖片。回傳的圖片連結有有效期限,過期後需重新產生;也可要求 b64_json 直接回傳圖片內容。
resp = client.images.generate(
model="bytedance/seedream-4.0",
prompt="A clean product photo of a ceramic coffee cup on a wooden desk",
size="1024x1024",
)
print(resp.data[0].url)圖片按張計費,計費金額與張數會記錄在請求日誌中。
呼叫 GET /v1/models 取得目前帳號可用的模型,回傳結果包含上下文長度、輸入輸出模態,以及是否支援視覺理解。
curl https://ai.runmailx.com/v1/models \
-H "Authorization: Bearer $RUNAI_API_KEY"限流依 API Key 與組織維度統計。達到門檻時回傳 429,回應標頭會帶 Retry-After,用戶端應依該時間退避重試。
遷移只需修改兩處,業務程式碼與訊息結構不需調整:
base_url 改為 https://ai.runmailx.com/v1。api_key 換成平台金鑰。使用 LangChain、LlamaIndex、Cursor、Cline、NextChat 等工具時,在自訂 API 位址中填入相同位址即可。若工具要求填寫完整路徑,請使用 /v1/chat/completions。
錯誤回應統一為 OpenAI 結構,包含 code 與 request_id:
{
"error": {
"message": "model is required in request body",
"type": "invalid_request_error",
"code": "model_required",
"request_id": "req_01J8X2K9Q7"
}
}| code | HTTP | 說明 |
|---|---|---|
| model_required | 400 | 請求 body 缺少 model 欄位,或欄位為空。 |
| model_not_found | 404 | 模型不存在,或未對目前帳號開放。 |
| unsupported_modality | 400 | 請求內容包含模型不支援的輸入模態。 |
| context_length_exceeded | 400 | 輸入長度超過模型上下文上限。 |
| invalid_api_key | 401 | 金鑰無效、已刪除或已過期。 |
| insufficient_quota | 402 | 餘額不足,且無可用免費額度。 |
| free_quota_exhausted | 429 | 免費額度已用盡,需確認後按量付費。 |
| rate_limited | 429 | 超過 Key 或帳號的並行 / 速率限制。 |
| risk_limited | 429 | 觸發風控規則(例如短時間高頻請求),請等待統計窗口結束後重試。 |
| upstream_unavailable | 503 | 該模型目前不可用,不會切換其他模型。 |
| upstream_timeout | 504 | 服務在逾時時間內未傳回首位元組。 |
| internal_error | 500 | 平台內部錯誤,可憑 request_id 查詢。 |