-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
108 lines (93 loc) · 3.52 KB
/
chatbot.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import requests
import os
import json
import const
import copy
# 参考revertGPT构造Chatbot
class Chatbot:
"""
Official ChatGPT API
"""
def __init__(
self,
api_key: str,
) -> None:
self.api_key: str = api_key
self.session = requests.Session()
# 最简单的查询
def query(
self,
story_id:str,
conversations:list,
is_davinci:bool,
output_query_logs:list,
):
try:
# 使用davinci模型
if is_davinci:
# 转换语句
query_convs = ""
for conv in conversations:
query_convs += conv['content'] + '\n'
# 带超时的访问
rsp = self.session.post(
os.environ.get("API_DAVINCI_URL"),
headers={"Content-Type": "application/json"},
verify = False,
json={
"auth": self.api_key,
"conversation": query_convs,
},
timeout=const.default_timeout,
)
# 解析返回值
for line in rsp.iter_lines():
processed_line = json.loads(line)
rsp_content = processed_line['data']['choices'][0]['text']
# insert query log
output_query_logs.append({
"Request to GPT": copy.deepcopy(query_convs),
"Response from GPT": rsp_content,
})
return rsp_content, True
else:
# 带超时的访问
rsp = self.session.post(
os.environ.get("API_TURBO_URL"),
headers={"Content-Type": "application/json"},
verify = False,
json={
"auth": self.api_key,
"conversation": conversations,
},
timeout=const.default_timeout,
)
# 解析返回值
for line in rsp.iter_lines():
processed_line = json.loads(line)
rsp = processed_line['data']['choices'][0]['message']
rsp_content = rsp['content']
# insert query log
output_query_logs.append({
"Request to GPT": copy.deepcopy(conversations),
"Response from GPT": rsp,
})
return rsp_content, True
# 超时
except requests.Timeout:
print(f"chatbot finish, story_id:{story_id}, response:")
print("AI接口访问失败,响应超时")
return "AI接口访问失败,响应超时", False
# 链接异常
except requests.ConnectionError:
print(f"chatbot finish, story_id:{story_id}, response:")
print("AI接口访问失败,连接错误")
return "AI接口访问失败,连接错误", False
# 其他异常
except Exception as errInfo:
print(f"chatbot finish, story_id:{story_id}, response:")
print("AI接口访问失败,其他错误, errInfo:")
print(errInfo)
return "AI接口访问失败,字数太多,或其他原因", False
# 初始化全局变量
chatbot = Chatbot(api_key=os.environ.get("API_KEY"))