文字補全(Legacy)
Endpoint
Section titled “Endpoint”POST /v1/completions完整位址:
https://api.routeapi.ai/v1/completions不含 /v1 前綴存取 /completions 也會被接受,RouteAPI 會將它改寫成 /v1/completions。
curl https://api.routeapi.ai/v1/completions \ -H "Authorization: Bearer $ROUTEAPI_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-3.5-turbo-instruct", "prompt": "Write one sentence that explains what an API gateway is:", "max_tokens": 64, "temperature": 0.7 }'範例中的模型 ID 僅作示意:請先用 GET /v1/models 確認你的 Key 能呼叫支援補全的模型。
| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型 ID,必須是當前帳戶可用模型 |
prompt | string/array | 是 | 待補全的文字。傳陣列表示一次請求多個 prompt,每個 prompt 對應一條 choices |
max_tokens | integer | 否 | 最大輸出 token 數;未傳時由上游預設值決定 |
temperature | number | 否 | 取樣溫度 |
top_p | number | 否 | nucleus sampling 參數 |
n | integer | 否 | 每個 prompt 生成的補全數量 |
stop | string/array | 否 | 自訂停止序列 |
stream | boolean | 否 | 是否回傳 SSE 串流輸出,預設 false |
suffix | string | 否 | 補全之後可能跟隨的文字,會透傳給支援程式碼插入的上游 |
presence_penalty | number | 否 | 存在懲罰 |
frequency_penalty | number | 否 | 頻率懲罰 |
seed | number | 否 | 盡力而為的取樣種子;能否重現取決於上游模型 |
user | string | 否 | 終端使用者標識,會透傳給支援該欄位的上游 |
RouteAPI 用與 Chat Completions 相同的欄位模型解析請求,其中 logprobs 是布林值。官方舊介面的整數寫法("logprobs": 5)不被接受,會回傳 400。
不帶 prompt 的請求會被上游拒絕;RouteAPI 自身只攔截空字串形式的 prompt,在路由前直接回傳 400。上表以外的欄位(例如官方舊介面的 echo、best_of)不在 RouteAPI 的請求模型裡,不會被轉發。
{ "id": "cmpl_xxx", "object": "text_completion", "created": 1730000000, "model": "gpt-3.5-turbo-instruct", "choices": [ { "text": " An API gateway is a single entry point that routes requests to multiple backend services.", "index": 0, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 14, "completion_tokens": 19, "total_tokens": 33 }}生成的文字在 choices[].text 裡;與 Chat Completions 不同,這裡沒有 message 物件。finish_reason 取值 stop、length 或 content_filter。n 大於 1 或 prompt 為陣列時,choices 會包含多筆結果。
設定 stream: true 後回傳 Server-Sent Events。每個資料塊是一個 text_completion 物件,增量在 choices[0].text 裡,串流以 data: [DONE] 結束:
data: {"id":"cmpl_xxx","object":"text_completion","created":1730000000,"model":"gpt-3.5-turbo-instruct","choices":[{"text":" An","index":0,"logprobs":null,"finish_reason":null}]}
data: {"id":"cmpl_xxx","object":"text_completion","created":1730000000,"model":"gpt-3.5-turbo-instruct","choices":[{"text":" API","index":0,"logprobs":null,"finish_reason":null}]}
data: {"id":"cmpl_xxx","object":"text_completion","created":1730000000,"model":"gpt-3.5-turbo-instruct","choices":[{"text":"","index":0,"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]把各資料塊的 choices[0].text 依序拼接即可;不要期待 message 或 delta 結構。上游沒有直接回傳用量時,RouteAPI 會在 data: [DONE] 之前追加一個收尾用量資料塊,結構是 chat.completion.chunk(object 為 chat.completion.chunk、choices 為空、usage 有值)——沒有 text 欄位的資料塊直接跳過。此端點與 Chat Completions 一樣接受 stream_options:在支援的渠道上傳 "stream_options": {"include_usage": false} 可以省掉這個收尾資料塊。本次請求的用量同時記錄在控制台用量日誌裡。
哪些模型支援
Section titled “哪些模型支援”某個模型是否接受 /v1/completions 取決於模型本身及其背後的上游服務,模型列表不反映這一點:無論對話模型還是舊版補全模型,在列表裡都標為 openai 端點類型。
- 舊版 instruct 模型是常見候選。先用
GET /v1/models找到你的 Key 能呼叫的模型 ID,再發一次測試請求。 - OpenAI 官方 API 提供的對話模型會在上游拒絕
/v1/completions,請改用 Chat Completions 呼叫。 - 背後沒有支援補全的上游時,會回傳上游錯誤。按 request ID 到控制台日誌查具體上游回應,見常見錯誤。
- 對話、工具呼叫、結構化輸出和多模態輸入請優先使用 Chat Completions;舊版介面不具備這些能力。
- 計費按模型的 token 價格:prompt 與 completion token 以上游回傳的用量計費,價格表與對話呼叫相同。見模型定價。
- 上游沒有回傳 token 用量時,RouteAPI 會用 prompt 和生成文字估算,
usage仍然有值,計費照常進行。