-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathnlp_xingchen.py
94 lines (89 loc) · 3.52 KB
/
nlp_xingchen.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
import requests
import json
from utils import util, config_util
from core import content_db
def question(cont, uid=0, observation=""):
url = 'https://nlp.aliyuncs.com/v2/api/chat/send'
headers = {
'accept': '*/*',
'Content-Type': 'application/json',
'X-AcA-DataInspection': 'disable',
'x-fag-servicename': 'aca-chat-send',
'x-fag-appcode': 'aca',
'Authorization': f"Bearer {config_util.key_xingchen_api_key}"
}
contentdb = content_db.new_instance()
if uid == 0:
communication_history = contentdb.get_list('all','desc', 11)
else:
communication_history = contentdb.get_list('all','desc', 11, uid)
#历史记录处理
message=[]
i = len(communication_history) - 1
if len(communication_history)>1:
while i >= 0:
answer_info = dict()
if communication_history[i][0] == "member":
answer_info["role"] = "user"
answer_info["content"] = communication_history[i][2]
elif communication_history[i][0] == "fay":
answer_info["role"] = "assistant"
answer_info["content"] = communication_history[i][2]
message.append(answer_info)
i -= 1
else:
answer_info = dict()
answer_info["role"] = "user"
answer_info["content"] = cont
message.append(answer_info)
data = {
"input": {
"messages": message,
"aca": {
"botProfile": {
"characterId": config_util.xingchen_characterid,
"version": 1
},
"userProfile": {
"userId": "1234567891",
"userName": "我",
"basicInfo": ""
},
"scenario": {
"description": "你是数字人Fay。用户问你问题的时候回答之前请一步一步想清楚。你的底层AI算法技术是Fay。"
},
"context": {
"useChatHistory": False,
"isRegenerate": False,
}
}
},
"parameters": {
"seed": 1683806810,
}
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
response_data = json.loads(response.text)
if response_data.get('success') and 'data' in response_data and 'choices' in response_data['data'] and len(response_data['data']['choices']) > 0:
content = response_data['data']['choices'][0]['messages'][0]['content']
return content
else:
util.log(1, "通义星辰调用失败,请检查配置")
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
return response_text
else:
util.log(1, f"通义星辰调用失败,请检查配置(错误码:{response.status_code})")
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
return response_text
except Exception as e:
util.log(1, f"通义星辰调用失败,请检查配置(错误:{e})")
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
return response_text
# # 调用函数测试
# result = question("你早")
# if result:
# print(f"Received response: {result}")
# else:
# print("Failed to get a valid response.")