nexusflow
Online
Loading...
POST/v1/chat/completions

对话补全 API

创建对话补全响应。支持流式输出、多轮对话、Function Calling、视觉理解等功能。

请求端点

POST https://nexusflow.hk/v1/chat/completions
Authorization: Bearer <API_KEY>Content-Type: application/json

请求参数

参数类型必填说明
modelstring*模型 ID。例如 claude-sonnet-4-6、qwen3.5-plus 等。查看列表 →
messagesarray*对话消息数组。每条消息包含 role 和 content 字段。
messages[].rolestring*消息角色:system / user / assistant / tool
messages[].contentstring | array*消息内容。可以是字符串或内容数组(用于图像输入)
streamboolean-是否启用流式输出。启用后以 SSE 格式逐字返回。默认:false
temperaturenumber-采样温度,范围 [0, 2)。值越高越随机。默认:1.0
top_pnumber-核采样概率阈值,范围 (0, 1]。与 temperature 二选一。默认:1.0
max_tokensinteger-生成的最大 token 数。不同模型有不同上限。
stopstring | array-停止词。遇到时停止输出,最多 4 个。
presence_penaltynumber-存在惩罚 [-2.0, 2.0]。正值减少重复话题。默认:0
frequency_penaltynumber-频率惩罚 [-2.0, 2.0]。正值减少重复词汇。默认:0
toolsarray-可用工具/函数列表,用于 Function Calling。
tool_choicestring | object-工具调用策略:"none" / "auto" / 指定函数。默认:"auto"
seedinteger-随机种子(实验性)。
userstring-终端用户 ID,用于监控和滥用检测。
ninteger-生成回复数量,范围 1-4。默认:1

代码示例

from openai import OpenAI

client = OpenAI(
    api_key="sk-air-your-key",
    base_url="https://nexusflow.hk/v1",
)

response = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[
        {"role": "system", "content": "你是一个有帮助的助手。"},
        {"role": "user", "content": "什么是机器学习?"}
    ],
    temperature=0.7,
    max_tokens=1000
)

print(response.choices[0].message.content)

响应结构

字段类型说明
idstring本次请求的唯一标识符
objectstring固定为 "chat.completion"
createdinteger创建时间 Unix 时间戳
modelstring实际使用的模型名称
choicesarray生成结果数组
choices[].indexinteger结果索引
choices[].messageobject生成的消息对象
choices[].message.rolestring固定为 "assistant"
choices[].message.contentstring | null生成内容(调用工具时可能为 null)
choices[].message.tool_callsarray工具调用请求
choices[].finish_reasonstring停止原因:stop / length / tool_calls
usageobjectToken 使用统计
usage.prompt_tokensinteger输入 token 数
usage.completion_tokensinteger输出 token 数
usage.total_tokensinteger总 token 数
响应示例
{
  "id": "chatcmpl-abc123xyz789",
  "object": "chat.completion",
  "created": 1709123456,
  "model": "claude-sonnet-4-6",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "机器学习是人工智能的一个分支..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 256,
    "total_tokens": 284
  }
}

注意事项

  • 不同模型 max_tokens 上限不同,参考 模型列表
  • temperaturetop_p 建议只使用其中一个
  • 流式输出时最后一个 chunk 的 finish_reason 才表示完成
  • 图像理解仅支持 Claude 和 Qwen-VL 系列模型
  • Function Calling 推荐使用 Claude 或 Qwen 系列

相关文档