Skip to content

流式与工具调用

流式输出(streaming)

流式让模型边生成边返回,适合聊天打字机效果和长输出。

接口开启方式
OpenAI 兼容 /v1/chat/completionsbody 加 "stream": true
Claude /v1/messagesbody 加 "stream": true
Gemini:streamGenerateContent 端点

OpenAI 兼容流式返回标准的 SSE(text/event-stream),逐块下发 data: {...},以 data: [DONE] 结束。各语言 SDK 已封装好,见 PythonNode.js

流式与计费

即使流被中途断开(客户端关闭、超时、服务异常),平台也会尽量按已产生的真实用量结算;拿不到真实用量时按预估处理,差额由后台兜底释放。详见 充值与计费

原生 SSE 解析(cURL 示意)

bash
curl -N https://gateway.mindproxy.ai/v1/chat/completions \
  -H "Authorization: Bearer $TT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o-mini","stream":true,
       "messages":[{"role":"user","content":"数到 5"}]}'

-N 关闭缓冲,逐行打印 data: 块。

工具调用(function calling / tools)

平台支持 OpenAI 兼容接口的 tools / tool_choice,由模型决定是否调用工具。

python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "查询某城市的天气",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "北京天气怎么样?"}],
    tools=tools,
    tool_choice="auto",
)

tool_calls = resp.choices[0].message.tool_calls
if tool_calls:
    # 解析 tool_calls[0].function.name / .arguments,执行后把结果作为
    # role="tool" 的消息回传,再次请求拿到最终回答。
    ...

模型能力差异

工具调用、JSON 模式、多模态(图片输入)、thinking 等能力是否生效,取决于你所选具体模型的支持情况。能力矩阵见 模型与套餐。能力取决于所选模型本身。

流式 + 工具调用

流式下工具调用的参数会分块(delta)下发,需要把 tool_calls 的片段按 index 累加拼接。各官方 SDK 的示例都有对应处理,建议直接参考 OpenAI / Anthropic 官方 SDK 文档的 streaming tools 部分。

线上网关:gateway.mindproxy.ai · 基于 VitePress 构建