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 916762cc8c4a5b4696bbe1a3df164b8712e6c059 Mon Sep 17 00:00:00 2001 From: stx116 <63991243+stx116@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:18:56 +0800 Subject: [PATCH 03/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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/xunfei/xunfei_spark_bot.py b/bot/xunfei/xunfei_spark_bot.py index 395d81e..cd9e63a 100644 --- a/bot/xunfei/xunfei_spark_bot.py +++ b/bot/xunfei/xunfei_spark_bot.py @@ -46,8 +46,8 @@ class XunFeiBot(Bot): 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" - self.spark_url = "ws://spark-api.xf-yun.com/v3.1/chat" + # v3.5版本为: "ws://spark-api.xf-yun.com/v3.5/chat" + self.spark_url = "ws://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 a25d7ea65b4ea4fb4a32d48a5ce03883a6ffd941 Mon Sep 17 00:00:00 2001 From: xiexin12138 Date: Tue, 20 Feb 2024 16:40:00 +0800 Subject: [PATCH 04/20] =?UTF-8?q?add=20feature=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=99=BA=E8=B0=B1=20AI=20=E7=9A=84=E5=91=BD=E4=BB=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=EF=BC=8C=E4=BD=BF=E5=85=B6=E6=94=AF=E6=8C=81=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/godcmd/godcmd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index a965a68..c36dd91 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -313,7 +313,7 @@ class Godcmd(Plugin): except Exception as e: ok, result = False, "你没有设置私有GPT模型" elif cmd == "reset": - if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI, const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI]: + if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI, const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI, const.ZHIPU_AI]: bot.sessions.clear_session(session_id) if Bridge().chat_bots.get(bottype): Bridge().chat_bots.get(bottype).sessions.clear_session(session_id) @@ -339,7 +339,7 @@ class Godcmd(Plugin): ok, result = True, "配置已重载" elif cmd == "resetall": if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI, - const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI]: + const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI, const.ZHIPU_AI]: channel.cancel_all_session() bot.sessions.clear_all_session() ok, result = True, "重置所有会话成功" From 64ae3d1e2184bf734f8d31317c04783f40e50996 Mon Sep 17 00:00:00 2001 From: Zhuoheng Lee Date: Wed, 21 Feb 2024 14:14:19 +0800 Subject: [PATCH 05/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 06/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 805bea0d5f939241b347b8e8407298630cd37115 Mon Sep 17 00:00:00 2001 From: FB208 Date: Tue, 12 Mar 2024 10:39:51 +0800 Subject: [PATCH 07/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86claude=20api?= =?UTF-8?q?=E7=9A=84=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/bot_factory.py | 5 +- bot/claudeapi/claude_api_bot.py | 125 ++++++++++++++++++++++++++++ bot/claudeapi/claude_api_session.py | 74 ++++++++++++++++ bridge/bridge.py | 7 +- common/const.py | 4 +- config-template.json | 1 + config.py | 2 + requirements.txt | 1 + 8 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 bot/claudeapi/claude_api_bot.py create mode 100644 bot/claudeapi/claude_api_session.py diff --git a/bot/bot_factory.py b/bot/bot_factory.py index 2046da7..b5936c4 100644 --- a/bot/bot_factory.py +++ b/bot/bot_factory.py @@ -2,6 +2,7 @@ channel factory """ from common import const +from common.log import logger def create_bot(bot_type): @@ -43,7 +44,9 @@ def create_bot(bot_type): elif bot_type == const.CLAUDEAI: from bot.claude.claude_ai_bot import ClaudeAIBot return ClaudeAIBot() - + elif bot_type == const.CLAUDEAPI: + from bot.claudeapi.claude_api_bot import ClaudeAPIBot + return ClaudeAPIBot() elif bot_type == const.QWEN: from bot.ali.ali_qwen_bot import AliQwenBot return AliQwenBot() diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py new file mode 100644 index 0000000..e9cf46d --- /dev/null +++ b/bot/claudeapi/claude_api_bot.py @@ -0,0 +1,125 @@ +# encoding:utf-8 + +import time + +import openai +import openai.error +import anthropic + +from bot.bot import Bot +from bot.openai.open_ai_image import OpenAIImage +from bot.claudeapi.claude_api_session import ClaudeAPISession +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 + +user_session = dict() + + +# OpenAI对话模型API (可用) +class ClaudeAPIBot(Bot, OpenAIImage): + def __init__(self): + super().__init__() + self.claudeClient = anthropic.Anthropic( + api_key=conf().get("claude_api_key") + ) + openai.api_key = conf().get("open_ai_api_key") + if conf().get("open_ai_api_base"): + openai.api_base = conf().get("open_ai_api_base") + proxy = conf().get("proxy") + if proxy: + openai.proxy = proxy + + self.sessions = SessionManager(ClaudeAPISession, model=conf().get("model") or "text-davinci-003") + + def reply(self, query, context=None): + # acquire reply content + if context and context.type: + if context.type == ContextType.TEXT: + logger.info("[CLAUDE_API] query={}".format(query)) + session_id = context["session_id"] + reply = None + if query == "#清除记忆": + self.sessions.clear_session(session_id) + reply = Reply(ReplyType.INFO, "记忆已清除") + elif query == "#清除所有": + self.sessions.clear_all_session() + reply = Reply(ReplyType.INFO, "所有人记忆已清除") + else: + session = self.sessions.session_query(query, session_id) + result = self.reply_text(session) + logger.info(result) + total_tokens, completion_tokens, reply_content = ( + result["total_tokens"], + result["completion_tokens"], + result["content"], + ) + logger.debug( + "[CLAUDE_API] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(str(session), session_id, reply_content, completion_tokens) + ) + + if total_tokens == 0: + reply = Reply(ReplyType.ERROR, reply_content) + else: + self.sessions.session_reply(reply_content, session_id, total_tokens) + reply = Reply(ReplyType.TEXT, 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 + + def reply_text(self, session: ClaudeAPISession, retry_count=0): + try: + logger.info("[CLAUDE_API] sendMessage={}".format(str(session))) + response = self.claudeClient.messages.create( + model=conf().get("model"), + max_tokens=1024, + # system=conf().get("system"), + messages=[ + {"role": "user", "content": "{}".format(str(session))} + ] + ) + # response = openai.Completion.create(prompt=str(session), **self.args) + res_content = response.content[0].text.strip().replace("<|endoftext|>", "") + total_tokens = response.usage.input_tokens+response.usage.output_tokens + completion_tokens = response.usage.output_tokens + logger.info("[CLAUDE_API] reply={}".format(res_content)) + return { + "total_tokens": total_tokens, + "completion_tokens": completion_tokens, + "content": res_content, + } + except Exception as e: + need_retry = retry_count < 2 + result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"} + if isinstance(e, openai.error.RateLimitError): + logger.warn("[CLAUDE_API] RateLimitError: {}".format(e)) + result["content"] = "提问太快啦,请休息一下再问我吧" + if need_retry: + time.sleep(20) + elif isinstance(e, openai.error.Timeout): + logger.warn("[CLAUDE_API] Timeout: {}".format(e)) + result["content"] = "我没有收到你的消息" + if need_retry: + time.sleep(5) + elif isinstance(e, openai.error.APIConnectionError): + logger.warn("[CLAUDE_API] APIConnectionError: {}".format(e)) + need_retry = False + result["content"] = "我连接不到你的网络" + else: + logger.warn("[CLAUDE_API] Exception: {}".format(e)) + need_retry = False + self.sessions.clear_session(session.session_id) + + if need_retry: + logger.warn("[CLAUDE_API] 第{}次重试".format(retry_count + 1)) + return self.reply_text(session, retry_count + 1) + else: + return result diff --git a/bot/claudeapi/claude_api_session.py b/bot/claudeapi/claude_api_session.py new file mode 100644 index 0000000..a5e9b54 --- /dev/null +++ b/bot/claudeapi/claude_api_session.py @@ -0,0 +1,74 @@ +from bot.session_manager import Session +from common.log import logger + + +class ClaudeAPISession(Session): + def __init__(self, session_id, system_prompt=None, model="text-davinci-003"): + super().__init__(session_id, system_prompt) + self.model = model + self.reset() + + def __str__(self): + # 构造对话模型的输入 + """ + e.g. Q: xxx + A: xxx + Q: xxx + """ + prompt = "" + for item in self.messages: + if item["role"] == "system": + prompt += item["content"] + "<|endoftext|>\n\n\n" + elif item["role"] == "user": + prompt += "Q: " + item["content"] + "\n" + elif item["role"] == "assistant": + prompt += "\n\nA: " + item["content"] + "<|endoftext|>\n" + + if len(self.messages) > 0 and self.messages[-1]["role"] == "user": + prompt += "A: " + return prompt + + 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) > 1: + self.messages.pop(0) + elif len(self.messages) == 1 and self.messages[0]["role"] == "assistant": + self.messages.pop(0) + if precise: + cur_tokens = self.calc_tokens() + else: + cur_tokens = len(str(self)) + break + elif len(self.messages) == 1 and self.messages[0]["role"] == "user": + logger.warn("user question exceed max_tokens. total_tokens={}".format(cur_tokens)) + break + else: + logger.debug("max_tokens={}, total_tokens={}, len(conversation)={}".format(max_tokens, cur_tokens, len(self.messages))) + break + if precise: + cur_tokens = self.calc_tokens() + else: + cur_tokens = len(str(self)) + return cur_tokens + def calc_tokens(self): + return num_tokens_from_string(str(self), self.model) + + +# refer to https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb +def num_tokens_from_string(string: str, model: str) -> int: + """Returns the number of tokens in a text string.""" + num_tokens = len(string) + return num_tokens + + + + diff --git a/bridge/bridge.py b/bridge/bridge.py index 88e6b18..2c76844 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -18,6 +18,7 @@ class Bridge(object): "text_to_voice": conf().get("text_to_voice", "google"), "translate": conf().get("translate", "baidu"), } + # 这边取配置的模型 model_type = conf().get("model") or const.GPT35 if model_type in ["text-davinci-003"]: self.btype["chat"] = const.OPEN_AI @@ -33,6 +34,8 @@ class Bridge(object): self.btype["chat"] = const.GEMINI if model_type in [const.ZHIPU_AI]: self.btype["chat"] = const.ZHIPU_AI + if model_type in [const.CLAUDE3]: + self.btype["chat"] = const.CLAUDEAPI if conf().get("use_linkai") and conf().get("linkai_api_key"): self.btype["chat"] = const.LINKAI @@ -40,12 +43,12 @@ class Bridge(object): self.btype["voice_to_text"] = const.LINKAI if not conf().get("text_to_voice") or conf().get("text_to_voice") in ["openai", const.TTS_1, const.TTS_1_HD]: self.btype["text_to_voice"] = const.LINKAI - if model_type in ["claude"]: self.btype["chat"] = const.CLAUDEAI + self.bots = {} self.chat_bots = {} - + # 模型对应的接口 def get_bot(self, typename): if self.bots.get(typename) is None: logger.info("create bot {} for {}".format(self.btype[typename], typename)) diff --git a/common/const.py b/common/const.py index aeb9dcc..ce11398 100644 --- a/common/const.py +++ b/common/const.py @@ -6,12 +6,14 @@ XUNFEI = "xunfei" CHATGPTONAZURE = "chatGPTOnAzure" LINKAI = "linkai" CLAUDEAI = "claude" +CLAUDEAPI= "claudeAPI" QWEN = "qwen" GEMINI = "gemini" ZHIPU_AI = "glm-4" # model +CLAUDE3="claude-3-opus-20240229" GPT35 = "gpt-3.5-turbo" GPT4 = "gpt-4" GPT4_TURBO_PREVIEW = "gpt-4-0125-preview" @@ -20,7 +22,7 @@ WHISPER_1 = "whisper-1" 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", +MODEL_LIST = ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "wenxin", "wenxin-4", "xunfei", "claude","claude-3-opus-20240229", "gpt-4-turbo", "gpt-4-turbo-preview", "gpt-4-1106-preview", GPT4_TURBO_PREVIEW, QWEN, GEMINI, ZHIPU_AI] # channel diff --git a/config-template.json b/config-template.json index bdaadde..f3b253d 100644 --- a/config-template.json +++ b/config-template.json @@ -2,6 +2,7 @@ "channel_type": "wx", "model": "", "open_ai_api_key": "YOUR API KEY", + "claude_api_key": "YOUR API KEY", "text_to_image": "dall-e-2", "voice_to_text": "openai", "text_to_voice": "openai", diff --git a/config.py b/config.py index 154c633..29a8f54 100644 --- a/config.py +++ b/config.py @@ -67,6 +67,8 @@ available_setting = { # claude 配置 "claude_api_cookie": "", "claude_uuid": "", + # claude api key + "claude_api_key":"", # 通义千问API, 获取方式查看文档 https://help.aliyun.com/document_detail/2587494.html "qwen_access_key_id": "", "qwen_access_key_secret": "", diff --git a/requirements.txt b/requirements.txt index c032e08..f49bdfa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ chardet>=5.1.0 Pillow pre-commit web.py +anthropic \ No newline at end of file From a241dc6785d7ac9725c9f97a9fefb26e0ce7c16a Mon Sep 17 00:00:00 2001 From: FB208 Date: Tue, 12 Mar 2024 13:09:55 +0800 Subject: [PATCH 08/20] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dadf801..cfd371d 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ pip3 install -r requirements-optional.txt # config.json文件内容示例 { "open_ai_api_key": "YOUR API KEY", # 填入上面创建的 OpenAI API KEY - "model": "gpt-3.5-turbo", # 模型名称, 支持 gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4, wenxin, xunfei + "model": "gpt-3.5-turbo", # 模型名称, 支持 gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4, wenxin, xunfei, claude-3-opus-20240229 + "claude_api_key":"YOUR API KEY" # 如果选用claude3模型的话,配置这个key,同时如想使用生图,语音等功能,仍需配置open_ai_api_key "proxy": "", # 代理客户端的ip和端口,国内环境开启代理的需要填写该项,如 "127.0.0.1:7890" "single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复 "single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人 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 09/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 10/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 11/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 12/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 13/20] fix: no need to send when message is empty --- bot/linkai/link_ai_bot.py | 1 + channel/chat_channel.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 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) + if reply and reply.content: + reply = self._decorate_reply(context, reply) - # reply的发送步骤 - self._send_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 14/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机制 From cac7a6228a5eaece625006e33ca1342ba02bee4b Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 25 Mar 2024 21:41:40 +0800 Subject: [PATCH 15/20] fix: claude api optimize --- bot/chatgpt/chat_gpt_session.py | 3 ++- bot/claudeapi/claude_api_bot.py | 25 ++++++++++++++++++------- bot/linkai/link_ai_bot.py | 14 ++++++++++---- bridge/bridge.py | 2 +- requirements-optional.txt | 2 ++ requirements.txt | 3 +-- 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/bot/chatgpt/chat_gpt_session.py b/bot/chatgpt/chat_gpt_session.py index c4b17fd..089fa4e 100644 --- a/bot/chatgpt/chat_gpt_session.py +++ b/bot/chatgpt/chat_gpt_session.py @@ -68,7 +68,8 @@ def num_tokens_from_messages(messages, model): "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "gpt-35-turbo-16k", "gpt-4-turbo-preview", "gpt-4-1106-preview", const.GPT4_TURBO_PREVIEW, const.GPT4_VISION_PREVIEW]: return num_tokens_from_messages(messages, model="gpt-4") - + elif model.startswith("claude-3"): + return num_tokens_from_messages(messages, model="gpt-3.5-turbo") try: encoding = tiktoken.encoding_for_model(model) except KeyError: diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index e9cf46d..c782d3e 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -9,6 +9,7 @@ import anthropic from bot.bot import Bot from bot.openai.open_ai_image import OpenAIImage from bot.claudeapi.claude_api_session import ClaudeAPISession +from bot.chatgpt.chat_gpt_session import ChatGPTSession from bot.session_manager import SessionManager from bridge.context import ContextType from bridge.reply import Reply, ReplyType @@ -32,7 +33,7 @@ class ClaudeAPIBot(Bot, OpenAIImage): if proxy: openai.proxy = proxy - self.sessions = SessionManager(ClaudeAPISession, model=conf().get("model") or "text-davinci-003") + self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "text-davinci-003") def reply(self, query, context=None): # acquire reply content @@ -75,16 +76,17 @@ class ClaudeAPIBot(Bot, OpenAIImage): reply = Reply(ReplyType.ERROR, retstring) return reply - def reply_text(self, session: ClaudeAPISession, retry_count=0): + def reply_text(self, session: ChatGPTSession, retry_count=0): try: - logger.info("[CLAUDE_API] sendMessage={}".format(str(session))) + if session.messages[0].get("role") == "system": + system = session.messages[0].get("content") + session.messages.pop(0) + actual_model = self._model_mapping(conf().get("model")) response = self.claudeClient.messages.create( - model=conf().get("model"), + model=actual_model, max_tokens=1024, # system=conf().get("system"), - messages=[ - {"role": "user", "content": "{}".format(str(session))} - ] + messages=session.messages ) # response = openai.Completion.create(prompt=str(session), **self.args) res_content = response.content[0].text.strip().replace("<|endoftext|>", "") @@ -123,3 +125,12 @@ class ClaudeAPIBot(Bot, OpenAIImage): return self.reply_text(session, retry_count + 1) else: return result + + def _model_mapping(self, model) -> str: + if model == "claude-3-opus": + return "claude-3-opus-20240229" + elif model == "claude-3-sonnet": + return "claude-3-sonnet-20240229" + elif model == "claude-3-haiku": + return "claude-3-haiku-20240307" + return model diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 0df20bc..2a3e57d 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -130,9 +130,12 @@ class LinkAIBot(Bot): response = res.json() reply_content = response["choices"][0]["message"]["content"] total_tokens = response["usage"]["total_tokens"] - logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}") - self.sessions.session_reply(reply_content, session_id, total_tokens, query=query) - + res_code = response.get('code') + logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}, res_code={res_code}") + if res_code == 429: + logger.warn(f"[LINKAI] 用户访问超出限流配置,sender_id={body.get('sender_id')}") + else: + self.sessions.session_reply(reply_content, session_id, total_tokens, query=query) agent_suffix = self._fetch_agent_suffix(response) if agent_suffix: reply_content += agent_suffix @@ -161,7 +164,10 @@ class LinkAIBot(Bot): logger.warn(f"[LINKAI] do retry, times={retry_count}") return self._chat(query, context, retry_count + 1) - return Reply(ReplyType.TEXT, "提问太快啦,请休息一下再问我吧") + error_reply = "提问太快啦,请休息一下再问我吧" + if res.status_code == 409: + error_reply = "这个问题我还没有学会,请问我其它问题吧" + return Reply(ReplyType.TEXT, error_reply) except Exception as e: logger.exception(e) diff --git a/bridge/bridge.py b/bridge/bridge.py index 2c76844..cf62bff 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -34,7 +34,7 @@ class Bridge(object): self.btype["chat"] = const.GEMINI if model_type in [const.ZHIPU_AI]: self.btype["chat"] = const.ZHIPU_AI - if model_type in [const.CLAUDE3]: + if model_type and model_type.startswith("claude-3"): self.btype["chat"] = const.CLAUDEAPI if conf().get("use_linkai") and conf().get("linkai_api_key"): diff --git a/requirements-optional.txt b/requirements-optional.txt index abb8a4e..e076558 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -26,6 +26,8 @@ websocket-client==1.2.0 # claude bot curl_cffi +# claude API +anthropic # tongyi qwen broadscope_bailian diff --git a/requirements.txt b/requirements.txt index f49bdfa..495917b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +6,4 @@ requests>=2.28.2 chardet>=5.1.0 Pillow pre-commit -web.py -anthropic \ No newline at end of file +web.py \ No newline at end of file From 23b750fc4fa0cb8a3515bd871824e511a3c7b298 Mon Sep 17 00:00:00 2001 From: "WILMAR\\dengjingren" Date: Mon, 25 Mar 2024 21:56:26 +0800 Subject: [PATCH 16/20] blackroom --- plugins/source.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/source.json b/plugins/source.json index ebacc28..b0a7811 100644 --- a/plugins/source.json +++ b/plugins/source.json @@ -19,10 +19,14 @@ "Apilot": { "url": "https://github.com/6vision/Apilot.git", "desc": "通过api直接查询早报、热榜、快递、天气等实用信息的插件" - } + }, "pictureChange": { "url": "https://github.com/Yanyutin753/pictureChange.git", "desc": "利用stable-diffusion和百度Ai进行图生图或者画图的插件" + }, + "Blackroom": { + "url": "https://github.com/dividduang/blackroom.git", + "desc": "小黑屋插件,被拉进小黑屋的人将不能使用@bot的功能的插件" } } } From 1fafd392980866ef696fd03111521075feef33d5 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Tue, 26 Mar 2024 00:06:50 +0800 Subject: [PATCH 17/20] fix: gemini session bug --- bot/chatgpt/chat_gpt_session.py | 2 +- bot/claudeapi/claude_api_bot.py | 7 +-- bot/claudeapi/claude_api_session.py | 74 ----------------------------- bot/gemini/google_gemini_bot.py | 5 +- bot/linkai/link_ai_bot.py | 1 + common/const.py | 5 +- plugins/godcmd/godcmd.py | 2 +- 7 files changed, 11 insertions(+), 85 deletions(-) delete mode 100644 bot/claudeapi/claude_api_session.py diff --git a/bot/chatgpt/chat_gpt_session.py b/bot/chatgpt/chat_gpt_session.py index 089fa4e..43b3b46 100644 --- a/bot/chatgpt/chat_gpt_session.py +++ b/bot/chatgpt/chat_gpt_session.py @@ -62,7 +62,7 @@ def num_tokens_from_messages(messages, model): import tiktoken - if model in ["gpt-3.5-turbo-0301", "gpt-35-turbo", "gpt-3.5-turbo-1106"]: + if model in ["gpt-3.5-turbo-0301", "gpt-35-turbo", "gpt-3.5-turbo-1106", "moonshot"]: return num_tokens_from_messages(messages, model="gpt-3.5-turbo") elif model in ["gpt-4-0314", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "gpt-35-turbo-16k", "gpt-4-turbo-preview", diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index c782d3e..dfb4b06 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -8,8 +8,8 @@ import anthropic from bot.bot import Bot from bot.openai.open_ai_image import OpenAIImage -from bot.claudeapi.claude_api_session import ClaudeAPISession from bot.chatgpt.chat_gpt_session import ChatGPTSession +from bot.gemini.google_gemini_bot import GoogleGeminiBot from bot.session_manager import SessionManager from bridge.context import ContextType from bridge.reply import Reply, ReplyType @@ -78,15 +78,12 @@ class ClaudeAPIBot(Bot, OpenAIImage): def reply_text(self, session: ChatGPTSession, retry_count=0): try: - if session.messages[0].get("role") == "system": - system = session.messages[0].get("content") - session.messages.pop(0) actual_model = self._model_mapping(conf().get("model")) response = self.claudeClient.messages.create( model=actual_model, max_tokens=1024, # system=conf().get("system"), - messages=session.messages + messages=GoogleGeminiBot.filter_messages(session.messages) ) # response = openai.Completion.create(prompt=str(session), **self.args) res_content = response.content[0].text.strip().replace("<|endoftext|>", "") diff --git a/bot/claudeapi/claude_api_session.py b/bot/claudeapi/claude_api_session.py deleted file mode 100644 index a5e9b54..0000000 --- a/bot/claudeapi/claude_api_session.py +++ /dev/null @@ -1,74 +0,0 @@ -from bot.session_manager import Session -from common.log import logger - - -class ClaudeAPISession(Session): - def __init__(self, session_id, system_prompt=None, model="text-davinci-003"): - super().__init__(session_id, system_prompt) - self.model = model - self.reset() - - def __str__(self): - # 构造对话模型的输入 - """ - e.g. Q: xxx - A: xxx - Q: xxx - """ - prompt = "" - for item in self.messages: - if item["role"] == "system": - prompt += item["content"] + "<|endoftext|>\n\n\n" - elif item["role"] == "user": - prompt += "Q: " + item["content"] + "\n" - elif item["role"] == "assistant": - prompt += "\n\nA: " + item["content"] + "<|endoftext|>\n" - - if len(self.messages) > 0 and self.messages[-1]["role"] == "user": - prompt += "A: " - return prompt - - 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) > 1: - self.messages.pop(0) - elif len(self.messages) == 1 and self.messages[0]["role"] == "assistant": - self.messages.pop(0) - if precise: - cur_tokens = self.calc_tokens() - else: - cur_tokens = len(str(self)) - break - elif len(self.messages) == 1 and self.messages[0]["role"] == "user": - logger.warn("user question exceed max_tokens. total_tokens={}".format(cur_tokens)) - break - else: - logger.debug("max_tokens={}, total_tokens={}, len(conversation)={}".format(max_tokens, cur_tokens, len(self.messages))) - break - if precise: - cur_tokens = self.calc_tokens() - else: - cur_tokens = len(str(self)) - return cur_tokens - def calc_tokens(self): - return num_tokens_from_string(str(self), self.model) - - -# refer to https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb -def num_tokens_from_string(string: str, model: str) -> int: - """Returns the number of tokens in a text string.""" - num_tokens = len(string) - return num_tokens - - - - diff --git a/bot/gemini/google_gemini_bot.py b/bot/gemini/google_gemini_bot.py index e63f1cb..6132b78 100644 --- a/bot/gemini/google_gemini_bot.py +++ b/bot/gemini/google_gemini_bot.py @@ -33,7 +33,7 @@ class GoogleGeminiBot(Bot): logger.info(f"[Gemini] query={query}") session_id = context["session_id"] session = self.sessions.session_query(query, session_id) - gemini_messages = self._convert_to_gemini_messages(self._filter_messages(session.messages)) + gemini_messages = self._convert_to_gemini_messages(self.filter_messages(session.messages)) genai.configure(api_key=self.api_key) model = genai.GenerativeModel('gemini-pro') response = model.generate_content(gemini_messages) @@ -61,7 +61,8 @@ class GoogleGeminiBot(Bot): }) return res - def _filter_messages(self, messages: list): + @staticmethod + def filter_messages(messages: list): res = [] turn = "user" if not messages: diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 2a3e57d..960b788 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -7,6 +7,7 @@ import requests import config from bot.bot import Bot from bot.chatgpt.chat_gpt_session import ChatGPTSession +from bot.gemini.google_gemini_bot import GoogleGeminiBot from bot.session_manager import SessionManager from bridge.context import Context, ContextType from bridge.reply import Reply, ReplyType diff --git a/common/const.py b/common/const.py index ce11398..6141b7d 100644 --- a/common/const.py +++ b/common/const.py @@ -10,10 +10,11 @@ CLAUDEAPI= "claudeAPI" QWEN = "qwen" GEMINI = "gemini" ZHIPU_AI = "glm-4" +MOONSHOT = "moonshot" # model -CLAUDE3="claude-3-opus-20240229" +CLAUDE3 = "claude-3-opus-20240229" GPT35 = "gpt-3.5-turbo" GPT4 = "gpt-4" GPT4_TURBO_PREVIEW = "gpt-4-0125-preview" @@ -23,7 +24,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","claude-3-opus-20240229", "gpt-4-turbo", - "gpt-4-turbo-preview", "gpt-4-1106-preview", GPT4_TURBO_PREVIEW, QWEN, GEMINI, ZHIPU_AI] + "gpt-4-turbo-preview", "gpt-4-1106-preview", GPT4_TURBO_PREVIEW, QWEN, GEMINI, ZHIPU_AI, MOONSHOT] # channel FEISHU = "feishu" diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index c36dd91..d6383bf 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -339,7 +339,7 @@ class Godcmd(Plugin): ok, result = True, "配置已重载" elif cmd == "resetall": if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI, - const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI, const.ZHIPU_AI]: + const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI, const.ZHIPU_AI, const.MOONSHOT]: channel.cancel_all_session() bot.sessions.clear_all_session() ok, result = True, "重置所有会话成功" From 21825faab01e3ce541d8ea5c5eb184de54e1520e Mon Sep 17 00:00:00 2001 From: zhayujie Date: Tue, 26 Mar 2024 16:01:05 +0800 Subject: [PATCH 18/20] docs: update README.md --- README.md | 20 +++++++------------- docs/version/old-version.md | 13 +++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 docs/version/old-version.md diff --git a/README.md b/README.md index cfd371d..2104829 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 最新版本支持的功能如下: - [x] **多端部署:** 有多种部署方式可选择且功能完备,目前已支持个人微信、微信公众号和、企业微信、飞书、钉钉等部署方式 -- [x] **基础对话:** 私聊及群聊的消息智能回复,支持多轮会话上下文记忆,支持 GPT-3.5, GPT-4, claude, Gemini, 文心一言, 讯飞星火, 通义千问,ChatGLM +- [x] **基础对话:** 私聊及群聊的消息智能回复,支持多轮会话上下文记忆,支持 GPT-3.5, GPT-4, Claude-3, Gemini, 文心一言, 讯飞星火, 通义千问,ChatGLM-4 - [x] **语音能力:** 可识别语音消息,通过文字或语音回复,支持 azure, baidu, google, openai(whisper/tts) 等多种语音模型 - [x] **图像能力:** 支持图片生成、图片识别、图生图(如照片修复),可选择 Dall-E-3, stable diffusion, replicate, midjourney, CogView-3, vision模型 - [x] **丰富插件:** 支持个性化插件扩展,已实现多角色切换、文字冒险、敏感词过滤、聊天记录总结、文档总结和对话、联网搜索等插件 @@ -35,7 +35,11 @@ SaaS服务、私有化部署、稳定托管接入 等多种模式。 # 更新日志 ->**2023.11.11:** [1.5.3版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.3) 和 [1.5.4版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.4),新增Google Gemini、通义千问模型 +>**2024.03.26:** [1.5.8版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.8) 和 [1.5.7版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.7),新增 GLM-4、Claude-3 模型,edge-tts 语音支持 + +>**2024.01.26:** [1.5.6版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.6) 和 [1.5.5版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.5),钉钉接入,tool插件升级,4-turbo模型更新 + +>**2023.11.11:** [1.5.3版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.3) 和 [1.5.4版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.4),新增通义千问模型、Google Gemini >**2023.11.10:** [1.5.2版本](https://github.com/zhayujie/chatgpt-on-wechat/releases/tag/1.5.2),新增飞书通道、图像识别对话、黑名单配置 @@ -49,17 +53,7 @@ SaaS服务、私有化部署、稳定托管接入 等多种模式。 >**2023.06.12:** 接入 [LinkAI](https://link-ai.tech/console) 平台,可在线创建领域知识库,并接入微信、公众号及企业微信中,打造专属客服机器人。使用参考 [接入文档](https://link-ai.tech/platform/link-app/wechat)。 ->**2023.04.26:** 支持企业微信应用号部署,兼容插件,并支持语音图片交互,私人助理理想选择,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatcom/README.md)。(contributed by [@lanvent](https://github.com/lanvent) in [#944](https://github.com/zhayujie/chatgpt-on-wechat/pull/944)) - ->**2023.04.05:** 支持微信公众号部署,兼容插件,并支持语音图片交互,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatmp/README.md)。(contributed by [@JS00000](https://github.com/JS00000) in [#686](https://github.com/zhayujie/chatgpt-on-wechat/pull/686)) - ->**2023.04.05:** 增加能让ChatGPT使用工具的`tool`插件,[使用文档](https://github.com/goldfishh/chatgpt-on-wechat/blob/master/plugins/tool/README.md)。工具相关issue可反馈至[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)。(contributed by [@goldfishh](https://github.com/goldfishh) in [#663](https://github.com/zhayujie/chatgpt-on-wechat/pull/663)) - ->**2023.03.25:** 支持插件化开发,目前已实现 多角色切换、文字冒险游戏、管理员指令、Stable Diffusion等插件,使用参考 [#578](https://github.com/zhayujie/chatgpt-on-wechat/issues/578)。(contributed by [@lanvent](https://github.com/lanvent) in [#565](https://github.com/zhayujie/chatgpt-on-wechat/pull/565)) - ->**2023.03.09:** 基于 `whisper API`(后续已接入更多的语音`API`服务) 实现对微信语音消息的解析和回复,添加配置项 `"speech_recognition":true` 即可启用,使用参考 [#415](https://github.com/zhayujie/chatgpt-on-wechat/issues/415)。(contributed by [wanggang1987](https://github.com/wanggang1987) in [#385](https://github.com/zhayujie/chatgpt-on-wechat/pull/385)) - ->**2023.02.09:** 扫码登录存在账号限制风险,请谨慎使用,参考[#58](https://github.com/AutumnWhj/ChatGPT-wechat-bot/issues/158) +更早更新日志查看: [归档日志](/docs/version/old-version.md) # 快速开始 diff --git a/docs/version/old-version.md b/docs/version/old-version.md new file mode 100644 index 0000000..ec719cd --- /dev/null +++ b/docs/version/old-version.md @@ -0,0 +1,13 @@ +## 归档更新日志 + +2023.04.26: 支持企业微信应用号部署,兼容插件,并支持语音图片交互,私人助理理想选择,使用文档。(contributed by @lanvent in #944) + +2023.04.05: 支持微信公众号部署,兼容插件,并支持语音图片交互,使用文档。(contributed by @JS00000 in #686) + +2023.04.05: 增加能让ChatGPT使用工具的tool插件,使用文档。工具相关issue可反馈至chatgpt-tool-hub。(contributed by @goldfishh in #663) + +2023.03.25: 支持插件化开发,目前已实现 多角色切换、文字冒险游戏、管理员指令、Stable Diffusion等插件,使用参考 #578。(contributed by @lanvent in #565) + +2023.03.09: 基于 whisper API(后续已接入更多的语音API服务) 实现对微信语音消息的解析和回复,添加配置项 "speech_recognition":true 即可启用,使用参考 #415。(contributed by wanggang1987 in #385) + +2023.02.09: 扫码登录存在账号限制风险,请谨慎使用,参考#58 \ No newline at end of file From 7f857d66f62fecb28312d2d8ff7a61b29715cefc Mon Sep 17 00:00:00 2001 From: zhayujie Date: Tue, 26 Mar 2024 20:12:25 +0800 Subject: [PATCH 19/20] docs: update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2104829..d7b6841 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # 简介 -> 本项目是基于大模型的智能对话机器人,支持微信、企业微信、公众号、飞书、钉钉接入,可选择GPT3.5/GPT4.0/Claude/文心一言/讯飞星火/通义千问/Gemini/LinkAI/ZhipuAI,能处理文本、语音和图片,通过插件访问操作系统和互联网等外部资源,支持基于自有知识库定制企业AI应用。 +> 本项目是基于大模型的智能对话机器人,支持企业微信、微信公众号、飞书、钉钉接入,可选择GPT3.5/GPT4.0/Claude/文心一言/讯飞星火/通义千问/Gemini/LinkAI/ZhipuAI,能处理文本、语音和图片,通过插件访问操作系统和互联网等外部资源,支持基于自有知识库定制企业AI应用。 最新版本支持的功能如下: -- [x] **多端部署:** 有多种部署方式可选择且功能完备,目前已支持个人微信、微信公众号和、企业微信、飞书、钉钉等部署方式 +- [x] **多端部署:** 有多种部署方式可选择且功能完备,目前已支持微信生态下公众号、企业微信应用、飞书、钉钉等部署方式 - [x] **基础对话:** 私聊及群聊的消息智能回复,支持多轮会话上下文记忆,支持 GPT-3.5, GPT-4, Claude-3, Gemini, 文心一言, 讯飞星火, 通义千问,ChatGLM-4 - [x] **语音能力:** 可识别语音消息,通过文字或语音回复,支持 azure, baidu, google, openai(whisper/tts) 等多种语音模型 - [x] **图像能力:** 支持图片生成、图片识别、图生图(如照片修复),可选择 Dall-E-3, stable diffusion, replicate, midjourney, CogView-3, vision模型 @@ -51,7 +51,7 @@ SaaS服务、私有化部署、稳定托管接入 等多种模式。 >**2023.08.08:** 接入百度文心一言模型,通过 [插件](https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/linkai) 支持 Midjourney 绘图 ->**2023.06.12:** 接入 [LinkAI](https://link-ai.tech/console) 平台,可在线创建领域知识库,并接入微信、公众号及企业微信中,打造专属客服机器人。使用参考 [接入文档](https://link-ai.tech/platform/link-app/wechat)。 +>**2023.06.12:** 接入 [LinkAI](https://link-ai.tech/console) 平台,可在线创建领域知识库,打造专属客服机器人。使用参考 [接入文档](https://link-ai.tech/platform/link-app/wechat)。 更早更新日志查看: [归档日志](/docs/version/old-version.md) @@ -187,7 +187,7 @@ pip3 install -r requirements-optional.txt python3 app.py # windows环境下该命令通常为 python app.py ``` -终端输出二维码后,使用微信进行扫码,当输出 "Start auto replying" 时表示自动回复程序已经成功运行了(注意:用于登录的微信需要在支付处已完成实名认证)。扫码登录后你的账号就成为机器人了,可以在微信手机端通过配置的关键词触发自动回复 (任意好友发送消息给你,或是自己发消息给好友),参考[#142](https://github.com/zhayujie/chatgpt-on-wechat/issues/142)。 +终端输出二维码后,使用微信进行扫码,当输出 "Start auto replying" 时表示自动回复程序已经成功运行了(注意:用于登录的微信需要在支付处已完成实名认证)。扫码登录后你的账号就成为机器人了,可以在手机端通过配置的关键词触发自动回复 (任意好友发送消息给你,或是自己发消息给好友),参考[#142](https://github.com/zhayujie/chatgpt-on-wechat/issues/142)。 ### 2.服务器部署 From c0eb433fa2174d3a31d550876830040a657b847a Mon Sep 17 00:00:00 2001 From: zhayujie Date: Wed, 27 Mar 2024 10:35:12 +0800 Subject: [PATCH 20/20] fix: remove unused import --- bot/linkai/link_ai_bot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 960b788..2a3e57d 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -7,7 +7,6 @@ import requests import config from bot.bot import Bot from bot.chatgpt.chat_gpt_session import ChatGPTSession -from bot.gemini.google_gemini_bot import GoogleGeminiBot from bot.session_manager import SessionManager from bridge.context import Context, ContextType from bridge.reply import Reply, ReplyType