Files
vcbot-napcat/model/AiCat.py
2025-07-05 17:26:12 +08:00

56 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
import logging
class AiCat:
def __init__(self,message,qid):
self.message = message
self.qid = qid
self.query = f"<qid>{self.qid}</qid>{self.message}"
print(self.query) # test
def main(self):
# API URL
url = "https://ai.mcunc.cn/v1/chat-messages" # 替换为实际的 API 地址
# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer app-pqib8uic8oBP95XmXuBi5ANq" # 替换为你的 API 密钥
}
# 请求体
payload = {
"query": self.query, # 用户输入/提问内容
"inputs": {}, # App 定义的变量值(默认为空)
"response_mode": "blocking", # 流式模式或阻塞模式
"user": "QBotAPI", # 用户标识,需保证唯一性
"conversation_id": "09cc6545-b0e0-4611-aad1-cf9bf51600e1", # (选填)会话 ID继续对话时需要传入
"files": [], # 文件列表(选填),适用于文件结合文本理解
"auto_generate_name": True # (选填)自动生成标题,默认为 True
}
# 发送 POST 请求
try:
response = requests.post(url, headers=headers, data=json.dumps(payload))
# 检查响应状态码
if response.status_code == 200:
logging.info("请求成功!返回结果:")
response_data = response.json()
logging.info(json.dumps(response_data, indent=4, ensure_ascii=False)) # 格式化输出 JSON
# 提取 answer 值
answer = response_data.get("answer", "answer 字段不存在")
print(response_data.get("conversation_id")) # test
return answer
else:
logging.info(f"请求失败!状态码: {response.status_code}")
logging.info(f"错误信息: {response.text}") # 打印错误信息
return "诶呀! 服务器好像出现了一点点错误呐~稍等一会再重试哦喵~"
except Exception as e:
logging.info(f"请求过程中出现异常: {e}")
return "诶呀! 服务器好像出现了一点点错误呐~稍等一会再重试哦喵~"