mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-07-31 08:59:51 +00:00
✨ 支持保存历史对话, 规范代码
This commit is contained in:
@ -14,8 +14,11 @@ from azure.ai.inference.models import SystemMessage
|
||||
from .config import config
|
||||
|
||||
nickname_json = None
|
||||
praises_json = None
|
||||
|
||||
|
||||
async def get_image_b64(url):
|
||||
# noinspection LongLine
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
}
|
||||
@ -28,7 +31,7 @@ async def get_image_b64(url):
|
||||
content_type = response.headers.get("Content-Type")
|
||||
if not content_type:
|
||||
content_type = mimetypes.guess_type(url)[0]
|
||||
image_format = content_type.split("/")[1] if content_type else "jpeg"
|
||||
# image_format = content_type.split("/")[1] if content_type else "jpeg"
|
||||
base64_image = base64.b64encode(image_data).decode("utf-8")
|
||||
data_url = f"data:{content_type};base64,{base64_image}"
|
||||
return data_url
|
||||
@ -36,7 +39,13 @@ async def get_image_b64(url):
|
||||
return None
|
||||
|
||||
|
||||
async def make_chat(client: ChatCompletionsClient, msg, model_name: str):
|
||||
async def make_chat(client: ChatCompletionsClient, msg: list, model_name: str):
|
||||
"""调用ai获取回复
|
||||
|
||||
参数:
|
||||
client: 用于与AI模型进行通信
|
||||
msg: 消息内容
|
||||
model_name: 指定AI模型名"""
|
||||
return await client.complete(
|
||||
messages=msg,
|
||||
model=model_name,
|
||||
@ -47,9 +56,29 @@ async def make_chat(client: ChatCompletionsClient, msg, model_name: str):
|
||||
|
||||
|
||||
def get_praises():
|
||||
praises_file = store.get_plugin_data_file(
|
||||
"praises.json"
|
||||
) # 夸赞名单文件使用localstore存储
|
||||
global praises_json
|
||||
if praises_json is None:
|
||||
praises_file = store.get_plugin_data_file("praises.json") # 夸赞名单文件使用localstore存储
|
||||
if not os.path.exists(praises_file):
|
||||
init_data = {
|
||||
"like": [
|
||||
{
|
||||
"name": "Asankilp",
|
||||
"advantages": "赋予了Marsho猫娘人格,使用vim与vscode为Marsho写了许多代码,使Marsho更加可爱",
|
||||
}
|
||||
]
|
||||
}
|
||||
with open(praises_file, "w", encoding="utf-8") as f:
|
||||
json.dump(init_data, f, ensure_ascii=False, indent=4)
|
||||
with open(praises_file, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
praises_json = data
|
||||
return praises_json
|
||||
|
||||
|
||||
async def refresh_praises_json():
|
||||
global praises_json
|
||||
praises_file = store.get_plugin_data_file("praises.json")
|
||||
if not os.path.exists(praises_file):
|
||||
init_data = {
|
||||
"like": [
|
||||
@ -63,7 +92,7 @@ def get_praises():
|
||||
json.dump(init_data, f, ensure_ascii=False, indent=4)
|
||||
with open(praises_file, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
return data
|
||||
praises_json = data
|
||||
|
||||
|
||||
def build_praises():
|
||||
@ -74,16 +103,16 @@ def build_praises():
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
async def save_context_to_json(name: str, context: Any):
|
||||
context_dir = store.get_plugin_data_dir() / "contexts"
|
||||
async def save_context_to_json(name: str, context: Any, path: str):
|
||||
context_dir = store.get_plugin_data_dir() / path
|
||||
os.makedirs(context_dir, exist_ok=True)
|
||||
file_path = os.path.join(context_dir, f"{name}.json")
|
||||
with open(file_path, "w", encoding="utf-8") as json_file:
|
||||
json.dump(context, json_file, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
async def load_context_from_json(name: str):
|
||||
context_dir = store.get_plugin_data_dir() / "contexts"
|
||||
async def load_context_from_json(name: str, path:str):
|
||||
context_dir = store.get_plugin_data_dir() / path
|
||||
os.makedirs(context_dir, exist_ok=True)
|
||||
file_path = os.path.join(context_dir, f"{name}.json")
|
||||
try:
|
||||
@ -109,22 +138,25 @@ async def set_nickname(user_id: str, name: str):
|
||||
nickname_json = data
|
||||
|
||||
|
||||
# noinspection PyBroadException
|
||||
async def get_nicknames():
|
||||
'''获取nickname_json, 优先来源于全局变量'''
|
||||
"""获取nickname_json, 优先来源于全局变量"""
|
||||
global nickname_json
|
||||
if nickname_json is None:
|
||||
filename = store.get_plugin_data_file("nickname.json")
|
||||
try:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
nickname_json = json.load(f)
|
||||
nickname_json = json.load(f)
|
||||
except Exception:
|
||||
nickname_json = {}
|
||||
return nickname_json
|
||||
|
||||
|
||||
async def refresh_nickname_json():
|
||||
'''强制刷新nickname_json, 刷新全局变量'''
|
||||
"""强制刷新nickname_json, 刷新全局变量"""
|
||||
global nickname_json
|
||||
filename = store.get_plugin_data_file("nickname.json")
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
nickname_json = json.load(f)
|
||||
@ -151,6 +183,7 @@ def get_prompt():
|
||||
|
||||
|
||||
def suggest_solution(errinfo: str) -> str:
|
||||
# noinspection LongLine
|
||||
suggestions = {
|
||||
"content_filter": "消息已被内容过滤器过滤。请调整聊天内容后重试。",
|
||||
"RateLimitReached": "模型达到调用速率限制。请稍等一段时间或联系Bot管理员。",
|
||||
|
Reference in New Issue
Block a user