feat: 添加对OpenAI工具调用功能的支持

改进消息转换器以处理OpenAI的tool_calls格式
添加JSON解析以正确转换函数调用参数
优化消息处理逻辑,增加更多空值检查
在流式响应中添加工具调用检测和处理
根据工具调用状态设置适当的finish_reason
This commit is contained in:
snaily
2025-03-22 02:48:25 +08:00
parent a8dc98ab6a
commit 89b9f7919a
2 changed files with 40 additions and 27 deletions
+9 -3
View File
@@ -78,7 +78,7 @@ def _build_tools(
tool.pop("googleSearch", None)
tool.pop("codeExecution", None)
return [tool]
return [tool] if tool else []
def _get_safety_settings(model: str) -> List[Dict[str, str]]:
@@ -201,10 +201,11 @@ class OpenAIChatService:
max_retries = 3
while retries < max_retries:
try:
tool_call_flag = False
async for line in self.api_client.stream_generate_content(
payload, model, api_key
):
# print(line)
print(line)
if line.startswith("data:"):
chunk = json.loads(line[6:])
openai_chunk = self.response_handler.handle_response(
@@ -227,8 +228,13 @@ class OpenAIChatService:
yield optimized_chunk
else:
# 如果没有文本内容(如工具调用等),整块输出
if "tool_calls" in json.dumps(openai_chunk):
tool_call_flag = True
yield f"data: {json.dumps(openai_chunk)}\n\n"
yield f"data: {json.dumps(self.response_handler.handle_response({}, model, stream=True, finish_reason='stop'))}\n\n"
if tool_call_flag:
yield f"data: {json.dumps(self.response_handler.handle_response({}, model, stream=True, finish_reason='tool_calls'))}\n\n"
else:
yield f"data: {json.dumps(self.response_handler.handle_response({}, model, stream=True, finish_reason='stop'))}\n\n"
yield "data: [DONE]\n\n"
logger.info("Streaming completed successfully")
break # 成功后退出循环