From 7dbd195e45be61a8a0259955a687decb2a495539 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 25 Jul 2024 01:12:53 +0800 Subject: [PATCH 01/10] Support images in webp format. --- channel/wechat/wechat_channel.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 870b487..de264ff 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -9,7 +9,7 @@ import json import os import threading import time - +import uuid import requests from bridge.context import * @@ -229,6 +229,12 @@ class WechatChannel(ChatChannel): image_storage.write(block) logger.info(f"[WX] download image success, size={size}, img_url={img_url}") image_storage.seek(0) + if img_url.endswith(".webp"): + try: + image_storage = _convert_webp_to_png(image_storage) + except Exception as e: + logger.error(f"Failed to convert image: {e}") + return itchat.send_image(image_storage, toUserName=receiver) logger.info("[WX] sendImage url={}, receiver={}".format(img_url, receiver)) elif reply.type == ReplyType.IMAGE: # 从文件读取图片 @@ -266,6 +272,7 @@ def _send_login_success(): except Exception as e: pass + def _send_logout(): try: from common.linkai_client import chat_client @@ -274,6 +281,7 @@ def _send_logout(): except Exception as e: pass + def _send_qr_code(qrcode_list: list): try: from common.linkai_client import chat_client @@ -281,3 +289,19 @@ def _send_qr_code(qrcode_list: list): chat_client.send_qrcode(qrcode_list) except Exception as e: pass + + +def _convert_webp_to_png(webp_image): + from PIL import Image + try: + webp_image.seek(0) + img = Image.open(webp_image).convert("RGBA") + png_image = io.BytesIO() + unique_filename = f"{uuid.uuid4()}.png" + img.save(png_image, format="PNG") + png_image.name = unique_filename + png_image.seek(0) + return png_image + except Exception as e: + logger.error(f"Failed to convert WEBP to PNG: {e}") + raise From e68936e36efa34753c8677407fc5aae9e72216e6 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 25 Jul 2024 01:19:44 +0800 Subject: [PATCH 02/10] Support images in webp format. --- channel/wechat/wechat_channel.py | 3 --- plugins/keyword/keyword.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index de264ff..f2c465d 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -9,7 +9,6 @@ import json import os import threading import time -import uuid import requests from bridge.context import * @@ -297,9 +296,7 @@ def _convert_webp_to_png(webp_image): webp_image.seek(0) img = Image.open(webp_image).convert("RGBA") png_image = io.BytesIO() - unique_filename = f"{uuid.uuid4()}.png" img.save(png_image, format="PNG") - png_image.name = unique_filename png_image.seek(0) return png_image except Exception as e: diff --git a/plugins/keyword/keyword.py b/plugins/keyword/keyword.py index 87cd054..281b8af 100644 --- a/plugins/keyword/keyword.py +++ b/plugins/keyword/keyword.py @@ -55,7 +55,7 @@ class Keyword(Plugin): reply_text = self.keyword[content] # 判断匹配内容的类型 - if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".gif", ".img"]): + if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".webp", ".jpeg", ".png", ".gif", ".img"]): # 如果是以 http:// 或 https:// 开头,且".jpg", ".jpeg", ".png", ".gif", ".img"结尾,则认为是图片 URL。 reply = Reply() reply.type = ReplyType.IMAGE_URL From 1673de73ba16d899a3d6c7df3cc5cc6c5e6b3813 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 25 Jul 2024 22:58:57 +0800 Subject: [PATCH 03/10] Role plugin supports more bots. --- plugins/role/role.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/role/role.py b/plugins/role/role.py index c75aa90..0f514d7 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -99,7 +99,8 @@ class Role(Plugin): if e_context["context"].type != ContextType.TEXT: return btype = Bridge().get_bot_type("chat") - if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI]: + if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.QWEN_DASHSCOPE, const.XUNFEI, const.BAIDU, const.ZHIPU_AI, const.MOONSHOT, const.MiniMax]: + logger.warn(f'不支持的bot: {btype}') return bot = Bridge().get_bot("chat") content = e_context["context"].content[:] From baff5fafec84cb89dbbb24964cccbd390ca8661d Mon Sep 17 00:00:00 2001 From: 6vision Date: Sun, 28 Jul 2024 00:03:16 +0800 Subject: [PATCH 04/10] Optimization --- channel/wechat/wechat_channel.py | 18 +++--------------- channel/wechatcom/wechatcomapp_channel.py | 8 +++++++- common/utils.py | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index f2c465d..7c52989 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -20,6 +20,7 @@ from common.expired_dict import ExpiredDict from common.log import logger from common.singleton import singleton from common.time_check import time_checker +from common.utils import convert_webp_to_png from config import conf, get_appdata_dir from lib import itchat from lib.itchat.content import * @@ -228,9 +229,9 @@ class WechatChannel(ChatChannel): image_storage.write(block) logger.info(f"[WX] download image success, size={size}, img_url={img_url}") image_storage.seek(0) - if img_url.endswith(".webp"): + if ".webp" in img_url: try: - image_storage = _convert_webp_to_png(image_storage) + image_storage = convert_webp_to_png(image_storage) except Exception as e: logger.error(f"Failed to convert image: {e}") return @@ -289,16 +290,3 @@ def _send_qr_code(qrcode_list: list): except Exception as e: pass - -def _convert_webp_to_png(webp_image): - from PIL import Image - try: - webp_image.seek(0) - img = Image.open(webp_image).convert("RGBA") - png_image = io.BytesIO() - img.save(png_image, format="PNG") - png_image.seek(0) - return png_image - except Exception as e: - logger.error(f"Failed to convert WEBP to PNG: {e}") - raise diff --git a/channel/wechatcom/wechatcomapp_channel.py b/channel/wechatcom/wechatcomapp_channel.py index e403850..f692802 100644 --- a/channel/wechatcom/wechatcomapp_channel.py +++ b/channel/wechatcom/wechatcomapp_channel.py @@ -17,7 +17,7 @@ from channel.wechatcom.wechatcomapp_client import WechatComAppClient from channel.wechatcom.wechatcomapp_message import WechatComAppMessage from common.log import logger from common.singleton import singleton -from common.utils import compress_imgfile, fsize, split_string_by_utf8_length +from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png from config import conf, subscribe_msg from voice.audio_convert import any_to_amr, split_audio @@ -99,6 +99,12 @@ class WechatComAppChannel(ChatChannel): image_storage = compress_imgfile(image_storage, 10 * 1024 * 1024 - 1) logger.info("[wechatcom] image compressed, sz={}".format(fsize(image_storage))) image_storage.seek(0) + if ".webp" in img_url: + try: + image_storage = convert_webp_to_png(image_storage) + except Exception as e: + logger.error(f"Failed to convert image: {e}") + return try: response = self.client.media.upload("image", image_storage) logger.debug("[wechatcom] upload image response: {}".format(response)) diff --git a/common/utils.py b/common/utils.py index dd69c9d..2349898 100644 --- a/common/utils.py +++ b/common/utils.py @@ -2,7 +2,7 @@ import io import os from urllib.parse import urlparse from PIL import Image - +from common.log import logger def fsize(file): if isinstance(file, io.BytesIO): @@ -54,3 +54,17 @@ def split_string_by_utf8_length(string, max_length, max_split=0): def get_path_suffix(path): path = urlparse(path).path return os.path.splitext(path)[-1].lstrip('.') + + +def convert_webp_to_png(webp_image): + from PIL import Image + try: + webp_image.seek(0) + img = Image.open(webp_image).convert("RGBA") + png_image = io.BytesIO() + img.save(png_image, format="PNG") + png_image.seek(0) + return png_image + except Exception as e: + logger.error(f"Failed to convert WEBP to PNG: {e}") + raise From b544a4c95411d31c62a2bdcd6679969bb201a8ec Mon Sep 17 00:00:00 2001 From: 6vision Date: Mon, 29 Jul 2024 20:14:41 +0800 Subject: [PATCH 05/10] fix: Use default expiration time for ExpiredDict if not set in config --- channel/dingtalk/dingtalk_channel.py | 2 +- channel/wechat/wechat_channel.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/channel/dingtalk/dingtalk_channel.py b/channel/dingtalk/dingtalk_channel.py index 9128d95..6c99e5f 100644 --- a/channel/dingtalk/dingtalk_channel.py +++ b/channel/dingtalk/dingtalk_channel.py @@ -100,7 +100,7 @@ class DingTalkChanel(ChatChannel, dingtalk_stream.ChatbotHandler): super(dingtalk_stream.ChatbotHandler, self).__init__() self.logger = self.setup_logger() # 历史消息id暂存,用于幂等控制 - self.receivedMsgs = ExpiredDict(conf().get("expires_in_seconds")) + self.receivedMsgs = ExpiredDict(conf().get("expires_in_seconds", 3600)) logger.info("[DingTalk] client_id={}, client_secret={} ".format( self.dingtalk_client_id, self.dingtalk_client_secret)) # 无需群校验和前缀 diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 870b487..414f9bb 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -109,7 +109,7 @@ class WechatChannel(ChatChannel): def __init__(self): super().__init__() - self.receivedMsgs = ExpiredDict(conf().get("expires_in_seconds")) + self.receivedMsgs = ExpiredDict(conf().get("expires_in_seconds", 3600)) self.auto_login_times = 0 def startup(self): From 55ca652ad88d808e18d710aa4b7623848d1bfe18 Mon Sep 17 00:00:00 2001 From: 6vision Date: Tue, 30 Jul 2024 23:14:23 +0800 Subject: [PATCH 06/10] Default close tool plugin. --- plugins/tool/tool.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index a2ce4b6..ab8edf1 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -21,11 +21,15 @@ from plugins import * class Tool(Plugin): def __init__(self): super().__init__() - self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context - - self.app = self._reset_app() - - logger.info("[tool] inited") + try: + self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + self.app = self._reset_app() + if not self.tool_config.get("tools"): + raise Exception("config.json not found") + logger.info("[tool] inited") + except Exception as e: + logger.warn("[tool] init failed, ignore ") + raise e def get_help_text(self, verbose=False, **kwargs): help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力。" From c4f10fe876ff230e6e7fe67b1fbae1de600bff67 Mon Sep 17 00:00:00 2001 From: 6vision Date: Wed, 31 Jul 2024 00:01:56 +0800 Subject: [PATCH 07/10] fix: Default close tool plugin. --- plugins/tool/tool.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index ab8edf1..fe36a68 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -21,15 +21,13 @@ from plugins import * class Tool(Plugin): def __init__(self): super().__init__() - try: - self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context - self.app = self._reset_app() - if not self.tool_config.get("tools"): - raise Exception("config.json not found") - logger.info("[tool] inited") - except Exception as e: + self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + self.app = self._reset_app() + if not self.tool_config.get("tools"): logger.warn("[tool] init failed, ignore ") - raise e + raise Exception("config.json not found") + logger.info("[tool] inited") + def get_help_text(self, verbose=False, **kwargs): help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力。" From c415485801da3e1b7062b98e00b6a314616195d6 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 1 Aug 2024 17:57:48 +0800 Subject: [PATCH 08/10] Support Spark4.0 Ultra model, optimize model configuration. --- bot/xunfei/xunfei_spark_bot.py | 17 +++++++++-------- config.py | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bot/xunfei/xunfei_spark_bot.py b/bot/xunfei/xunfei_spark_bot.py index 9ca6b96..e9065e1 100644 --- a/bot/xunfei/xunfei_spark_bot.py +++ b/bot/xunfei/xunfei_spark_bot.py @@ -41,14 +41,15 @@ class XunFeiBot(Bot): self.api_key = conf().get("xunfei_api_key") self.api_secret = conf().get("xunfei_api_secret") # 默认使用v2.0版本: "generalv2" - # v1.5版本为 "general" - # v3.0版本为: "generalv3" - self.domain = "generalv3" - # 默认使用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" - # v3.5版本为: "wss://spark-api.xf-yun.com/v3.5/chat" - self.spark_url = "wss://spark-api.xf-yun.com/v3.5/chat" + # Spark Lite请求地址(spark_url): wss://spark-api.xf-yun.com/v1.1/chat, 对应的domain参数为: "general" + # Spark V2.0请求地址(spark_url): wss://spark-api.xf-yun.com/v2.1/chat, 对应的domain参数为: "generalv2" + # Spark Pro 请求地址(spark_url): wss://spark-api.xf-yun.com/v3.1/chat, 对应的domain参数为: "generalv3" + # Spark Pro-128K请求地址(spark_url): wss://spark-api.xf-yun.com/chat/pro-128k, 对应的domain参数为: "pro-128k" + # Spark Max 请求地址(spark_url): wss://spark-api.xf-yun.com/v3.5/chat, 对应的domain参数为: "generalv3.5" + # Spark4.0 Ultra 请求地址(spark_url): wss://spark-api.xf-yun.com/v4.0/chat, 对应的domain参数为: "4.0Ultra" + # 后续模型更新,对应的参数可以参考官网文档获取:https://www.xfyun.cn/doc/spark/Web.html + self.domain = conf().get("xunfei_domain", "generalv3.5") + self.spark_url = conf().get("xunfei_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机制 diff --git a/config.py b/config.py index cad6872..b6ae49e 100644 --- a/config.py +++ b/config.py @@ -73,6 +73,8 @@ available_setting = { "xunfei_app_id": "", # 讯飞应用ID "xunfei_api_key": "", # 讯飞 API key "xunfei_api_secret": "", # 讯飞 API secret + "xunfei_domain": "", # 讯飞模型对应的domain参数,Spark4.0 Ultra为 4.0Ultra,其他模型详见: https://www.xfyun.cn/doc/spark/Web.html + "xunfei_spark_url": "", # 讯飞模型对应的请求地址,Spark4.0 Ultra为 wss://spark-api.xf-yun.com/v4.0/chat,其他模型参考详见: https://www.xfyun.cn/doc/spark/Web.html # claude 配置 "claude_api_cookie": "", "claude_uuid": "", From 7e724b3fa3f541539e6321deedcdc18b7bca297f Mon Sep 17 00:00:00 2001 From: Saboteur7 <138805485+Saboteur7@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:06:25 +0800 Subject: [PATCH 09/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8177132..f2ca5db 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ DEMO视频:https://cdn.link-ai.tech/doc/cow_demo.mp4 # 🏷 更新日志 +>**2024.08.02:** [1.7.0版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.6.9) 新增 讯飞4.0 模型、知识库引用来源展示、相关插件优化 + >**2024.07.19:** [1.6.9版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.6.9) 新增 gpt-4o-mini 模型、阿里语音识别、企微应用渠道路由优化 >**2024.07.05:** [1.6.8版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.6.8) 和 [1.6.7版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.6.7),Claude3.5, Gemini 1.5 Pro, MiniMax模型、工作流图片输入、模型列表完善 From da81f05804e95f97fa3d1515e5e63338613ee7c4 Mon Sep 17 00:00:00 2001 From: 6vision Date: Wed, 14 Aug 2024 23:03:57 +0800 Subject: [PATCH 10/10] Optimize log information printing --- plugins/role/role.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/role/role.py b/plugins/role/role.py index 0f514d7..7c7b106 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -99,8 +99,8 @@ class Role(Plugin): if e_context["context"].type != ContextType.TEXT: return btype = Bridge().get_bot_type("chat") - if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.QWEN_DASHSCOPE, const.XUNFEI, const.BAIDU, const.ZHIPU_AI, const.MOONSHOT, const.MiniMax]: - logger.warn(f'不支持的bot: {btype}') + if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.QWEN_DASHSCOPE, const.XUNFEI, const.BAIDU, const.ZHIPU_AI, const.MOONSHOT, const.MiniMax, const.LINKAI]: + logger.debug(f'不支持的bot: {btype}') return bot = Bridge().get_bot("chat") content = e_context["context"].content[:]