-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (54 loc) · 1.88 KB
/
main.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
import json
from revChatGPT.V1 import Chatbot
from flask import Flask, jsonify, request
server = Flask(__name__)
with open("config.json", "r") as f:
config = json.load(f)
# init chatbot
chatbot = Chatbot(config['auth'])
def send_gpt(message, cid, pid):
if message == 'hi' and cid == None:
chatbot.reset_chat()
result = ""
for data in chatbot.ask(
message, conversation_id=cid, parent_id=pid
):
result = data
return result
class Data:
def __init__(self, code, data, err):
self.code = code
self.data = data
self.err = err
@server.route(config['path'], methods=['POST'])
def get_request_json():
if request.method == 'POST':
mess = Data(200, "null", "null")
getData = request.get_json()
print("======================================")
print("接到请求:", getData)
if len(getData) != 4:
mess.err = "bad data"
jsonString = json.dumps(mess.__dict__)
return jsonString, {"Content-Type": "application/json"}
else:
user = getData[3]
msg = getData[0]
cid = getData[1]
pid = getData[2]
# res = "test"
if user == 'getSeesions':
res = send_gpt("hi", None, None)
print(res)
mess.data = res
jsonString = json.dumps(mess.__dict__)
else:
res = send_gpt(msg, cid, pid)
mess.data = res
jsonString = json.dumps(mess.__dict__)
return jsonString, {"Content-Type": "application/json"}
@server.errorhandler(Exception)
def handle_error(e):
return jsonify({'code':'500','data':'null','err': str(e)}), 200
if __name__ == '__main__':
server.run(debug=True, host='0.0.0.0', port=config['port'])