-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1723 from zRzRzRzRzRzRzR/master
支持ZhipuAI GLM系列模型和画图代码
- Loading branch information
Showing
9 changed files
with
246 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, "画图出现问题,请休息一下再问我吧" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": 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), # 值在(0,1)之间(智谱AI 的 top_p 不能取 0 或者 1) | ||
} | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,5 @@ linkai | |
# dingtalk | ||
dingtalk_stream | ||
|
||
# zhipu | ||
zhipuai | ||
# zhipuai | ||
zhipuai>=2.0.1 |