更新marsho函数以处理tool_calls,优化函数调用参数,添加占位符参数以兼容部分模型(如GLM)

This commit is contained in:
Asankilp 2024-12-29 06:33:52 +08:00
parent 7c6319b839
commit 4b2676b9fc
3 changed files with 24 additions and 4 deletions

View File

@ -337,6 +337,10 @@ async def marsho(
)
# await UniMessage(str(response)).send()
choice = response.choices[0]
# Sprint(choice)
# 当tool_calls非空时将finish_reason设置为TOOL_CALLS
if choice.message.tool_calls != None:
choice["finish_reason"] = CompletionsFinishReason.TOOL_CALLS
if choice["finish_reason"] == CompletionsFinishReason.STOPPED:
# 当对话成功时将dict的上下文添加到上下文类中
context.append(
@ -379,6 +383,9 @@ async def marsho(
function_args = json.loads(
tool_call.function.arguments.replace("'", '"')
)
# 删除args的placeholder参数
if "placeholder" in function_args:
del function_args["placeholder"]
logger.info(
f"调用函数 {tool_call.function.name.replace('-', '.')}\n参数:"
+ "\n".join([f"{k}={v}" for k, v in function_args.items()])
@ -414,15 +421,21 @@ async def marsho(
)
func_return = f"未找到函数 {tool_call.function.name.replace('-', '.')}"
tool_msg.append(
ToolMessage(tool_call_id=tool_call.id, content=func_return) # type: ignore
ToolMessage(tool_call_id=tool_call.id, content=func_return).as_dict() # type: ignore
)
request_msg = context_msg + [UserMessage(content=usermsg).as_dict()] + tool_msg # type: ignore
response = await make_chat(
client=client,
model_name=model_name,
msg=context_msg + [UserMessage(content=usermsg)] + tool_msg, # type: ignore
tools=tools.get_tools_list(),
msg=request_msg, # type: ignore
tools=(
tools_lists if tools_lists else None
), # TODO 临时追加函数,后期优化
)
choice = response.choices[0]
# 当tool_calls非空时将finish_reason设置为TOOL_CALLS
if choice.message.tool_calls != None:
choice["finish_reason"] = CompletionsFinishReason.TOOL_CALLS
if choice["finish_reason"] == CompletionsFinishReason.STOPPED:
# 对话成功 添加上下文

View File

@ -163,7 +163,13 @@ class Caller:
"parameters": {
"type": "object",
"properties": {
key: value.data() for key, value in self._parameters.items()
**{
key: value.data() for key, value in self._parameters.items()
},
"placeholder": {
"type": "string",
"description": "占位符,用于显示在对话框中", # 为保证兼容性而设置的无用参数
},
},
},
"required": [

View File

@ -95,6 +95,7 @@ async def make_chat(
messages=msg,
model=model_name,
tools=tools,
tool_choice="auto",
temperature=config.marshoai_temperature,
max_tokens=config.marshoai_max_tokens,
top_p=config.marshoai_top_p,