diff --git a/nonebot_plugin_marshoai/__init__.py b/nonebot_plugin_marshoai/__init__.py index e0807e1e..d9728f74 100644 --- a/nonebot_plugin_marshoai/__init__.py +++ b/nonebot_plugin_marshoai/__init__.py @@ -3,6 +3,7 @@ from nonebot.plugin import require require("nonebot_plugin_alconna") require("nonebot_plugin_localstore") from .azure import * +from .hunyuan import * from nonebot import get_driver, logger from .config import config from .metadata import metadata diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index c635c95a..d0b12573 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -110,7 +110,8 @@ async def load_context(target: MsgTarget, arg: Message = CommandArg()): @resetmem_cmd.handle() async def resetmem(target: MsgTarget): - if [target.id, target.private] not in target_list: target_list.append([target.id, target.private]) + if [target.id, target.private] not in target_list: + target_list.append([target.id, target.private]) context.reset(target.id, target.private) await resetmem_cmd.finish("上下文已重置") diff --git a/nonebot_plugin_marshoai/config.py b/nonebot_plugin_marshoai/config.py index f9b425d3..7194ca06 100644 --- a/nonebot_plugin_marshoai/config.py +++ b/nonebot_plugin_marshoai/config.py @@ -29,6 +29,8 @@ class ConfigModel(BaseModel): marshoai_max_tokens: int | None = None marshoai_top_p: float | None = None marshoai_additional_image_models: list = [] + marshoai_tencent_secretid: str | None = None + marshoai_tencent_secretkey:str | None = None config: ConfigModel = get_plugin_config(ConfigModel) diff --git a/nonebot_plugin_marshoai/hunyuan.py b/nonebot_plugin_marshoai/hunyuan.py new file mode 100644 index 00000000..504fd922 --- /dev/null +++ b/nonebot_plugin_marshoai/hunyuan.py @@ -0,0 +1,36 @@ +import contextlib +import traceback +import json +from typing import Optional + +from arclet.alconna import Alconna, Args, AllParam +from nonebot import on_command, logger +from nonebot.adapters import Message, Event +from nonebot.params import CommandArg +from nonebot.permission import SUPERUSER +from nonebot_plugin_alconna import on_alconna, MsgTarget +from nonebot_plugin_alconna.uniseg import UniMessage, UniMsg +from nonebot import get_driver +from .constants import * +from .metadata import metadata +from .models import MarshoContext +from .util_hunyuan import * +from .config import config +genimage_cmd = on_alconna( + Alconna( + "genimage", + Args["prompt?", str], + ) + ) + +@genimage_cmd.handle() +async def genimage(event: Event, prompt=None): + if not prompt: + await genimage_cmd.finish("无提示词") + try: + result = generate_image(prompt) + url = json.loads(result)["ResultImage"] + await UniMessage.image(url=url).send() + except Exception as e: + # await genimage_cmd.finish(str(e)) + traceback.print_exc() diff --git a/nonebot_plugin_marshoai/util_hunyuan.py b/nonebot_plugin_marshoai/util_hunyuan.py new file mode 100644 index 00000000..3d0f9942 --- /dev/null +++ b/nonebot_plugin_marshoai/util_hunyuan.py @@ -0,0 +1,31 @@ +import json +import types +from tencentcloud.common import credential +from tencentcloud.common.profile.client_profile import ClientProfile +from tencentcloud.common.profile.http_profile import HttpProfile +from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException +from tencentcloud.hunyuan.v20230901 import hunyuan_client, models +from .config import config +def generate_image(prompt: str): + cred = credential.Credential(config.marshoai_tencent_secretid, config.marshoai_tencent_secretkey) + # 实例化一个http选项,可选的,没有特殊需求可以跳过 + httpProfile = HttpProfile() + httpProfile.endpoint = "hunyuan.tencentcloudapi.com" + + # 实例化一个client选项,可选的,没有特殊需求可以跳过 + clientProfile = ClientProfile() + clientProfile.httpProfile = httpProfile + client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", clientProfile) + + req = models.TextToImageLiteRequest() + params = { + "Prompt": prompt, + "RspImgType": "url", + "Resolution": "1080:1920" + } + req.from_json_string(json.dumps(params)) + + # 返回的resp是一个TextToImageLiteResponse的实例,与请求对象对应 + resp = client.TextToImageLite(req) + # 输出json格式的字符串回包 + return resp.to_json_string()