From 7812cbda32a42a0d094156464baf9ad294c3e5f1 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Thu, 8 Dec 2022 17:45:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=80=E4=B8=AA=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E4=BA=BA=E7=9A=84=E5=9F=BA=E6=9C=AC=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/openai/core.py | 29 +++++++++++++++++++++++++++++ cores/qqbot/core.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 cores/openai/core.py create mode 100644 cores/qqbot/core.py diff --git a/cores/openai/core.py b/cores/openai/core.py new file mode 100644 index 000000000..1f48c8416 --- /dev/null +++ b/cores/openai/core.py @@ -0,0 +1,29 @@ +import openai +import yaml + + +inst = None + +class ChatGPT: + def __init__(self, chatGPT_configs): + with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile: + cfg = yaml.safe_load(ymlfile) + if cfg['openai']['key'] != '': + print("读取ChatGPT Key成功") + openai.api_key = cfg['openai']['key'] + else: + print("请先去完善ChatGPT的Key。详情请前往https://beta.openai.com/account/api-keys") + self.chatGPT_configs = chatGPT_configs + global inst + inst = self + + async def chat(self, prompt): + print("[ChatGPT] 接收到prompt: "+prompt) + response = openai.Completion.create( + self.chatGPT_configs + ) + return response["choices"][0]["text"] + +def getInst() -> ChatGPT: + global inst + return inst \ No newline at end of file diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py new file mode 100644 index 000000000..ae62384f2 --- /dev/null +++ b/cores/qqbot/core.py @@ -0,0 +1,33 @@ +import botpy +from botpy.message import Message +import yaml +import asyncio +import cores.openai.core +import re + +chatgpt = "" + +session_list = [] + +class botClient(botpy.Client): + async def on_at_message_create(self, message: Message): + # 过滤@头 + pattern = r"<@!\d+>\s+(.+)" + result = re.search(pattern, message.content) + if result: + qq_msg = "[ChatGPT]"+result.group(1).strip() + chatgpt_res = await chatgpt.chat(qq_msg) + await message.reply(content=f"{chatgpt_res}") + +def initBot(chatgpt_inst): + global chatgpt + chatgpt = chatgpt_inst + with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile: + cfg = yaml.safe_load(ymlfile) + if cfg['qqbot']['appid'] != '' or cfg['qqbot']['token'] != '': + print(cfg['qqbot']['appid']) + intents = botpy.Intents(public_guild_messages=True) + client = botClient(intents=intents) + client.run(appid=cfg['qqbot']['appid'], token=cfg['qqbot']['token']) + else: + raise BaseException("请在config中完善你的appid和token") \ No newline at end of file