forked from microsoft/sample-app-aoai-chatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
545 lines (446 loc) · 24 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
import json
import os
import logging
from backend.conversationtelemetry import ConversationTelemetryClient
import openai
import uuid
import aiohttp
from azure.identity import DefaultAzureCredential
from flask import Flask, request, jsonify, send_from_directory, render_template
from dotenv import load_dotenv
from azure.cognitiveservices.speech import SpeechConfig
from backend.auth.auth_utils import get_authenticated_user_details
from backend.history.cosmosdbservice import CosmosConversationClient
from backend.appinsightsmiddleware import AppInsightsMiddleware
from backend.orchestrators.utils import create_orchestrator_instance
load_dotenv()
app = Flask(__name__, static_folder="static", template_folder="static")
AppInsightsMiddleware(app)
CUSTOM_ORCHESTRATOR_CLASS_NAME = os.environ.get("CUSTOM_ORCHESTRATOR_CLASS_NAME") or "DefaultOrchestrator"
orchestrator = create_orchestrator_instance(CUSTOM_ORCHESTRATOR_CLASS_NAME)
# Static Files
@app.route("/")
def index():
AppConfig = {
"REACT_APP_THEME": os.environ.get("REACT_APP_THEME", "light"),
"REACT_APP_SITE_TITLE": os.environ.get("REACT_APP_SITE_TITLE", "MSR Copilot"),
}
# Render the React's index.html with additional context
return render_template('index.html', app_config=AppConfig)
@app.route("/favicon.ico")
def favicon():
return app.send_static_file('favicon.ico')
@app.route("/assets/<path:path>")
def assets(path):
return send_from_directory("static/assets", path)
# Debug settings
DEBUG = os.environ.get("DEBUG", "false")
DEBUG_LOGGING = DEBUG.lower() == "true"
if DEBUG_LOGGING:
logging.basicConfig(level=logging.DEBUG)
# On Your Data Settings
SEARCH_TOP_K = os.environ.get("SEARCH_TOP_K", 5)
SEARCH_STRICTNESS = os.environ.get("SEARCH_STRICTNESS", 3)
SEARCH_ENABLE_IN_DOMAIN = os.environ.get("SEARCH_ENABLE_IN_DOMAIN", "true")
# ACS Integration Settings
AZURE_SEARCH_SERVICE = os.environ.get("AZURE_SEARCH_SERVICE")
AZURE_SEARCH_INDEX = os.environ.get("AZURE_SEARCH_INDEX")
AZURE_SEARCH_TOP_K = os.environ.get("AZURE_SEARCH_TOP_K", SEARCH_TOP_K)
AZURE_SEARCH_ENABLE_IN_DOMAIN = os.environ.get("AZURE_SEARCH_ENABLE_IN_DOMAIN", SEARCH_ENABLE_IN_DOMAIN)
AZURE_SEARCH_CONTENT_COLUMNS = os.environ.get("AZURE_SEARCH_CONTENT_COLUMNS")
AZURE_SEARCH_FILENAME_COLUMN = os.environ.get("AZURE_SEARCH_FILENAME_COLUMN")
AZURE_SEARCH_TITLE_COLUMN = os.environ.get("AZURE_SEARCH_TITLE_COLUMN")
AZURE_SEARCH_URL_COLUMN = os.environ.get("AZURE_SEARCH_URL_COLUMN")
AZURE_SEARCH_VECTOR_COLUMNS = os.environ.get("AZURE_SEARCH_VECTOR_COLUMNS")
AZURE_SEARCH_QUERY_TYPE = os.environ.get("AZURE_SEARCH_QUERY_TYPE")
AZURE_SEARCH_PERMITTED_GROUPS_COLUMN = os.environ.get("AZURE_SEARCH_PERMITTED_GROUPS_COLUMN")
# AOAI Integration Settings
AZURE_OPENAI_RESOURCE = os.environ.get("AZURE_OPENAI_RESOURCE")
AZURE_OPENAI_MODEL = os.environ.get("AZURE_OPENAI_MODEL")
AZURE_OPENAI_ENDPOINT = os.environ.get("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_KEY = os.environ.get("AZURE_OPENAI_KEY")
AZURE_OPENAI_MODEL_NAME = os.environ.get("AZURE_OPENAI_MODEL_NAME", "gpt-35-turbo-16k") # Name of the model, e.g. 'gpt-35-turbo-16k' or 'gpt-4'
# CosmosDB Mongo vcore vector db Settings
AZURE_COSMOSDB_MONGO_VCORE_CONNECTION_STRING = os.environ.get("AZURE_COSMOSDB_MONGO_VCORE_CONNECTION_STRING") #This has to be secure string
AZURE_COSMOSDB_MONGO_VCORE_DATABASE = os.environ.get("AZURE_COSMOSDB_MONGO_VCORE_DATABASE")
AZURE_COSMOSDB_MONGO_VCORE_CONTAINER = os.environ.get("AZURE_COSMOSDB_MONGO_VCORE_CONTAINER")
AZURE_COSMOSDB_MONGO_VCORE_INDEX = os.environ.get("AZURE_COSMOSDB_MONGO_VCORE_INDEX")
# MSR CosmosDB Settings
MSR_AZURE_COSMOSDB_ACCOUNT = os.environ.get("MSR_AZURE_COSMOSDB_ACCOUNT")
MSR_AZURE_COSMOSDB_FEEDBACK_CONTAINER="feedback"
MSR_AZURE_COSMOSDB_DATABASE=os.environ.get("MSR_AZURE_COSMOSDB_DATABASE")
MSR_AZURE_COSMOSDB_FEEDBACK_ENABLED = os.environ.get("MSR_AZURE_COSMOSDB_FEEDBACK_ENABLED", "false").lower() == "true"
# Chat History CosmosDB Integration Settings
AZURE_COSMOSDB_DATABASE = os.environ.get("AZURE_COSMOSDB_DATABASE")
AZURE_COSMOSDB_ACCOUNT = os.environ.get("AZURE_COSMOSDB_ACCOUNT")
AZURE_COSMOSDB_CONVERSATIONS_CONTAINER = os.environ.get("AZURE_COSMOSDB_CONVERSATIONS_CONTAINER")
AZURE_COSMOSDB_ENABLE_FEEDBACK = os.environ.get("AZURE_COSMOSDB_ENABLE_FEEDBACK", "false").lower() == "true"
# Speech
AZURE_SPEECH_REGION = os.environ.get("AZURE_SPEECH_REGION")
AZURE_SPEECH_TOKEN_ENDPOINT = os.environ.get("AZURE_SPEECH_TOKEN_ENDPOINT")
AZURE_SPEECH_RESOURCE_ID = os.environ.get("AZURE_SPEECH_RESOURCE_ID")
# Frontend Settings via Environment Variables
AUTH_ENABLED = os.environ.get("AUTH_ENABLED", "true").lower() == "true"
SPEECH_ENABLED = os.environ.get("AZURE_SPEECH_ENABLED", "false").lower() == "true"
REACT_APP_SITE_TITLE = os.environ.get("REACT_APP_SITE_TITLE", "MSR Coplilot")
REACT_APP_FRONTPAGE_HEADING = os.environ.get("REACT_APP_FRONTPAGE_HEADING", "Welcome to MSR Copilot")
REACT_APP_FRONTPAGE_SUBHEADING = os.environ.get("REACT_APP_FRONTPAGE_SUBHEADING", "")
REACT_APP_FRONTPAGE_LINKS = os.environ.get("REACT_APP_FRONTPAGE_LINKS", "[]")
REACT_APP_FRONTPAGE_QUESTIONS = os.environ.get("REACT_APP_FRONTPAGE_QUESTIONS", "[]")
REACT_APP_FRONTPAGE_SHOW_IMAGE = os.environ.get("REACT_APP_FRONTPAGE_SHOW_IMAGE", "true").lower() == "true"
REACT_APP_FRONTPAGE_IMAGE_URL = os.environ.get("REACT_APP_FRONTPAGE_IMAGE_URL", "")
REACT_APP_FRONTPAGE_VERTICAL_QUESTIONS = os.environ.get("REACT_APP_FRONTPAGE_VERTICAL_QUESTIONS", "true").lower() == "true"
REACT_APP_FRONTPAGE_QUESTION_HEADING= os.environ.get("REACT_APP_FRONTPAGE_QUESTION_HEADING", "")
REACT_APP_INPUT_PLACEHOLDER= os.environ.get("REACT_APP_INPUT_PLACEHOLDER", "Ask a question...")
REACT_APP_CONTACT_US_LINK = os.environ.get("REACT_APP_CONTACT_US_LINK", "")
REACT_APP_SUBMIT_FEEDBACK_URL = os.environ.get("REACT_APP_SUBMIT_FEEDBACK_URL", "")
credential = DefaultAzureCredential()
# FRONT_PAGE_LINKS = json.loads(REACT_APP_FRONTPAGE_LINKS) AND CHECK IF IT IS A VALID JSON OBJECT. IF NOT SET IT TO EMPTY LIST
try:
FRONT_PAGE_LINKS = json.loads(REACT_APP_FRONTPAGE_LINKS)
except:
FRONT_PAGE_LINKS = []
# FRONT_PAGE_QUESTIONS = json.loads(REACT_APP_FRONTPAGE_QUESTIONS) AND CHECK IF IT IS A VALID JSON OBJECT. IF NOT SET IT TO EMPTY LIST
try:
FRONTPAGE_QUESTIONS = json.loads(REACT_APP_FRONTPAGE_QUESTIONS)
except:
FRONTPAGE_QUESTIONS = []
frontend_settings = {
"auth_enabled": AUTH_ENABLED,
"feedback_enabled": AZURE_COSMOSDB_ENABLE_FEEDBACK and AZURE_COSMOSDB_DATABASE not in [None, ""],
"speech_enabled": SPEECH_ENABLED,
"msr_feedback_enabled": MSR_AZURE_COSMOSDB_FEEDBACK_ENABLED,
"site_title": REACT_APP_SITE_TITLE,
"frontpage_heading": REACT_APP_FRONTPAGE_HEADING,
"frontpage_subheading": REACT_APP_FRONTPAGE_SUBHEADING,
"frontpage_links": FRONT_PAGE_LINKS,
"frontpage_questions": FRONTPAGE_QUESTIONS,
"frontpage_show_image": REACT_APP_FRONTPAGE_SHOW_IMAGE,
"frontpage_image_url": REACT_APP_FRONTPAGE_IMAGE_URL,
"frontpage_vertical_questions": REACT_APP_FRONTPAGE_VERTICAL_QUESTIONS,
"frontpage_question_heading": REACT_APP_FRONTPAGE_QUESTION_HEADING,
"input_placeholder": REACT_APP_INPUT_PLACEHOLDER,
"contact_us_link": REACT_APP_CONTACT_US_LINK,
"submit_feedback_url": REACT_APP_SUBMIT_FEEDBACK_URL
}
message_uuid = ""
# Initialize a CosmosDB client with AAD auth and containers for Chat History
cosmos_conversation_client = None
if AZURE_COSMOSDB_DATABASE and AZURE_COSMOSDB_ACCOUNT and AZURE_COSMOSDB_CONVERSATIONS_CONTAINER:
try :
cosmos_endpoint = f'https://{AZURE_COSMOSDB_ACCOUNT}.documents.azure.com:443/'
cosmos_conversation_client = CosmosConversationClient(
cosmosdb_endpoint=cosmos_endpoint,
credential=credential,
database_name=AZURE_COSMOSDB_DATABASE,
container_name=AZURE_COSMOSDB_CONVERSATIONS_CONTAINER,
enable_message_feedback = AZURE_COSMOSDB_ENABLE_FEEDBACK
)
except Exception as e:
logging.exception("Exception in CosmosDB initialization", e)
cosmos_conversation_client = None
# Initialize MSR CosmosDB client for feedback
msr_cosmos_db_client = None
if MSR_AZURE_COSMOSDB_ACCOUNT and MSR_AZURE_COSMOSDB_DATABASE:
try:
msr_cosmos_db_client = ConversationTelemetryClient(
cosmosdb_endpoint=f'https://{MSR_AZURE_COSMOSDB_ACCOUNT}.documents.azure.com:443/',
credential=credential,
database_name=MSR_AZURE_COSMOSDB_DATABASE,
container_name=MSR_AZURE_COSMOSDB_FEEDBACK_CONTAINER
)
except Exception as e:
logging.exception("Exception in MSR CosmosDB initialization", e)
msr_cosmos_db_client = None
def is_chat_model():
if 'gpt-4' in AZURE_OPENAI_MODEL_NAME.lower() or AZURE_OPENAI_MODEL_NAME.lower() in ['gpt-35-turbo-4k', 'gpt-35-turbo-16k']:
return True
return False
def should_use_data():
if AZURE_SEARCH_SERVICE and AZURE_SEARCH_INDEX:
if DEBUG_LOGGING:
logging.debug("Using Azure Cognitive Search")
return True
if AZURE_COSMOSDB_MONGO_VCORE_DATABASE and AZURE_COSMOSDB_MONGO_VCORE_CONTAINER and AZURE_COSMOSDB_MONGO_VCORE_INDEX and AZURE_COSMOSDB_MONGO_VCORE_CONNECTION_STRING:
if DEBUG_LOGGING:
logging.debug("Using Azure CosmosDB Mongo vcore")
return True
return False
@app.route("/conversation", methods=["GET", "POST"])
def conversation():
message_uuid = str(uuid.uuid4())
request_body = request.json
return conversation_internal(request_body, message_uuid)
def conversation_internal(request_body, message_uuid):
try:
use_data = should_use_data()
if use_data:
return orchestrator.conversation_with_data(request_body, message_uuid)
else:
return orchestrator.conversation_without_data(request_body, message_uuid)
except Exception as e:
logging.exception("Exception in /conversation")
return jsonify({"error": str(e)}), 500
## Conversation History API ##
@app.route("/history/generate", methods=["POST"])
def add_conversation():
global message_uuid
message_uuid = str(uuid.uuid4())
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## check request for conversation_id
conversation_id = request.json.get("conversation_id", None)
try:
# make sure cosmos is configured
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured")
# check for the conversation_id, if the conversation is not set, we will create a new one
history_metadata = {}
if not conversation_id:
title = generate_title(request.json["messages"])
conversation_dict = 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":
cosmos_conversation_client.create_message(
uuid=str(uuid.uuid4()),
conversation_id=conversation_id,
user_id=user_id,
input_message=messages[-1]
)
else:
raise Exception("No user message found")
# Submit request to Chat Completions for response
request_body = request.json
history_metadata['conversation_id'] = conversation_id
request_body['history_metadata'] = history_metadata
return conversation_internal(request_body, message_uuid)
except Exception as e:
logging.exception("Exception in /history/generate")
return jsonify({"error": str(e)}), 500
@app.route("/history/update", methods=["POST"])
def update_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## check request for conversation_id
conversation_id = request.json.get("conversation_id", None)
try:
# make sure cosmos is configured
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured")
# 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
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
cosmos_conversation_client.create_message(
uuid=message_uuid,
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
@app.route("/history/message_feedback", methods=["POST"])
def update_message():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## check request for message_id
message_id = request.json.get("message_id", None)
message_feedback = request.json.get("message_feedback", None)
## check request for the optional value msr_feedback being true
msr_feedback = request.json.get("msr_feedback", False)
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
## if msr_feedback is true, write the feedback to the MSR CosmosDB
if msr_feedback and msr_cosmos_db_client:
msr_cosmos_db_client.upsert_feedback(user_id, message_id, message_feedback)
return jsonify({"message": f"Successfully added feedback to message {message_id} in MSR CosmosDB"}), 200
## update the message in cosmos
updated_message = 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
@app.route("/history/delete", methods=["DELETE"])
def delete_conversation():
## 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
conversation_id = request.json.get("conversation_id", None)
try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## delete the conversation messages from cosmos first
deleted_messages = cosmos_conversation_client.delete_messages(conversation_id, user_id)
## Now delete the conversation
deleted_conversation = 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
@app.route("/history/list", methods=["GET"])
def list_conversations():
offset = request.args.get("offset", 0)
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## get the conversations from cosmos
conversations = 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
@app.route("/history/read", methods=["POST"])
def get_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## check request for conversation_id
conversation_id = request.json.get("conversation_id", None)
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## get the conversation object and the related messages from cosmos
conversation = 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 = 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
@app.route("/history/rename", methods=["POST"])
def rename_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user['user_principal_id']
## check request for conversation_id
conversation_id = request.json.get("conversation_id", None)
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## get the conversation from cosmos
conversation = 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 = cosmos_conversation_client.upsert_conversation(conversation)
return jsonify(updated_conversation), 200
@app.route("/history/delete_all", methods=["DELETE"])
def delete_all_conversations():
## 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:
conversations = 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 = cosmos_conversation_client.delete_messages(conversation['id'], user_id)
## Now delete the conversation
deleted_conversation = 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
@app.route("/history/clear", methods=["POST"])
def clear_messages():
## 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
conversation_id = request.json.get("conversation_id", None)
try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400
## delete the conversation messages from cosmos
deleted_messages = 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
@app.route("/history/ensure", methods=["GET"])
def ensure_cosmos():
if not AZURE_COSMOSDB_ACCOUNT:
return jsonify({"error": "CosmosDB is not configured"}), 404
if not cosmos_conversation_client or not cosmos_conversation_client.ensure():
return jsonify({"error": "CosmosDB is not working"}), 500
return jsonify({"message": "CosmosDB is configured and working"}), 200
@app.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
@app.route("/speech/issueToken", methods=["GET"])
async def speech_issue_token():
if not AZURE_SPEECH_REGION:
return jsonify({"error": "Azure Speech region is not configured"}), 404
try:
credential = DefaultAzureCredential()
token = credential.get_token(AZURE_SPEECH_TOKEN_ENDPOINT).token
authorizationToken = "aad#" + AZURE_SPEECH_RESOURCE_ID + "#" + token
return jsonify({"access_token": authorizationToken, "region": AZURE_SPEECH_REGION}), 200
except Exception:
logging.exception("Exception in /speech/issueToken")
return jsonify({"error": "Azure Speech is not working."}), 500
def generate_title(conversation_messages):
## 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. Respond with a json object in the format {{"title": string}}. 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:
## Submit prompt to Chat Completions for response
base_url = AZURE_OPENAI_ENDPOINT if AZURE_OPENAI_ENDPOINT else f"https://{AZURE_OPENAI_RESOURCE}.openai.azure.com/"
openai.api_type = "azure"
openai.api_base = base_url
openai.api_version = "2023-03-15-preview"
openai.api_key = AZURE_OPENAI_KEY
completion = openai.ChatCompletion.create(
engine=AZURE_OPENAI_MODEL,
messages=messages,
temperature=1,
max_tokens=64
)
title = json.loads(completion['choices'][0]['message']['content'])['title']
return title
except Exception as e:
return messages[-2]['content']
if __name__ == "__main__":
if DEBUG.lower() == "true":
app.run(debug=True, use_debugger=True, use_reloader=True)
else:
app.run()