完成一个机器人的基本模型

This commit is contained in:
Soulter
2022-12-08 17:45:07 +08:00
commit 7812cbda32
2 changed files with 62 additions and 0 deletions
+29
View File
@@ -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
+33
View File
@@ -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")