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] =?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