跳转至

OpenAI 兼容格式(Chat Completions)

这是最常用的接口格式,兼容 OpenAI Chat Completions API。

端点

POST /v1/chat/completions

鉴权

Authorization: Bearer YOUR_API_KEY

请求示例

基础文本对话 ✅

curl https://www.lalc.ltd/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $LALC_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的助手。"},
      {"role": "user", "content": "你好"}
    ]
  }'

响应示例:

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1741569952,
  "model": "gpt-4.1-2025-04-14",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!我能为你提供什么帮助?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 19,
    "completion_tokens": 10,
    "total_tokens": 29
  }
}

图像分析 ✅

curl https://www.lalc.ltd/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $LALC_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "user",
        "content": [
          {"type": "text", "text": "这张图片里有什么?"},
          {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
        ]
      }
    ],
    "max_tokens": 300
  }'

流式响应 ✅

在请求体中添加 "stream": true 即可启用流式输出:

curl https://www.lalc.ltd/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $LALC_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的助手。"},
      {"role": "user", "content": "你好"}
    ],
    "stream": true
  }'

函数调用 ✅

curl https://www.lalc.ltd/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $LALC_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "user", "content": "波士顿今天的天气怎么样?"}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_weather",
          "description": "获取指定位置的当前天气",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {"type": "string", "description": "城市名称"}
            },
            "required": ["location"]
          }
        }
      }
    ],
    "tool_choice": "auto"
  }'

请求参数

必需参数

参数 类型 说明
model string 模型 ID
messages array 对话消息列表

消息角色

角色 说明
system 系统指令,设定模型行为
developer 开发者指令(o1 及更新模型推荐使用)
user 用户输入
assistant 模型回复
tool 工具调用结果

可选参数

参数 类型 默认值 说明
stream boolean false 是否启用流式输出
temperature number 1 采样温度(0-2)
top_p number 1 核采样参数
n integer 1 生成回复数量
max_tokens integer - 最大生成 Token 数(已弃用,推荐用 max_completion_tokens
max_completion_tokens integer - 最大补全 Token 数(含推理 Token)
stop string/array null 停止序列
presence_penalty number 0 存在惩罚(-2.0 到 2.0)
frequency_penalty number 0 频率惩罚(-2.0 到 2.0)
tools array - 可调用的工具列表(最多 128 个)
tool_choice string/object auto 工具调用策略
response_format object - 输出格式(text / json_object / json_schema)
reasoning_effort string medium 推理强度(low / medium / high,仅 o 系列)
seed integer - 随机种子(Beta)
logprobs boolean false 是否返回 Token 对数概率

响应字段

字段 类型 说明
id string 响应唯一标识
object string 固定为 chat.completion
created integer 创建时间戳
model string 实际使用的模型
choices array 生成的回复列表
choices[].message object 回复消息(含 role、content)
choices[].finish_reason string 停止原因(stop / length / tool_calls)
usage object Token 用量统计
usage.prompt_tokens integer 提示 Token 数
usage.completion_tokens integer 补全 Token 数
usage.total_tokens integer 总 Token 数