新版本重构了ai模型提供商逻辑,新增MCUNC,阿里云百炼ai提供商,支持语言可配置,优化日志输出格式
This commit is contained in:
57
model/ai_models/aliyun.py
Normal file
57
model/ai_models/aliyun.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import toml
|
||||
from model.logger import setup_logger
|
||||
from http import HTTPStatus
|
||||
from dashscope import Application
|
||||
from model.sql_tools import init_db
|
||||
from model.sql_tools import get_uuid
|
||||
from model.sql_tools import add_uuid
|
||||
|
||||
|
||||
logger = setup_logger()
|
||||
def main(message, group_openid):
|
||||
with open('./config.toml', 'r', encoding='utf-8') as f:
|
||||
config = toml.load(f)
|
||||
|
||||
error_message = config.get("error_message")
|
||||
|
||||
# aliyun
|
||||
aliyun_app_id = config.get("aliyun_app_id")
|
||||
aliyun_api_key = config.get("aliyun_api_key")
|
||||
init_db()
|
||||
|
||||
uuid = get_uuid(group_openid)
|
||||
|
||||
if uuid == "":
|
||||
logger.info("未找到 UUID")
|
||||
else:
|
||||
logger.info(f"找到 UUID: {uuid}")
|
||||
|
||||
# 发送请求
|
||||
response = Application.call(
|
||||
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
||||
api_key=aliyun_api_key,
|
||||
app_id=aliyun_app_id,
|
||||
prompt= message,
|
||||
session_id = group_openid
|
||||
)
|
||||
|
||||
|
||||
# 检查响应状态码
|
||||
if response.status_code != HTTPStatus.OK:
|
||||
logger.info(f"请求失败!状态码: {response.status_code}")
|
||||
logger.info(f"错误信息: {response.message}") # 打印错误信息
|
||||
return error_message
|
||||
else:
|
||||
logger.info("请求成功!返回结果:")
|
||||
print(f"message: {response.output.text}, session_id: {response.output.session_id}") # test
|
||||
# 存储uuid
|
||||
if uuid == "":
|
||||
uuid = response.output.session_id
|
||||
add_uuid(group_openid, uuid)
|
||||
|
||||
# 提取 answer 值
|
||||
answer = response.output.text
|
||||
return answer
|
||||
|
||||
|
||||
|
||||
77
model/ai_models/dify.py
Normal file
77
model/ai_models/dify.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import requests
|
||||
import json
|
||||
from model.logger import setup_logger
|
||||
import toml
|
||||
from model.sql_tools import init_db
|
||||
from model.sql_tools import get_uuid
|
||||
from model.sql_tools import add_uuid
|
||||
|
||||
|
||||
logger = setup_logger()
|
||||
def main(message, group_openid):
|
||||
with open('./config.toml', 'r', encoding='utf-8') as f:
|
||||
config = toml.load(f)
|
||||
|
||||
error_message = config.get("error_message")
|
||||
|
||||
# dify
|
||||
dify_ip = config.get("dify_ip")
|
||||
dify_token = config.get("dify_token")
|
||||
init_db()
|
||||
|
||||
uuid = get_uuid(group_openid)
|
||||
|
||||
if uuid == "":
|
||||
logging.info("未找到 UUID")
|
||||
else:
|
||||
logging.info(f"找到 UUID: {uuid}")
|
||||
|
||||
url = f"{dify_ip}/v1/chat-messages" # 替换为实际的 API 地址
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": dify_token # 替换为你的 API 密钥
|
||||
}
|
||||
|
||||
# 请求体
|
||||
payload = {
|
||||
"query": message, # 用户输入/提问内容
|
||||
"inputs": {}, # App 定义的变量值(默认为空)
|
||||
"response_mode": "blocking", # 流式模式或阻塞模式
|
||||
"user": "chatbot_napcat", # 用户标识,需保证唯一性
|
||||
"conversation_id": uuid, # (选填)会话 ID,继续对话时需要传入
|
||||
"files": [], # 文件列表(选填),适用于文件结合文本理解
|
||||
"auto_generate_name": True # (选填)自动生成标题,默认为 True
|
||||
}
|
||||
|
||||
logging.info("请求平台:dify")
|
||||
|
||||
|
||||
# 发送 POST 请求
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||||
|
||||
# 检查响应状态码
|
||||
if response.status_code == 200:
|
||||
logging.info("请求成功!返回结果:")
|
||||
response_data = response.json()
|
||||
print(response_data) # test
|
||||
# 存储uuid
|
||||
if uuid == "":
|
||||
uuid = response_data.get("conversation_id")
|
||||
add_uuid(group_openid, uuid)
|
||||
|
||||
# 提取 answer 值
|
||||
answer = response_data.get("answer", error_message)
|
||||
return answer
|
||||
else:
|
||||
logging.info(f"请求失败!状态码: {response.status_code}")
|
||||
logging.info(f"错误信息: {response.text}") # 打印错误信息
|
||||
return error_message
|
||||
|
||||
except Exception as e:
|
||||
logging.info(f"请求过程中出现异常: {e}")
|
||||
return error_message
|
||||
|
||||
|
||||
75
model/ai_models/mcunc.py
Normal file
75
model/ai_models/mcunc.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import requests
|
||||
import json
|
||||
from model.logger import setup_logger
|
||||
import toml
|
||||
from model.sql_tools import init_db
|
||||
from model.sql_tools import get_uuid
|
||||
from model.sql_tools import add_uuid
|
||||
|
||||
|
||||
|
||||
logger = setup_logger()
|
||||
def main(message, group_openid):
|
||||
with open('./config.toml', 'r', encoding='utf-8') as f:
|
||||
config = toml.load(f)
|
||||
|
||||
error_message = config.get("error_message")
|
||||
|
||||
# mcunc
|
||||
mcunc_ip = config.get("mcunc_ip")
|
||||
mcunc_appID = config.get("mcunc_appID")
|
||||
mcunc_appKEY = config.get("mcunc_appKEY")
|
||||
mcunc_model = config.get("mcunc_model")
|
||||
|
||||
init_db()
|
||||
|
||||
uuid = get_uuid(group_openid)
|
||||
|
||||
if uuid == "":
|
||||
logger.info("未找到 UUID")
|
||||
else:
|
||||
logger.info(f"找到 UUID: {uuid}")
|
||||
|
||||
url = f"{mcunc_ip}/models/{mcunc_model}/" # 替换为实际的 API 地址
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"appID": mcunc_appID, # 替换为你的 API 密钥
|
||||
"appKEY": mcunc_appKEY
|
||||
}
|
||||
|
||||
# 请求体
|
||||
payload = {
|
||||
"query": message, # 用户输入/提问内容
|
||||
"conversation_id": uuid, # (选填)会话 ID,继续对话时需要传入
|
||||
}
|
||||
|
||||
logger.info("请求平台:mcunc")
|
||||
|
||||
|
||||
# 发送 POST 请求
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||||
|
||||
# 检查响应状态码
|
||||
if response.status_code == 200:
|
||||
logger.info("请求成功!返回结果:")
|
||||
response_data = response.json()
|
||||
print(response_data) # test
|
||||
# 存储uuid
|
||||
if uuid == "":
|
||||
uuid = response_data.get("conversation_id")
|
||||
add_uuid(group_openid, uuid)
|
||||
|
||||
# 提取 answer 值
|
||||
answer = response_data.get("answer", error_message)
|
||||
return answer
|
||||
else:
|
||||
logger.info(f"请求失败!状态码: {response.status_code}")
|
||||
logger.info(f"错误信息: {response.text}") # 打印错误信息
|
||||
return error_message
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"请求过程中出现异常: {e}")
|
||||
return error_message
|
||||
76
model/ai_models/xyit.py
Normal file
76
model/ai_models/xyit.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import requests
|
||||
import json
|
||||
from model.logger import setup_logger
|
||||
import toml
|
||||
from model.sql_tools import init_db
|
||||
from model.sql_tools import get_uuid
|
||||
from model.sql_tools import add_uuid
|
||||
|
||||
|
||||
|
||||
logger = setup_logger()
|
||||
def main(message, group_openid):
|
||||
with open('./config.toml', 'r', encoding='utf-8') as f:
|
||||
config = toml.load(f)
|
||||
|
||||
error_message = config.get("error_message")
|
||||
|
||||
# xyit
|
||||
xyit_ip = config.get("xyit_ip")
|
||||
xyit_appID = config.get("xyit_appID")
|
||||
xyit_appKEY = config.get("xyit_appKEY")
|
||||
xyit_model = config.get("xyit_model")
|
||||
|
||||
init_db()
|
||||
|
||||
uuid = get_uuid(group_openid)
|
||||
|
||||
if uuid == "":
|
||||
logger.info("未找到 UUID")
|
||||
else:
|
||||
logger.info(f"找到 UUID: {uuid}")
|
||||
|
||||
url = f"{xyit_ip}/models/{xyit_model}/" # 替换为实际的 API 地址
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"appID": xyit_appID, # 替换为你的 API 密钥
|
||||
"appKEY": xyit_appKEY
|
||||
}
|
||||
|
||||
# 请求体
|
||||
payload = {
|
||||
"query": message, # 用户输入/提问内容
|
||||
"conversation_id": uuid, # (选填)会话 ID,继续对话时需要传入
|
||||
}
|
||||
|
||||
logger.info("请求平台:xyit")
|
||||
|
||||
|
||||
# 发送 POST 请求
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||||
|
||||
# 检查响应状态码
|
||||
if response.status_code == 200:
|
||||
logger.info("请求成功!返回结果:")
|
||||
response_data = response.json()
|
||||
print(response_data) # test
|
||||
# 存储uuid
|
||||
if uuid == "":
|
||||
uuid = response_data.get("conversation_id")
|
||||
add_uuid(group_openid, uuid)
|
||||
|
||||
# 提取 answer 值
|
||||
answer = response_data.get("answer", error_message)
|
||||
return answer
|
||||
else:
|
||||
logger.info(f"请求失败!状态码: {response.status_code}")
|
||||
logger.info(f"错误信息: {response.text}") # 打印错误信息
|
||||
return error_message
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"请求过程中出现异常: {e}")
|
||||
return error_message
|
||||
|
||||
Reference in New Issue
Block a user