关于流式输出
在流式输出模式(stream)下,tool_calls 同样适用,但有一些需要额外注意的地方,列举如下:
在流式输出的过程中,由于 finish_reason 将会在最后的数据块中出现,因此建议使用 delta.tool_calls 字段是否存在来判断当前回复是否包含工具调用;
在流式输出的过程中,会先输出 delta.content,再输出 delta.tool_calls,因此你必须等待 delta.content 输出完成后,才能判断和识别 tool_calls;
在流式输出的过程中,我们会在最初的数据块中,指明当前调用 tool_calls 的 tool_call.id 和 tool_call.function.name,在后续的数据块中将只输出 tool_call.function.arguments;
在流式输出的过程中,如果 Kimi 大模型一次性返回多个 tool_calls,那么我们会额外使用一个名为 index 的字段来标识当前 tool_call 的索引,以便于你能正确拼接 tool_call.function.arguments 参数;
https://platform.moonshot.cn/docs/guide/use-kimi-api-to-complete-tool-calls
流式响应示例如下:
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"name":"itsvse-get_current_time","arguments":""},"index":0,"id":"call_deb0063d315441b18b50d8","type":"function"}],"role":"assistant"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"{}"},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"},{"function":{"name":"itsvse-get_current_weather","arguments":""},"index":1,"id":"call_9790fb45e2b7419097d578","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"{\"location\": \""},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"上海市\"}"},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"delta":{"tool_calls":[{"function":{"arguments":null},"index":1,"id":"","type":"function"},{"function":{"arguments":"{}","name":"itsvse-open_calculator"},"index":2,"id":"call_3ad6478075f04021ab9ea1","type":"function"}]},"index":0}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[{"finish_reason":"tool_calls","delta":{},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":500,"completion_tokens":53,"total_tokens":553,"prompt_tokens_details":{"cached_tokens":0}},"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}
data: [DONE]
|