From 854d613a813a49b6ecd7eb5a9b4c83e4a83521e2 Mon Sep 17 00:00:00 2001 From: Clivia <132346501+Yanyutin753@users.noreply.github.com> Date: Sat, 9 Sep 2023 12:25:40 +0800 Subject: [PATCH 01/20] Update source.json --- plugins/source.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/source.json b/plugins/source.json index 003d391..8b1984f 100644 --- a/plugins/source.json +++ b/plugins/source.json @@ -16,5 +16,9 @@ "url": "https://github.com/haikerapples/timetask.git", "desc": "一款定时任务系统的插件" } + "pictureChange": { + "url": "https://github.com/Yanyutin753/pictureChange.git", + "desc": "利用stable-diffusion和百度Ai进行图生图或者画图的插件" + } } } From 977d3bc02eb73dfc13d7f5c5b3730f55d6389480 Mon Sep 17 00:00:00 2001 From: FMStereo Date: Thu, 18 Jan 2024 12:46:18 +0800 Subject: [PATCH 02/20] =?UTF-8?q?=E7=99=BE=E5=BA=A6=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E8=BD=AC=E5=86=99=E6=94=AF=E6=8C=818000=E9=87=87=E6=A0=B7?= =?UTF-8?q?=E7=8E=87,=20pcm=5Fs16le=E7=BC=96=E7=A0=81,=20=E5=8D=95?= =?UTF-8?q?=E9=80=9A=E9=81=93=E8=AF=AD=E9=9F=B3=E7=9A=84=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voice/audio_convert.py | 4 +++- voice/baidu/baidu_voice.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/voice/audio_convert.py b/voice/audio_convert.py index 18fe3c2..5c80528 100644 --- a/voice/audio_convert.py +++ b/voice/audio_convert.py @@ -64,7 +64,9 @@ def any_to_wav(any_path, wav_path): if any_path.endswith(".sil") or any_path.endswith(".silk") or any_path.endswith(".slk"): return sil_to_wav(any_path, wav_path) audio = AudioSegment.from_file(any_path) - audio.export(wav_path, format="wav") + audio.set_frame_rate(8000) # 百度语音转写支持8000采样率, pcm_s16le, 单通道语音识别 + audio.set_channels(1) + audio.export(wav_path, format="wav", codec='pcm_s16le') def any_to_sil(any_path, sil_path): diff --git a/voice/baidu/baidu_voice.py b/voice/baidu/baidu_voice.py index fbf53ce..66ba4d8 100644 --- a/voice/baidu/baidu_voice.py +++ b/voice/baidu/baidu_voice.py @@ -62,7 +62,7 @@ class BaiduVoice(Voice): # 识别本地文件 logger.debug("[Baidu] voice file name={}".format(voice_file)) pcm = get_pcm_from_wav(voice_file) - res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.dev_id}) + res = self.client.asr(pcm, "pcm", 8000, {"dev_pid": self.dev_id}) if res["err_no"] == 0: logger.info("百度语音识别到了:{}".format(res["result"])) text = "".join(res["result"]) From ced0fa46089b4e4043bacc76a0ec754d20eb25a1 Mon Sep 17 00:00:00 2001 From: weishao zeng Date: Tue, 30 Jan 2024 10:17:53 +0800 Subject: [PATCH 03/20] =?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 d6fdf8ca2adceeade631b5e250d8de094ff4e365 Mon Sep 17 00:00:00 2001 From: zR <2448370773@qq.com> Date: Thu, 1 Feb 2024 11:31:56 +0800 Subject: [PATCH 04/20] =?UTF-8?q?=E6=94=AF=E6=8C=81ZhipuAI=20GLM=E7=B3=BB?= =?UTF-8?q?=E5=88=97=E6=A8=A1=E5=9E=8B=E5=92=8C=E7=94=BB=E5=9B=BE=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- bot/bot_factory.py | 4 + bot/zhipuai/zhipu_ai_image.py | 29 +++++++ bot/zhipuai/zhipu_ai_session.py | 51 +++++++++++ bot/zhipuai/zhipuai_bot.py | 149 ++++++++++++++++++++++++++++++++ bridge/bridge.py | 2 + common/const.py | 3 +- config.py | 5 ++ requirements-optional.txt | 3 + 9 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 bot/zhipuai/zhipu_ai_image.py create mode 100644 bot/zhipuai/zhipu_ai_session.py create mode 100644 bot/zhipuai/zhipuai_bot.py diff --git a/README.md b/README.md index 4ee6e61..46541d0 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # 简介 -> 本项目是基于大模型的智能对话机器人,支持微信、企业微信、公众号、飞书、钉钉接入,可选择GPT3.5/GPT4.0/Claude/文心一言/讯飞星火/通义千问/Gemini/LinkAI,能处理文本、语音和图片,通过插件访问操作系统和互联网等外部资源,支持基于自有知识库定制企业AI应用。 +> 本项目是基于大模型的智能对话机器人,支持微信、企业微信、公众号、飞书、钉钉接入,可选择GPT3.5/GPT4.0/Claude/文心一言/讯飞星火/通义千问/Gemini/LinkAI/ZhipuAI,能处理文本、语音和图片,通过插件访问操作系统和互联网等外部资源,支持基于自有知识库定制企业AI应用。 最新版本支持的功能如下: - [x] **多端部署:** 有多种部署方式可选择且功能完备,目前已支持个人微信、微信公众号和、企业微信、飞书、钉钉等部署方式 -- [x] **基础对话:** 私聊及群聊的消息智能回复,支持多轮会话上下文记忆,支持 GPT-3.5, GPT-4, claude, Gemini, 文心一言, 讯飞星火, 通义千问 +- [x] **基础对话:** 私聊及群聊的消息智能回复,支持多轮会话上下文记忆,支持 GPT-3.5, GPT-4, claude, Gemini, 文心一言, 讯飞星火, 通义千问,ChatGLM - [x] **语音能力:** 可识别语音消息,通过文字或语音回复,支持 azure, baidu, google, openai(whisper/tts) 等多种语音模型 -- [x] **图像能力:** 支持图片生成、图片识别、图生图(如照片修复),可选择 Dall-E-3, stable diffusion, replicate, midjourney, vision模型 +- [x] **图像能力:** 支持图片生成、图片识别、图生图(如照片修复),可选择 Dall-E-3, stable diffusion, replicate, midjourney, CogView-3, vision模型 - [x] **丰富插件:** 支持个性化插件扩展,已实现多角色切换、文字冒险、敏感词过滤、聊天记录总结、文档总结和对话、联网搜索等插件 - [x] **知识库:** 通过上传知识库文件自定义专属机器人,可作为数字分身、智能客服、私域助手使用,基于 [LinkAI](https://link-ai.tech) 实现 diff --git a/bot/bot_factory.py b/bot/bot_factory.py index bfc740e..2e544a6 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.ZHIPU_AI: + from bot.zhipuai.zhipuai_bot import ZHIPUAIBot + return ZHIPUAIBot() + raise RuntimeError diff --git a/bot/zhipuai/zhipu_ai_image.py b/bot/zhipuai/zhipu_ai_image.py new file mode 100644 index 0000000..84eb567 --- /dev/null +++ b/bot/zhipuai/zhipu_ai_image.py @@ -0,0 +1,29 @@ +from common.log import logger +from config import conf + + +# ZhipuAI提供的画图接口 + +class ZhipuAIImage(object): + def __init__(self): + from zhipuai import ZhipuAI + self.client = ZhipuAI(api_key=conf().get("zhipu_ai_api_key")) + + def create_img(self, query, retry_count=0, api_key=None, api_base=None): + try: + if conf().get("rate_limit_dalle"): + return False, "请求太快了,请休息一下再问我吧" + logger.info("[ZHIPU_AI] image_query={}".format(query)) + response = self.client.images.generations( + prompt=query, + n=1, # 每次生成图片的数量 + model=conf().get("text_to_image") or "cogview-3", + size=conf().get("image_create_size", "1024x1024"), # 图片大小,可选有 256x256, 512x512, 1024x1024 + quality="standard", + ) + image_url = response.data[0].url + logger.info("[ZHIPU_AI] image_url={}".format(image_url)) + return True, image_url + except Exception as e: + logger.exception(e) + return False, "画图出现问题,请休息一下再问我吧" diff --git a/bot/zhipuai/zhipu_ai_session.py b/bot/zhipuai/zhipu_ai_session.py new file mode 100644 index 0000000..394d521 --- /dev/null +++ b/bot/zhipuai/zhipu_ai_session.py @@ -0,0 +1,51 @@ +from bot.session_manager import Session +from common.log import logger + + +class ZhipuAISession(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/bot/zhipuai/zhipuai_bot.py b/bot/zhipuai/zhipuai_bot.py new file mode 100644 index 0000000..894e7f9 --- /dev/null +++ b/bot/zhipuai/zhipuai_bot.py @@ -0,0 +1,149 @@ +# encoding:utf-8 + +import time + +import openai +import openai.error +from bot.bot import Bot +from bot.zhipuai.zhipu_ai_session import ZhipuAISession +from bot.zhipuai.zhipu_ai_image import ZhipuAIImage +from bot.session_manager import SessionManager +from bridge.context import ContextType +from bridge.reply import Reply, ReplyType +from common.log import logger +from config import conf, load_config +from zhipuai import ZhipuAI + + +# ZhipuAI对话模型API +class ZHIPUAIBot(Bot, ZhipuAIImage): + def __init__(self): + super().__init__() + self.sessions = SessionManager(ZhipuAISession, model=conf().get("model") or "ZHIPU_AI") + self.args = { + "model": "glm-4", # 对话模型的名称,可选择 glm-3.5-turbo + "temperature": conf().get("temperature", 0.9), # 值在(0,1)之间(智谱AI 的温度不能取 0 或者 1) + "top_p": conf().get("top_p", 0.7), + } + self.client = ZhipuAI(api_key=conf().get("zhipu_ai_api_key")) + + def reply(self, query, context=None): + # acquire reply content + if context.type == ContextType.TEXT: + logger.info("[ZHIPU_AI] 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("[ZHIPU_AI] 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( + "[ZHIPU_AI] 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("[ZHIPU_AI] reply {} used 0 tokens.".format(reply_content)) + return reply + elif context.type == ContextType.IMAGE_CREATE: + ok, retstring = self.create_img(query, 0) + reply = None + if ok: + reply = Reply(ReplyType.IMAGE_URL, retstring) + else: + reply = Reply(ReplyType.ERROR, retstring) + return reply + + else: + reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type)) + return reply + + def reply_text(self, session: ZhipuAISession, 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("[ZHIPU_AI] response={}".format(response)) + # logger.info("[ZHIPU_AI] 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("[ZHIPU_AI] RateLimitError: {}".format(e)) + result["content"] = "提问太快啦,请休息一下再问我吧" + if need_retry: + time.sleep(20) + elif isinstance(e, openai.error.Timeout): + logger.warn("[ZHIPU_AI] Timeout: {}".format(e)) + result["content"] = "我没有收到你的消息" + if need_retry: + time.sleep(5) + elif isinstance(e, openai.error.APIError): + logger.warn("[ZHIPU_AI] Bad Gateway: {}".format(e)) + result["content"] = "请再问我一次" + if need_retry: + time.sleep(10) + elif isinstance(e, openai.error.APIConnectionError): + logger.warn("[ZHIPU_AI] APIConnectionError: {}".format(e)) + result["content"] = "我连接不到你的网络" + if need_retry: + time.sleep(5) + else: + logger.exception("[ZHIPU_AI] Exception: {}".format(e), e) + need_retry = False + self.sessions.clear_session(session.session_id) + + if need_retry: + logger.warn("[ZHIPU_AI] 第{}次重试".format(retry_count + 1)) + return self.reply_text(session, api_key, args, retry_count + 1) + else: + return result diff --git a/bridge/bridge.py b/bridge/bridge.py index 53ee878..88e6b18 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.ZHIPU_AI]: + self.btype["chat"] = const.ZHIPU_AI 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..bec3186 100644 --- a/common/const.py +++ b/common/const.py @@ -8,6 +8,7 @@ LINKAI = "linkai" CLAUDEAI = "claude" QWEN = "qwen" GEMINI = "gemini" +ZHIPU_AI = "glm-4" # 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, ZHIPU_AI] # channel FEISHU = "feishu" diff --git a/config.py b/config.py index c8068a2..048327f 100644 --- a/config.py +++ b/config.py @@ -155,6 +155,11 @@ 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..627561e 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -37,3 +37,6 @@ linkai # dingtalk dingtalk_stream + +# zhipuai +zhipuai>=2.0.1 \ No newline at end of file From 0247cd4c45ef39bdcf60b729a7c0591080286045 Mon Sep 17 00:00:00 2001 From: zR <2448370773@qq.com> Date: Fri, 2 Feb 2024 11:08:06 +0800 Subject: [PATCH 05/20] =?UTF-8?q?=E6=94=B9=E5=96=84=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/zhipuai/zhipuai_bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/zhipuai/zhipuai_bot.py b/bot/zhipuai/zhipuai_bot.py index 894e7f9..d8eed4d 100644 --- a/bot/zhipuai/zhipuai_bot.py +++ b/bot/zhipuai/zhipuai_bot.py @@ -21,9 +21,9 @@ class ZHIPUAIBot(Bot, ZhipuAIImage): super().__init__() self.sessions = SessionManager(ZhipuAISession, model=conf().get("model") or "ZHIPU_AI") self.args = { - "model": "glm-4", # 对话模型的名称,可选择 glm-3.5-turbo + "model": conf().get("model") or "glm-4", # 对话模型的名称 "temperature": conf().get("temperature", 0.9), # 值在(0,1)之间(智谱AI 的温度不能取 0 或者 1) - "top_p": conf().get("top_p", 0.7), + "top_p": conf().get("top_p", 0.7), # 值在(0,1)之间(智谱AI 的 top_p 不能取 0 或者 1) } self.client = ZhipuAI(api_key=conf().get("zhipu_ai_api_key")) From af5bc73dc0c98ee0418bb1befd96d49e0d703090 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 5 Feb 2024 12:01:41 +0800 Subject: [PATCH 06/20] 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 From c41e486bfc7c3af2e666b06b61573bba26cc7257 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 5 Feb 2024 12:15:28 +0800 Subject: [PATCH 07/20] Update config.py --- config.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index 18be1c5..154c633 100644 --- a/config.py +++ b/config.py @@ -150,15 +150,14 @@ available_setting = { "use_global_plugin_config": False, "max_media_send_count": 3, # 单次最大发送媒体资源的个数 "media_send_interval": 1, # 发送图片的事件间隔,单位秒 + # 智谱AI 平台配置 + "zhipu_ai_api_key": "", + "zhipu_ai_api_base": "https://open.bigmodel.cn/api/paas/v4", # LinkAI平台配置 "use_linkai": False, "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", } From 5346dfdd8b52a2291f59d8250eb0f17dedd30b93 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 5 Feb 2024 12:21:50 +0800 Subject: [PATCH 08/20] feat: code tidying up --- bot/zhipu/chat_glm_bot.py | 155 ---------------------------------- bot/zhipu/chat_glm_session.py | 48 ----------- 2 files changed, 203 deletions(-) delete mode 100644 bot/zhipu/chat_glm_bot.py delete mode 100644 bot/zhipu/chat_glm_session.py diff --git a/bot/zhipu/chat_glm_bot.py b/bot/zhipu/chat_glm_bot.py deleted file mode 100644 index e2127dd..0000000 --- a/bot/zhipu/chat_glm_bot.py +++ /dev/null @@ -1,155 +0,0 @@ -# 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 deleted file mode 100644 index ab3d62b..0000000 --- a/bot/zhipu/chat_glm_session.py +++ /dev/null @@ -1,48 +0,0 @@ -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 From 74ebbdd7617b08bfc4afb4c095b0c38356387536 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 19 Feb 2024 13:32:32 +0800 Subject: [PATCH 09/20] fix: client resource usage bug --- bot/linkai/link_ai_bot.py | 2 +- requirements-optional.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index d37d82a..f142d97 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -92,7 +92,7 @@ class LinkAIBot(Bot): "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "session_id": session_id, - "channel_type": conf().get("channel_type") + "channel_type": conf().get("channel_type", "wx") } try: from linkai import LinkAIClient diff --git a/requirements-optional.txt b/requirements-optional.txt index bae0e37..74f1780 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -33,7 +33,7 @@ broadscope_bailian google-generativeai # linkai -linkai +linkai>=0.0.3.5 # dingtalk dingtalk_stream From 64ae3d1e2184bf734f8d31317c04783f40e50996 Mon Sep 17 00:00:00 2001 From: Zhuoheng Lee Date: Wed, 21 Feb 2024 14:14:19 +0800 Subject: [PATCH 10/20] Update xunfei_spark_bot.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 讯飞接口升级到v3.5版本,同时升级到wss协议,避免请求时出现11200错误码的问题 --- bot/xunfei/xunfei_spark_bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/xunfei/xunfei_spark_bot.py b/bot/xunfei/xunfei_spark_bot.py index 395d81e..b9e46ba 100644 --- a/bot/xunfei/xunfei_spark_bot.py +++ b/bot/xunfei/xunfei_spark_bot.py @@ -47,7 +47,8 @@ class XunFeiBot(Bot): # 默认使用v2.0版本: "ws://spark-api.xf-yun.com/v2.1/chat" # v1.5版本为: "ws://spark-api.xf-yun.com/v1.1/chat" # v3.0版本为: "ws://spark-api.xf-yun.com/v3.1/chat" - self.spark_url = "ws://spark-api.xf-yun.com/v3.1/chat" + # 升级到v3.5版本,同时升级到wss协议,避免请求时出现11200错误码 + self.spark_url = "wss://spark-api.xf-yun.com/v3.5/chat" self.host = urlparse(self.spark_url).netloc self.path = urlparse(self.spark_url).path # 和wenxin使用相同的session机制 From 3a20461abf0446349578771aba3a1c6aba8d884a Mon Sep 17 00:00:00 2001 From: Lecter Date: Mon, 4 Mar 2024 00:14:19 +0800 Subject: [PATCH 11/20] add edge-tts --- config.py | 2 +- requirements-optional.txt | 1 + voice/edge/edge_voice.py | 50 +++++++++++++++++++++++++++++++++++++++ voice/factory.py | 4 ++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 voice/edge/edge_voice.py diff --git a/config.py b/config.py index 154c633..acfb6a6 100644 --- a/config.py +++ b/config.py @@ -83,7 +83,7 @@ available_setting = { "voice_reply_voice": False, # 是否使用语音回复语音,需要设置对应语音合成引擎的api key "always_reply_voice": False, # 是否一直使用语音回复 "voice_to_text": "openai", # 语音识别引擎,支持openai,baidu,google,azure - "text_to_voice": "openai", # 语音合成引擎,支持openai,baidu,google,pytts(offline),azure,elevenlabs + "text_to_voice": "openai", # 语音合成引擎,支持openai,baidu,google,pytts(offline),azure,elevenlabs,edge(online) "text_to_voice_model": "tts-1", "tts_voice_id": "alloy", # baidu 语音api配置, 使用百度语音识别和语音合成时需要 diff --git a/requirements-optional.txt b/requirements-optional.txt index 74f1780..abb8a4e 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -7,6 +7,7 @@ gTTS>=2.3.1 # google text to speech pyttsx3>=2.90 # pytsx text to speech baidu_aip>=4.16.10 # baidu voice azure-cognitiveservices-speech # azure voice +edge-tts # edge-tts numpy<=1.24.2 langid # language detect diff --git a/voice/edge/edge_voice.py b/voice/edge/edge_voice.py new file mode 100644 index 0000000..7bb8b2e --- /dev/null +++ b/voice/edge/edge_voice.py @@ -0,0 +1,50 @@ +import time + +import edge_tts +import asyncio + +from bridge.reply import Reply, ReplyType +from common.log import logger +from common.tmp_dir import TmpDir +from voice.voice import Voice + + +class EdgeVoice(Voice): + + def __init__(self): + ''' + # 普通话 + zh-CN-XiaoxiaoNeural + zh-CN-XiaoyiNeural + zh-CN-YunjianNeural + zh-CN-YunxiNeural + zh-CN-YunxiaNeural + zh-CN-YunyangNeural + # 地方口音 + zh-CN-liaoning-XiaobeiNeural + zh-CN-shaanxi-XiaoniNeural + # 粤语 + zh-HK-HiuGaaiNeural + zh-HK-HiuMaanNeural + zh-HK-WanLungNeural + # 湾湾腔 + zh-TW-HsiaoChenNeural + zh-TW-HsiaoYuNeural + zh-TW-YunJheNeural + ''' + self.voice = "zh-CN-YunjianNeural" + + def voiceToText(self, voice_file): + pass + + async def gen_voice(self, text, fileName): + communicate = edge_tts.Communicate(text, self.voice) + await communicate.save(fileName) + + def textToVoice(self, text): + fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3" + + asyncio.run(self.gen_voice(text, fileName)) + + logger.info("[EdgeTTS] textToVoice text={} voice file name={}".format(text, fileName)) + return Reply(ReplyType.VOICE, fileName) diff --git a/voice/factory.py b/voice/factory.py index ed80758..bc9c9c3 100644 --- a/voice/factory.py +++ b/voice/factory.py @@ -42,4 +42,8 @@ def create_voice(voice_type): from voice.ali.ali_voice import AliVoice return AliVoice() + elif voice_type == "edge": + from voice.edge.edge_voice import EdgeVoice + + return EdgeVoice() raise RuntimeError From 2812a5026cef35c71a36eacb7acfcd599ab317d1 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Tue, 5 Mar 2024 20:56:37 +0800 Subject: [PATCH 12/20] Update README.md --- README.md | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/README.md b/README.md index 46541d0..e767784 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,6 @@ https://github.com/zhayujie/chatgpt-on-wechat/assets/26161723/d5154020-36e3-41db Demo made by [Visionn](https://www.wangpc.cc/) -# 商业支持 - -> 我们还提供企业级的 **AI应用平台**,包含知识库、Agent插件、应用管理等能力,支持多平台聚合的应用接入、客户端管理、对话管理,以及提供 -SaaS服务、私有化部署、稳定托管接入 等多种模式。 -> -> 目前已在私域运营、智能客服、企业效率助手等场景积累了丰富的 AI 解决方案, 在电商、文教、健康、新消费等各行业沉淀了 AI 落地的最佳实践,致力于打造助力中小企业拥抱 AI 的一站式平台。 - -企业服务和商用咨询可联系产品顾问: - - - # 开源社区 添加小助手微信加入开源项目交流群: From 07e10a79437589d5ba22f5ebe05d6ec0fe3fc88b Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 8 Mar 2024 00:19:59 +0800 Subject: [PATCH 13/20] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index e767784..dadf801 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,16 @@ https://github.com/zhayujie/chatgpt-on-wechat/assets/26161723/d5154020-36e3-41db Demo made by [Visionn](https://www.wangpc.cc/) +# 商业支持 + +> 我们还提供企业级的 **AI应用平台**,包含知识库、Agent插件、应用管理等能力,支持多平台聚合的应用接入、客户端管理、对话管理,以及提供 +SaaS服务、私有化部署、稳定托管接入 等多种模式。 +> +> 目前已在私域运营、智能客服、企业效率助手等场景积累了丰富的 AI 解决方案, 在电商、文教、健康、新消费等各行业沉淀了 AI 落地的最佳实践,致力于打造助力中小企业拥抱 AI 的一站式平台。 +企业服务和商用咨询可联系产品顾问: + + + # 开源社区 添加小助手微信加入开源项目交流群: From 283ad48b8645ccdeb3f9b504c1fd24f8f1a8a2e1 Mon Sep 17 00:00:00 2001 From: goldfishh Date: Sun, 10 Mar 2024 13:11:45 +0800 Subject: [PATCH 14/20] disable plugin(tool) log printing --- plugins/tool/tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index c80a945..a2ce4b6 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -137,7 +137,7 @@ class Tool(Plugin): return { # 全局配置相关 - "log": True, # tool 日志开关 + "log": False, # tool 日志开关 "debug": kwargs.get("debug", False), # 输出更多日志 "no_default": kwargs.get("no_default", False), # 不要默认的工具,只加载自己导入的工具 "think_depth": kwargs.get("think_depth", 2), # 一个问题最多使用多少次工具 From 800419e7cc79c558c201872e555f12bb91d7ecfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E4=BC=9F=2810007228=29?= Date: Thu, 14 Mar 2024 13:44:14 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A6=82=E4=B8=8B?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9A=201.=E8=B0=83=E7=94=A8gemini=20api?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E5=BC=82=E5=B8=B8=E6=97=B6=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=90=91=E4=B8=8B=E6=B8=B8=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=90=8E=E7=BB=AD=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=E5=8F=AF=E8=83=BD=E8=A6=81=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E5=81=9A=E7=9B=B8=E5=BA=94?= =?UTF-8?q?=E8=A1=A5=E5=81=BF=E6=9C=BA=E5=88=B6=202.=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=9C=BA=E6=99=AF=E4=B8=AD=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E8=B6=8A=E7=95=8C=E5=AF=BC=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/gemini/google_gemini_bot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/gemini/google_gemini_bot.py b/bot/gemini/google_gemini_bot.py index 1a49d60..e63f1cb 100644 --- a/bot/gemini/google_gemini_bot.py +++ b/bot/gemini/google_gemini_bot.py @@ -44,6 +44,7 @@ class GoogleGeminiBot(Bot): except Exception as e: logger.error("[Gemini] fetch reply error, may contain unsafe content") logger.error(e) + return Reply(ReplyType.ERROR, "invoke [Gemini] api failed!") def _convert_to_gemini_messages(self, messages: list): res = [] @@ -63,6 +64,8 @@ class GoogleGeminiBot(Bot): def _filter_messages(self, messages: list): res = [] turn = "user" + if not messages: + return res for i in range(len(messages) - 1, -1, -1): message = messages[i] if message.get("role") != turn: From 8093fcc64ca919fd8e16709b5fb4e8812a183a16 Mon Sep 17 00:00:00 2001 From: "rowan.wu" <> Date: Sat, 16 Mar 2024 12:34:40 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=A4=E5=A4=84BUG?= =?UTF-8?q?=201=E3=80=81=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89=E4=B8=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BA=86=E9=A9=BC=E5=B3=B0=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E4=BD=8D=E7=BD=AE=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=A4=A7=E5=86=99=202=E3=80=81=E5=BE=AE=E4=BF=A1channel?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E5=8F=91=E9=80=81IMAGE=EF=BC=8C=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E4=BA=86seek=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bridge/reply.py | 2 +- channel/wechat/wechat_channel.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bridge/reply.py b/bridge/reply.py index 0031484..f2293bd 100644 --- a/bridge/reply.py +++ b/bridge/reply.py @@ -11,7 +11,7 @@ class ReplyType(Enum): VIDEO_URL = 5 # 视频URL FILE = 6 # 文件 CARD = 7 # 微信名片,仅支持ntchat - InviteRoom = 8 # 邀请好友进群 + INVITE_ROOM = 8 # 邀请好友进群 INFO = 9 ERROR = 10 TEXT_ = 11 # 强制文本 diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 717b068..a65269c 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -233,7 +233,6 @@ class WechatChannel(ChatChannel): logger.info("[WX] sendImage url={}, receiver={}".format(img_url, receiver)) elif reply.type == ReplyType.IMAGE: # 从文件读取图片 image_storage = reply.content - image_storage.seek(0) itchat.send_image(image_storage, toUserName=receiver) logger.info("[WX] sendImage, receiver={}".format(receiver)) elif reply.type == ReplyType.FILE: # 新增文件回复类型 From a2bc22c37dcd8e2658006a9629f9357e85819a37 Mon Sep 17 00:00:00 2001 From: 13476573407 <1062424570@qq.com> Date: Mon, 18 Mar 2024 14:40:01 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E7=94=B1=E4=BA=8E=E4=BD=BF=E7=94=A8#scan?= =?UTF-8?q?p=E5=92=8C#reloadp=E6=89=AB=E6=8F=8F=E6=8F=92=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=BD=93=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6=E4=BB=A5?= =?UTF-8?q?=E5=90=8E=E5=B9=B6=E4=B8=8D=E4=BC=9A=E5=AE=9E=E7=8E=B0=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E6=96=B0=E7=9A=84=E6=8F=92=E4=BB=B6=20=E6=89=80?= =?UTF-8?q?=E4=BB=A5=E5=8F=96=E6=B6=88=E4=BA=86=E5=B7=B2=E8=BD=BD=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E4=BB=B6=E5=88=A4=E6=96=AD=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E9=99=A4Godcmd=E4=BB=A5=E5=A4=96=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=9D=A5=E5=AE=9E=E7=8E=B0=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E9=87=8D=E5=90=AF=E9=A1=B9=E7=9B=AE=E5=8D=B3=E5=8F=AF?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/plugin_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index 49c13ca..8433e8f 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -99,7 +99,7 @@ class PluginManager: try: self.current_plugin_path = plugin_path if plugin_path in self.loaded: - if self.loaded[plugin_path] == None: + if plugin_name.upper() != 'GODCMD': logger.info("reload module %s" % plugin_name) self.loaded[plugin_path] = importlib.reload(sys.modules[import_path]) dependent_module_names = [name for name in sys.modules.keys() if name.startswith(import_path + ".")] @@ -141,7 +141,7 @@ class PluginManager: failed_plugins = [] for name, plugincls in self.plugins.items(): if plugincls.enabled: - if name not in self.instances: + # if name not in self.instances: try: instance = plugincls() except Exception as e: From 15e070232994c8ec195319b6ce3279ad7afd3a4b Mon Sep 17 00:00:00 2001 From: 13476573407 <1062424570@qq.com> Date: Wed, 20 Mar 2024 10:52:34 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BD=BF=E7=94=A8scanp?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=E6=97=B6=E4=BC=9A=E9=87=8D=E6=96=B0=E7=94=9F?= =?UTF-8?q?=E6=88=90godcmd=E7=9A=84=E5=AE=9E=E4=BE=8B=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4auth=E6=9D=83=E9=99=90=E8=A2=AB=E6=B8=85=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/plugin_manager.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index 8433e8f..cecf75d 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -141,19 +141,21 @@ class PluginManager: failed_plugins = [] for name, plugincls in self.plugins.items(): if plugincls.enabled: + if 'GODCMD' in self.instances and name == 'GODCMD': + continue # if name not in self.instances: - try: - instance = plugincls() - except Exception as e: - logger.warn("Failed to init %s, diabled. %s" % (name, e)) - self.disable_plugin(name) - failed_plugins.append(name) - continue - self.instances[name] = instance - for event in instance.handlers: - if event not in self.listening_plugins: - self.listening_plugins[event] = [] - self.listening_plugins[event].append(name) + try: + instance = plugincls() + except Exception as e: + logger.warn("Failed to init %s, diabled. %s" % (name, e)) + self.disable_plugin(name) + failed_plugins.append(name) + continue + self.instances[name] = instance + for event in instance.handlers: + if event not in self.listening_plugins: + self.listening_plugins[event] = [] + self.listening_plugins[event].append(name) self.refresh_order() return failed_plugins From c22c7102d5c49c5d7335de5e2a6d86a7f3796704 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Wed, 20 Mar 2024 12:07:05 +0800 Subject: [PATCH 19/20] fix: no need to send when message is empty --- bot/linkai/link_ai_bot.py | 1 + channel/chat_channel.py | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index f142d97..0df20bc 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -92,6 +92,7 @@ class LinkAIBot(Bot): "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "session_id": session_id, + "sender_id": session_id, "channel_type": conf().get("channel_type", "wx") } try: diff --git a/channel/chat_channel.py b/channel/chat_channel.py index fe71207..907cea2 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -170,11 +170,13 @@ class ChatChannel(Channel): reply = self._generate_reply(context) logger.debug("[WX] ready to decorate reply: {}".format(reply)) - # reply的包装步骤 - reply = self._decorate_reply(context, reply) - # reply的发送步骤 - self._send_reply(context, reply) + # reply的包装步骤 + if reply and reply.content: + reply = self._decorate_reply(context, reply) + + # reply的发送步骤 + self._send_reply(context, reply) def _generate_reply(self, context: Context, reply: Reply = Reply()) -> Reply: e_context = PluginManager().emit_event( From 943aa05eaa58dba46a0a2cccd94b1d412313adbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1Q=E5=9B=A0?= <56462540+Meng-de-Cao@users.noreply.github.com> Date: Wed, 20 Mar 2024 21:22:15 +0800 Subject: [PATCH 20/20] Update xunfei_spark_bot.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认使用讯飞3.5模型 --- bot/xunfei/xunfei_spark_bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/xunfei/xunfei_spark_bot.py b/bot/xunfei/xunfei_spark_bot.py index 395d81e..9ca6b96 100644 --- a/bot/xunfei/xunfei_spark_bot.py +++ b/bot/xunfei/xunfei_spark_bot.py @@ -47,7 +47,8 @@ class XunFeiBot(Bot): # 默认使用v2.0版本: "ws://spark-api.xf-yun.com/v2.1/chat" # v1.5版本为: "ws://spark-api.xf-yun.com/v1.1/chat" # v3.0版本为: "ws://spark-api.xf-yun.com/v3.1/chat" - self.spark_url = "ws://spark-api.xf-yun.com/v3.1/chat" + # v3.5版本为: "wss://spark-api.xf-yun.com/v3.5/chat" + self.spark_url = "wss://spark-api.xf-yun.com/v3.5/chat" self.host = urlparse(self.spark_url).netloc self.path = urlparse(self.spark_url).path # 和wenxin使用相同的session机制