From 84da0a8a355199933c98d37f86cffc4ff23e9d3c Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 24 Oct 2024 20:57:03 +0800 Subject: [PATCH 1/4] feat:update claude-35-sonnet model --- bot/claudeapi/claude_api_bot.py | 9 +++++---- common/const.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index e5452e5c0..7f71d65c1 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -14,6 +14,7 @@ from bridge.context import ContextType from bridge.reply import Reply, ReplyType from common.log import logger +from common import const from config import conf user_session = dict() @@ -125,11 +126,11 @@ def reply_text(self, session: ChatGPTSession, retry_count=0): def _model_mapping(self, model) -> str: if model == "claude-3-opus": - return "claude-3-opus-20240229" + return const.CLAUDE_3_OPUS elif model == "claude-3-sonnet": - return "claude-3-sonnet-20240229" + return const.CLAUDE_3_SONNET elif model == "claude-3-haiku": - return "claude-3-haiku-20240307" + return const.CLAUDE_3_HAIKU elif model == "claude-3.5-sonnet": - return "claude-3-5-sonnet-20240620" + return const.CLAUDE_35_SONNET return model diff --git a/common/const.py b/common/const.py index f26838f7e..9eb8795da 100644 --- a/common/const.py +++ b/common/const.py @@ -69,6 +69,17 @@ GLM_4_AIR = "glm-4-air" GLM_4_AIRX = "glm-4-airx" + +CLAUDE_3_OPUS = "claude-3-opus-latest" +CLAUDE_3_OPUS_0229 = "claude-3-opus-20240229" + +CLAUDE_35_SONNET = "claude-3-5-sonnet-latest" # 带 latest 标签的模型名称,会不断更新指向最新发布的模型 +CLAUDE_35_SONNET_1022 = "claude-3-5-sonnet-20241022" # 带具体日期的模型名称,会固定为该日期发布的模型 +CLAUDE_35_SONNET_0620 = "claude-3-5-sonnet-20240620" +CLAUDE_3_SONNET = "claude-3-sonnet-20240229" + +CLAUDE_3_HAIKU = "claude-3-haiku-20240307" + MODEL_LIST = [ GPT35, GPT35_0125, GPT35_1106, "gpt-3.5-turbo-16k", O1, O1_MINI, GPT_4o, GPT_4O_0806, GPT_4o_MINI, GPT4_TURBO, GPT4_TURBO_PREVIEW, GPT4_TURBO_01_25, GPT4_TURBO_11_06, GPT4, GPT4_32k, GPT4_06_13, GPT4_32k_06_13, @@ -77,7 +88,7 @@ ZHIPU_AI, GLM_4, GLM_4_PLUS, GLM_4_flash, GLM_4_LONG, GLM_4_ALLTOOLS, GLM_4_0520, GLM_4_AIR, GLM_4_AIRX, MOONSHOT, MiniMax, GEMINI, GEMINI_PRO, GEMINI_15_flash, GEMINI_15_PRO, - "claude", "claude-3-haiku", "claude-3-sonnet", "claude-3-opus", "claude-3-opus-20240229", "claude-3.5-sonnet", + CLAUDE_3_OPUS, CLAUDE_3_OPUS_0229, CLAUDE_35_SONNET, CLAUDE_35_SONNET_1022, CLAUDE_35_SONNET_0620, CLAUDE_3_SONNET, CLAUDE_3_HAIKU, "claude", "claude-3-haiku", "claude-3-sonnet", "claude-3-opus", "claude-3.5-sonnet", "moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k", QWEN, QWEN_TURBO, QWEN_PLUS, QWEN_MAX, LINKAI_35, LINKAI_4_TURBO, LINKAI_4o From d25cab0627da6e64cb4ed8d8b3d77eb8daeaf050 Mon Sep 17 00:00:00 2001 From: 6vision Date: Sun, 27 Oct 2024 21:37:58 +0800 Subject: [PATCH 2/4] Claude model supports system prompts. --- bot/claudeapi/claude_api_bot.py | 13 ++++++------- common/utils.py | 1 + 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index 7f71d65c1..f81606096 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -8,8 +8,7 @@ from bot.bot import Bot from bot.openai.open_ai_image import OpenAIImage -from bot.chatgpt.chat_gpt_session import ChatGPTSession -from bot.gemini.google_gemini_bot import GoogleGeminiBot +from bot.baidu.baidu_wenxin_session import BaiduWenxinSession from bot.session_manager import SessionManager from bridge.context import ContextType from bridge.reply import Reply, ReplyType @@ -34,7 +33,7 @@ def __init__(self): if proxy: openai.proxy = proxy - self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "text-davinci-003") + self.sessions = SessionManager(BaiduWenxinSession, model=conf().get("model") or "text-davinci-003") def reply(self, query, context=None): # acquire reply content @@ -77,14 +76,14 @@ def reply(self, query, context=None): reply = Reply(ReplyType.ERROR, retstring) return reply - def reply_text(self, session: ChatGPTSession, retry_count=0): + def reply_text(self, session: BaiduWenxinSession, retry_count=0): try: actual_model = self._model_mapping(conf().get("model")) response = self.claudeClient.messages.create( model=actual_model, - max_tokens=1024, - # system=conf().get("system"), - messages=GoogleGeminiBot.filter_messages(session.messages) + max_tokens=4096, + system=conf().get("character_desc", ""), + messages=session.messages ) # response = openai.Completion.create(prompt=str(session), **self.args) res_content = response.content[0].text.strip().replace("<|endoftext|>", "") diff --git a/common/utils.py b/common/utils.py index f509cb9d9..4b178182e 100644 --- a/common/utils.py +++ b/common/utils.py @@ -70,6 +70,7 @@ def convert_webp_to_png(webp_image): logger.error(f"Failed to convert WEBP to PNG: {e}") raise + def remove_markdown_symbol(text: str): # 移除markdown格式,目前先移除** if not text: From 8b753a5a1f071a3972247ddcd9759b24381ecac9 Mon Sep 17 00:00:00 2001 From: 6vision Date: Sun, 27 Oct 2024 21:44:06 +0800 Subject: [PATCH 3/4] Signed-off-by: 6vision --- bot/claudeapi/claude_api_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index f81606096..cd20800f7 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -97,7 +97,7 @@ def reply_text(self, session: BaiduWenxinSession, retry_count=0): } except Exception as e: need_retry = retry_count < 2 - result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"} + result = {"total_tokens": 0, "completion_tokens": 0, "content": "我现在有点累了,等会再来吧"} if isinstance(e, openai.error.RateLimitError): logger.warn("[CLAUDE_API] RateLimitError: {}".format(e)) result["content"] = "提问太快啦,请休息一下再问我吧" From ccb8c7227f45610e6fae8cda1138a39df14113f2 Mon Sep 17 00:00:00 2001 From: 6vision Date: Mon, 28 Oct 2024 00:32:05 +0800 Subject: [PATCH 4/4] Support setting base URL and proxy for Claude model. Also support reset command. --- bot/claudeapi/claude_api_bot.py | 13 +++++-------- plugins/godcmd/godcmd.py | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bot/claudeapi/claude_api_bot.py b/bot/claudeapi/claude_api_bot.py index cd20800f7..e3e7a2b58 100644 --- a/bot/claudeapi/claude_api_bot.py +++ b/bot/claudeapi/claude_api_bot.py @@ -23,16 +23,13 @@ class ClaudeAPIBot(Bot, OpenAIImage): def __init__(self): super().__init__() + proxy = conf().get("proxy", None) + base_url = conf().get("open_ai_api_base", None) # 复用"open_ai_api_base"参数作为base_url self.claudeClient = anthropic.Anthropic( - api_key=conf().get("claude_api_key") + api_key=conf().get("claude_api_key"), + proxies=proxy if proxy else None, + base_url=base_url if base_url else None ) - 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(BaiduWenxinSession, model=conf().get("model") or "text-davinci-003") def reply(self, query, context=None): diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index d6383bfa8..3e9d3eabd 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -313,7 +313,7 @@ def on_handle_context(self, e_context: EventContext): 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, const.ZHIPU_AI]: + if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI, const.BAIDU, const.XUNFEI, const.QWEN, const.GEMINI, const.ZHIPU_AI, const.CLAUDEAPI]: bot.sessions.clear_session(session_id) if Bridge().chat_bots.get(bottype): Bridge().chat_bots.get(bottype).sessions.clear_session(session_id)