Files
iOSAI/Utils/Requester.py

82 lines
2.7 KiB
Python
Raw Normal View History

2025-08-12 18:53:06 +08:00
import requests
2025-08-27 21:58:55 +08:00
from Entity.Variables import prologueList
2025-09-11 19:33:21 +08:00
from Utils.JsonUtils import JsonUtils
2025-09-09 20:45:02 +08:00
from Utils.LogManager import LogManager
2025-08-12 18:53:06 +08:00
2025-08-18 19:22:20 +08:00
BaseUrl = "https://crawlclient.api.yolozs.com/api/common/"
2025-08-12 18:53:06 +08:00
2025-08-11 22:06:48 +08:00
class Requester():
2025-08-12 18:53:06 +08:00
comment = "comment"
prologue = "prologue"
2025-08-14 14:30:36 +08:00
2025-08-12 18:53:06 +08:00
@classmethod
2025-08-18 22:20:23 +08:00
def requestPrologue(cls, token):
2025-09-10 16:54:05 +08:00
try:
headers = {
"vvtoken": token,
}
url = BaseUrl + cls.prologue
result = requests.get(headers=headers, url=url)
json = result.json()
data = json.get("data")
for i in data:
prologueList.append(i)
except Exception as e:
LogManager.method_error(f"获取requestPrologue失败,报错的原因:{e}", "获取requestPrologue异常")
2025-08-12 18:53:06 +08:00
# 翻译
@classmethod
2025-09-08 21:42:09 +08:00
def translation(cls, msg, country="英国"):
2025-09-09 20:45:02 +08:00
try:
2025-09-12 21:36:47 +08:00
param = {
2025-09-09 20:45:02 +08:00
"msg": msg,
"country": country,
}
2025-09-10 16:54:05 +08:00
url = "https://ai.yolozs.com/translation"
2025-09-12 21:36:47 +08:00
result = requests.post(url=url, json=param)
2025-09-16 14:03:43 +08:00
LogManager.info(f"翻译,状态码:{result.status_code},服务器返回的内容:{result.text}")
2025-09-12 21:36:47 +08:00
if result.status_code != 200:
LogManager.error(f"翻译失败,状态码:{result.status_code},服务器返回的内容:{result.text}")
return None
2025-09-09 20:45:02 +08:00
json = result.json()
data = json.get("data")
return data
except Exception as e:
2025-09-10 16:54:05 +08:00
LogManager.method_error(f"翻译失败,报错的原因:{e}", "翻译失败异常")
2025-08-11 22:06:48 +08:00
2025-08-12 18:53:06 +08:00
# ai聊天
2025-08-11 22:06:48 +08:00
@classmethod
2025-08-12 18:53:06 +08:00
def chatToAi(cls, param):
2025-09-11 19:33:21 +08:00
aiConfig = JsonUtils.read_json("aiConfig")
agentName = aiConfig.get("agentName")
guildName = aiConfig.get("guildName")
contactTool = aiConfig.get("contactTool", "")
contact = aiConfig.get("contact", "")
2025-09-11 20:32:08 +08:00
2025-09-11 22:39:57 +08:00
inputs = {
2025-09-16 14:03:43 +08:00
"name": agentName,
"Trade_union": guildName,
"contcat_method": contactTool,
"contcat_info": contact
2025-09-11 22:39:57 +08:00
}
param["inputs"] = inputs
2025-09-11 20:32:08 +08:00
2025-09-12 21:36:47 +08:00
print(param)
2025-09-10 16:54:05 +08:00
try:
url = "https://ai.yolozs.com/chat"
2025-09-12 21:36:47 +08:00
result = requests.post(url=url, json=param)
2025-09-10 16:54:05 +08:00
json = result.json()
2025-09-12 21:36:47 +08:00
data = json.get("answer", {})
session_id = json.get("conversation_id", {})
2025-09-16 14:03:43 +08:00
LogManager.method_info(f"ai聊天的参数{param},ai聊天返回的内容{result.json()}", "ai聊天")
return data, session_id
2025-09-10 16:54:05 +08:00
except Exception as e:
LogManager.method_error(f"ai聊天失败,ai聊天出现异常,报错的原因:{e}", "ai聊天接口异常")