-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (66 loc) · 2.61 KB
/
Copy pathmain.py
File metadata and controls
99 lines (66 loc) · 2.61 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
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
import uuid
from flask import render_template, request, make_response
from app import app
import prompts
import roles
from chat_completion import respond
import db
@app.route('/healthcheck')
def hello_world(): # put application's code here
return 'Hello World!'
def add_message(chat_id, role, message):
max_index = db.query('SELECT MAX(MsgIndex) FROM MESSAGES where ChatId = ?', (chat_id,), one=True)[0]
msg_index = 0
if max_index is not None:
msg_index = max_index + 1
sql_request = f"INSERT INTO MESSAGES VALUES (?, ?, ?, ?)"
db.execute(sql_request, (chat_id, msg_index, role, message))
def get_chat_messages(chat_id) -> list[dict[str, str]]:
messages = db.query(f'SELECT * FROM MESSAGES WHERE ChatId = ? ORDER BY MsgIndex', (chat_id,))
return [{'role':message[2], 'message':message[3]} for message in messages]
def get_chat_id():
chat_id = uuid.uuid4().hex
add_message(chat_id, roles.assistant, prompts.agent_first_message)
return chat_id
def add_system_prompt(message_history):
prompt = {"role": roles.system, "message": prompts.system_prompt}
message_history.insert(0, prompt)
return message_history
@app.route('/')
def index():
chat_id = request.cookies.get('ChatId')
if not chat_id:
chat_id = get_chat_id()
messages = get_chat_messages(chat_id)
messages = [(message.get('role'), message.get('message')) for message in messages]
resp = make_response(render_template('index.html', messages=messages))
resp.set_cookie('ChatId', chat_id)
return resp
@app.route('/send', methods=['POST'])
def send():
chat_id = request.cookies.get('ChatId')
message = request.form['message']
if message == "":
return ""
add_message(chat_id, roles.user, message)
return render_template('reply.html', message=message)
@app.route('/reply')
def reply():
chat_id = request.cookies.get('ChatId')
messages = get_chat_messages(chat_id)
messages = add_system_prompt(messages)
answer = respond(messages)
add_message(chat_id, roles.assistant, answer)
return render_template('message.html', username=roles.assistant, message=answer)
@app.route('/reset')
def reset():
chat_id = request.cookies.get('ChatId')
db.execute(f'DELETE FROM MESSAGES WHERE ChatId = ?', (chat_id,))
new_id = get_chat_id()
messages = get_chat_messages(new_id)
messages = [(message.get('role'), message.get('message')) for message in messages]
resp = make_response(render_template('chat_window.html', messages=messages))
resp.set_cookie('ChatId', new_id)
return resp
if __name__ == '__main__':
app.run()