-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
app.py
885 lines (741 loc) · 31.3 KB
/
app.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
import copy
import json
import os
import logging
import uuid
import httpx
import asyncio
from quart import (
Blueprint,
Quart,
jsonify,
make_response,
request,
send_from_directory,
render_template,
current_app,
)
from openai import AsyncAzureOpenAI
from azure.identity.aio import (
DefaultAzureCredential,
get_bearer_token_provider
)
from backend.auth.auth_utils import get_authenticated_user_details
from backend.security.ms_defender_utils import get_msdefender_user_json
from backend.history.cosmosdbservice import CosmosConversationClient
from backend.settings import (
app_settings,
MINIMUM_SUPPORTED_AZURE_OPENAI_PREVIEW_API_VERSION
)
from backend.utils import (
format_as_ndjson,
format_stream_response,
format_non_streaming_response,
convert_to_pf_format,
format_pf_non_streaming_response,
)
bp = Blueprint("routes", __name__, static_folder="static", template_folder="static")
cosmos_db_ready = asyncio.Event()
def create_app():
app = Quart(__name__)
app.register_blueprint(bp)
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.before_serving
async def init():
try:
app.cosmos_conversation_client = await init_cosmosdb_client()
cosmos_db_ready.set()
except Exception as e:
logging.exception("Failed to initialize CosmosDB client")
app.cosmos_conversation_client = None
raise e
return app
@bp.route("/")
async def index():
return await render_template(
"index.html",
title=app_settings.ui.title,
favicon=app_settings.ui.favicon
)
@bp.route("/favicon.ico")
async def favicon():
return await bp.send_static_file("favicon.ico")
@bp.route("/assets/<path:path>")
async def assets(path):
return await send_from_directory("static/assets", path)
# Debug settings
DEBUG = os.environ.get("DEBUG", "false")
if DEBUG.lower() == "true":
logging.basicConfig(level=logging.DEBUG)
USER_AGENT = "GitHubSampleWebApp/AsyncAzureOpenAI/1.0.0"
# Frontend Settings via Environment Variables
frontend_settings = {
"auth_enabled": app_settings.base_settings.auth_enabled,
"feedback_enabled": (
app_settings.chat_history and
app_settings.chat_history.enable_feedback
),
"ui": {
"title": app_settings.ui.title,
"logo": app_settings.ui.logo,
"chat_logo": app_settings.ui.chat_logo or app_settings.ui.logo,
"chat_title": app_settings.ui.chat_title,
"chat_description": app_settings.ui.chat_description,
"show_share_button": app_settings.ui.show_share_button,
"show_chat_history_button": app_settings.ui.show_chat_history_button,
},
"sanitize_answer": app_settings.base_settings.sanitize_answer,
"oyd_enabled": app_settings.base_settings.datasource_type,
}
# Enable Microsoft Defender for Cloud Integration
MS_DEFENDER_ENABLED = os.environ.get("MS_DEFENDER_ENABLED", "true").lower() == "true"
# Initialize Azure OpenAI Client
async def init_openai_client():
azure_openai_client = None
try:
# API version check
if (
app_settings.azure_openai.preview_api_version
< MINIMUM_SUPPORTED_AZURE_OPENAI_PREVIEW_API_VERSION
):
raise ValueError(
f"The minimum supported Azure OpenAI preview API version is '{MINIMUM_SUPPORTED_AZURE_OPENAI_PREVIEW_API_VERSION}'"
)
# Endpoint
if (
not app_settings.azure_openai.endpoint and
not app_settings.azure_openai.resource
):
raise ValueError(
"AZURE_OPENAI_ENDPOINT or AZURE_OPENAI_RESOURCE is required"
)
endpoint = (
app_settings.azure_openai.endpoint
if app_settings.azure_openai.endpoint
else f"https://{app_settings.azure_openai.resource}.openai.azure.com/"
)
# Authentication
aoai_api_key = app_settings.azure_openai.key
ad_token_provider = None
if not aoai_api_key:
logging.debug("No AZURE_OPENAI_KEY found, using Azure Entra ID auth")
async with DefaultAzureCredential() as credential:
ad_token_provider = get_bearer_token_provider(
credential,
"https://cognitiveservices.azure.com/.default"
)
# Deployment
deployment = app_settings.azure_openai.model
if not deployment:
raise ValueError("AZURE_OPENAI_MODEL is required")
# Default Headers
default_headers = {"x-ms-useragent": USER_AGENT}
azure_openai_client = AsyncAzureOpenAI(
api_version=app_settings.azure_openai.preview_api_version,
api_key=aoai_api_key,
azure_ad_token_provider=ad_token_provider,
default_headers=default_headers,
azure_endpoint=endpoint,
)
return azure_openai_client
except Exception as e:
logging.exception("Exception in Azure OpenAI initialization", e)
azure_openai_client = None
raise e
async def init_cosmosdb_client():
cosmos_conversation_client = None
if app_settings.chat_history:
try:
cosmos_endpoint = (
f"https://{app_settings.chat_history.account}.documents.azure.com:443/"
)
if not app_settings.chat_history.account_key:
async with DefaultAzureCredential() as cred:
credential = cred
else:
credential = app_settings.chat_history.account_key
cosmos_conversation_client = CosmosConversationClient(
cosmosdb_endpoint=cosmos_endpoint,
credential=credential,
database_name=app_settings.chat_history.database,
container_name=app_settings.chat_history.conversations_container,
enable_message_feedback=app_settings.chat_history.enable_feedback,
)
except Exception as e:
logging.exception("Exception in CosmosDB initialization", e)
cosmos_conversation_client = None
raise e
else:
logging.debug("CosmosDB not configured")
return cosmos_conversation_client
def prepare_model_args(request_body, request_headers):
request_messages = request_body.get("messages", [])
messages = []
if not app_settings.datasource:
messages = [
{
"role": "system",
"content": app_settings.azure_openai.system_message
}
]
for message in request_messages:
if message:
if message["role"] == "assistant" and "context" in message:
context_obj = json.loads(message["context"])
messages.append(
{
"role": message["role"],
"content": message["content"],
"context": context_obj
}
)
else:
messages.append(
{
"role": message["role"],
"content": message["content"]
}
)
user_json = None
if (MS_DEFENDER_ENABLED):
authenticated_user_details = get_authenticated_user_details(request_headers)
conversation_id = request_body.get("conversation_id", None)
application_name = app_settings.ui.title
user_json = get_msdefender_user_json(authenticated_user_details, request_headers, conversation_id, application_name)
model_args = {
"messages": messages,
"temperature": app_settings.azure_openai.temperature,
"max_tokens": app_settings.azure_openai.max_tokens,
"top_p": app_settings.azure_openai.top_p,
"stop": app_settings.azure_openai.stop_sequence,
"stream": app_settings.azure_openai.stream,
"model": app_settings.azure_openai.model,
"user": user_json
}
if app_settings.datasource:
model_args["extra_body"] = {
"data_sources": [
app_settings.datasource.construct_payload_configuration(
request=request
)
]
}
model_args_clean = copy.deepcopy(model_args)
if model_args_clean.get("extra_body"):
secret_params = [
"key",
"connection_string",
"embedding_key",
"encoded_api_key",
"api_key",
]
for secret_param in secret_params:
if model_args_clean["extra_body"]["data_sources"][0]["parameters"].get(
secret_param
):
model_args_clean["extra_body"]["data_sources"][0]["parameters"][
secret_param
] = "*****"
authentication = model_args_clean["extra_body"]["data_sources"][0][
"parameters"
].get("authentication", {})
for field in authentication:
if field in secret_params:
model_args_clean["extra_body"]["data_sources"][0]["parameters"][
"authentication"
][field] = "*****"
embeddingDependency = model_args_clean["extra_body"]["data_sources"][0][
"parameters"
].get("embedding_dependency", {})
if "authentication" in embeddingDependency:
for field in embeddingDependency["authentication"]:
if field in secret_params:
model_args_clean["extra_body"]["data_sources"][0]["parameters"][
"embedding_dependency"
]["authentication"][field] = "*****"
logging.debug(f"REQUEST BODY: {json.dumps(model_args_clean, indent=4)}")
return model_args
async def promptflow_request(request):
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {app_settings.promptflow.api_key}",
}
# Adding timeout for scenarios where response takes longer to come back
logging.debug(f"Setting timeout to {app_settings.promptflow.response_timeout}")
async with httpx.AsyncClient(
timeout=float(app_settings.promptflow.response_timeout)
) as client:
pf_formatted_obj = convert_to_pf_format(
request,
app_settings.promptflow.request_field_name,
app_settings.promptflow.response_field_name
)
# NOTE: This only support question and chat_history parameters
# If you need to add more parameters, you need to modify the request body
response = await client.post(
app_settings.promptflow.endpoint,
json={
app_settings.promptflow.request_field_name: pf_formatted_obj[-1]["inputs"][app_settings.promptflow.request_field_name],
"chat_history": pf_formatted_obj[:-1],
},
headers=headers,
)
resp = response.json()
resp["id"] = request["messages"][-1]["id"]
return resp
except Exception as e:
logging.error(f"An error occurred while making promptflow_request: {e}")
async def send_chat_request(request_body, request_headers):
filtered_messages = []
messages = request_body.get("messages", [])
for message in messages:
if message.get("role") != 'tool':
filtered_messages.append(message)
request_body['messages'] = filtered_messages
model_args = prepare_model_args(request_body, request_headers)
try:
azure_openai_client = await init_openai_client()
raw_response = await azure_openai_client.chat.completions.with_raw_response.create(**model_args)
response = raw_response.parse()
apim_request_id = raw_response.headers.get("apim-request-id")
except Exception as e:
logging.exception("Exception in send_chat_request")
raise e
return response, apim_request_id
async def complete_chat_request(request_body, request_headers):
if app_settings.base_settings.use_promptflow:
response = await promptflow_request(request_body)
history_metadata = request_body.get("history_metadata", {})
return format_pf_non_streaming_response(
response,
history_metadata,
app_settings.promptflow.response_field_name,
app_settings.promptflow.citations_field_name
)
else:
response, apim_request_id = await send_chat_request(request_body, request_headers)
history_metadata = request_body.get("history_metadata", {})
return format_non_streaming_response(response, history_metadata, apim_request_id)
async def stream_chat_request(request_body, request_headers):
response, apim_request_id = await send_chat_request(request_body, request_headers)
history_metadata = request_body.get("history_metadata", {})
async def generate():
async for completionChunk in response:
yield format_stream_response(completionChunk, history_metadata, apim_request_id)
return generate()
async def conversation_internal(request_body, request_headers):
try:
if app_settings.azure_openai.stream and not app_settings.base_settings.use_promptflow:
result = await stream_chat_request(request_body, request_headers)
response = await make_response(format_as_ndjson(result))
response.timeout = None
response.mimetype = "application/json-lines"
return response
else:
result = await complete_chat_request(request_body, request_headers)
return jsonify(result)
except Exception as ex:
logging.exception(ex)
if hasattr(ex, "status_code"):
return jsonify({"error": str(ex)}), ex.status_code
else:
return jsonify({"error": str(ex)}), 500
@bp.route("/conversation", methods=["POST"])
async def conversation():
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
request_json = await request.get_json()
return await conversation_internal(request_json, request.headers)
@bp.route("/frontend_settings", methods=["GET"])
def get_frontend_settings():
try:
return jsonify(frontend_settings), 200
except Exception as e:
logging.exception("Exception in /frontend_settings")
return jsonify({"error": str(e)}), 500
## Conversation History API ##
@bp.route("/history/generate", methods=["POST"])
async def add_conversation():
await cosmos_db_ready.wait()
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
try:
# make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
# check for the conversation_id, if the conversation is not set, we will create a new one
history_metadata = {}
if not conversation_id:
title = await generate_title(request_json["messages"])
conversation_dict = await current_app.cosmos_conversation_client.create_conversation(
user_id=user_id, title=title
)
conversation_id = conversation_dict["id"]
history_metadata["title"] = title
history_metadata["date"] = conversation_dict["createdAt"]
## Format the incoming message object in the "chat/completions" messages format
## then write it to the conversation history in cosmos
messages = request_json["messages"]
if len(messages) > 0 and messages[-1]["role"] == "user":
createdMessageValue = await current_app.cosmos_conversation_client.create_message(
uuid=str(uuid.uuid4()),
conversation_id=conversation_id,
user_id=user_id,
input_message=messages[-1],
)
if createdMessageValue == "Conversation not found":
raise Exception(
"Conversation not found for the given conversation ID: "
+ conversation_id
+ "."
)
else:
raise Exception("No user message found")
# Submit request to Chat Completions for response
request_body = await request.get_json()
history_metadata["conversation_id"] = conversation_id
request_body["history_metadata"] = history_metadata
return await conversation_internal(request_body, request.headers)
except Exception as e:
logging.exception("Exception in /history/generate")
return jsonify({"error": str(e)}), 500
@bp.route("/history/update", methods=["POST"])
async def update_conversation():
await cosmos_db_ready.wait()
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
try:
# make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
# check for the conversation_id, if the conversation is not set, we will create a new one
if not conversation_id:
raise Exception("No conversation_id found")
## Format the incoming message object in the "chat/completions" messages format
## then write it to the conversation history in cosmos
messages = request_json["messages"]
if len(messages) > 0 and messages[-1]["role"] == "assistant":
if len(messages) > 1 and messages[-2].get("role", None) == "tool":
# write the tool message first
await current_app.cosmos_conversation_client.create_message(
uuid=str(uuid.uuid4()),
conversation_id=conversation_id,
user_id=user_id,
input_message=messages[-2],
)
# write the assistant message
await current_app.cosmos_conversation_client.create_message(
uuid=messages[-1]["id"],
conversation_id=conversation_id,
user_id=user_id,
input_message=messages[-1],
)
else:
raise Exception("No bot messages found")
# Submit request to Chat Completions for response
response = {"success": True}
return jsonify(response), 200
except Exception as e:
logging.exception("Exception in /history/update")
return jsonify({"error": str(e)}), 500
@bp.route("/history/message_feedback", methods=["POST"])
async def update_message():
await cosmos_db_ready.wait()
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for message_id
request_json = await request.get_json()
message_id = request_json.get("message_id", None)
message_feedback = request_json.get("message_feedback", None)
try:
if not message_id:
return jsonify({"error": "message_id is required"}), 400
if not message_feedback:
return jsonify({"error": "message_feedback is required"}), 400
## update the message in cosmos
updated_message = await current_app.cosmos_conversation_client.update_message_feedback(
user_id, message_id, message_feedback
)
if updated_message:
return (
jsonify(
{
"message": f"Successfully updated message with feedback {message_feedback}",
"message_id": message_id,
}
),
200,
)
else:
return (
jsonify(
{
"error": f"Unable to update message {message_id}. It either does not exist or the user does not have access to it."
}
),
404,
)
except Exception as e:
logging.exception("Exception in /history/message_feedback")
return jsonify({"error": str(e)}), 500
@bp.route("/history/delete", methods=["DELETE"])
async def delete_conversation():
await cosmos_db_ready.wait()
## get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
## delete the conversation messages from cosmos first
deleted_messages = await current_app.cosmos_conversation_client.delete_messages(
conversation_id, user_id
)
## Now delete the conversation
deleted_conversation = await current_app.cosmos_conversation_client.delete_conversation(
user_id, conversation_id
)
return (
jsonify(
{
"message": "Successfully deleted conversation and messages",
"conversation_id": conversation_id,
}
),
200,
)
except Exception as e:
logging.exception("Exception in /history/delete")
return jsonify({"error": str(e)}), 500
@bp.route("/history/list", methods=["GET"])
async def list_conversations():
await cosmos_db_ready.wait()
offset = request.args.get("offset", 0)
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
## get the conversations from cosmos
conversations = await current_app.cosmos_conversation_client.get_conversations(
user_id, offset=offset, limit=25
)
if not isinstance(conversations, list):
return jsonify({"error": f"No conversations for {user_id} were found"}), 404
## return the conversation ids
return jsonify(conversations), 200
@bp.route("/history/read", methods=["POST"])
async def get_conversation():
await cosmos_db_ready.wait()
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
## get the conversation object and the related messages from cosmos
conversation = await current_app.cosmos_conversation_client.get_conversation(
user_id, conversation_id
)
## return the conversation id and the messages in the bot frontend format
if not conversation:
return (
jsonify(
{
"error": f"Conversation {conversation_id} was not found. It either does not exist or the logged in user does not have access to it."
}
),
404,
)
# get the messages for the conversation from cosmos
conversation_messages = await current_app.cosmos_conversation_client.get_messages(
user_id, conversation_id
)
## format the messages in the bot frontend format
messages = [
{
"id": msg["id"],
"role": msg["role"],
"content": msg["content"],
"createdAt": msg["createdAt"],
"feedback": msg.get("feedback"),
}
for msg in conversation_messages
]
return jsonify({"conversation_id": conversation_id, "messages": messages}), 200
@bp.route("/history/rename", methods=["POST"])
async def rename_conversation():
await cosmos_db_ready.wait()
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
## get the conversation from cosmos
conversation = await current_app.cosmos_conversation_client.get_conversation(
user_id, conversation_id
)
if not conversation:
return (
jsonify(
{
"error": f"Conversation {conversation_id} was not found. It either does not exist or the logged in user does not have access to it."
}
),
404,
)
## update the title
title = request_json.get("title", None)
if not title:
return jsonify({"error": "title is required"}), 400
conversation["title"] = title
updated_conversation = await current_app.cosmos_conversation_client.upsert_conversation(
conversation
)
return jsonify(updated_conversation), 200
@bp.route("/history/delete_all", methods=["DELETE"])
async def delete_all_conversations():
await cosmos_db_ready.wait()
## get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
# get conversations for user
try:
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
conversations = await current_app.cosmos_conversation_client.get_conversations(
user_id, offset=0, limit=None
)
if not conversations:
return jsonify({"error": f"No conversations for {user_id} were found"}), 404
# delete each conversation
for conversation in conversations:
## delete the conversation messages from cosmos first
deleted_messages = await current_app.cosmos_conversation_client.delete_messages(
conversation["id"], user_id
)
## Now delete the conversation
deleted_conversation = await current_app.cosmos_conversation_client.delete_conversation(
user_id, conversation["id"]
)
return (
jsonify(
{
"message": f"Successfully deleted conversation and messages for user {user_id}"
}
),
200,
)
except Exception as e:
logging.exception("Exception in /history/delete_all")
return jsonify({"error": str(e)}), 500
@bp.route("/history/clear", methods=["POST"])
async def clear_messages():
await cosmos_db_ready.wait()
## get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
## check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)
try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## make sure cosmos is configured
if not current_app.cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
## delete the conversation messages from cosmos
deleted_messages = await current_app.cosmos_conversation_client.delete_messages(
conversation_id, user_id
)
return (
jsonify(
{
"message": "Successfully deleted messages in conversation",
"conversation_id": conversation_id,
}
),
200,
)
except Exception as e:
logging.exception("Exception in /history/clear_messages")
return jsonify({"error": str(e)}), 500
@bp.route("/history/ensure", methods=["GET"])
async def ensure_cosmos():
await cosmos_db_ready.wait()
if not app_settings.chat_history:
return jsonify({"error": "CosmosDB is not configured"}), 404
try:
success, err = await current_app.cosmos_conversation_client.ensure()
if not current_app.cosmos_conversation_client or not success:
if err:
return jsonify({"error": err}), 422
return jsonify({"error": "CosmosDB is not configured or not working"}), 500
return jsonify({"message": "CosmosDB is configured and working"}), 200
except Exception as e:
logging.exception("Exception in /history/ensure")
cosmos_exception = str(e)
if "Invalid credentials" in cosmos_exception:
return jsonify({"error": cosmos_exception}), 401
elif "Invalid CosmosDB database name" in cosmos_exception:
return (
jsonify(
{
"error": f"{cosmos_exception} {app_settings.chat_history.database} for account {app_settings.chat_history.account}"
}
),
422,
)
elif "Invalid CosmosDB container name" in cosmos_exception:
return (
jsonify(
{
"error": f"{cosmos_exception}: {app_settings.chat_history.conversations_container}"
}
),
422,
)
else:
return jsonify({"error": "CosmosDB is not working"}), 500
async def generate_title(conversation_messages) -> str:
## make sure the messages are sorted by _ts descending
title_prompt = "Summarize the conversation so far into a 4-word or less title. Do not use any quotation marks or punctuation. Do not include any other commentary or description."
messages = [
{"role": msg["role"], "content": msg["content"]}
for msg in conversation_messages
]
messages.append({"role": "user", "content": title_prompt})
try:
azure_openai_client = await init_openai_client()
response = await azure_openai_client.chat.completions.create(
model=app_settings.azure_openai.model, messages=messages, temperature=1, max_tokens=64
)
title = response.choices[0].message.content
return title
except Exception as e:
logging.exception("Exception while generating title", e)
return messages[-2]["content"]
app = create_app()