From 852adb72a234e4283b164aa71bc736698d7b124f Mon Sep 17 00:00:00 2001 From: B1gM8c <89020353+B1gM8c@users.noreply.github.com> Date: Mon, 20 Mar 2023 01:17:29 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=94=AF=E6=8C=81Wechaty=E7=9A=84=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=89=8D=E7=BC=80+=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E8=AF=8D=E7=94=9F=E6=88=90AI=E5=9B=BE=E7=89=87=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wechaty判断is_at为True,返回的内容是过滤掉@之后的内容;而is_at为False,则会返回完整的内容 故判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容,用于实现类似自定义+前缀触发生成AI图片的功能 --- channel/wechat/wechaty_channel.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/channel/wechat/wechaty_channel.py b/channel/wechat/wechaty_channel.py index 79ce601..a958d85 100644 --- a/channel/wechat/wechaty_channel.py +++ b/channel/wechat/wechaty_channel.py @@ -147,6 +147,13 @@ class WechatyChannel(Channel): match_prefix = (is_at and not config.get("group_at_off", False)) \ or self.check_prefix(content, config.get('group_chat_prefix')) \ or self.check_contain(content, config.get('group_chat_keyword')) + # Wechaty判断is_at为True,返回的内容是过滤掉@之后的内容;而is_at为False,则会返回完整的内容 + # 故判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容,用于实现类似自定义+前缀触发生成AI图片的功能 + prefixes = config.get('group_chat_prefix') + for prefix in prefixes: + if content.startswith(prefix): + content = content.replace(prefix, '', 1).strip() + break if ('ALL_GROUP' in config.get('group_name_white_list') or room_name in config.get( 'group_name_white_list') or self.check_contain(room_name, config.get( 'group_name_keyword_white_list'))) and match_prefix: From 3f889ab75f9e1cd1cd61326c4fabfd50da521154 Mon Sep 17 00:00:00 2001 From: goldfishh Date: Mon, 20 Mar 2023 22:18:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feature(rate-limit):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BB=A4=E7=89=8C=E6=A1=B6=E7=B1=BB=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E9=99=90=E5=88=B6=E8=B0=83=E7=94=A8gpt3.5,?= =?UTF-8?q?=20dalle=E6=8E=A5=E5=8F=A3=E9=A2=91=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/chatgpt/chat_gpt_bot.py | 11 ++++++++- common/token_bucket.py | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 common/token_bucket.py diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index b5c2a5b..71e1d9d 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -3,6 +3,7 @@ from bot.bot import Bot from config import conf, load_config from common.log import logger +from common.token_bucket import TokenBucket from common.expired_dict import ExpiredDict import openai import time @@ -21,6 +22,10 @@ class ChatGPTBot(Bot): proxy = conf().get('proxy') if proxy: openai.proxy = proxy + if conf().get('rate_limit_chatgpt'): + self.tb4chatgpt = TokenBucket(conf().get('rate_limit_chatgpt', 20)) + if conf().get('rate_limit_dalle'): + self.tb4dalle = TokenBucket(conf().get('rate_limit_dalle', 50)) def reply(self, query, context=None): # acquire reply content @@ -63,6 +68,8 @@ class ChatGPTBot(Bot): :return: {} ''' try: + if conf().get('rate_limit_chatgpt') and not self.tb4chatgpt.get_token(): + return {"completion_tokens": 0, "content": "提问太快啦,请休息一下再问我吧"} response = openai.ChatCompletion.create( model= conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称 messages=session, @@ -102,6 +109,8 @@ class ChatGPTBot(Bot): def create_img(self, query, retry_count=0): try: + if conf().get('rate_limit_dalle') and not self.tb4dalle.get_token(): + return "请求太快了,请休息一下再问我吧" logger.info("[OPEN_AI] image_query={}".format(query)) response = openai.Image.create( prompt=query, #图片描述 @@ -118,7 +127,7 @@ class ChatGPTBot(Bot): logger.warn("[OPEN_AI] ImgCreate RateLimit exceed, 第{}次重试".format(retry_count+1)) return self.create_img(query, retry_count+1) else: - return "提问太快啦,请休息一下再问我吧" + return "请求太快啦,请休息一下再问我吧" except Exception as e: logger.exception(e) return None diff --git a/common/token_bucket.py b/common/token_bucket.py new file mode 100644 index 0000000..23901b6 --- /dev/null +++ b/common/token_bucket.py @@ -0,0 +1,45 @@ +import threading +import time + + +class TokenBucket: + def __init__(self, tpm, timeout=None): + self.capacity = int(tpm) # 令牌桶容量 + self.tokens = 0 # 初始令牌数为0 + self.rate = int(tpm) / 60 # 令牌每秒生成速率 + self.timeout = timeout # 等待令牌超时时间 + self.cond = threading.Condition() # 条件变量 + self.is_running = True + # 开启令牌生成线程 + threading.Thread(target=self._generate_tokens).start() + + def _generate_tokens(self): + """生成令牌""" + while self.is_running: + with self.cond: + if self.tokens < self.capacity: + self.tokens += 1 + self.cond.notify() # 通知获取令牌的线程 + time.sleep(1 / self.rate) + + def get_token(self): + """获取令牌""" + with self.cond: + while self.tokens <= 0: + flag = self.cond.wait(self.timeout) + if not flag: # 超时 + return False + self.tokens -= 1 + return True + + def close(self): + self.is_running = False + + +if __name__ == "__main__": + token_bucket = TokenBucket(20, None) # 创建一个每分钟生产20个tokens的令牌桶 + # token_bucket = TokenBucket(20, 0.1) + for i in range(3): + if token_bucket.get_token(): + print(f"第{i+1}次请求成功") + token_bucket.close() From 3d264207a8ecac53ed7b6137986f61c233dcb821 Mon Sep 17 00:00:00 2001 From: lichengzhe <38408577@qq.com> Date: Tue, 21 Mar 2023 22:12:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A6=82=E5=90=AF=E7=94=A8hot=5Freload?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=A4=84=E7=90=861=E5=88=86=E9=92=9F?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E5=8E=86=E5=8F=B2=E6=B6=88=E6=81=AF=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E9=87=8D=E5=A4=8D=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_channel.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 5a06227..45fd537 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -14,6 +14,7 @@ from common.tmp_dir import TmpDir from config import conf import requests import io +import time thread_pool = ThreadPoolExecutor(max_workers=8) @@ -74,7 +75,11 @@ class WechatChannel(Channel): from_user_id = msg['FromUserName'] to_user_id = msg['ToUserName'] # 接收人id other_user_id = msg['User']['UserName'] # 对手方id + create_time = msg['CreateTime'] # 消息时间 match_prefix = self.check_prefix(content, conf().get('single_chat_prefix')) + if conf().get('hot_reload') == True and int(create_time) < int(time.time()) - 60: #跳过1分钟前的历史消息 + logger.debug("[WX]history message skipped") + return if "」\n- - - - - - - - - - - - - - -" in content: logger.debug("[WX]reference query skipped") return @@ -108,6 +113,10 @@ class WechatChannel(Channel): logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) group_name = msg['User'].get('NickName', None) group_id = msg['User'].get('UserName', None) + create_time = msg['CreateTime'] # 消息时间 + if conf().get('hot_reload') == True and int(create_time) < int(time.time()) - 60: #跳过1分钟前的历史消息 + logger.debug("[WX]history group message skipped") + return if not group_name: return "" origin_content = msg['Content'] From b190db73dc46fec1eb48bc31028e3fcef482be0d Mon Sep 17 00:00:00 2001 From: lichengzhe <38408577@qq.com> Date: Tue, 21 Mar 2023 22:56:30 +0800 Subject: [PATCH 4/5] =?UTF-8?q?-=E5=A2=9E=E5=8A=A0=E9=99=90=E9=80=9F?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0=E6=96=87=E6=A1=A3=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5d35975..4a773c9 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ pip3 install --upgrade openai + 对于图像生成,在满足个人或群组触发条件外,还需要额外的关键词前缀来触发,对应配置 `image_create_prefix ` + 关于OpenAI对话及图片接口的参数配置(内容自由度、回复字数限制、图片大小等),可以参考 [对话接口](https://beta.openai.com/docs/api-reference/completions) 和 [图像接口](https://beta.openai.com/docs/api-reference/completions) 文档直接在 [代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/bot/openai/open_ai_bot.py) `bot/openai/open_ai_bot.py` 中进行调整。 + `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话) ++ `rate_limit_chatgpt`,`rate_limit_dalle`:每分钟最高问答速率、画图速率,超速后排队按序处理。 + `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。 + `hot_reload`: 程序退出后,暂存微信扫码状态,默认关闭。 + `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的设定,你可以为他定制任何人格 (关于会话上下文的更多内容参考该 [issue](https://github.com/zhayujie/chatgpt-on-wechat/issues/43)) From 5e48dd50ac6bf855a536ab69aef1a983dd43fd8e Mon Sep 17 00:00:00 2001 From: a5225662 Date: Thu, 23 Mar 2023 00:36:41 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=98=8E=E7=A1=AEconfig.py=E4=B8=ADconfig.?= =?UTF-8?q?json=E7=9A=84=E6=9F=A5=E6=89=BE=E7=9B=AE=E5=BD=95=E4=B8=BA?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E7=9B=AE=E5=BD=95=20#547?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 19b0f55..9cce2f0 100644 --- a/config.py +++ b/config.py @@ -9,7 +9,7 @@ config = {} def load_config(): global config - config_path = "config.json" + config_path = "./config.json" if not os.path.exists(config_path): raise Exception('配置文件不存在,请根据config-template.json模板创建config.json文件')