-
Notifications
You must be signed in to change notification settings - Fork 89
/
server_api.py
559 lines (491 loc) · 19.9 KB
/
server_api.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
"""
The backend API that runs dialog agents, returns agent utterance to the front-end, and stores user data in a MongoDB database
The API has the following three functions that can be used by any front-end.
All inputs/outputs are string, except for `log_object` which is a json object and `turn_id` and `user_naturalness_rating` which are integers.
- `/chat`
Inputs: (experiment_id, new_user_utterance, dialog_id, turn_id, system_name)
Outputs: (agent_utterance, log_object)
Each time a user types something and clicks send, the front-end should make one call per system to /chat. So e.g. it should make two separate calls for two systems.
- `/user_rating`
Inputs: (experiment_id, dialog_id, turn_id, system_name, user_naturalness_rating, user_factuality_rating, user_factuality_confidence)
Outputs: None
When the user submits their ratings, the front-end should make one call per system to /user_rating. So e.g. it should make two separate calls for two systems.
- `/user_preference`
Inputs: (experiment_id, dialog_id, turn_id, winner_system, loser_systems)
Outputs: None
Each time the user selects one of the agent utterances over the other, you make one call to /user_preference.
`turn_id` starts from 0 and is incremented by 1 after a user and agent turn
"""
import time
from typing import List
from pipelines.dialog_turn import DialogueTurn
from pipelines.pipeline_arguments import (
add_pipeline_arguments,
check_pipeline_arguments,
)
from pipelines.chatbot import Chatbot
import argparse
import os
import json
import logging
from flask import Flask, request
from flask_cors import CORS
from flask_restful import reqparse
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import pymongo
# set up the MongoDB connection
CONNECTION_STRING = os.environ.get("COSMOS_CONNECTION_STRING")
client = pymongo.MongoClient(CONNECTION_STRING)
db = client["wikichat"] # the database name is wikichat
dialog_db_collection = db[
"dialog_turns"
] # the collection that stores dialog turns and their user ratings
dialog_db_collection.create_index(
"$**"
) # necessary to build an index before we can call sort()
preference_db_collection = db[
"preferences"
] # the collection that stores information about what utterance users preferred
preference_db_collection.create_index(
"$**"
) # necessary to build an index before we can call sort()
# The "schema" of dialog_db_collection is: {_id=(dialog_id, turn_id, system_name), experiment_id, dialog_id, turn_id, system_name, user_utterance, agent_utterance, agent_log_object, user_naturalness_rating}
# The "schema" of preference_db_collection is: {_id=(dialog_id, turn_id), experiment_id, dialog_id, turn_id, winner_system, loser_systems}
# set up the Flask app
app = Flask(__name__)
app.config["JSON_SORT_KEYS"] = False
CORS(app)
logger = app.logger
# default rate limit for all routs. Note that each turn may have multiple calls to each API below if user rating is collected
per_user_limiter = Limiter(
key_func=get_remote_address,
app=app,
default_limits=["100 per day", "5 per second"], # the second limit prevents bursts
strategy="fixed-window",
storage_uri="redis://localhost:5003",
)
global_limiter = Limiter(
key_func=lambda: "fixed_key",
app=app,
default_limits=[
"2000 per day",
"50 per second",
], # 2000 turns with gpt-35-turbo-instruct is ~$80
strategy="moving-window",
storage_uri="redis://localhost:5003",
)
logging.getLogger("openai").setLevel(logging.ERROR)
chatbot = None
args = None
context = None
# The input arguments coming from the front-end
req_parser = reqparse.RequestParser()
req_parser.add_argument(
"experiment_id",
type=str,
location="json",
help="Identifier that differentiates data from different experiments.",
)
req_parser.add_argument(
"dialog_id",
type=str,
location="json",
help="Globally unique identifier for each dialog",
)
req_parser.add_argument(
"turn_id", type=int, location="json", help="Turn number in the dialog"
)
req_parser.add_argument("user_naturalness_rating", type=int, location="json")
req_parser.add_argument("user_factuality_rating", type=int, location="json")
req_parser.add_argument("user_factuality_confidence", type=int, location="json")
req_parser.add_argument(
"new_user_utterance", type=str, location="json", help="The new user utterance"
)
req_parser.add_argument(
"system_name",
type=str,
location="json",
help="The system to use for generating agent utterances",
)
# arguments for when a user makes a head-to-head comparison
req_parser.add_argument(
"winner_system",
type=str,
location="json",
help="The system that was preferred by the user in the current dialog turn",
)
req_parser.add_argument(
"loser_systems",
type=list,
location="json",
help="The system(s) that was not preferred by the user in the current dialog turn",
)
def extract_system_parameters(system: str):
"""
Example input: [pipeline=generate, engine=text-davinci-003]
"""
# return a dictionary of parameters inside []
# if there are no parameters, return an empty dictionary
parameters = system[system.find("[") + 1 : system.find("]")]
# split by comma
parameters = parameters.split(",")
# split by equal sign
parameters = [p.strip().split("=") for p in parameters]
# convert to dictionary
parameters = {p[0].strip(): p[1].strip() for p in parameters}
# find pipeline
pipeline = parameters["pipeline"]
parameters.pop("pipeline", None)
return pipeline, parameters
def filter_nons(request_args):
return dict(
[(k, request_args[k]) for k in request_args if request_args[k] is not None]
)
def extract_db_history(dialog_id, turn_id) -> List[str]:
db_dialog_history = []
if turn_id > 0:
# query DB to get previous turns
# sort based on turn_id
previous_turns = list(
dialog_db_collection.find(
{"dialog_id": dialog_id, "turn_id": {"$lt": turn_id}},
{
"turn_id": 1,
"system_name": 1,
"user_utterance": 1,
"agent_utterance": 1,
},
).sort("turn_id", pymongo.ASCENDING)
)
assert (len(previous_turns) == 0 and turn_id == 0) or len(
previous_turns
) % turn_id == 0, "This turn should be the first turn of the dialog, or we should have a dialog turn document in the database per turn per system"
# query db to see which system did the user prefer in the previous turns
# sort based on turn_id
previous_turn_preferences = list(
preference_db_collection.find(
{"dialog_id": dialog_id},
{"turn_id": 1, "winner_system": 1, "loser_systems": 1},
).sort("turn_id", pymongo.ASCENDING)
)
for p in previous_turn_preferences:
assert (
p["turn_id"] < turn_id
), "We should not have any preferences for the current turn"
if len(previous_turn_preferences) > 0:
# we are in the middle of comparing two systems, so we need to figure out the actual history of the conversation
assert (
len(previous_turns) >= len(previous_turn_preferences)
and len(previous_turns) % len(previous_turn_preferences) == 0
), "All previous turns need to have a user preference"
# number_of_systems = len(previous_turns) // len(previous_turn_preferences)
for t_id, turn_preference in enumerate(previous_turn_preferences):
turn_preference["winner_system"]
turn_with_winner_utterance = [
t
for t in previous_turns
if t["turn_id"] == t_id
and t["system_name"] == turn_preference["winner_system"]
]
assert (
len(turn_with_winner_utterance) == 1
), "There should be only one utterance for each (turn_id, system_name)"
db_dialog_history.append(
turn_with_winner_utterance[0]["user_utterance"]
)
db_dialog_history.append(
turn_with_winner_utterance[0]["agent_utterance"]
)
else:
# there is only one system (we are not comparing multiple systems)
for t in previous_turns:
db_dialog_history.append(t["user_utterance"])
db_dialog_history.append(t["agent_utterance"])
return db_dialog_history
# `route` decorator should come before `limit` decorator
@app.route("/chat", methods=["POST"])
@per_user_limiter.limit(
None, methods=["POST"]
) # important not to block OPTIONS method. If we do, preflight requests will be blocked, and the error type won't be clear in the front-end
@global_limiter.limit(None, methods=["POST"])
def chat():
"""
Inputs: (experiment_id, new_user_utterance, dialog_id, turn_id, system_name)
Outputs: (agent_utterance, log_object)
"""
# logger.info('Entered /chat')
request_args = req_parser.parse_args()
logger.info("Input arguments received: %s", str(filter_nons(request_args)))
experiment_id = request_args["experiment_id"]
new_user_utterance = request_args["new_user_utterance"]
dialog_id = request_args["dialog_id"]
turn_id = request_args["turn_id"]
system_name = request_args["system_name"]
if args.test:
logger.info("request from IP address %s", str(request.remote_addr))
return {
"agent_utterance": "Sample agent utterance. experiment_id=%s, new_user_utterance=%s, dialog_id=%s, turn_id=%d, system_name=%s"
% (experiment_id, new_user_utterance, dialog_id, turn_id, system_name),
"log_object": {"log object parameter 1": "log object value 1"},
}
# parse system parameters
try:
pipeline, system_parameters = extract_system_parameters(system_name)
except Exception as e:
logger.exception(e)
new_dlg_turn = DialogueTurn(
user_utterance=new_user_utterance,
agent_utterance="Invalid system parameters.",
pipeline="unknown",
)
turn_log = new_dlg_turn.log()
return {"agent_utterance": new_dlg_turn.agent_utterance, "log_object": turn_log}
# reconstruct the dialog history from DB
try:
db_dialog_history = extract_db_history(dialog_id, turn_id)
except Exception as e:
logger.exception(e)
new_dlg_turn = DialogueTurn(
user_utterance=new_user_utterance,
agent_utterance="Database error while retrieving dialog history. Please refresh the page and try again.",
pipeline=pipeline,
)
turn_log = new_dlg_turn.log()
return {"agent_utterance": new_dlg_turn.agent_utterance, "log_object": turn_log}
# generate system response
try:
dialog_history, u = DialogueTurn.utterance_list_to_dialog_history(
db_dialog_history + [new_user_utterance]
)
new_dlg_turn = chatbot.generate_next_turn(
dialog_history, u, pipeline=pipeline, system_parameters=system_parameters
)
agent_utterance = new_dlg_turn.agent_utterance
turn_log = new_dlg_turn.log()
# logger.info('system output = %s', json.dumps(turn_log, indent=4))
logger.info("Response took %.1f seconds", turn_log["wall_time_seconds"])
except Exception as e:
logger.exception(e)
new_dlg_turn = DialogueTurn(
user_utterance=new_user_utterance,
agent_utterance="Encountered an error while generating this response. Please refresh the page and try again.",
pipeline=pipeline,
)
turn_log = new_dlg_turn.log()
return {"agent_utterance": new_dlg_turn.agent_utterance, "log_object": turn_log}
# save system response to DB
try:
logger.info(
"Inserting new entry with _id = %s", str((dialog_id, turn_id, system_name))
)
dialog_db_collection.insert_one(
{
"_id": str((dialog_id, turn_id, system_name)),
"experiment_id": experiment_id,
"dialog_id": dialog_id,
"turn_id": turn_id,
"system_name": system_name,
"user_utterance": new_user_utterance,
"agent_utterance": agent_utterance,
"agent_log_object": turn_log,
"user_naturalness_rating": -1,
"user_factuality_rating": False,
"user_factuality_confidence": -1,
}
)
except Exception as e:
logger.exception(e)
new_dlg_turn = DialogueTurn(
user_utterance=new_user_utterance,
agent_utterance="Database error while saving the current turn.",
pipeline=pipeline,
)
turn_log = new_dlg_turn.log()
return {"agent_utterance": new_dlg_turn.agent_utterance, "log_object": turn_log}
# code for adding delay to faster pipielines, so that the user study subjects focus on quality not latency
# time_spent = turn_log["wall_time_seconds"]
# if time_spent < 15 and pipeline == "generate":
# logger.info("Waiting for an extra %.2f seconds", 15 - time_spent)
# time.sleep(15 - time_spent)
return {"agent_utterance": agent_utterance, "log_object": turn_log}
@app.route("/user_rating", methods=["POST"])
@per_user_limiter.limit(None, methods=["POST"])
@global_limiter.limit(None, methods=["POST"])
def user_rating():
"""
Inputs: (experiment_id, dialog_id, turn_id, system_name, user_naturalness_rating, user_factuality_rating, user_factuality_confidence)
Outputs: None
"""
# logger.info('Entered /user_rating')
request_args = req_parser.parse_args()
logger.info("Input arguments received: %s", str(filter_nons(request_args)))
experiment_id = request_args["experiment_id"]
dialog_id = request_args["dialog_id"]
turn_id = request_args["turn_id"]
system_name = request_args["system_name"]
user_naturalness_rating = request_args["user_naturalness_rating"]
user_factuality_rating = request_args["user_factuality_rating"]
user_factuality_confidence = request_args["user_factuality_confidence"]
if args.test:
return {}
matching_turn = list(
dialog_db_collection.find(
{"dialog_id": dialog_id, "turn_id": turn_id, "system_name": system_name},
{
"experiment_id": 1,
"user_naturalness_rating": 1,
"user_factuality_rating": 1,
"user_factuality_confidence": 1,
},
)
)
assert (
len(matching_turn) == 1
), "There should be exactly one (dialog_id, turn_id, system_name)"
matching_turn = matching_turn[0]
assert (
matching_turn["user_naturalness_rating"] == -1
), "This turn has already been rated by the user."
assert (
matching_turn["user_factuality_rating"] == False
), "This turn has already been rated by the user."
assert (
matching_turn["user_factuality_confidence"] == -1
), "This turn has already been rated by the user."
assert (
matching_turn["experiment_id"] == experiment_id
), "experiment_id of the found document does not match the experiment_id received from the front-end" # we check this just to be safe
# update the document
dialog_db_collection.update_one(
{"dialog_id": dialog_id, "turn_id": turn_id, "system_name": system_name},
{
"$set": {
"user_naturalness_rating": user_naturalness_rating,
"user_factuality_rating": user_factuality_rating,
"user_factuality_confidence": user_factuality_confidence,
}
},
)
return {}
@app.route("/user_preference", methods=["POST"])
@per_user_limiter.limit(None, methods=["POST"])
@global_limiter.limit(None, methods=["POST"])
def user_preference():
"""
Inputs: (experiment_id, dialog_id, turn_id, winner_system, loser_systems)
Outputs: None
"""
# logger.info('Entered /user_preference')
request_args = req_parser.parse_args()
logger.info("Input arguments received: %s", str(filter_nons(request_args)))
experiment_id = request_args["experiment_id"]
dialog_id = request_args["dialog_id"]
turn_id = request_args["turn_id"]
winner_system = request_args["winner_system"]
loser_systems = request_args["loser_systems"]
if args.test:
return {}
# check these just to be safe
matching_turn = list(
dialog_db_collection.find(
{"dialog_id": dialog_id, "turn_id": turn_id, "system_name": winner_system}
)
)
assert (
len(matching_turn) == 1
), "There should be exactly one (dialog_id, turn_id, system_name)"
assert (
matching_turn[0]["experiment_id"] == experiment_id
), "experiment_id of the found document does not match the experiment_id received from the front-end" # we check this just to be safe
for loser_system in loser_systems:
matching_turn = list(
dialog_db_collection.find(
{
"dialog_id": dialog_id,
"turn_id": turn_id,
"system_name": loser_system,
}
)
)
assert len(matching_turn) == 1, (
"There should be exactly one (dialog_id, turn_id, system_name) but found %s"
% str(matching_turn)
)
assert (
matching_turn[0]["experiment_id"] == experiment_id
), "experiment_id of the found document does not match the experiment_id received from the front-end" # we check this just to be safe
logger.info("Inserting new entry with _id = %s", str((dialog_id, turn_id)))
preference_db_collection.insert_one(
{
"_id": str((dialog_id, turn_id)),
"experiment_id": experiment_id,
"dialog_id": dialog_id,
"turn_id": turn_id,
"winner_system": winner_system,
"loser_system": loser_systems,
}
)
return {}
def init():
arg_parser = argparse.ArgumentParser()
add_pipeline_arguments(arg_parser)
arg_parser.add_argument(
"--ssl_certificate_file",
type=str,
help="Where to read the SSL certificate for HTTPS",
)
arg_parser.add_argument(
"--ssl_key_file",
type=str,
help="Where to read the SSL certificate for HTTPS",
)
arg_parser.add_argument(
"--test",
action="store_true",
help="If set, no database query will be made, and mock outputs will be returned.",
)
arg_parser.add_argument(
"--no_logging", action="store_true", help="Disables logging except for errors"
)
global args
args, unknown = arg_parser.parse_known_args()
check_pipeline_arguments(args)
global chatbot
chatbot = Chatbot(args)
global context
context = (args.ssl_certificate_file, args.ssl_key_file)
if args.no_logging:
logging.basicConfig(
level=logging.ERROR, format=" %(name)s : %(levelname)-8s : %(message)s"
)
else:
logging.basicConfig(
level=logging.INFO, format=" %(name)s : %(levelname)-8s : %(message)s"
)
def convert_to_argparse_list(input_flags: str):
# TODO add logic to handle special cases if needed
str_list = input_flags.split()
return str_list
# used as a way to pass commandline arguments to gunicorn
def gunicorn_app(input_flags: str):
# Gunicorn CLI args are useless.
# https://stackoverflow.com/questions/8495367/
#
# Start the application in modified environment.
# https://stackoverflow.com/questions/18668947/
#
import sys
sys.argv = ["--gunicorn"] + convert_to_argparse_list(input_flags)
init()
return app
if __name__ == "__main__":
# Run the app with Flask
init()
app.run(
host="0.0.0.0",
port=5001,
debug=args.test,
use_reloader=False,
ssl_context=context,
)