提示词库/任务指令/在这里,我们使用了 iter_lines 方法来逐行读取响应体 在这里,我们使用了 iter_lines 方法来逐行读取响应体
中文exp-2503未验证
for line in r.iter_lines():
# 去除每一行收尾的空格,以便更好地处理数据块
line = line.strip()
# 接下来我们要处理三种不同的情况:
# 1. 如果当前行是空行,则表明前一个数据块已接收完毕(即前文提到的,通过两个换行符结束数据块传输),我们可以对该数据块进行反序列化,并打印出对应的 content 内容;
# 2. 如果当前行为非空行,且以 data: 开头,则表明这是一个数据块传输的开始,我们去除 data: 前缀后,首先判断是否是结束符 [DONE],如果不是,将数据内容保存到 data 变量;
# 3. 如果当前行为非空行,但不以 data: 开头,则表明当前行仍然归属上一个正在传输的数据块,我们将当前行的内容追加到 data 变量尾部;
if len(line) == 0:
chunk = json.loads(data)
# 通过循环获取每个数据块中所有的 choice,并获取 index 对应的 message 对象
for choice in chunk["choices"]:
index = choice["index"]
message = messages[index]
usage = choice.get("usage")
if usage:
message["usage"] = usage
delta = choice["delta"]
role = delta.get("role")
if role:
message["role"] = role
content = delta.get("content")
if content:
if "content" not in message:
message["content"] = content
else:
message["content"] = message["content"] + content
# 从这里,我们开始处理 tool_calls
tool_calls = delta.get("tool_calls") # <-- 先判断数据块中是否包含 tool_calls
if tool_calls:
if "tool_calls" not in message:
message["tool_calls"] = [] # <-- 如果包含 tool_calls,我们初始化一个列表来保存这些 tool_calls,注意此时的列表中没有任何元素,长度为 0
for tool_call in tool_calls:
tool_call_index = tool_call["index"] # <-- 获取当前 tool_call 的 index 索引
if len(message["tool_calls"]) < (
tool_call_index + 1): # <-- 根据 index 索引扩充 tool_calls 列表,以便于我们能通过下标访问到对应的 tool_call
message["tool_calls"].extend([{}] * (tool_call_index + 1 - len(message["tool_calls"])))
tool_call_object = message["tool_calls"][tool_call_index] # <-- 根据下标访问对应的 tool_call
tool_call_object["index"] = tool_call_index
# 下面的步骤,是根据数据块中的信息填充每个 tool_call 的 id、type、function 字段
# 在 function 字段中,又包括 name 和 arguments 字段,arguments 字段会由每个数据块
# 依次补充,如同 delta.content 字段一般。
tool_call_id = tool_call.get("id")
if tool_call_id:
tool_call_object["id"] = tool_call_id
tool_call_type = tool_call.get("type")
if tool_call_type:
tool_call_object["type"] = tool_call_type
tool_call_function = tool_call.get("function")
if tool_call_function:
if "function" not in tool_call_object:
tool_call_object["function"] = {}
tool_call_function_name = tool_call_function.get("name")
if tool_call_function_name:
tool_call_object["function"]["name"] = tool_call_function_name
tool_call_function_arguments = tool_call_function.get("arguments")
if tool_call_function_arguments:
if "arguments" not in tool_call_object["function"]:
tool_call_object["function"]["arguments"] = tool_call_function_arguments
else:
tool_call_object["function"]["arguments"] = tool_call_object["function"][
"arguments"] + tool_call_function_arguments # <-- 依次补充 function.arguments 字段的值
message["tool_calls"][tool_call_index] = tool_call_object
data = "" # 重置 data
elif line.startswith("data: "):
data = line[len("data: "):]
# 当数据块内容为 [DONE] 时,则表明所有数据块已发送完毕,可断开网络连接
if data == "[DONE]":
break
else:
data = data + "\n" + line # 我们仍然在追加内容时,为其添加一个换行符,因为这可能是该数据块有意将数据分行展示
复制后粘贴到 AI 对话框发送,本次对话全程生效
出处:Kimi/Moonshot 官方提示词指南 · platform.moonshot.cn/docs/guide/use-kimi-api-to-complete-tool-calls · 可信度 S · 标签:中文 / 提示词工程 / Kimi / 工具调用 / tool_calls 怎么用(对话型)复制后粘贴到对话框发送,本次对话全程生效。想长期用:支持自定义指令的产品(设置→个性化)可设为默认。