FIM Gate 文档
API 参考

Chat Completions

POST /v1/chat/completions 的参数、流式与工具调用说明。

Chat Completions 是 FIM Gate 上使用最广的对话端点,遵循 OpenAI Chat Completions 规范,支持文本对话、图像输入、SSE 流式输出和函数调用。

端点

项目值
方法与路径POST /v1/chat/completions
Base URLhttps://api.gate.fim.ai/v1
鉴权Authorization: Bearer $FIM_API_KEY

请求参数

参数类型必填说明
modelstring是模型 ID,如 gpt-4o-mini、gpt-4.1、claude-sonnet-4-5
messagesarray是对话消息数组,每条含 role(system / user / assistant / tool)和 content
streamboolean否设为 true 启用 SSE 流式输出,默认 false
max_tokensinteger否输出 token 上限。注意推理模型的思考 token 也计入该上限,设得过小会导致空响应
temperaturenumber否采样温度,0–2,越大输出越发散
top_pnumber否核采样阈值,0–1,与 temperature 二选一调整即可
ninteger否一次生成的候选回复数量,默认 1
stopstring / array否终止序列,命中后停止生成,最多 4 个
presence_penaltynumber否-2.0–2.0,正值鼓励引入新话题
frequency_penaltynumber否-2.0–2.0,正值抑制重复用词
response_formatobject否设 {"type": "json_object"} 强制输出合法 JSON
toolsarray否可供模型调用的函数列表,见下文
tool_choicestring / object否工具调用策略:auto、none、required 或指定某个函数
userstring否终端用户标识,便于审计与滥用追踪

messages 中的 content 既可以是纯字符串,也可以是内容块数组(用于多模态输入,见下文)。

文本对话

curl https://api.gate.fim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $FIM_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "system", "content": "你是一个简洁的中文助手"},
      {"role": "user", "content": "解释一下什么是幂等性"}
    ]
  }'

响应结构:

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1765500000,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "幂等性指同一操作执行一次和执行多次的效果相同……"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 96,
    "total_tokens": 124
  }
}

finish_reason 取值

取值含义
stop模型自然结束或命中终止序列
length达到 max_tokens 上限被截断
tool_calls模型请求调用工具
content_filter内容被安全策略过滤

图像输入(多模态)

把 content 写成内容块数组,混合 text 与 image_url 块。图片可以是公网 URL,也可以是 data:image/...;base64, 形式的内联数据。

curl https://api.gate.fim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $FIM_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "user",
        "content": [
          {"type": "text", "text": "描述这张图片的内容"},
          {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
        ]
      }
    ],
    "max_tokens": 300
  }'

流式输出(SSE)

请求体加 "stream": true,响应变为 SSE 事件流,每个事件是一个 chat.completion.chunk 对象,增量文本在 choices[0].delta.content 中。

curl https://api.gate.fim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $FIM_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "stream": true,
    "messages": [
      {"role": "user", "content": "写一首关于秋天的短诗"}
    ]
  }'

chunk 结构示例:

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "created": 1765500000,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "delta": {"content": "落叶"},
      "finish_reason": null
    }
  ]
}

判断结束:最后一个有效 chunk 的 finish_reason 为 "stop"(或其他终止原因),随后收到 data: [DONE]。

函数调用(Tools)

通过 tools 声明函数及其 JSON Schema 参数,模型判断需要时会在响应中返回 tool_calls,由你的代码执行后把结果以 role: "tool" 消息回传。

curl https://api.gate.fim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $FIM_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "user", "content": "上海现在天气怎么样?"}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_weather",
          "description": "查询指定城市的实时天气",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {"type": "string", "description": "城市名,如:上海"},
              "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["city"]
          }
        }
      }
    ],
    "tool_choice": "auto"
  }'

模型决定调用工具时的响应片段:

{
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_xyz789",
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "arguments": "{\"city\": \"上海\", \"unit\": \"celsius\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}

使用提示

  • 推理类模型(如 o 系列)建议不设 max_tokens 或设得足够大:思考 token 计入上限,过小会得到空内容且 finish_reason 为 length。
  • 长对话注意控制 messages 总长度,超出模型上下文窗口会返回 400。