From 6c6b45a168c5b732a6203bf83686a7b7a3ccd20c Mon Sep 17 00:00:00 2001 From: Snowykami Date: Sun, 15 Dec 2024 02:59:06 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA=E4=BB=A5=E6=8C=87=E7=A4=BA=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD=E5=BC=80=E5=8F=91=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=9B=E7=A7=BB=E9=99=A4=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=B3=A8=E5=86=8C=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/plugin/__init__.py | 4 + .../plugin/func_call/__init__.py | 1 - .../plugin/func_call/caller.py | 19 ++- .../plugin/func_call/register.py | 126 ------------------ .../plugins/marshoai_bangumi/__init__.py | 47 +------ 5 files changed, 22 insertions(+), 175 deletions(-) delete mode 100755 nonebot_plugin_marshoai/plugin/func_call/register.py diff --git a/nonebot_plugin_marshoai/plugin/__init__.py b/nonebot_plugin_marshoai/plugin/__init__.py index 3304092f..1de4dd97 100755 --- a/nonebot_plugin_marshoai/plugin/__init__.py +++ b/nonebot_plugin_marshoai/plugin/__init__.py @@ -1,7 +1,11 @@ """该功能目前正在开发中,暂时不可用,受影响的文件夹 `plugin`, `plugins` """ +from nonebot import logger + from .func_call import * from .load import * from .models import * from .utils import * + +logger.opt(colors=True).info("MarshoAI 插件功能开发中,用户请忽略相关日志") diff --git a/nonebot_plugin_marshoai/plugin/func_call/__init__.py b/nonebot_plugin_marshoai/plugin/func_call/__init__.py index 6a79b24a..401b2826 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/__init__.py +++ b/nonebot_plugin_marshoai/plugin/func_call/__init__.py @@ -1,3 +1,2 @@ from .caller import * from .params import * -from .register import * diff --git a/nonebot_plugin_marshoai/plugin/func_call/caller.py b/nonebot_plugin_marshoai/plugin/func_call/caller.py index 4ba01192..cc8fd767 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/caller.py +++ b/nonebot_plugin_marshoai/plugin/func_call/caller.py @@ -1,8 +1,11 @@ -from typing import Generic +from typing import Generic, TypeVar + +from nonebot import logger from ..typing import FUNCTION_CALL_FUNC from .params import P -from .register import F + +F = TypeVar("F", bound=FUNCTION_CALL_FUNC) class Caller(Generic[P]): @@ -60,6 +63,17 @@ class Caller(Generic[P]): return self def __call__(self, func: F) -> F: + """装饰函数,注册为一个可被AI调用的function call函数 + + Args: + func (F): 函数对象 + + Returns: + F: 函数对象 + """ + logger.opt(colors=True).info( + f"加载函数 {func.__name__} {self._description}" + ) self.func = func return func @@ -73,4 +87,5 @@ def on_function_call(name: str | None = None, description: str | None = None) -> Returns: Caller: Caller对象 """ + return Caller(name=name, description=description) diff --git a/nonebot_plugin_marshoai/plugin/func_call/register.py b/nonebot_plugin_marshoai/plugin/func_call/register.py deleted file mode 100755 index 67594b98..00000000 --- a/nonebot_plugin_marshoai/plugin/func_call/register.py +++ /dev/null @@ -1,126 +0,0 @@ -"""此模块用于获取function call中函数定义信息以及注册函数 -""" - -from typing import TypeVar - -from nonebot import logger - -from ..docstring.parser import parse -from ..typing import ( - ASYNC_FUNCTION_CALL_FUNC, - FUNCTION_CALL_FUNC, - SYNC_FUNCTION_CALL_FUNC, -) -from .params import * - -F = TypeVar("F", bound=FUNCTION_CALL_FUNC) - -_loaded_functions: dict[str, FUNCTION_CALL_FUNC] = {} - - -def async_wrapper(func: SYNC_FUNCTION_CALL_FUNC) -> ASYNC_FUNCTION_CALL_FUNC: - """将同步函数包装为异步函数,但是不会真正异步执行,仅用于统一调用及函数签名 - - Args: - func: 同步函数 - - Returns: - ASYNC_FUNCTION_CALL: 异步函数 - """ - - async def wrapper(*args, **kwargs) -> str: - return func(*args, **kwargs) - - return wrapper - - -def function_call(func: F) -> F: - """返回一个装饰器,装饰一个函数, 使其注册为一个可被AI调用的function call函数 - - Args: - func: 函数对象,要有完整的 Google Style Docstring - - Returns: - str: 函数定义信息 - """ - # TODO - # pre check docstring - if not func.__doc__: - logger.error(f"函数 {func.__name__} 没有文档字串,不被加载") - return func - else: - # 解析函数文档字串 - result = parse(docstring=func.__doc__) - logger.debug(result.reduction()) - return func - - -def caller( - description: str | None = None, - parameters: dict[str, P] | None = None, -): - """返回一个装饰器,装饰一个函数, 使其注册为一个可被AI调用的function call函数 - - Args: - description: 函数描述 - parameters: 函数参数 - - Returns: - str: 函数定义信息 - """ - - def decorator(func: FUNCTION_CALL_FUNC) -> FUNCTION_CALL_FUNC: - # TODO - # pre check docstring - if not func.__doc__: - logger.error(f"函数 {func.__name__} 没有文档字串,不被加载") - return func - else: - # 解析函数文档字串 - result = parse(docstring=func.__doc__) - logger.debug(result.reduction()) - return func - - return decorator - - -# TODO 草案 -# @caller( -# description="这个函数用来给你算命", -# parameters={ -# "birthday": String(description="生日"), -# "gender": String(enum=["男", "女"], description="性别"), -# "name": String(description="姓名"), -# }, -# ) -# async def tell_fortune(birthday: str, name: str, gender: str) -> str: -# """这个函数用来给你算命 - -# Args: -# birthday: 生日 -# name: 姓名 - -# Returns: -# str: 算命结果 -# """ -# return f"{name},你的生日是{birthday},你的运势是大吉大利" - - -@caller( - description="这个函数用来给你算命", -).parameters( - birthday=String(description="生日"), - name=String(enum=["男", "女"], description="性别"), - gender=String(description="姓名"), -) -async def tell_fortune(birthday: str, name: str, gender: str) -> str: - """这个函数用来给你算命 - - Args: - birthday: 生日 - name: 姓名 - - Returns: - str: 算命结果 - """ - return f"{name},你的生日是{birthday},你的运势是大吉大利" diff --git a/nonebot_plugin_marshoai/plugins/marshoai_bangumi/__init__.py b/nonebot_plugin_marshoai/plugins/marshoai_bangumi/__init__.py index bb4a85bc..6825364d 100755 --- a/nonebot_plugin_marshoai/plugins/marshoai_bangumi/__init__.py +++ b/nonebot_plugin_marshoai/plugins/marshoai_bangumi/__init__.py @@ -2,13 +2,7 @@ import traceback import httpx -from nonebot_plugin_marshoai.plugin import ( - Integer, - PluginMetadata, - String, - function_call, - on_function_call, -) +from nonebot_plugin_marshoai.plugin import PluginMetadata __marsho_meta__ = PluginMetadata( name="Bangumi 番剧信息", @@ -17,42 +11,3 @@ __marsho_meta__ = PluginMetadata( author="Liteyuki", homepage="", ) - - -@function_call -async def fetch_calendar(): - """获取今天日期""" - url = "https://api.bgm.tv/calendar" - headers = { - "User-Agent": "LiteyukiStudio/nonebot-plugin-marshoai (https://github.com/LiteyukiStudio/nonebot-plugin-marshoai)" - } - async with httpx.AsyncClient() as client: - response = await client.get(url, headers=headers) - # print(response.text) - return response.json() - - -@function_call -async def get_bangumi_news() -> str: - """获取今天的新番(动漫)列表,在调用之前,你需要知道今天星期几。""" - result = await fetch_calendar() - info = "" - try: - for i in result: - weekday = i["weekday"]["cn"] - # print(weekday) - info += f"{weekday}:" - items = i["items"] - for item in items: - name = item["name_cn"] - info += f"《{name}》" - info += "\n" - return info - except Exception as e: - traceback.print_exc() - return "" - - -@function_call -def test_sync() -> str: - return "sync"