-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatapi.py
123 lines (111 loc) · 4.19 KB
/
chatapi.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import openai
import os
import sys
import time
from flask import Flask, render_template, abort, request, jsonify, redirect, session
from flask_cors import CORS
from gevent.pywsgi import WSGIServer
from datetime import timedelta
from geventwebsocket.handler import WebSocketHandler
import logging
import hashlib
from conf.chatconfig import getConfig
# use your own config for api key, secret, etc
config = getConfig()
# Set your API key
openai.api_key = config["api_key"]
app = Flask(__name__)
app.config['SECRET_KEY'] = config["SECRET_KEY"]
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1)
# Set the initial prompt and conversation history
defaulthint = ["Be an expert.No repeat question.","Give short answer, explain only when asked.","Be a teacher, explain the answer."]
hint=defaulthint[0]
conversation_history = dict()
logged = dict()
basicContent = [{"role":"system", "content":hint}]
def talkToOpenAI(chat_id, user_input):
if chat_id in conversation_history:
history = conversation_history[chat_id]
else:
history = basicContent.copy()
if user_input == ":q":
del conversation_history[chat_id]
conversation_history[chat_id] = basicContent.copy()
return("Conversation reset.")
if user_input == ":size":
total=0
for v in history:
for key,value in v.items():
total=total+len(key)
total=total+len(value)
return("Current size:"+str(total))
# Add user input to the conversation history
history.append({"role":"user", "content":user_input})
# Set the new prompt with the conversation history
prompt = history
app.logger.info("Log prompt=%d",len(history))
# Send the API request
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=prompt,
)
except Exception as e:
return ("error from ai:",str(e))
response_text = response.choices[0]["message"]["content"]
# Add the generated response to the conversation history
history.append({"role":"assistant", "content":response_text})
conversation_history[chat_id] = history
# Print the generated response
return(f"{response_text}")
def checkSession(chatID):
if chatID not in logged:
app.logger.info("chatID"+str(chatID)+"not exist")
return False
if time.time() - logged[chatID] > 3600 *2:
app.logger.info("chatID"+str(chatID)+"expired")
del logged[chatID]
return False
logged[chatID] = time.time()
return True
@app.route("/")
@app.route("/main.html")
def index():
return render_template('main.html')
@app.route('/chat/', methods=['POST'])
def chat():
if 'chat_id' in session:
chatID = session['chat_id']
if not checkSession(chatID):
abort(401)
if not request.json or 'content' not in request.json:
abort(401)
reply = talkToOpenAI(session['chat_id'],request.json['content'])
response=jsonify({'result': reply})
return response
else:
response=jsonify({'error': "not login"})
return response
@app.route('/login/', methods=['POST'])
def login():
if not request.json or 'chat_id' not in request.json or 'passwd' not in request.json:
app.logger.info("login error: wrong input")
abort(401)
toHash = request.json['passwd']
hmd5 = hashlib.md5()
hmd5.update(toHash.encode('utf-8'))
sig = hmd5.hexdigest().upper()
if sig != config["passwd"]:
app.logger.info("login error: wrong password=%s, name=%s",toHash,chat_id)
abort(401)
chatID = request.json['chat_id']
if checkSession(chatID):
return jsonify({'error': "already existed."})
session['chat_id']=chatID
logged[chatID]=time.time()
response = jsonify({"result":"welcome!"})
return response
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, filename='log.log', filemode='w', format="%(asctime)s:%(levelname)s:%(name)s -- %(message)s", datefmt="%Y/%m/%d %H:%M:%S" )
http_server = WSGIServer(('0.0.0.0', 666), app, keyfile=config["keyfile"], certfile=config["certfile"],handler_class=WebSocketHandler)
http_server.serve_forever()