-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
252 lines (189 loc) · 6.44 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import logging
import log
import sys
import signal
from datetime import datetime, timezone
from flask import Flask, request, render_template, abort
from markupsafe import Markup
import markdown
import database
import orchestrator
def signal_handler(signal, frame):
logging.warning('SIGTERM received, exiting...')
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)
app = Flask(__name__)
@app.before_request
def before_request():
"""log http request (except for health checks)"""
if request.path != "/health":
logging.info(f"HTTP {request.method} {request.url}")
@app.after_request
def after_request(response):
"""log http response (except for health checks)"""
if request.path != "/health":
logging.info(
f"HTTP {request.method} {request.url} {response.status_code}")
return response
# initialize database client
db = database.Database()
@app.template_filter('markdown')
def render_markdown(text):
"""Render Markdown text to HTML"""
return Markup(markdown.markdown(text))
@app.route("/health")
def health_check():
return "healthy"
def get_current_user_id():
"""get the currently logged in user"""
# TODO: get current user id from auth
return "1"
def get_chat_history(user_id):
"""
fetches the user's latest chat history
"""
logging.info(f"fetching chat history for user {user_id}")
# fetch last 10 questions from db
history = db.list_by_user(user_id, 10)
# build list of objects that have
# the first question in each conversation and the date
result = []
for h in history:
if len(h["questions"]) == 0:
continue
result.append({
"conversationId": h["conversationId"],
"created": datetime.fromisoformat(h["created"]).strftime("%B %d, %Y"),
"initial_question": h["questions"][0]["q"],
})
return result
@app.route("/")
def index():
"""home page"""
user_id = get_current_user_id()
return render_template("index.html",
conversation={},
chat_history=get_chat_history(user_id))
@app.route("/new", methods=["POST"])
def new():
"""POST /new starts a new conversation"""
return render_template("chat.html", conversation={})
@app.route("/ask", methods=["POST"])
def ask():
"""POST /ask adds a new Q&A to the conversation"""
# get conversation id and question from form
if "conversation_id" not in request.values:
m = "missing required form data: conversation_id"
logging.error(m)
abort(400, m)
id = request.values["conversation_id"]
logging.info(f"conversation id: {id}")
if "question" not in request.values:
m = "missing required form data: question"
logging.error(m)
abort(400, m)
question = request.values["question"]
question = question.rstrip()
logging.info(f"question: {question}")
user_id = get_current_user_id()
# if conversation id is blank, start a new one
# else, fetch conversation history from db
if id == "":
conversation = db.new(user_id, datetime.now(timezone.utc).isoformat())
else:
conversation = db.get(id)
logging.info("fetched conversation")
log.debug(conversation)
_, conversation, sources = ask_internal(conversation, question)
# render ui with question history, answer, and top 3 document references
return render_template("body.html",
conversation=conversation,
sources=sources,
chat_history=get_chat_history(user_id))
@app.route("/conversation/<id>", methods=["GET"])
def get_conversation(id):
"""GET /conversation/<id> fetches a conversation by id"""
conversation = db.get(id)
return render_template("chat.html", conversation=conversation)
@app.route("/api/ask", methods=["POST"])
def ask_api_new():
"""returns an answer to a question in a new conversation"""
# get request json from body
body = request.get_json()
log.debug(body)
if "question" not in body:
m = "missing field: question"
logging.error(m)
abort(400, m)
question = body["question"]
conversation = db.new(datetime.now(timezone.utc))
answer, conversation, sources = ask_internal(conversation, question)
return {
"conversationId": conversation["conversationId"],
"answer": answer,
"sources": sources,
}
@app.route("/api/ask/<id>", methods=["POST"])
def ask_api(id):
"""returns an answer to a question in a conversation"""
# get request json from body
body = request.get_json()
log.debug(body)
if "question" not in body:
m = "missing field: question"
logging.error(m)
abort(400, m)
question = body["question"]
if id == "":
m = "conversation id is required"
logging.error(m)
abort(400, m)
else:
conversation = db.get(id)
logging.info("fetched conversation")
log.debug(conversation)
answer, _, sources = ask_internal(conversation, question)
return {
"conversationId": id,
"answer": answer,
"sources": sources,
}
@app.route("/api/conversations")
def conversations_list():
"""fetch top 10 conversations"""
return db.list(10)
@app.route("/api/conversations/users/<user_id>")
def conversations_get_by_user(user_id):
"""fetch top 10 conversations for a user"""
return db.list_by_user(user_id, 10)
@app.route("/api/conversations/<id>")
def conversations_get(id):
"""fetch a conversation by id"""
return db.get(id)
def ask_internal(conversation, question):
"""
core ask implementation shared by app and api.
Args:
conversation: conversation object
question: question to ask
Returns:
answer: answer to question
conversation: updated conversation object
sources: search results
"""
# RAG orchestration to get answer
answer, sources = orchestrator.orchestrate(conversation, question)
# add final Q&A to conversation
conversation["questions"].append({
"q": question,
"a": answer,
"created": datetime.now(timezone.utc),
})
logging.info("updating conversation in db")
log.debug(conversation)
db.update(conversation)
return answer, conversation, sources
if __name__ == '__main__':
port = 8080
print(f"listening on http://localhost:{port}")
app.run(host="0.0.0.0", port=port)