From 4303487d327dfc6e2fca9f06fefce17748abd1aa Mon Sep 17 00:00:00 2001 From: Rock Chin Date: Wed, 7 Dec 2022 22:27:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9F=BA=E6=9C=AC=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 10 +++++--- main.py | 4 ++-- pkg/__init__.py | 0 pkg/database/__init__.py | 0 pkg/database/manager.py | 35 +++++++++++++++++++++++++++ pkg/openai/__init__.py | 0 pkg/openai/manager.py | 30 +++++++++++++++++++++++ pkg/openai/session.py | 52 ++++++++++++++++++++++++++++++++++++++++ pkg/qqbot/__init__.py | 0 pkg/qqbot/manager.py | 0 tests/__init__.py | 0 11 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 pkg/__init__.py create mode 100644 pkg/database/__init__.py create mode 100644 pkg/database/manager.py create mode 100644 pkg/openai/__init__.py create mode 100644 pkg/openai/manager.py create mode 100644 pkg/openai/session.py create mode 100644 pkg/qqbot/__init__.py create mode 100644 pkg/qqbot/manager.py create mode 100644 tests/__init__.py diff --git a/config-template.py b/config-template.py index 2c60ca9..c368ccb 100644 --- a/config-template.py +++ b/config-template.py @@ -1,6 +1,5 @@ mirai_http_api_config = { "host": "", - "port": 8080, "verifyKey": "", "qq": 0 } @@ -18,5 +17,10 @@ openai_config = { } completion_api_params = { - -} \ No newline at end of file + "model": "text-davinci-003", + "temperature": 0.9, + "max_tokens": 1024, + "top_p": 1, + "frequency_penalty": 0.4, + "presence_penalty": 0.3, +} diff --git a/main.py b/main.py index f8a5a23..d4be11d 100644 --- a/main.py +++ b/main.py @@ -12,8 +12,8 @@ def main(): assert os.path.exists('config.py') import config - print(config.mirai_http_api_config) + # print(config.mirai_http_api_config) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/pkg/__init__.py b/pkg/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkg/database/__init__.py b/pkg/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkg/database/manager.py b/pkg/database/manager.py new file mode 100644 index 0000000..2a81433 --- /dev/null +++ b/pkg/database/manager.py @@ -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 diff --git a/pkg/openai/__init__.py b/pkg/openai/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkg/openai/manager.py b/pkg/openai/manager.py new file mode 100644 index 0000000..5cbd501 --- /dev/null +++ b/pkg/openai/manager.py @@ -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 diff --git a/pkg/openai/session.py b/pkg/openai/session.py new file mode 100644 index 0000000..5b5dc42 --- /dev/null +++ b/pkg/openai/session.py @@ -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 diff --git a/pkg/qqbot/__init__.py b/pkg/qqbot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29