From 6120805853ae8ccefc6a087929d22a88a91db4a2 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 15:44:37 +0800 Subject: [PATCH 1/9] =?UTF-8?q?perf:=20=E4=B8=8D=E5=86=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=99=90=E5=88=B6=E6=8F=90=E4=BA=A4=E7=9A=84=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E5=9B=9E=E5=90=88=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- config-template.py | 5 ----- pkg/openai/session.py | 3 ++- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index eac84f11..160aa2fb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ ✅回复符合上下文 - 程序向模型发送近几次对话内容,模型根据上下文生成回复 - - 您可在`config.py`中修改`prompt_submit_round_amount`及`process_message_timeout`自定义联系上下文的范围 + - 您可在`config.py`中修改`process_message_timeout`自定义联系上下文的范围 diff --git a/config-template.py b/config-template.py index 7ccd4279..923ea91c 100644 --- a/config-template.py +++ b/config-template.py @@ -66,11 +66,6 @@ completion_api_params = { # 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快 prompt_submit_length = 1536 -# 每次向OpenAI接口发送对话记录上下文的聊天回合数 -# 不建议过大,向OpenAI接口发送对话上下文时保证内容不超过prompt_submit_length个字符, -# 不超过prompt_submit_round_amount个回合 -prompt_submit_round_amount = 16 - # 消息处理的超时时间,单位为秒 process_message_timeout = 15 diff --git a/pkg/openai/session.py b/pkg/openai/session.py index b7d60aca..8e65a338 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -139,7 +139,8 @@ class Session: def append(self, text: str) -> str: self.last_interact_timestamp = int(time.time()) - max_rounds = config.prompt_submit_round_amount if hasattr(config, 'prompt_submit_round_amount') else 7 + # max_rounds = config.prompt_submit_round_amount if hasattr(config, 'prompt_submit_round_amount') else 7 + max_rounds = 1000 # 不再限制回合数 max_length = config.prompt_submit_length if hasattr(config, "prompt_submit_length") else 1024 # 向API请求补全 From f33f205be3cd1065ee280eb092882b187a9b444d Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 16:07:02 +0800 Subject: [PATCH 2/9] =?UTF-8?q?perf:=20=E6=95=B4=E7=90=86config-template.p?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/config-template.py b/config-template.py index 923ea91c..14f73a5b 100644 --- a/config-template.py +++ b/config-template.py @@ -28,21 +28,9 @@ openai_config = { }, } -# 单个api-key的使用量警告阈值 -# 当使用此api-key进行请求的文字量达到此阈值时,会在控制台输出警告并通知管理员 -# 若之后还有未使用超过此值的api-key,则会切换到新的api-key进行请求 -api_key_usage_threshold = 900000 - # 管理员QQ号,用于接收报错等通知,为0时不发送通知 admin_qq = 0 -# 回复消息时是否显示[GPT]前缀 -show_prefix = False - -# 敏感词过滤开关,以同样数量的*代替敏感词回复 -# 请在sensitive.json中添加敏感词 -sensitive_word_filter = True - # 每个会话的预设信息,影响所有会话,无视指令重置 # 可以通过这个字段指定某些情况的回复,可直接用自然语言描述指令 # 例如: 如果我之后想获取帮助,请你说“输入!help获取帮助”, @@ -50,25 +38,37 @@ sensitive_word_filter = True # 可参考 https://github.com/PlexPt/awesome-chatgpt-prompts-zh default_prompt = "如果我之后想获取帮助,请你说“输入!help获取帮助”" -# OpenAI的completion API的参数 -# 具体请查看OpenAI的文档 -completion_api_params = { - "model": "text-davinci-003", - "temperature": 0.6, # 数值越低得到的回答越理性,取值范围[0, 1] - "max_tokens": 512, # 每次向OpenAI请求的最大字符数, 不高于4096 - "top_p": 1, # 生成的文本的文本与要求的符合度, 取值范围[0, 1] - "frequency_penalty": 0.2, - "presence_penalty": 0.4, -} +# 单个api-key的使用量警告阈值 +# 当使用此api-key进行请求的文字量达到此阈值时,会在控制台输出警告并通知管理员 +# 若之后还有未使用超过此值的api-key,则会切换到新的api-key进行请求 +api_key_usage_threshold = 900000 + +# 敏感词过滤开关,以同样数量的*代替敏感词回复 +# 请在sensitive.json中添加敏感词 +sensitive_word_filter = True # 每次向OpenAI接口发送对话记录上下文的字符数 # 最大不超过(4096 - max_tokens)个字符,max_tokens为上述completion_api_params中的max_tokens # 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快 prompt_submit_length = 1536 +# OpenAI的completion API的参数 +# 具体请查看OpenAI的文档: https://beta.openai.com/docs/api-reference/completions/create +completion_api_params = { + "model": "text-davinci-003", + "temperature": 0.6, # 数值越低得到的回答越理性,取值范围[0, 1] + "max_tokens": 512, # 每次向OpenAI请求的最大字符数, 不高于4096 + "top_p": 1, # 生成的文本的文本与要求的符合度, 取值范围[0, 1] + "frequency_penalty": 0.2, + "presence_penalty": 1.0, +} + # 消息处理的超时时间,单位为秒 process_message_timeout = 15 +# 回复消息时是否显示[GPT]前缀 +show_prefix = False + # 消息处理超时重试次数 retry_times = 3 From 2921a20b85fcd42fa3a9349a1715e45843e411c7 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 16:47:13 +0800 Subject: [PATCH 3/9] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BE=A4?= =?UTF-8?q?=E5=86=85=E5=93=8D=E5=BA=94=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config-template.py b/config-template.py index 14f73a5b..6505a13e 100644 --- a/config-template.py +++ b/config-template.py @@ -38,6 +38,17 @@ admin_qq = 0 # 可参考 https://github.com/PlexPt/awesome-chatgpt-prompts-zh default_prompt = "如果我之后想获取帮助,请你说“输入!help获取帮助”" +# 群内响应规则 +# 符合此消息的群内消息即使不包含at机器人也会响应 +# 支持消息前缀匹配及正则表达式匹配 +# 注意:由消息前缀(prefix)匹配的消息中将会删除此前缀,正则表达式匹配的消息不会删除匹配的部分 +# 前缀匹配优先级高于正则表达式匹配 +# 正则表达式简明教程:https://www.runoob.com/regexp/regexp-tutorial.html +response_rules = { + "prefix": ["/ai", "!ai", "!ai", "/chat", "!chat", "!chat"], + "regexp": ["为什么.*", "怎么?样.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ] +} + # 单个api-key的使用量警告阈值 # 当使用此api-key进行请求的文字量达到此阈值时,会在控制台输出警告并通知管理员 # 若之后还有未使用超过此值的api-key,则会切换到新的api-key进行请求 From 42a85ddbb105fcdf322526b88471f5e7b9cd64e4 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:07:31 +0800 Subject: [PATCH 4/9] =?UTF-8?q?feat:=20=E7=BE=A4=E5=86=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=8Dat=E6=9C=BA=E5=99=A8=E4=BA=BA=E4=B9=9F?= =?UTF-8?q?=E8=83=BD=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index ec7c2090..b06e7be4 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -28,6 +28,24 @@ def go(func, args=()): thread.start() +# 检查消息是否符合泛响应匹配机制 +def check_response_rule(text: str) -> (bool, str): + rules = config.response_rules + # 检查前缀匹配 + for rule in rules['prefix']: + if text.startswith(rule): + return True, text.replace(rule, "", 1) + + # 检查正则表达式匹配 + for rule in rules['regex']: + import re + match = re.match(rule, text) + if match: + return True, match.group(1) + + return False, "" + + # 控制QQ消息输入输出的类 class QQBotManager: timeout = 60 @@ -265,11 +283,8 @@ class QQBotManager: reply = '' - if Image in event.message_chain: - pass - elif At(self.bot.qq) not in event.message_chain: - pass - else: + def process(text = None) -> str: + replys = "" event.message_chain.remove(At(self.bot.qq)) processing.append("group_{}".format(event.sender.id)) @@ -278,7 +293,8 @@ class QQBotManager: failed = 0 for i in range(self.retry): try: - reply = self.process_message('group', event.group.id, str(event.message_chain).strip()) + replys = self.process_message('group', event.group.id, + str(event.message_chain).strip() if text is None else text) break except FunctionTimedOut: failed += 1 @@ -286,7 +302,20 @@ class QQBotManager: if failed == self.retry: self.notify_admin("{} 请求超时".format("group_{}".format(event.sender.id))) - reply = "[bot]err:请求超时" + replys = "[bot]err:请求超时" + + return replys + + if Image in event.message_chain: + pass + elif At(self.bot.qq) not in event.message_chain: + check, result = check_response_rule(str(event.message_chain).strip()) + + if check: + reply = process(result) + else: + # 直接调用 + reply = process() if reply != '': return self.send(event, reply) From 0227ff0329bb026814bf1ed9ed3d21e740dedbc8 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:15:17 +0800 Subject: [PATCH 5/9] =?UTF-8?q?fix:=20=E5=8C=B9=E9=85=8D=E8=A7=84=E5=88=99?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AE=B9=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index b06e7be4..fb0c8e96 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -30,18 +30,23 @@ def go(func, args=()): # 检查消息是否符合泛响应匹配机制 def check_response_rule(text: str) -> (bool, str): + if not hasattr(config, 'response_rules'): + return False, '' + rules = config.response_rules # 检查前缀匹配 - for rule in rules['prefix']: - if text.startswith(rule): - return True, text.replace(rule, "", 1) + if 'prefix' in rules: + for rule in rules['prefix']: + if text.startswith(rule): + return True, text.replace(rule, "", 1) # 检查正则表达式匹配 - for rule in rules['regex']: - import re - match = re.match(rule, text) - if match: - return True, match.group(1) + if 'regexp' in rules: + for rule in rules['regexp']: + import re + match = re.match(rule, text) + if match: + return True, match.group(1) return False, "" From d762fea382268f729e63f3b06038c3fc80f7a622 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:16:34 +0800 Subject: [PATCH 6/9] =?UTF-8?q?debug:=20=E8=BE=93=E5=87=BA=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=E5=8C=B9=E9=85=8D=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index fb0c8e96..b97f8418 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -46,6 +46,7 @@ def check_response_rule(text: str) -> (bool, str): import re match = re.match(rule, text) if match: + print(match) return True, match.group(1) return False, "" From 23df6c07073de35d4e8372e228ef7347ffd70fa5 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:17:49 +0800 Subject: [PATCH 7/9] =?UTF-8?q?fix:=20=E5=8C=B9=E9=85=8D=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=E5=8E=9F=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index b97f8418..b3783d0e 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -46,8 +46,7 @@ def check_response_rule(text: str) -> (bool, str): import re match = re.match(rule, text) if match: - print(match) - return True, match.group(1) + return True, text return False, "" From eada778ab5b215e68213bd57040e5a2151113059 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:19:09 +0800 Subject: [PATCH 8/9] =?UTF-8?q?fix:=20=E4=BB=8E=E6=B6=88=E6=81=AF=E9=93=BE?= =?UTF-8?q?=E4=B8=AD=E7=A7=BB=E9=99=A4at=20me=E5=89=8D=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index b3783d0e..84e96596 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -290,7 +290,8 @@ class QQBotManager: def process(text = None) -> str: replys = "" - event.message_chain.remove(At(self.bot.qq)) + if At(self.bot.qq) in event.message_chain: + event.message_chain.remove(At(self.bot.qq)) processing.append("group_{}".format(event.sender.id)) From 1f5329edc9a7f5c6412a78d8943b1af2bb2b9fe5 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 19 Dec 2022 17:25:09 +0800 Subject: [PATCH 9/9] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 2 +- pkg/qqbot/manager.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config-template.py b/config-template.py index 6505a13e..63eba7b0 100644 --- a/config-template.py +++ b/config-template.py @@ -45,7 +45,7 @@ default_prompt = "如果我之后想获取帮助,请你说“输入!help获取 # 前缀匹配优先级高于正则表达式匹配 # 正则表达式简明教程:https://www.runoob.com/regexp/regexp-tutorial.html response_rules = { - "prefix": ["/ai", "!ai", "!ai", "/chat", "!chat", "!chat"], + "prefix": ["/ai", "!ai", "!ai", "ai"], "regexp": ["为什么.*", "怎么?样.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ] } diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 84e96596..33752178 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -318,7 +318,7 @@ class QQBotManager: check, result = check_response_rule(str(event.message_chain).strip()) if check: - reply = process(result) + reply = process(result.strip()) else: # 直接调用 reply = process()