跳转到主要内容
POST
/
v1
/
chat
/
completions
创建聊天补全
curl --request POST \
  --url https://api.example.com/v1/chat/completions \
  --header 'Authorization: <authorization>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "<string>",
  "messages": [
    {
      "tool_calls": [
        {
          "id": "<string>",
          "index": 123,
          "type": "<string>",
          "function": {
            "name": "<string>",
            "arguments": "<string>"
          }
        }
      ],
      "tool_call_id": "<string>",
      "reasoning_content": "<string>",
      "content": {},
      "name": "<string>",
      "refusal": "<string>",
      "annotations": [
        {}
      ]
    }
  ],
  "stream_options": {
    "include_usage": true
  },
  "top_p": 123,
  "max_tokens": 123,
  "max_completion_tokens": 123,
  "frequency_penalty": 123,
  "presence_penalty": 123,
  "logit_bias": {
    "key": 123
  },
  "top_logprobs": 123,
  "response_format": {
    "json_schema": {
      "name": "<string>",
      "description": "<string>",
      "schema": {},
      "strict": true
    },
    "type": "<string>"
  },
  "prompt_cache_key": "<string>",
  "prompt_cache_retention": "<string>",
  "safety_identifier": "<string>",
  "tool_choice": {},
  "parallel_tool_calls": true,
  "service_tier": "<string>",
  "reasoning_effort": "<string>",
  "web_search_options": {},
  "function_call": {},
  "stream": true,
  "temperature": 123,
  "stop": {},
  "n": 123,
  "logprobs": true,
  "seed": 123,
  "user": "<string>",
  "tools": [
    {
      "type": "<string>",
      "function": {
        "name": "<string>",
        "description": "<string>",
        "parameters": {},
        "strict": true
      }
    }
  ],
  "modalities": [
    "<string>"
  ],
  "audio": {},
  "metadata": {
    "key": "<string>"
  },
  "prediction": {},
  "verbosity": "<string>",
  "functions": [
    {}
  ]
}
'
{
  "system_fingerprint": "<string>",
  "service_tier": "<string>",
  "id": "<string>",
  "object": "<string>",
  "created": 123,
  "model": "<string>",
  "choices": [
    {
      "finish_reason": "<string>",
      "index": 123,
      "message": {
        "tool_calls": [
          {
            "id": "<string>",
            "index": 123,
            "type": "<string>",
            "function": {
              "name": "<string>",
              "arguments": "<string>"
            }
          }
        ],
        "tool_call_id": "<string>",
        "reasoning_content": "<string>",
        "content": {},
        "name": "<string>",
        "refusal": "<string>",
        "annotations": [
          {}
        ]
      },
      "logprobs": {
        "content": [
          {
            "top_logprobs": [
              {
                "token": "<string>",
                "logprob": 123,
                "bytes": [
                  123
                ]
              }
            ],
            "token": "<string>",
            "logprob": 123,
            "bytes": [
              123
            ]
          }
        ],
        "refusal": [
          {
            "top_logprobs": [
              {
                "token": "<string>",
                "logprob": 123,
                "bytes": [
                  123
                ]
              }
            ],
            "token": "<string>",
            "logprob": 123,
            "bytes": [
              123
            ]
          }
        ]
      }
    }
  ],
  "usage": {
    "prompt_tokens": 123,
    "completion_tokens": 123,
    "total_tokens": 123,
    "prompt_tokens_details": {
      "cached_tokens": 123,
      "audio_tokens": 123
    },
    "completion_tokens_details": {
      "reasoning_tokens": 123,
      "audio_tokens": 123,
      "accepted_prediction_tokens": 123,
      "rejected_prediction_tokens": 123
    },
    "ephemeral_5m_input_tokens": 123,
    "ephemeral_1h_input_tokens": 123,
    "web_search_requests": 123
  }
}
此接口兼容 OpenAI Chat Completions 协议,用于创建聊天补全请求。

功能特性

  • 支持流式(SSE)和非流式两种响应模式
  • 支持多模态输入(文本、图片等)
  • 支持函数调用与工具调用(Function/Tool Calling)
  • 支持结构化输出(JSON Schema)
  • 支持推理模式配置(reasoning_effort)
  • 支持网络搜索选项

使用场景

适用于需要与 OpenAI 兼容模型进行对话交互的场景,包括:
  • 单轮或多轮对话
  • 函数调用与工具编排
  • 结构化 JSON 输出
  • 流式实时响应

认证方式

在请求头中携带 Authorization 字段,格式为 Bearer YOUR_API_KEY
请勿在客户端代码中暴露 API Key,建议通过服务端代理转发请求。

快速示例

import requests

url = "https://api.haitoken.ai/v1/chat/completions"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
data = {
    "model": "gpt-4o",
    "messages": [
        {"role": "user", "content": "你好,请介绍一下自己"}
    ]
}

response = requests.post(url, json=data, headers=headers)
print(response.json())

请求头

Authorization
string
必填
示例:

"Bearer YOUR_API_KEY"

Content-Type
string
示例:

"application/json"

请求体

application/json
model
string
必填

模型 ID,如 gpt-4o、deepseek-chat 等

messages
object[]
必填

消息列表

stream_options
object

流式响应选项(仅在 stream=true 时使用)

top_p
number

核采样,0-1 之间

max_tokens
integer

生成的最大 token 数量(已弃用,建议使用 max_completion_tokens)

max_completion_tokens
integer

生成的最大 token 数量(包括可见输出 token 和推理 token)

frequency_penalty
number

频率惩罚,-2.0 到 2.0

presence_penalty
number

存在惩罚,-2.0 到 2.0

logit_bias
object

修改指定 token 出现概率的映射

top_logprobs
integer

每个 token 位置返回的最可能 token 的最大数量(0-20)

response_format
object

响应格式,支持 "text"、"json_object"、"json_schema"

prompt_cache_key
string

稳定标识符,用于缓存优化

prompt_cache_retention
string

缓存保留策略:"in_memory" 或 "24h"

safety_identifier
string

安全标识符,用于检测违规用户

tool_choice
object

控制模型调用哪个工具,可以是字符串("none"/"auto"/"required")或对象

parallel_tool_calls
boolean

是否启用并行工具调用

service_tier
string

服务层级:"auto"、"default"、"flex"、"scale"、"priority"

reasoning_effort
string

推理努力程度:"none"、"minimal"、"low"、"medium"、"high"、"xhigh"

web_search_options
object

网络搜索选项

function_call
object

已弃用:函数调用控制

stream
boolean

是否启用流式输出

temperature
number

采样温度,0-2 之间,值越高输出越随机

stop
object

停止序列

n
integer

每个输入消息生成的聊天补全选择数量

logprobs
boolean

是否返回输出 token 的对数概率

seed
integer<int64>

用于确定性采样的种子值

user
string

用户标识(已弃用,建议使用 prompt_cache_key)

tools
object[]

模型可调用的工具列表

modalities
string[]

输出类型列表:["text"] 或 ["text", "audio"]

audio
object

音频输出参数

metadata
object

元数据,最多16个键值对

prediction
object

预测输出内容

verbosity
string

响应详细程度:"low"、"medium"、"high"

functions
object[]

已弃用:函数定义列表

响应

200 - application/json

OpenAI Chat Completions API 响应对象

system_fingerprint
string

系统指纹,表示后端配置

service_tier
string

实际使用的服务层级

id
string

聊天补全的唯一标识

object
string

对象类型,始终为 "chat.completion"

created
integer<int64>

创建时间(Unix 时间戳)

model
string

使用的模型 ID

choices
object[]

聊天补全选择列表

usage
object

使用统计信息