feat(chatcmpl): 对函数调用进行异常捕获 (#749)

This commit is contained in:
Junyan Qin 2024-03-30 09:45:30 +00:00
parent cac5a5adff
commit fdd7c1864d

View File

@ -98,6 +98,7 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
# req_messages.append({"role": "user", "content": str(query.message_chain)}) # req_messages.append({"role": "user", "content": str(query.message_chain)})
# 首次请求
msg = await self._closure(req_messages, query.use_model, query.use_funcs) msg = await self._closure(req_messages, query.use_model, query.use_funcs)
yield msg yield msg
@ -106,8 +107,10 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
req_messages.append(msg.dict(exclude_none=True)) req_messages.append(msg.dict(exclude_none=True))
# 持续请求,只要还有待处理的工具调用就继续处理调用
while pending_tool_calls: while pending_tool_calls:
for tool_call in pending_tool_calls: for tool_call in pending_tool_calls:
try:
func = tool_call.function func = tool_call.function
parameters = json.loads(func.arguments) parameters = json.loads(func.arguments)
@ -123,6 +126,17 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
yield msg yield msg
req_messages.append(msg.dict(exclude_none=True)) req_messages.append(msg.dict(exclude_none=True))
except Exception as e:
# 出错,添加一个报错信息到 req_messages
err_msg = llm_entities.Message(
role="tool", content=f"err: {e}", tool_call_id=tool_call.id
)
yield err_msg
req_messages.append(
err_msg.dict(exclude_none=True)
)
# 处理完所有调用,继续请求 # 处理完所有调用,继续请求
msg = await self._closure(req_messages, query.use_model, query.use_funcs) msg = await self._closure(req_messages, query.use_model, query.use_funcs)