mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
Merge branch 'master' into master
This commit is contained in:
@@ -14,6 +14,8 @@ from bridge.reply import Reply, ReplyType
|
|||||||
from common.log import logger
|
from common.log import logger
|
||||||
from config import conf
|
from config import conf
|
||||||
from bot.chatgpt.chat_gpt_session import ChatGPTSession
|
from bot.chatgpt.chat_gpt_session import ChatGPTSession
|
||||||
|
from bot.baidu.baidu_wenxin_session import BaiduWenxinSession
|
||||||
|
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
||||||
|
|
||||||
|
|
||||||
# OpenAI对话模型API (可用)
|
# OpenAI对话模型API (可用)
|
||||||
@@ -39,16 +41,41 @@ class GoogleGeminiBot(Bot):
|
|||||||
logger.info(f"[Gemini] messages={gemini_messages}")
|
logger.info(f"[Gemini] messages={gemini_messages}")
|
||||||
genai.configure(api_key=self.api_key)
|
genai.configure(api_key=self.api_key)
|
||||||
model = genai.GenerativeModel(self.model)
|
model = genai.GenerativeModel(self.model)
|
||||||
response = model.generate_content(gemini_messages)
|
|
||||||
reply_text = response.text
|
# 添加安全设置
|
||||||
self.sessions.session_reply(reply_text, session_id)
|
safety_settings = {
|
||||||
logger.info(f"[Gemini] reply={reply_text}")
|
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
||||||
return Reply(ReplyType.TEXT, reply_text)
|
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
||||||
|
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
||||||
|
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
||||||
|
}
|
||||||
|
|
||||||
|
# 生成回复,包含安全设置
|
||||||
|
response = model.generate_content(
|
||||||
|
gemini_messages,
|
||||||
|
safety_settings=safety_settings
|
||||||
|
)
|
||||||
|
if response.candidates and response.candidates[0].content:
|
||||||
|
reply_text = response.candidates[0].content.parts[0].text
|
||||||
|
logger.info(f"[Gemini] reply={reply_text}")
|
||||||
|
self.sessions.session_reply(reply_text, session_id)
|
||||||
|
return Reply(ReplyType.TEXT, reply_text)
|
||||||
|
else:
|
||||||
|
# 没有有效响应内容,可能内容被屏蔽,输出安全评分
|
||||||
|
logger.warning("[Gemini] No valid response generated. Checking safety ratings.")
|
||||||
|
if hasattr(response, 'candidates') and response.candidates:
|
||||||
|
for rating in response.candidates[0].safety_ratings:
|
||||||
|
logger.warning(f"Safety rating: {rating.category} - {rating.probability}")
|
||||||
|
error_message = "No valid response generated due to safety constraints."
|
||||||
|
self.sessions.session_reply(error_message, session_id)
|
||||||
|
return Reply(ReplyType.ERROR, error_message)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("[Gemini] fetch reply error, may contain unsafe content")
|
logger.error(f"[Gemini] Error generating response: {str(e)}", exc_info=True)
|
||||||
logger.error(e)
|
error_message = "Failed to invoke [Gemini] api!"
|
||||||
return Reply(ReplyType.ERROR, "invoke [Gemini] api failed!")
|
self.sessions.session_reply(error_message, session_id)
|
||||||
|
return Reply(ReplyType.ERROR, error_message)
|
||||||
|
|
||||||
def _convert_to_gemini_messages(self, messages: list):
|
def _convert_to_gemini_messages(self, messages: list):
|
||||||
res = []
|
res = []
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
|
|||||||
@@ -19,8 +19,11 @@ class MoonshotBot(Bot):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.sessions = SessionManager(MoonshotSession, model=conf().get("model") or "moonshot-v1-128k")
|
self.sessions = SessionManager(MoonshotSession, model=conf().get("model") or "moonshot-v1-128k")
|
||||||
|
model = conf().get("model") or "moonshot-v1-128k"
|
||||||
|
if model == "moonshot":
|
||||||
|
model = "moonshot-v1-32k"
|
||||||
self.args = {
|
self.args = {
|
||||||
"model": conf().get("model") or "moonshot-v1-128k", # 对话模型的名称
|
"model": model, # 对话模型的名称
|
||||||
"temperature": conf().get("temperature", 0.3), # 如果设置,值域须为 [0, 1] 我们推荐 0.3,以达到较合适的效果。
|
"temperature": conf().get("temperature", 0.3), # 如果设置,值域须为 [0, 1] 我们推荐 0.3,以达到较合适的效果。
|
||||||
"top_p": conf().get("top_p", 1.0), # 使用默认值
|
"top_p": conf().get("top_p", 1.0), # 使用默认值
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Bridge(object):
|
|||||||
self.btype["chat"] = const.QWEN_DASHSCOPE
|
self.btype["chat"] = const.QWEN_DASHSCOPE
|
||||||
if model_type and model_type.startswith("gemini"):
|
if model_type and model_type.startswith("gemini"):
|
||||||
self.btype["chat"] = const.GEMINI
|
self.btype["chat"] = const.GEMINI
|
||||||
if model_type in [const.ZHIPU_AI]:
|
if model_type and model_type.startswith("glm"):
|
||||||
self.btype["chat"] = const.ZHIPU_AI
|
self.btype["chat"] = const.ZHIPU_AI
|
||||||
if model_type and model_type.startswith("claude-3"):
|
if model_type and model_type.startswith("claude-3"):
|
||||||
self.btype["chat"] = const.CLAUDEAPI
|
self.btype["chat"] = const.CLAUDEAPI
|
||||||
@@ -46,7 +46,7 @@ class Bridge(object):
|
|||||||
if model_type in ["claude"]:
|
if model_type in ["claude"]:
|
||||||
self.btype["chat"] = const.CLAUDEAI
|
self.btype["chat"] = const.CLAUDEAI
|
||||||
|
|
||||||
if model_type in ["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]:
|
if model_type in [const.MOONSHOT, "moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]:
|
||||||
self.btype["chat"] = const.MOONSHOT
|
self.btype["chat"] = const.MOONSHOT
|
||||||
|
|
||||||
if model_type in ["abab6.5-chat"]:
|
if model_type in ["abab6.5-chat"]:
|
||||||
|
|||||||
@@ -60,11 +60,22 @@ GEMINI_PRO = "gemini-1.0-pro"
|
|||||||
GEMINI_15_flash = "gemini-1.5-flash"
|
GEMINI_15_flash = "gemini-1.5-flash"
|
||||||
GEMINI_15_PRO = "gemini-1.5-pro"
|
GEMINI_15_PRO = "gemini-1.5-pro"
|
||||||
|
|
||||||
|
GLM_4 = "glm-4"
|
||||||
|
GLM_4_PLUS = "glm-4-plus"
|
||||||
|
GLM_4_flash = "glm-4-flash"
|
||||||
|
GLM_4_LONG = "glm-4-long"
|
||||||
|
GLM_4_ALLTOOLS = "glm-4-alltools"
|
||||||
|
GLM_4_0520 = "glm-4-0520"
|
||||||
|
GLM_4_AIR = "glm-4-air"
|
||||||
|
GLM_4_AIRX = "glm-4-airx"
|
||||||
|
|
||||||
MODEL_LIST = [
|
MODEL_LIST = [
|
||||||
GPT35, GPT35_0125, GPT35_1106, "gpt-3.5-turbo-16k",
|
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,
|
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,
|
||||||
WEN_XIN, WEN_XIN_4,
|
WEN_XIN, WEN_XIN_4,
|
||||||
XUNFEI, ZHIPU_AI, MOONSHOT, MiniMax,
|
XUNFEI,
|
||||||
|
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,
|
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", "claude-3-haiku", "claude-3-sonnet", "claude-3-opus", "claude-3-opus-20240229", "claude-3.5-sonnet",
|
||||||
"moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k",
|
"moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k",
|
||||||
|
|||||||
Reference in New Issue
Block a user