Skip to Content
API 參考OpenAI 相容協議Chat Completions

Chat Completions

建立對話補全回應。支援文本生成、多模態輸入、函式呼叫、串流回應等功能。

端點

POST https://api.ofox.ai/v1/chat/completions

請求參數

參數類型必填說明
modelstring模型識別碼,如 openai/gpt-4o
messagesarray訊息陣列
temperaturenumber取樣溫度 0-2,預設 1
max_tokensnumber最大生成 token 數
streamboolean是否啟用串流回應
top_pnumber核取樣參數
frequency_penaltynumber頻率懲罰 -2 到 2
presence_penaltynumber存在懲罰 -2 到 2
toolsarray可用工具定義(Function Calling)
tool_choicestring/object工具選擇策略
response_formatobject回應格式(JSON Mode)
providerobjectOfoxAI 擴充:路由和回退設定

Message 格式

interface Message { role: 'system' | 'user' | 'assistant' | 'tool' content: string | ContentPart[] // 文字或多模態內容 name?: string tool_calls?: ToolCall[] // assistant 訊息的工具呼叫 tool_call_id?: string // tool 訊息的呼叫 ID } // 多模態內容 type ContentPart = | { type: 'text'; text: string } | { type: 'image_url'; image_url: { url: string; detail?: 'auto' | 'low' | 'high' } }

請求範例

Terminal
curl https://api.ofox.ai/v1/chat/completions \ -H "Authorization: Bearer $OFOX_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "openai/gpt-4o", "messages": [ {"role": "system", "content": "你是一個有幫助的助手。"}, {"role": "user", "content": "解釋什麼是 API Gateway"} ], "temperature": 0.7 }'

回應格式

{ "id": "chatcmpl-abc123", "object": "chat.completion", "created": 1703123456, "model": "openai/gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "API Gateway(API 閘道)是一個..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 25, "completion_tokens": 150, "total_tokens": 175 } }

串流回應

設定 stream: true 啟用 SSE 串流回應:

stream.py
stream = client.chat.completions.create( model="openai/gpt-4o", messages=[{"role": "user", "content": "講一個故事"}], stream=True ) for chunk in stream: content = chunk.choices[0].delta.content if content: print(content, end="", flush=True)

串流回應格式

每個 chunk 透過 SSE 發送:

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: [DONE]

多模態輸入(視覺)

發送圖片讓模型分析:

response = client.chat.completions.create( model="openai/gpt-4o", messages=[{ "role": "user", "content": [ {"type": "text", "text": "這張圖片裡有什麼?"}, {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}} ] }] )

支援視覺能力的模型包括:openai/gpt-4oanthropic/claude-sonnet-4.5google/gemini-3-flash-preview 等。 詳見 視覺理解指南

Function Calling

詳見 函式呼叫指南

Last updated on