Skip to content

Commit

Permalink
✨ 更新marsho函数以处理tool_calls,优化函数调用参数,添加占位符参数以兼容部分模型(如GLM)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asankilp committed Dec 28, 2024
1 parent 7c6319b commit 4b2676b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
19 changes: 16 additions & 3 deletions nonebot_plugin_marshoai/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()])
Expand Down Expand Up @@ -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:

# 对话成功 添加上下文
Expand Down
8 changes: 7 additions & 1 deletion nonebot_plugin_marshoai/plugin/func_call/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ def data(self) -> dict[str, Any]:
"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": [
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_marshoai/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4b2676b

Please sign in to comment.