-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat-client
- Loading branch information
Showing
21 changed files
with
456 additions
and
67 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
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,62 @@ | ||
from bot.session_manager import Session | ||
from common.log import logger | ||
|
||
""" | ||
e.g. | ||
[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "Who won the world series in 2020?"}, | ||
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, | ||
{"role": "user", "content": "Where was it played?"} | ||
] | ||
""" | ||
|
||
class AliQwenSession(Session): | ||
def __init__(self, session_id, system_prompt=None, model="qianwen"): | ||
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): | ||
"""Returns the number of tokens used by a list of messages.""" | ||
# 官方token计算规则:"对于中文文本来说,1个token通常对应一个汉字;对于英文文本来说,1个token通常对应3至4个字母或1个单词" | ||
# 详情请产看文档:https://help.aliyun.com/document_detail/2586397.html | ||
# 目前根据字符串长度粗略估计token数,不影响正常使用 | ||
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
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,75 @@ | ||
""" | ||
Google gemini bot | ||
@author zhayujie | ||
@Date 2023/12/15 | ||
""" | ||
# encoding:utf-8 | ||
|
||
from bot.bot import Bot | ||
import google.generativeai as genai | ||
from bot.session_manager import SessionManager | ||
from bridge.context import ContextType, Context | ||
from bridge.reply import Reply, ReplyType | ||
from common.log import logger | ||
from config import conf | ||
from bot.baidu.baidu_wenxin_session import BaiduWenxinSession | ||
|
||
|
||
# OpenAI对话模型API (可用) | ||
class GoogleGeminiBot(Bot): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.api_key = conf().get("gemini_api_key") | ||
# 复用文心的token计算方式 | ||
self.sessions = SessionManager(BaiduWenxinSession, model=conf().get("model") or "gpt-3.5-turbo") | ||
|
||
def reply(self, query, context: Context = None) -> Reply: | ||
try: | ||
if context.type != ContextType.TEXT: | ||
logger.warn(f"[Gemini] Unsupported message type, type={context.type}") | ||
return Reply(ReplyType.TEXT, None) | ||
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)) | ||
genai.configure(api_key=self.api_key) | ||
model = genai.GenerativeModel('gemini-pro') | ||
response = model.generate_content(gemini_messages) | ||
reply_text = response.text | ||
self.sessions.session_reply(reply_text, session_id) | ||
logger.info(f"[Gemini] reply={reply_text}") | ||
return Reply(ReplyType.TEXT, reply_text) | ||
except Exception as e: | ||
logger.error("[Gemini] fetch reply error, may contain unsafe content") | ||
logger.error(e) | ||
|
||
def _convert_to_gemini_messages(self, messages: list): | ||
res = [] | ||
for msg in messages: | ||
if msg.get("role") == "user": | ||
role = "user" | ||
elif msg.get("role") == "assistant": | ||
role = "model" | ||
else: | ||
continue | ||
res.append({ | ||
"role": role, | ||
"parts": [{"text": msg.get("content")}] | ||
}) | ||
return res | ||
|
||
def _filter_messages(self, messages: list): | ||
res = [] | ||
turn = "user" | ||
for i in range(len(messages) - 1, -1, -1): | ||
message = messages[i] | ||
if message.get("role") != turn: | ||
continue | ||
res.insert(0, message) | ||
if turn == "user": | ||
turn = "assistant" | ||
elif turn == "assistant": | ||
turn = "user" | ||
return res |
Oops, something went wrong.