From ced0fa46089b4e4043bacc76a0ec754d20eb25a1 Mon Sep 17 00:00:00 2001 From: weishao zeng Date: Tue, 30 Jan 2024 10:17:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=99=BA?= =?UTF-8?q?=E8=B0=B1chatglm4=E6=A8=A1=E5=9E=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/bot_factory.py | 4 + bot/zhipu/chat_glm_bot.py | 155 ++++++++++++++++++++++++++++++++++ bot/zhipu/chat_glm_session.py | 48 +++++++++++ bridge/bridge.py | 2 + common/const.py | 3 +- config.py | 4 + requirements-optional.txt | 3 + 7 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 bot/zhipu/chat_glm_bot.py create mode 100644 bot/zhipu/chat_glm_session.py diff --git a/bot/bot_factory.py b/bot/bot_factory.py index bfc740e..6c138fa 100644 --- a/bot/bot_factory.py +++ b/bot/bot_factory.py @@ -52,4 +52,8 @@ def create_bot(bot_type): from bot.gemini.google_gemini_bot import GoogleGeminiBot return GoogleGeminiBot() + elif bot_type == const.CHATGLM: + from bot.zhipu.chat_glm_bot import ChatGLMBot + return ChatGLMBot() + raise RuntimeError diff --git a/bot/zhipu/chat_glm_bot.py b/bot/zhipu/chat_glm_bot.py new file mode 100644 index 0000000..e2127dd --- /dev/null +++ b/bot/zhipu/chat_glm_bot.py @@ -0,0 +1,155 @@ +# encoding:utf-8 + +import time + +import openai +import openai.error +import requests + +from bot.bot import Bot +from bot.zhipu.chat_glm_session import ChatGLMSession +from bot.openai.open_ai_image import OpenAIImage +from bot.session_manager import SessionManager +from bridge.context import ContextType +from bridge.reply import Reply, ReplyType +from common.log import logger +# from common.token_bucket import TokenBucket +from config import conf, load_config +from zhipuai import ZhipuAI + + +# ZhipuAI对话模型API +class ChatGLMBot(Bot): + def __init__(self): + super().__init__() + # set the default api_key + self.api_key = conf().get("zhipu_ai_api_key") + if conf().get("zhipu_ai_api_base"): + openai.api_base = conf().get("zhipu_ai_api_base") + # if conf().get("rate_limit_chatgpt"): + # self.tb4chatgpt = TokenBucket(conf().get("rate_limit_chatgpt", 20)) + + self.sessions = SessionManager(ChatGLMSession, model=conf().get("model") or "chatglm") + self.args = { + "model": "glm-4", # 对话模型的名称 + "temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性 + # "max_tokens":4096, # 回复最大的字符数 + "top_p": conf().get("top_p", 0.7), + # "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + # "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + # "request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 + # "timeout": conf().get("request_timeout", None), # 重试超时时间,在这个时间内,将会自动重试 + } + self.client = ZhipuAI(api_key=self.api_key) + + def reply(self, query, context=None): + # acquire reply content + if context.type == ContextType.TEXT: + logger.info("[CHATGLM] query={}".format(query)) + + session_id = context["session_id"] + reply = None + clear_memory_commands = conf().get("clear_memory_commands", ["#清除记忆"]) + if query in clear_memory_commands: + self.sessions.clear_session(session_id) + reply = Reply(ReplyType.INFO, "记忆已清除") + elif query == "#清除所有": + self.sessions.clear_all_session() + reply = Reply(ReplyType.INFO, "所有人记忆已清除") + elif query == "#更新配置": + load_config() + reply = Reply(ReplyType.INFO, "配置已更新") + if reply: + return reply + session = self.sessions.session_query(query, session_id) + logger.debug("[CHATGLM] session query={}".format(session.messages)) + + api_key = context.get("openai_api_key") or openai.api_key + model = context.get("gpt_model") + new_args = None + if model: + new_args = self.args.copy() + new_args["model"] = model + # if context.get('stream'): + # # reply in stream + # return self.reply_text_stream(query, new_query, session_id) + + reply_content = self.reply_text(session, api_key, args=new_args) + logger.debug( + "[CHATGLM] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format( + session.messages, + session_id, + reply_content["content"], + reply_content["completion_tokens"], + ) + ) + if reply_content["completion_tokens"] == 0 and len(reply_content["content"]) > 0: + reply = Reply(ReplyType.ERROR, reply_content["content"]) + elif reply_content["completion_tokens"] > 0: + self.sessions.session_reply(reply_content["content"], session_id, reply_content["total_tokens"]) + reply = Reply(ReplyType.TEXT, reply_content["content"]) + else: + reply = Reply(ReplyType.ERROR, reply_content["content"]) + logger.debug("[CHATGLM] reply {} used 0 tokens.".format(reply_content)) + return reply + else: + reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type)) + return reply + + def reply_text(self, session: ChatGLMSession, api_key=None, args=None, retry_count=0) -> dict: + """ + call openai's ChatCompletion to get the answer + :param session: a conversation session + :param session_id: session id + :param retry_count: retry count + :return: {} + """ + try: + # if conf().get("rate_limit_chatgpt") and not self.tb4chatgpt.get_token(): + # raise openai.error.RateLimitError("RateLimitError: rate limit exceeded") + # if api_key == None, the default openai.api_key will be used + if args is None: + args = self.args + # response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args) + response = self.client.chat.completions.create(messages=session.messages, **args) + # logger.debug("[CHATGLM] response={}".format(response)) + # logger.info("[CHATGLM] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"])) + return { + "total_tokens": response.usage.total_tokens, + "completion_tokens": response.usage.completion_tokens, + "content": response.choices[0].message.content, + } + except Exception as e: + need_retry = retry_count < 2 + result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"} + if isinstance(e, openai.error.RateLimitError): + logger.warn("[CHATGLM] RateLimitError: {}".format(e)) + result["content"] = "提问太快啦,请休息一下再问我吧" + if need_retry: + time.sleep(20) + elif isinstance(e, openai.error.Timeout): + logger.warn("[CHATGLM] Timeout: {}".format(e)) + result["content"] = "我没有收到你的消息" + if need_retry: + time.sleep(5) + elif isinstance(e, openai.error.APIError): + logger.warn("[CHATGLM] Bad Gateway: {}".format(e)) + result["content"] = "请再问我一次" + if need_retry: + time.sleep(10) + elif isinstance(e, openai.error.APIConnectionError): + logger.warn("[CHATGLM] APIConnectionError: {}".format(e)) + result["content"] = "我连接不到你的网络" + if need_retry: + time.sleep(5) + else: + logger.exception("[CHATGLM] Exception: {}".format(e), e) + need_retry = False + self.sessions.clear_session(session.session_id) + + if need_retry: + logger.warn("[CHATGLM] 第{}次重试".format(retry_count + 1)) + return self.reply_text(session, api_key, args, retry_count + 1) + else: + return result + diff --git a/bot/zhipu/chat_glm_session.py b/bot/zhipu/chat_glm_session.py new file mode 100644 index 0000000..ab3d62b --- /dev/null +++ b/bot/zhipu/chat_glm_session.py @@ -0,0 +1,48 @@ +from bot.session_manager import Session +from common.log import logger + +class ChatGLMSession(Session): + def __init__(self, session_id, system_prompt=None, model="glm-4"): + super().__init__(session_id, system_prompt) + self.model = model + self.reset() + + def discard_exceeding(self, max_tokens, cur_tokens=None): + precise = True + try: + cur_tokens = self.calc_tokens() + except Exception as e: + precise = False + if cur_tokens is None: + raise e + logger.debug("Exception when counting tokens precisely for query: {}".format(e)) + while cur_tokens > max_tokens: + if len(self.messages) > 2: + self.messages.pop(1) + elif len(self.messages) == 2 and self.messages[1]["role"] == "assistant": + self.messages.pop(1) + if precise: + cur_tokens = self.calc_tokens() + else: + cur_tokens = cur_tokens - max_tokens + break + elif len(self.messages) == 2 and self.messages[1]["role"] == "user": + logger.warn("user message exceed max_tokens. total_tokens={}".format(cur_tokens)) + break + else: + logger.debug("max_tokens={}, total_tokens={}, len(messages)={}".format(max_tokens, cur_tokens, len(self.messages))) + break + if precise: + cur_tokens = self.calc_tokens() + else: + cur_tokens = cur_tokens - max_tokens + return cur_tokens + + def calc_tokens(self): + return num_tokens_from_messages(self.messages, self.model) + +def num_tokens_from_messages(messages, model): + tokens = 0 + for msg in messages: + tokens += len(msg["content"]) + return tokens diff --git a/bridge/bridge.py b/bridge/bridge.py index 53ee878..c474391 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -31,6 +31,8 @@ class Bridge(object): self.btype["chat"] = const.QWEN if model_type in [const.GEMINI]: self.btype["chat"] = const.GEMINI + if model_type in [const.CHATGLM]: + self.btype["chat"] = const.CHATGLM if conf().get("use_linkai") and conf().get("linkai_api_key"): self.btype["chat"] = const.LINKAI diff --git a/common/const.py b/common/const.py index 3347f6a..964fe59 100644 --- a/common/const.py +++ b/common/const.py @@ -8,6 +8,7 @@ LINKAI = "linkai" CLAUDEAI = "claude" QWEN = "qwen" GEMINI = "gemini" +CHATGLM = "chatglm" # model GPT35 = "gpt-3.5-turbo" @@ -19,7 +20,7 @@ TTS_1 = "tts-1" TTS_1_HD = "tts-1-hd" MODEL_LIST = ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "wenxin", "wenxin-4", "xunfei", "claude", "gpt-4-turbo", - "gpt-4-turbo-preview", "gpt-4-1106-preview", GPT4_TURBO_PREVIEW, QWEN, GEMINI] + "gpt-4-turbo-preview", "gpt-4-1106-preview", GPT4_TURBO_PREVIEW, QWEN, GEMINI, CHATGLM] # channel FEISHU = "feishu" diff --git a/config.py b/config.py index c8068a2..3cc8171 100644 --- a/config.py +++ b/config.py @@ -155,6 +155,10 @@ available_setting = { "linkai_api_key": "", "linkai_app_code": "", "linkai_api_base": "https://api.link-ai.chat", # linkAI服务地址,若国内无法访问或延迟较高可改为 https://api.link-ai.tech + +# # 智谱AI + "zhipu_ai_api_key": "", + "zhipu_ai_api_base": "https://open.bigmodel.cn/api/paas/v4", } diff --git a/requirements-optional.txt b/requirements-optional.txt index 7cc3ea0..c45f2b3 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -37,3 +37,6 @@ linkai # dingtalk dingtalk_stream + +# zhipu +zhipuai From 95708489c979d3d891e01ca684939a4d8e623ee3 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Wed, 31 Jan 2024 16:24:29 +0800 Subject: [PATCH 2/3] fix: wxcomapp user name --- bot/linkai/link_ai_bot.py | 6 +++++- channel/wechat/wechat_channel.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index efd571a..a0b92c1 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -107,7 +107,11 @@ class LinkAIBot(Bot): body["group_name"] = context.kwargs.get("msg").from_user_nickname body["sender_name"] = context.kwargs.get("msg").actual_user_nickname else: - body["sender_name"] = context.kwargs.get("msg").from_user_nickname + if body.get("channel_type") in ["wechatcom_app"]: + body["sender_name"] = context.kwargs.get("msg").from_user_id + else: + body["sender_name"] = context.kwargs.get("msg").from_user_nickname + except Exception as e: pass file_id = context.kwargs.get("file_id") diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index bb59c8f..fe14daa 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -251,20 +251,25 @@ class WechatChannel(ChatChannel): def _send_login_success(): try: from common.linkai_client import chat_client - chat_client.send_login_success() + if chat_client.client_id: + chat_client.send_login_success() except Exception as e: pass def _send_logout(): try: from common.linkai_client import chat_client - chat_client.send_logout() + time.sleep(2) + if chat_client.client_id: + chat_client.send_logout() except Exception as e: pass def _send_qr_code(qrcode_list: list): try: from common.linkai_client import chat_client - chat_client.send_qrcode(qrcode_list) + time.sleep(2) + if chat_client.client_id: + chat_client.send_qrcode(qrcode_list) except Exception as e: pass From af5bc73dc0c98ee0418bb1befd96d49e0d703090 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 5 Feb 2024 12:01:41 +0800 Subject: [PATCH 3/3] feat: optimize consumer thread pool --- app.py | 33 +++++++++++-------- bot/linkai/link_ai_bot.py | 2 +- channel/chat_channel.py | 6 ++-- channel/wechat/wechat_channel.py | 56 ++++++++++++++++++-------------- common/linkai_client.py | 27 ++++++++++++++- plugins/godcmd/godcmd.py | 8 +++++ plugins/plugin.py | 3 ++ 7 files changed, 93 insertions(+), 42 deletions(-) diff --git a/app.py b/app.py index 7f766d3..ff2a6c7 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import os import signal import sys +import time from channel import channel_factory from common import const @@ -24,6 +25,21 @@ def sigterm_handler_wrap(_signo): signal.signal(_signo, func) +def start_channel(channel_name: str): + channel = channel_factory.create_channel(channel_name) + if channel_name in ["wx", "wxy", "terminal", "wechatmp", "wechatmp_service", "wechatcom_app", "wework", + const.FEISHU, const.DINGTALK]: + PluginManager().load_plugins() + + if conf().get("use_linkai"): + try: + from common import linkai_client + threading.Thread(target=linkai_client.start, args=(channel,)).start() + except Exception as e: + pass + channel.startup() + + def run(): try: # load config @@ -41,22 +57,11 @@ def run(): if channel_name == "wxy": os.environ["WECHATY_LOG"] = "warn" - # os.environ['WECHATY_PUPPET_SERVICE_ENDPOINT'] = '127.0.0.1:9001' - - channel = channel_factory.create_channel(channel_name) - if channel_name in ["wx", "wxy", "terminal", "wechatmp", "wechatmp_service", "wechatcom_app", "wework", const.FEISHU,const.DINGTALK]: - PluginManager().load_plugins() - - if conf().get("use_linkai"): - try: - from common import linkai_client - threading.Thread(target=linkai_client.start, args=(channel, )).start() - except Exception as e: - pass - # startup channel - channel.startup() + start_channel(channel_name) + while True: + time.sleep(1) except Exception as e: logger.error("App startup failed!") logger.exception(e) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index a0b92c1..d37d82a 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -400,7 +400,7 @@ class LinkAIBot(Bot): i += 1 if url.endswith(".mp4"): reply_type = ReplyType.VIDEO_URL - elif url.endswith(".pdf") or url.endswith(".doc") or url.endswith(".docx"): + elif url.endswith(".pdf") or url.endswith(".doc") or url.endswith(".docx") or url.endswith(".csv"): reply_type = ReplyType.FILE url = _download_file(url) if not url: diff --git a/channel/chat_channel.py b/channel/chat_channel.py index ba017af..fe71207 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -4,6 +4,7 @@ import threading import time from asyncio import CancelledError from concurrent.futures import Future, ThreadPoolExecutor +from concurrent import futures from bridge.context import * from bridge.reply import * @@ -17,6 +18,8 @@ try: except Exception as e: pass +handler_pool = ThreadPoolExecutor(max_workers=8) # 处理消息的线程池 + # 抽象类, 它包含了与消息通道无关的通用处理逻辑 class ChatChannel(Channel): @@ -25,7 +28,6 @@ class ChatChannel(Channel): futures = {} # 记录每个session_id提交到线程池的future对象, 用于重置会话时把没执行的future取消掉,正在执行的不会被取消 sessions = {} # 用于控制并发,每个session_id同时只能有一个context在处理 lock = threading.Lock() # 用于控制对sessions的访问 - handler_pool = ThreadPoolExecutor(max_workers=8) # 处理消息的线程池 def __init__(self): _thread = threading.Thread(target=self.consume) @@ -339,7 +341,7 @@ class ChatChannel(Channel): if not context_queue.empty(): context = context_queue.get() logger.debug("[WX] consume context: {}".format(context)) - future: Future = self.handler_pool.submit(self._handle, context) + future: Future = handler_pool.submit(self._handle, context) future.add_done_callback(self._thread_pool_callback(session_id, context=context)) if session_id not in self.futures: self.futures[session_id] = [] diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index fe14daa..717b068 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -15,6 +15,7 @@ import requests from bridge.context import * from bridge.reply import * from channel.chat_channel import ChatChannel +from channel import chat_channel from channel.wechat.wechat_message import * from common.expired_dict import ExpiredDict from common.log import logger @@ -112,30 +113,39 @@ class WechatChannel(ChatChannel): self.auto_login_times = 0 def startup(self): - itchat.instance.receivingRetryCount = 600 # 修改断线超时时间 - # login by scan QRCode - hotReload = conf().get("hot_reload", False) - status_path = os.path.join(get_appdata_dir(), "itchat.pkl") - itchat.auto_login( - enableCmdQR=2, - hotReload=hotReload, - statusStorageDir=status_path, - qrCallback=qrCallback, - exitCallback=self.exitCallback, - loginCallback=self.loginCallback - ) - self.user_id = itchat.instance.storageClass.userName - self.name = itchat.instance.storageClass.nickName - logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name)) - # start message listener - itchat.run() + try: + itchat.instance.receivingRetryCount = 600 # 修改断线超时时间 + # login by scan QRCode + hotReload = conf().get("hot_reload", False) + status_path = os.path.join(get_appdata_dir(), "itchat.pkl") + itchat.auto_login( + enableCmdQR=2, + hotReload=hotReload, + statusStorageDir=status_path, + qrCallback=qrCallback, + exitCallback=self.exitCallback, + loginCallback=self.loginCallback + ) + self.user_id = itchat.instance.storageClass.userName + self.name = itchat.instance.storageClass.nickName + logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name)) + # start message listener + itchat.run() + except Exception as e: + logger.error(e) def exitCallback(self): - _send_logout() - time.sleep(3) - self.auto_login_times += 1 - if self.auto_login_times < 100: - self.startup() + try: + from common.linkai_client import chat_client + if chat_client.client_id and conf().get("use_linkai"): + _send_logout() + time.sleep(2) + self.auto_login_times += 1 + if self.auto_login_times < 100: + chat_channel.handler_pool._shutdown = False + self.startup() + except Exception as e: + pass def loginCallback(self): logger.debug("Login success") @@ -259,7 +269,6 @@ def _send_login_success(): def _send_logout(): try: from common.linkai_client import chat_client - time.sleep(2) if chat_client.client_id: chat_client.send_logout() except Exception as e: @@ -268,7 +277,6 @@ def _send_logout(): def _send_qr_code(qrcode_list: list): try: from common.linkai_client import chat_client - time.sleep(2) if chat_client.client_id: chat_client.send_qrcode(qrcode_list) except Exception as e: diff --git a/common/linkai_client.py b/common/linkai_client.py index 80f3168..ad7d213 100644 --- a/common/linkai_client.py +++ b/common/linkai_client.py @@ -2,7 +2,9 @@ from bridge.context import Context, ContextType from bridge.reply import Reply, ReplyType from common.log import logger from linkai import LinkAIClient, PushMsg -from config import conf +from config import conf, pconf, plugin_config +from plugins import PluginManager + chat_client: LinkAIClient @@ -22,6 +24,29 @@ class ChatClient(LinkAIClient): context["isgroup"] = push_msg.is_group self.channel.send(Reply(ReplyType.TEXT, content=msg_content), context) + def on_config(self, config: dict): + if not self.client_id: + return + logger.info(f"从控制台加载配置: {config}") + local_config = conf() + for key in local_config.keys(): + if config.get(key) is not None: + local_config[key] = config.get(key) + if config.get("reply_voice_mode"): + if config.get("reply_voice_mode") == "voice_reply_voice": + local_config["voice_reply_voice"] = True + elif config.get("reply_voice_mode") == "always_reply_voice": + local_config["always_reply_voice"] = True + # if config.get("admin_password") and plugin_config["Godcmd"]: + # plugin_config["Godcmd"]["password"] = config.get("admin_password") + # PluginManager().instances["Godcmd"].reload() + # if config.get("group_app_map") and pconf("linkai"): + # local_group_map = {} + # for mapping in config.get("group_app_map"): + # local_group_map[mapping.get("group_name")] = mapping.get("app_code") + # pconf("linkai")["group_app_map"] = local_group_map + # PluginManager().instances["linkai"].reload() + def start(channel): global chat_client diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 1c8b09c..a965a68 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -475,3 +475,11 @@ class Godcmd(Plugin): if model == "gpt-4-turbo": return const.GPT4_TURBO_PREVIEW return model + + def reload(self): + gconf = plugin_config[self.name] + if gconf: + if gconf.get("password"): + self.password = gconf["password"] + if gconf.get("admin_users"): + self.admin_users = gconf["admin_users"] diff --git a/plugins/plugin.py b/plugins/plugin.py index 801997b..f4c9618 100644 --- a/plugins/plugin.py +++ b/plugins/plugin.py @@ -46,3 +46,6 @@ class Plugin: def get_help_text(self, **kwargs): return "暂无帮助信息" + + def reload(self): + pass