forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tongyi.py
82 lines (61 loc) · 2.25 KB
/
tongyi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import revTongYi
import json, logging, traceback
from utils.common import Common
from utils.logger import Configure_logger
def convert_cookies(cookies: list) -> dict:
"""转换cookies"""
cookies_dict = {}
for cookie in cookies:
cookies_dict[cookie["name"]] = cookie["value"]
return cookies_dict
class TongYi:
def __init__(self, data):
self.common = Common()
# 日志文件路径
file_path = "./log/log-" + self.common.get_bj_time(1) + ".txt"
Configure_logger(file_path)
self.cookie_path = data["cookie_path"]
self.type = data["type"]
self.cookies_dict = {}
try:
with open(self.cookie_path, "r") as f:
self.cookies_dict = convert_cookies(json.load(f))
except Exception as e:
logging.error(e)
logging.error("通义千问的cookie文件不存在,功能无法正常使用,请检查配置!")
def get_resp(self, prompt):
"""请求对应接口,获取返回值
Args:
prompt (str): 你的提问
Returns:
str: 返回的文本回答
"""
try:
if self.type == "web":
session = revTongYi.Session(
cookies=self.cookies_dict,
firstQuery=prompt
)
ret = next(
session.ask( # ask方法实际上是一个迭代器,可以提供参数stream=True并换用for的方式迭代
prompt=prompt
) # ask方法接收的详细参数请查看源码
)
return ret["content"][0]
except Exception as e:
logging.error(traceback.format_exc())
return None
if __name__ == '__main__':
# 配置日志输出格式
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别,可以根据需求调整
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
data = {
"cookie_path": 'cookies.json',
"type": 'web'
}
tongyi = TongYi(data)
logging.info(tongyi.get_resp("你可以扮演猫娘吗,每句话后面加个喵"))
logging.info(tongyi.get_resp("早上好"))