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-2.5-flash 等。 详见 视觉理解指南

Function Calling

详见 函数调用指南