mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-01-26 18:12:47 +08:00
✨ 更新Caller类,支持自定义函数类型和模块名选项,support moonshot AI builtin function
This commit is contained in:
parent
1c09a5f663
commit
68eb2fc946
@ -323,10 +323,14 @@ async def marsho(
|
||||
# 需要获取额外信息,调用函数工具
|
||||
tool_msg = []
|
||||
while choice.message.tool_calls != None:
|
||||
tool_msg.append(
|
||||
AssistantMessage(tool_calls=response.choices[0].message.tool_calls)
|
||||
)
|
||||
for tool_call in choice.message.tool_calls:
|
||||
# await UniMessage(str(response)).send()
|
||||
tool_calls = choice.message.tool_calls
|
||||
if tool_calls[0]["function"]["name"].startswith("$"):
|
||||
choice.message.tool_calls[0][
|
||||
"type"
|
||||
] = "builtin_function" # 兼容 moonshot AI 内置函数的临时方案
|
||||
tool_msg.append(choice.message.as_dict())
|
||||
for tool_call in tool_calls:
|
||||
if isinstance(
|
||||
tool_call, ChatCompletionsToolCall
|
||||
): # 循环调用工具直到不需要调用
|
||||
@ -376,6 +380,8 @@ async def marsho(
|
||||
tool_msg.append(
|
||||
ToolMessage(tool_call_id=tool_call.id, content=func_return).as_dict() # type: ignore
|
||||
)
|
||||
# tool_msg[0]["tool_calls"][0]["type"] = "builtin_function"
|
||||
# await UniMessage(str(tool_msg)).send()
|
||||
request_msg = context_msg + [UserMessage(content=usermsg).as_dict()] + tool_msg # type: ignore
|
||||
response = await make_chat(
|
||||
client=client,
|
||||
|
@ -17,11 +17,21 @@ _caller_data: dict[str, "Caller"] = {}
|
||||
|
||||
|
||||
class Caller:
|
||||
def __init__(self, name: str = "", description: str | None = None):
|
||||
def __init__(
|
||||
self,
|
||||
name: str = "",
|
||||
description: str | None = None,
|
||||
func_type: str = "function",
|
||||
no_module_name: bool = False,
|
||||
):
|
||||
self._name: str = name
|
||||
"""函数名称"""
|
||||
self._description = description
|
||||
"""函数描述"""
|
||||
self._func_type = func_type
|
||||
"""函数类型"""
|
||||
self.no_module_name = no_module_name
|
||||
"""是否不包含模块名"""
|
||||
self._plugin: Plugin | None = None
|
||||
"""所属插件对象,装饰时声明"""
|
||||
self.func: ASYNC_FUNCTION_CALL_FUNC | None = None
|
||||
@ -162,7 +172,7 @@ class Caller:
|
||||
"description": "占位符,用于显示在对话框中", # 为保证兼容性而设置的无用参数
|
||||
}
|
||||
return {
|
||||
"type": "function",
|
||||
"type": self._func_type,
|
||||
"function": {
|
||||
"name": self.aifc_name,
|
||||
"description": self._description,
|
||||
@ -237,6 +247,8 @@ class Caller:
|
||||
@property
|
||||
def aifc_name(self) -> str:
|
||||
"""AI调用名,没有点"""
|
||||
if self.no_module_name:
|
||||
return self._name
|
||||
return self.full_name.replace(".", "-")
|
||||
|
||||
@property
|
||||
@ -249,16 +261,29 @@ class Caller:
|
||||
return f"{self.full_name}({self._description})"
|
||||
|
||||
|
||||
def on_function_call(name: str = "", description: str | None = None) -> Caller:
|
||||
def on_function_call(
|
||||
name: str = "",
|
||||
description: str | None = None,
|
||||
func_type: str = "function",
|
||||
no_module_name: bool = False,
|
||||
) -> Caller:
|
||||
"""返回一个Caller类,可用于装饰一个函数,使其注册为一个可被AI调用的function call函数
|
||||
|
||||
Args:
|
||||
name: 函数名称,若为空则从函数的__name__属性获取
|
||||
description: 函数描述,若为None则从函数的docstring中获取
|
||||
func_type: 函数类型,默认为function,若要注册为 Moonshot AI 的内置函数则为builtin_function
|
||||
no_module_name: 是否不包含模块名,当注册为 Moonshot AI 的内置函数时为True
|
||||
|
||||
Returns:
|
||||
Caller: Caller对象
|
||||
"""
|
||||
caller = Caller(name=name, description=description)
|
||||
caller = Caller(
|
||||
name=name,
|
||||
description=description,
|
||||
func_type=func_type,
|
||||
no_module_name=no_module_name,
|
||||
)
|
||||
return caller
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@ from zhDateTime import DateTime # type: ignore
|
||||
|
||||
from nonebot_plugin_marshoai.plugin import PluginMetadata, String, on_function_call
|
||||
|
||||
# from .web import *
|
||||
# 定义插件元数据
|
||||
__marsho_meta__ = PluginMetadata(
|
||||
name="基本功能",
|
||||
|
Loading…
x
Reference in New Issue
Block a user