-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.py
More file actions
61 lines (47 loc) · 1.57 KB
/
web.py
File metadata and controls
61 lines (47 loc) · 1.57 KB
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
import traceback
from flask import Flask, request, jsonify
from flask_cors import CORS
from config import config
from gpt import chat_completion
import enum
max_conversion = 5
conversions = []
system_message = ''
app = Flask(__name__)
CORS(app)
class RespStatus(enum.IntEnum):
Success = 0
InteralException = 100
InvaildParams = 101
InvaildRequest = 102
def sucess_resp(data:dict):
return jsonify({"status": RespStatus.Success, "msg":"OK", "result":data})
def fail_resp(code:int, msg:str):
return jsonify({"status":code, "msg":msg})
def except_resp(e):
return jsonify({"status": RespStatus.InteralException, "msg": "exception"})
@app.get('/api/status')
def status():
return 'OK'
@app.post('/api/chat')
def chat():
try:
token = request.headers.get('Token')
if token is None or token != config.WEB_TOKEN:
return fail_resp(RespStatus.InvaildRequest, 'access denied')
text = None
if 'text' in request.json:
text = request.json['text']
if text is None or len(text) == 0:
return fail_resp(RespStatus.InvaildParams, 'text needed')
resp = chat_completion(text, 'web',
conversions=conversions,
max_conversion=max_conversion,
system_message=system_message)
return sucess_resp({'response': resp})
except Exception as e:
app.logger.error(str(e) + '\n' + traceback.format_exc())
return except_resp(e)
@app.get('/')
def index():
return '<h1>Hello, FlyBot!</h1>'