diff --git a/nonebot_plugin_marshoai/decos.py b/nonebot_plugin_marshoai/decos.py new file mode 100644 index 0000000..66a12e3 --- /dev/null +++ b/nonebot_plugin_marshoai/decos.py @@ -0,0 +1,15 @@ +from .instances import cache + + +def from_cache(key): + def decorator(func): + def wrapper(*args, **kwargs): + cached = cache.get(key) + if cached: + return cached + else: + result = func(*args, **kwargs) + cache.set(key, result) + return result + + return wrapper diff --git a/nonebot_plugin_marshoai/instances.py b/nonebot_plugin_marshoai/instances.py index d5aaf70..d450d2d 100644 --- a/nonebot_plugin_marshoai/instances.py +++ b/nonebot_plugin_marshoai/instances.py @@ -3,7 +3,7 @@ from nonebot import get_driver from openai import AsyncOpenAI from .config import config -from .models import MarshoContext, MarshoTools +from .models import Cache, MarshoContext, MarshoTools driver = get_driver() @@ -11,6 +11,7 @@ command_start = driver.config.command_start model_name = config.marshoai_default_model context = MarshoContext() tools = MarshoTools() +cache = Cache() token = config.marshoai_token endpoint = config.marshoai_azure_endpoint # client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(token)) diff --git a/nonebot_plugin_marshoai/models.py b/nonebot_plugin_marshoai/models.py index 47f02c8..701f76a 100755 --- a/nonebot_plugin_marshoai/models.py +++ b/nonebot_plugin_marshoai/models.py @@ -11,6 +11,25 @@ from nonebot import logger from .config import config +class Cache: + """ + 缓存类 + """ + + def __init__(self): + self.cache = {} + + def get(self, key): + if key in self.cache: + return self.cache[key] + else: + self.cache[key] = None + return None + + def set(self, key, value): + self.cache[key] = value + + class MarshoContext: """ Marsho 的上下文类 diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index bceef64..0fbd6ec 100755 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -23,6 +23,7 @@ from ._types import DeveloperMessage from .config import config from .constants import CODE_BLOCK_PATTERN, IMG_LATEX_PATTERN, OPENAI_NEW_MODELS from .deal_latex import ConvertLatex +from .instances import cache nickname_json = None # 记录昵称 praises_json = None # 记录夸赞名单