feat: 基本架构

This commit is contained in:
Rock Chin 2022-12-07 22:27:05 +08:00
parent 4ed13bcdf2
commit 4303487d32
11 changed files with 126 additions and 5 deletions

View File

@ -1,6 +1,5 @@
mirai_http_api_config = { mirai_http_api_config = {
"host": "", "host": "",
"port": 8080,
"verifyKey": "", "verifyKey": "",
"qq": 0 "qq": 0
} }
@ -18,5 +17,10 @@ openai_config = {
} }
completion_api_params = { completion_api_params = {
"model": "text-davinci-003",
"temperature": 0.9,
"max_tokens": 1024,
"top_p": 1,
"frequency_penalty": 0.4,
"presence_penalty": 0.3,
} }

View File

@ -12,7 +12,7 @@ def main():
assert os.path.exists('config.py') assert os.path.exists('config.py')
import config import config
print(config.mirai_http_api_config) # print(config.mirai_http_api_config)
if __name__ == '__main__': if __name__ == '__main__':

0
pkg/__init__.py Normal file
View File

0
pkg/database/__init__.py Normal file
View File

35
pkg/database/manager.py Normal file
View File

@ -0,0 +1,35 @@
import pymysql
inst = None
class DatabaseManager:
host = ''
port = 0
user = ''
password = ''
database = ''
conn = None
cursor = None
def __init__(self, host: str, port: int, user: str, password: str, database: str):
self.host = host
self.port = port
self.user = user
self.password = password
self.database = database
self.reconnect()
global inst
inst = self
def reconnect(self):
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password,
database=self.database)
self.cursor = self.conn.cursor()
def get_inst() -> DatabaseManager:
global inst
return inst

0
pkg/openai/__init__.py Normal file
View File

30
pkg/openai/manager.py Normal file
View File

@ -0,0 +1,30 @@
import openai
inst = None
class OpenAIInteract:
api_key = ''
api_params = {}
def __init__(self, api_key: str, api_params: dict):
self.api_key = api_key
self.api_params = api_params
openai.api_key = self.api_key
global inst
inst = self
def request_completion(self, prompt, stop):
response = openai.Completion.create(
prompt=prompt,
stop=stop,
**self.api_params
)
return response
def get_inst() -> OpenAIInteract:
global inst
return inst

52
pkg/openai/session.py Normal file
View File

@ -0,0 +1,52 @@
import time
import pkg.openai.manager
session = {}
# 通用的OpenAI API交互session
class Session:
name = ''
prompt = ''
user_name = 'You'
bot_name = 'Bot'
create_timestamp = 0
last_interact_timestamp = 0
def __init__(self, name: str):
self.name = name
self.create_timestamp = int(time.time())
global session
session[name] = self
# 请求回复
# 这个函数是阻塞的
def append(self, text: str) -> str:
self.prompt += self.user_name + ':' + text + '\n'+self.bot_name+':'
self.last_interact_timestamp = int(time.time())
# 向API请求补全
response = pkg.openai.manager.get_inst().request_completion(self.prompt, self.user_name+':')
# 处理回复
res_test = response["choices"][0]["text"]
res_ans = res_test
# 去除开头可能的提示
res_ans_spt = res_test.split("\n\n")
if len(res_ans_spt) > 1:
del (res_ans_spt[0])
res_ans = '\n\n'.join(res_ans_spt)
self.prompt += "\n" + self.bot_name + ":{}".format(res_ans)
return res_ans
def persistence(self):
pass

0
pkg/qqbot/__init__.py Normal file
View File

0
pkg/qqbot/manager.py Normal file
View File

0
tests/__init__.py Normal file
View File