-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathapp.py
815 lines (740 loc) · 38.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
import dataclasses
import io
import json
import logging
import mimetypes
import os
import time
from pathlib import Path
from typing import Any, AsyncGenerator, Dict, Union, cast
from azure.cognitiveservices.speech import (
ResultReason,
SpeechConfig,
SpeechSynthesisOutputFormat,
SpeechSynthesisResult,
SpeechSynthesizer,
)
from azure.core.exceptions import ResourceNotFoundError
from azure.identity.aio import (
AzureDeveloperCliCredential,
ManagedIdentityCredential,
get_bearer_token_provider,
)
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.search.documents.aio import SearchClient
from azure.search.documents.indexes.aio import SearchIndexClient
from azure.storage.blob.aio import ContainerClient
from azure.storage.blob.aio import StorageStreamDownloader as BlobDownloader
from azure.storage.filedatalake.aio import FileSystemClient
from azure.storage.filedatalake.aio import StorageStreamDownloader as DatalakeDownloader
from openai import AsyncAzureOpenAI, AsyncOpenAI
from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from opentelemetry.instrumentation.httpx import (
HTTPXClientInstrumentor,
)
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
from quart import (
Blueprint,
Quart,
abort,
current_app,
jsonify,
make_response,
request,
send_file,
send_from_directory,
)
from quart_cors import cors
from approaches.approach import Approach
from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach
from approaches.chatreadretrievereadvision import ChatReadRetrieveReadVisionApproach
from approaches.promptmanager import PromptyManager
from approaches.retrievethenread import RetrieveThenReadApproach
from approaches.retrievethenreadvision import RetrieveThenReadVisionApproach
from chat_history.cosmosdb import chat_history_cosmosdb_bp
from config import (
CONFIG_ASK_APPROACH,
CONFIG_ASK_VISION_APPROACH,
CONFIG_AUTH_CLIENT,
CONFIG_BLOB_CONTAINER_CLIENT,
CONFIG_CHAT_APPROACH,
CONFIG_CHAT_HISTORY_BROWSER_ENABLED,
CONFIG_CHAT_HISTORY_COSMOS_ENABLED,
CONFIG_CHAT_VISION_APPROACH,
CONFIG_CREDENTIAL,
CONFIG_DEFAULT_REASONING_EFFORT,
CONFIG_GPT4V_DEPLOYED,
CONFIG_INGESTER,
CONFIG_LANGUAGE_PICKER_ENABLED,
CONFIG_OPENAI_CLIENT,
CONFIG_QUERY_REWRITING_ENABLED,
CONFIG_REASONING_EFFORT_ENABLED,
CONFIG_SEARCH_CLIENT,
CONFIG_SEMANTIC_RANKER_DEPLOYED,
CONFIG_SPEECH_INPUT_ENABLED,
CONFIG_SPEECH_OUTPUT_AZURE_ENABLED,
CONFIG_SPEECH_OUTPUT_BROWSER_ENABLED,
CONFIG_SPEECH_SERVICE_ID,
CONFIG_SPEECH_SERVICE_LOCATION,
CONFIG_SPEECH_SERVICE_TOKEN,
CONFIG_SPEECH_SERVICE_VOICE,
CONFIG_STREAMING_ENABLED,
CONFIG_USER_BLOB_CONTAINER_CLIENT,
CONFIG_USER_UPLOAD_ENABLED,
CONFIG_VECTOR_SEARCH_ENABLED,
)
from core.authentication import AuthenticationHelper
from core.sessionhelper import create_session_id
from decorators import authenticated, authenticated_path
from error import error_dict, error_response
from prepdocs import (
clean_key_if_exists,
setup_embeddings_service,
setup_file_processors,
setup_search_info,
)
from prepdocslib.filestrategy import UploadUserFileStrategy
from prepdocslib.listfilestrategy import File
bp = Blueprint("routes", __name__, static_folder="static")
# Fix Windows registry issue with mimetypes
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("text/css", ".css")
@bp.route("/")
async def index():
return await bp.send_static_file("index.html")
# Empty page is recommended for login redirect to work.
# See https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/initialization.md#redirecturi-considerations for more information
@bp.route("/redirect")
async def redirect():
return ""
@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(Path(__file__).resolve().parent / "static" / "assets", path)
@bp.route("/content/<path>")
@authenticated_path
async def content_file(path: str, auth_claims: Dict[str, Any]):
"""
Serve content files from blob storage from within the app to keep the example self-contained.
*** NOTE *** if you are using app services authentication, this route will return unauthorized to all users that are not logged in
if AZURE_ENFORCE_ACCESS_CONTROL is not set or false, logged in users can access all files regardless of access control
if AZURE_ENFORCE_ACCESS_CONTROL is set to true, logged in users can only access files they have access to
This is also slow and memory hungry.
"""
# Remove page number from path, filename-1.txt -> filename.txt
# This shouldn't typically be necessary as browsers don't send hash fragments to servers
if path.find("#page=") > 0:
path_parts = path.rsplit("#page=", 1)
path = path_parts[0]
current_app.logger.info("Opening file %s", path)
blob_container_client: ContainerClient = current_app.config[CONFIG_BLOB_CONTAINER_CLIENT]
blob: Union[BlobDownloader, DatalakeDownloader]
try:
blob = await blob_container_client.get_blob_client(path).download_blob()
except ResourceNotFoundError:
current_app.logger.info("Path not found in general Blob container: %s", path)
if current_app.config[CONFIG_USER_UPLOAD_ENABLED]:
try:
user_oid = auth_claims["oid"]
user_blob_container_client = current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT]
user_directory_client: FileSystemClient = user_blob_container_client.get_directory_client(user_oid)
file_client = user_directory_client.get_file_client(path)
blob = await file_client.download_file()
except ResourceNotFoundError:
current_app.logger.exception("Path not found in DataLake: %s", path)
abort(404)
else:
abort(404)
if not blob.properties or not blob.properties.has_key("content_settings"):
abort(404)
mime_type = blob.properties["content_settings"]["content_type"]
if mime_type == "application/octet-stream":
mime_type = mimetypes.guess_type(path)[0] or "application/octet-stream"
blob_file = io.BytesIO()
await blob.readinto(blob_file)
blob_file.seek(0)
return await send_file(blob_file, mimetype=mime_type, as_attachment=False, attachment_filename=path)
@bp.route("/ask", methods=["POST"])
@authenticated
async def ask(auth_claims: Dict[str, Any]):
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
request_json = await request.get_json()
context = request_json.get("context", {})
context["auth_claims"] = auth_claims
try:
use_gpt4v = context.get("overrides", {}).get("use_gpt4v", False)
approach: Approach
if use_gpt4v and CONFIG_ASK_VISION_APPROACH in current_app.config:
approach = cast(Approach, current_app.config[CONFIG_ASK_VISION_APPROACH])
else:
approach = cast(Approach, current_app.config[CONFIG_ASK_APPROACH])
r = await approach.run(
request_json["messages"], context=context, session_state=request_json.get("session_state")
)
return jsonify(r)
except Exception as error:
return error_response(error, "/ask")
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if dataclasses.is_dataclass(o) and not isinstance(o, type):
return dataclasses.asdict(o)
return super().default(o)
async def format_as_ndjson(r: AsyncGenerator[dict, None]) -> AsyncGenerator[str, None]:
try:
async for event in r:
yield json.dumps(event, ensure_ascii=False, cls=JSONEncoder) + "\n"
except Exception as error:
logging.exception("Exception while generating response stream: %s", error)
yield json.dumps(error_dict(error))
@bp.route("/chat", methods=["POST"])
@authenticated
async def chat(auth_claims: Dict[str, Any]):
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
request_json = await request.get_json()
context = request_json.get("context", {})
context["auth_claims"] = auth_claims
try:
use_gpt4v = context.get("overrides", {}).get("use_gpt4v", False)
approach: Approach
if use_gpt4v and CONFIG_CHAT_VISION_APPROACH in current_app.config:
approach = cast(Approach, current_app.config[CONFIG_CHAT_VISION_APPROACH])
else:
approach = cast(Approach, current_app.config[CONFIG_CHAT_APPROACH])
# If session state is provided, persists the session state,
# else creates a new session_id depending on the chat history options enabled.
session_state = request_json.get("session_state")
if session_state is None:
session_state = create_session_id(
current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED],
current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED],
)
result = await approach.run(
request_json["messages"],
context=context,
session_state=session_state,
)
return jsonify(result)
except Exception as error:
return error_response(error, "/chat")
@bp.route("/chat/stream", methods=["POST"])
@authenticated
async def chat_stream(auth_claims: Dict[str, Any]):
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
request_json = await request.get_json()
context = request_json.get("context", {})
context["auth_claims"] = auth_claims
try:
use_gpt4v = context.get("overrides", {}).get("use_gpt4v", False)
approach: Approach
if use_gpt4v and CONFIG_CHAT_VISION_APPROACH in current_app.config:
approach = cast(Approach, current_app.config[CONFIG_CHAT_VISION_APPROACH])
else:
approach = cast(Approach, current_app.config[CONFIG_CHAT_APPROACH])
# If session state is provided, persists the session state,
# else creates a new session_id depending on the chat history options enabled.
session_state = request_json.get("session_state")
if session_state is None:
session_state = create_session_id(
current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED],
current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED],
)
result = await approach.run_stream(
request_json["messages"],
context=context,
session_state=session_state,
)
response = await make_response(format_as_ndjson(result))
response.timeout = None # type: ignore
response.mimetype = "application/json-lines"
return response
except Exception as error:
return error_response(error, "/chat")
# Send MSAL.js settings to the client UI
@bp.route("/auth_setup", methods=["GET"])
def auth_setup():
auth_helper = current_app.config[CONFIG_AUTH_CLIENT]
return jsonify(auth_helper.get_auth_setup_for_client())
@bp.route("/config", methods=["GET"])
def config():
return jsonify(
{
"showGPT4VOptions": current_app.config[CONFIG_GPT4V_DEPLOYED],
"showSemanticRankerOption": current_app.config[CONFIG_SEMANTIC_RANKER_DEPLOYED],
"showQueryRewritingOption": current_app.config[CONFIG_QUERY_REWRITING_ENABLED],
"showReasoningEffortOption": current_app.config[CONFIG_REASONING_EFFORT_ENABLED],
"streamingEnabled": current_app.config[CONFIG_STREAMING_ENABLED],
"defaultReasoningEffort": current_app.config[CONFIG_DEFAULT_REASONING_EFFORT],
"showVectorOption": current_app.config[CONFIG_VECTOR_SEARCH_ENABLED],
"showUserUpload": current_app.config[CONFIG_USER_UPLOAD_ENABLED],
"showLanguagePicker": current_app.config[CONFIG_LANGUAGE_PICKER_ENABLED],
"showSpeechInput": current_app.config[CONFIG_SPEECH_INPUT_ENABLED],
"showSpeechOutputBrowser": current_app.config[CONFIG_SPEECH_OUTPUT_BROWSER_ENABLED],
"showSpeechOutputAzure": current_app.config[CONFIG_SPEECH_OUTPUT_AZURE_ENABLED],
"showChatHistoryBrowser": current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED],
"showChatHistoryCosmos": current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED],
}
)
@bp.route("/speech", methods=["POST"])
async def speech():
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
speech_token = current_app.config.get(CONFIG_SPEECH_SERVICE_TOKEN)
if speech_token is None or speech_token.expires_on < time.time() + 60:
speech_token = await current_app.config[CONFIG_CREDENTIAL].get_token(
"https://cognitiveservices.azure.com/.default"
)
current_app.config[CONFIG_SPEECH_SERVICE_TOKEN] = speech_token
request_json = await request.get_json()
text = request_json["text"]
try:
# Construct a token as described in documentation:
# https://learn.microsoft.com/azure/ai-services/speech-service/how-to-configure-azure-ad-auth?pivots=programming-language-python
auth_token = (
"aad#"
+ current_app.config[CONFIG_SPEECH_SERVICE_ID]
+ "#"
+ current_app.config[CONFIG_SPEECH_SERVICE_TOKEN].token
)
speech_config = SpeechConfig(auth_token=auth_token, region=current_app.config[CONFIG_SPEECH_SERVICE_LOCATION])
speech_config.speech_synthesis_voice_name = current_app.config[CONFIG_SPEECH_SERVICE_VOICE]
speech_config.speech_synthesis_output_format = SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=None)
result: SpeechSynthesisResult = synthesizer.speak_text_async(text).get()
if result.reason == ResultReason.SynthesizingAudioCompleted:
return result.audio_data, 200, {"Content-Type": "audio/mp3"}
elif result.reason == ResultReason.Canceled:
cancellation_details = result.cancellation_details
current_app.logger.error(
"Speech synthesis canceled: %s %s", cancellation_details.reason, cancellation_details.error_details
)
raise Exception("Speech synthesis canceled. Check logs for details.")
else:
current_app.logger.error("Unexpected result reason: %s", result.reason)
raise Exception("Speech synthesis failed. Check logs for details.")
except Exception as e:
current_app.logger.exception("Exception in /speech")
return jsonify({"error": str(e)}), 500
@bp.post("/upload")
@authenticated
async def upload(auth_claims: dict[str, Any]):
request_files = await request.files
if "file" not in request_files:
# If no files were included in the request, return an error response
return jsonify({"message": "No file part in the request", "status": "failed"}), 400
user_oid = auth_claims["oid"]
file = request_files.getlist("file")[0]
user_blob_container_client: FileSystemClient = current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT]
user_directory_client = user_blob_container_client.get_directory_client(user_oid)
try:
await user_directory_client.get_directory_properties()
except ResourceNotFoundError:
current_app.logger.info("Creating directory for user %s", user_oid)
await user_directory_client.create_directory()
await user_directory_client.set_access_control(owner=user_oid)
file_client = user_directory_client.get_file_client(file.filename)
file_io = file
file_io.name = file.filename
file_io = io.BufferedReader(file_io)
await file_client.upload_data(file_io, overwrite=True, metadata={"UploadedBy": user_oid})
file_io.seek(0)
ingester: UploadUserFileStrategy = current_app.config[CONFIG_INGESTER]
await ingester.add_file(File(content=file_io, acls={"oids": [user_oid]}, url=file_client.url))
return jsonify({"message": "File uploaded successfully"}), 200
@bp.post("/delete_uploaded")
@authenticated
async def delete_uploaded(auth_claims: dict[str, Any]):
request_json = await request.get_json()
filename = request_json.get("filename")
user_oid = auth_claims["oid"]
user_blob_container_client: FileSystemClient = current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT]
user_directory_client = user_blob_container_client.get_directory_client(user_oid)
file_client = user_directory_client.get_file_client(filename)
await file_client.delete_file()
ingester = current_app.config[CONFIG_INGESTER]
await ingester.remove_file(filename, user_oid)
return jsonify({"message": f"File {filename} deleted successfully"}), 200
@bp.get("/list_uploaded")
@authenticated
async def list_uploaded(auth_claims: dict[str, Any]):
user_oid = auth_claims["oid"]
user_blob_container_client: FileSystemClient = current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT]
files = []
try:
all_paths = user_blob_container_client.get_paths(path=user_oid)
async for path in all_paths:
files.append(path.name.split("/", 1)[1])
except ResourceNotFoundError as error:
if error.status_code != 404:
current_app.logger.exception("Error listing uploaded files", error)
return jsonify(files), 200
@bp.before_app_serving
async def setup_clients():
# Replace these with your own values, either in environment variables or directly here
AZURE_STORAGE_ACCOUNT = os.environ["AZURE_STORAGE_ACCOUNT"]
AZURE_STORAGE_CONTAINER = os.environ["AZURE_STORAGE_CONTAINER"]
AZURE_USERSTORAGE_ACCOUNT = os.environ.get("AZURE_USERSTORAGE_ACCOUNT")
AZURE_USERSTORAGE_CONTAINER = os.environ.get("AZURE_USERSTORAGE_CONTAINER")
AZURE_SEARCH_SERVICE = os.environ["AZURE_SEARCH_SERVICE"]
AZURE_SEARCH_INDEX = os.environ["AZURE_SEARCH_INDEX"]
# Shared by all OpenAI deployments
OPENAI_HOST = os.getenv("OPENAI_HOST", "azure")
OPENAI_CHATGPT_MODEL = os.environ["AZURE_OPENAI_CHATGPT_MODEL"]
OPENAI_EMB_MODEL = os.getenv("AZURE_OPENAI_EMB_MODEL_NAME", "text-embedding-ada-002")
OPENAI_EMB_DIMENSIONS = int(os.getenv("AZURE_OPENAI_EMB_DIMENSIONS") or 1536)
OPENAI_REASONING_EFFORT = os.getenv("AZURE_OPENAI_REASONING_EFFORT")
# Used with Azure OpenAI deployments
AZURE_OPENAI_SERVICE = os.getenv("AZURE_OPENAI_SERVICE")
AZURE_OPENAI_GPT4V_DEPLOYMENT = os.environ.get("AZURE_OPENAI_GPT4V_DEPLOYMENT")
AZURE_OPENAI_GPT4V_MODEL = os.environ.get("AZURE_OPENAI_GPT4V_MODEL")
AZURE_OPENAI_CHATGPT_DEPLOYMENT = (
os.getenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT") if OPENAI_HOST.startswith("azure") else None
)
AZURE_OPENAI_EMB_DEPLOYMENT = os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT") if OPENAI_HOST.startswith("azure") else None
AZURE_OPENAI_CUSTOM_URL = os.getenv("AZURE_OPENAI_CUSTOM_URL")
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-10-21"
AZURE_VISION_ENDPOINT = os.getenv("AZURE_VISION_ENDPOINT", "")
# Used only with non-Azure OpenAI deployments
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_ORGANIZATION = os.getenv("OPENAI_ORGANIZATION")
AZURE_TENANT_ID = os.getenv("AZURE_TENANT_ID")
AZURE_USE_AUTHENTICATION = os.getenv("AZURE_USE_AUTHENTICATION", "").lower() == "true"
AZURE_ENFORCE_ACCESS_CONTROL = os.getenv("AZURE_ENFORCE_ACCESS_CONTROL", "").lower() == "true"
AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS = os.getenv("AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS", "").lower() == "true"
AZURE_ENABLE_UNAUTHENTICATED_ACCESS = os.getenv("AZURE_ENABLE_UNAUTHENTICATED_ACCESS", "").lower() == "true"
AZURE_SERVER_APP_ID = os.getenv("AZURE_SERVER_APP_ID")
AZURE_SERVER_APP_SECRET = os.getenv("AZURE_SERVER_APP_SECRET")
AZURE_CLIENT_APP_ID = os.getenv("AZURE_CLIENT_APP_ID")
AZURE_AUTH_TENANT_ID = os.getenv("AZURE_AUTH_TENANT_ID", AZURE_TENANT_ID)
KB_FIELDS_CONTENT = os.getenv("KB_FIELDS_CONTENT", "content")
KB_FIELDS_SOURCEPAGE = os.getenv("KB_FIELDS_SOURCEPAGE", "sourcepage")
AZURE_SEARCH_QUERY_LANGUAGE = os.getenv("AZURE_SEARCH_QUERY_LANGUAGE") or "en-us"
AZURE_SEARCH_QUERY_SPELLER = os.getenv("AZURE_SEARCH_QUERY_SPELLER") or "lexicon"
AZURE_SEARCH_SEMANTIC_RANKER = os.getenv("AZURE_SEARCH_SEMANTIC_RANKER", "free").lower()
AZURE_SEARCH_QUERY_REWRITING = os.getenv("AZURE_SEARCH_QUERY_REWRITING", "false").lower()
# This defaults to the previous field name "embedding", for backwards compatibility
AZURE_SEARCH_FIELD_NAME_EMBEDDING = os.getenv("AZURE_SEARCH_FIELD_NAME_EMBEDDING", "embedding")
AZURE_SEARCH_FIELD_NAME_IMAGE_EMBEDDING = os.getenv("AZURE_SEARCH_FIELD_NAME_IMAGE_EMBEDDING", "imageEmbedding")
AZURE_SPEECH_SERVICE_ID = os.getenv("AZURE_SPEECH_SERVICE_ID")
AZURE_SPEECH_SERVICE_LOCATION = os.getenv("AZURE_SPEECH_SERVICE_LOCATION")
AZURE_SPEECH_SERVICE_VOICE = os.getenv("AZURE_SPEECH_SERVICE_VOICE") or "en-US-AndrewMultilingualNeural"
USE_GPT4V = os.getenv("USE_GPT4V", "").lower() == "true"
USE_USER_UPLOAD = os.getenv("USE_USER_UPLOAD", "").lower() == "true"
ENABLE_LANGUAGE_PICKER = os.getenv("ENABLE_LANGUAGE_PICKER", "").lower() == "true"
USE_SPEECH_INPUT_BROWSER = os.getenv("USE_SPEECH_INPUT_BROWSER", "").lower() == "true"
USE_SPEECH_OUTPUT_BROWSER = os.getenv("USE_SPEECH_OUTPUT_BROWSER", "").lower() == "true"
USE_SPEECH_OUTPUT_AZURE = os.getenv("USE_SPEECH_OUTPUT_AZURE", "").lower() == "true"
USE_CHAT_HISTORY_BROWSER = os.getenv("USE_CHAT_HISTORY_BROWSER", "").lower() == "true"
USE_CHAT_HISTORY_COSMOS = os.getenv("USE_CHAT_HISTORY_COSMOS", "").lower() == "true"
# WEBSITE_HOSTNAME is always set by App Service, RUNNING_IN_PRODUCTION is set in main.bicep
RUNNING_ON_AZURE = os.getenv("WEBSITE_HOSTNAME") is not None or os.getenv("RUNNING_IN_PRODUCTION") is not None
# Use the current user identity for keyless authentication to Azure services.
# This assumes you use 'azd auth login' locally, and managed identity when deployed on Azure.
# The managed identity is setup in the infra/ folder.
azure_credential: Union[AzureDeveloperCliCredential, ManagedIdentityCredential]
if RUNNING_ON_AZURE:
current_app.logger.info("Setting up Azure credential using ManagedIdentityCredential")
if AZURE_CLIENT_ID := os.getenv("AZURE_CLIENT_ID"):
# ManagedIdentityCredential should use AZURE_CLIENT_ID if set in env, but its not working for some reason,
# so we explicitly pass it in as the client ID here. This is necessary for user-assigned managed identities.
current_app.logger.info(
"Setting up Azure credential using ManagedIdentityCredential with client_id %s", AZURE_CLIENT_ID
)
azure_credential = ManagedIdentityCredential(client_id=AZURE_CLIENT_ID)
else:
current_app.logger.info("Setting up Azure credential using ManagedIdentityCredential")
azure_credential = ManagedIdentityCredential()
elif AZURE_TENANT_ID:
current_app.logger.info(
"Setting up Azure credential using AzureDeveloperCliCredential with tenant_id %s", AZURE_TENANT_ID
)
azure_credential = AzureDeveloperCliCredential(tenant_id=AZURE_TENANT_ID, process_timeout=60)
else:
current_app.logger.info("Setting up Azure credential using AzureDeveloperCliCredential for home tenant")
azure_credential = AzureDeveloperCliCredential(process_timeout=60)
# Set the Azure credential in the app config for use in other parts of the app
current_app.config[CONFIG_CREDENTIAL] = azure_credential
# Set up clients for AI Search and Storage
search_client = SearchClient(
endpoint=f"https://{AZURE_SEARCH_SERVICE}.search.windows.net",
index_name=AZURE_SEARCH_INDEX,
credential=azure_credential,
)
blob_container_client = ContainerClient(
f"https://{AZURE_STORAGE_ACCOUNT}.blob.core.windows.net", AZURE_STORAGE_CONTAINER, credential=azure_credential
)
# Set up authentication helper
search_index = None
if AZURE_USE_AUTHENTICATION:
current_app.logger.info("AZURE_USE_AUTHENTICATION is true, setting up search index client")
search_index_client = SearchIndexClient(
endpoint=f"https://{AZURE_SEARCH_SERVICE}.search.windows.net",
credential=azure_credential,
)
search_index = await search_index_client.get_index(AZURE_SEARCH_INDEX)
await search_index_client.close()
auth_helper = AuthenticationHelper(
search_index=search_index,
use_authentication=AZURE_USE_AUTHENTICATION,
server_app_id=AZURE_SERVER_APP_ID,
server_app_secret=AZURE_SERVER_APP_SECRET,
client_app_id=AZURE_CLIENT_APP_ID,
tenant_id=AZURE_AUTH_TENANT_ID,
require_access_control=AZURE_ENFORCE_ACCESS_CONTROL,
enable_global_documents=AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS,
enable_unauthenticated_access=AZURE_ENABLE_UNAUTHENTICATED_ACCESS,
)
if USE_USER_UPLOAD:
current_app.logger.info("USE_USER_UPLOAD is true, setting up user upload feature")
if not AZURE_USERSTORAGE_ACCOUNT or not AZURE_USERSTORAGE_CONTAINER:
raise ValueError(
"AZURE_USERSTORAGE_ACCOUNT and AZURE_USERSTORAGE_CONTAINER must be set when USE_USER_UPLOAD is true"
)
user_blob_container_client = FileSystemClient(
f"https://{AZURE_USERSTORAGE_ACCOUNT}.dfs.core.windows.net",
AZURE_USERSTORAGE_CONTAINER,
credential=azure_credential,
)
current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT] = user_blob_container_client
# Set up ingester
file_processors = setup_file_processors(
azure_credential=azure_credential,
document_intelligence_service=os.getenv("AZURE_DOCUMENTINTELLIGENCE_SERVICE"),
local_pdf_parser=os.getenv("USE_LOCAL_PDF_PARSER", "").lower() == "true",
local_html_parser=os.getenv("USE_LOCAL_HTML_PARSER", "").lower() == "true",
search_images=USE_GPT4V,
)
search_info = await setup_search_info(
search_service=AZURE_SEARCH_SERVICE, index_name=AZURE_SEARCH_INDEX, azure_credential=azure_credential
)
text_embeddings_service = setup_embeddings_service(
azure_credential=azure_credential,
openai_host=OPENAI_HOST,
openai_model_name=OPENAI_EMB_MODEL,
openai_service=AZURE_OPENAI_SERVICE,
openai_custom_url=AZURE_OPENAI_CUSTOM_URL,
openai_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
openai_dimensions=OPENAI_EMB_DIMENSIONS,
openai_api_version=AZURE_OPENAI_API_VERSION,
openai_key=clean_key_if_exists(OPENAI_API_KEY),
openai_org=OPENAI_ORGANIZATION,
disable_vectors=os.getenv("USE_VECTORS", "").lower() == "false",
)
ingester = UploadUserFileStrategy(
search_info=search_info,
embeddings=text_embeddings_service,
file_processors=file_processors,
search_field_name_embedding=AZURE_SEARCH_FIELD_NAME_EMBEDDING,
search_field_name_image_embedding=AZURE_SEARCH_FIELD_NAME_IMAGE_EMBEDDING,
)
current_app.config[CONFIG_INGESTER] = ingester
# Used by the OpenAI SDK
openai_client: AsyncOpenAI
if USE_SPEECH_OUTPUT_AZURE:
current_app.logger.info("USE_SPEECH_OUTPUT_AZURE is true, setting up Azure speech service")
if not AZURE_SPEECH_SERVICE_ID or AZURE_SPEECH_SERVICE_ID == "":
raise ValueError("Azure speech resource not configured correctly, missing AZURE_SPEECH_SERVICE_ID")
if not AZURE_SPEECH_SERVICE_LOCATION or AZURE_SPEECH_SERVICE_LOCATION == "":
raise ValueError("Azure speech resource not configured correctly, missing AZURE_SPEECH_SERVICE_LOCATION")
current_app.config[CONFIG_SPEECH_SERVICE_ID] = AZURE_SPEECH_SERVICE_ID
current_app.config[CONFIG_SPEECH_SERVICE_LOCATION] = AZURE_SPEECH_SERVICE_LOCATION
current_app.config[CONFIG_SPEECH_SERVICE_VOICE] = AZURE_SPEECH_SERVICE_VOICE
# Wait until token is needed to fetch for the first time
current_app.config[CONFIG_SPEECH_SERVICE_TOKEN] = None
if OPENAI_HOST.startswith("azure"):
if OPENAI_HOST == "azure_custom":
current_app.logger.info("OPENAI_HOST is azure_custom, setting up Azure OpenAI custom client")
if not AZURE_OPENAI_CUSTOM_URL:
raise ValueError("AZURE_OPENAI_CUSTOM_URL must be set when OPENAI_HOST is azure_custom")
endpoint = AZURE_OPENAI_CUSTOM_URL
else:
current_app.logger.info("OPENAI_HOST is azure, setting up Azure OpenAI client")
if not AZURE_OPENAI_SERVICE:
raise ValueError("AZURE_OPENAI_SERVICE must be set when OPENAI_HOST is azure")
endpoint = f"https://{AZURE_OPENAI_SERVICE}.openai.azure.com"
if api_key := os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"):
current_app.logger.info("AZURE_OPENAI_API_KEY_OVERRIDE found, using as api_key for Azure OpenAI client")
openai_client = AsyncAzureOpenAI(
api_version=AZURE_OPENAI_API_VERSION, azure_endpoint=endpoint, api_key=api_key
)
else:
current_app.logger.info("Using Azure credential (passwordless authentication) for Azure OpenAI client")
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
openai_client = AsyncAzureOpenAI(
api_version=AZURE_OPENAI_API_VERSION,
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider,
)
elif OPENAI_HOST == "local":
current_app.logger.info("OPENAI_HOST is local, setting up local OpenAI client for OPENAI_BASE_URL with no key")
openai_client = AsyncOpenAI(
base_url=os.environ["OPENAI_BASE_URL"],
api_key="no-key-required",
)
else:
current_app.logger.info(
"OPENAI_HOST is not azure, setting up OpenAI client using OPENAI_API_KEY and OPENAI_ORGANIZATION environment variables"
)
openai_client = AsyncOpenAI(
api_key=OPENAI_API_KEY,
organization=OPENAI_ORGANIZATION,
)
current_app.config[CONFIG_OPENAI_CLIENT] = openai_client
current_app.config[CONFIG_SEARCH_CLIENT] = search_client
current_app.config[CONFIG_BLOB_CONTAINER_CLIENT] = blob_container_client
current_app.config[CONFIG_AUTH_CLIENT] = auth_helper
current_app.config[CONFIG_GPT4V_DEPLOYED] = bool(USE_GPT4V)
current_app.config[CONFIG_SEMANTIC_RANKER_DEPLOYED] = AZURE_SEARCH_SEMANTIC_RANKER != "disabled"
current_app.config[CONFIG_QUERY_REWRITING_ENABLED] = (
AZURE_SEARCH_QUERY_REWRITING == "true" and AZURE_SEARCH_SEMANTIC_RANKER != "disabled"
)
current_app.config[CONFIG_DEFAULT_REASONING_EFFORT] = OPENAI_REASONING_EFFORT
current_app.config[CONFIG_REASONING_EFFORT_ENABLED] = OPENAI_CHATGPT_MODEL in Approach.GPT_REASONING_MODELS
current_app.config[CONFIG_STREAMING_ENABLED] = (
bool(USE_GPT4V)
or OPENAI_CHATGPT_MODEL not in Approach.GPT_REASONING_MODELS
or Approach.GPT_REASONING_MODELS[OPENAI_CHATGPT_MODEL].streaming
)
current_app.config[CONFIG_VECTOR_SEARCH_ENABLED] = os.getenv("USE_VECTORS", "").lower() != "false"
current_app.config[CONFIG_USER_UPLOAD_ENABLED] = bool(USE_USER_UPLOAD)
current_app.config[CONFIG_LANGUAGE_PICKER_ENABLED] = ENABLE_LANGUAGE_PICKER
current_app.config[CONFIG_SPEECH_INPUT_ENABLED] = USE_SPEECH_INPUT_BROWSER
current_app.config[CONFIG_SPEECH_OUTPUT_BROWSER_ENABLED] = USE_SPEECH_OUTPUT_BROWSER
current_app.config[CONFIG_SPEECH_OUTPUT_AZURE_ENABLED] = USE_SPEECH_OUTPUT_AZURE
current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED] = USE_CHAT_HISTORY_BROWSER
current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED] = USE_CHAT_HISTORY_COSMOS
prompt_manager = PromptyManager()
# Set up the two default RAG approaches for /ask and /chat
# RetrieveThenReadApproach is used by /ask for single-turn Q&A
current_app.config[CONFIG_ASK_APPROACH] = RetrieveThenReadApproach(
search_client=search_client,
openai_client=openai_client,
auth_helper=auth_helper,
chatgpt_model=OPENAI_CHATGPT_MODEL,
chatgpt_deployment=AZURE_OPENAI_CHATGPT_DEPLOYMENT,
embedding_model=OPENAI_EMB_MODEL,
embedding_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
embedding_dimensions=OPENAI_EMB_DIMENSIONS,
embedding_field=AZURE_SEARCH_FIELD_NAME_EMBEDDING,
sourcepage_field=KB_FIELDS_SOURCEPAGE,
content_field=KB_FIELDS_CONTENT,
query_language=AZURE_SEARCH_QUERY_LANGUAGE,
query_speller=AZURE_SEARCH_QUERY_SPELLER,
prompt_manager=prompt_manager,
reasoning_effort=OPENAI_REASONING_EFFORT,
)
# ChatReadRetrieveReadApproach is used by /chat for multi-turn conversation
current_app.config[CONFIG_CHAT_APPROACH] = ChatReadRetrieveReadApproach(
search_client=search_client,
openai_client=openai_client,
auth_helper=auth_helper,
chatgpt_model=OPENAI_CHATGPT_MODEL,
chatgpt_deployment=AZURE_OPENAI_CHATGPT_DEPLOYMENT,
embedding_model=OPENAI_EMB_MODEL,
embedding_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
embedding_dimensions=OPENAI_EMB_DIMENSIONS,
embedding_field=AZURE_SEARCH_FIELD_NAME_EMBEDDING,
sourcepage_field=KB_FIELDS_SOURCEPAGE,
content_field=KB_FIELDS_CONTENT,
query_language=AZURE_SEARCH_QUERY_LANGUAGE,
query_speller=AZURE_SEARCH_QUERY_SPELLER,
prompt_manager=prompt_manager,
reasoning_effort=OPENAI_REASONING_EFFORT,
)
if USE_GPT4V:
current_app.logger.info("USE_GPT4V is true, setting up GPT4V approach")
if not AZURE_OPENAI_GPT4V_MODEL:
raise ValueError("AZURE_OPENAI_GPT4V_MODEL must be set when USE_GPT4V is true")
if any(
model in Approach.GPT_REASONING_MODELS
for model in [
OPENAI_CHATGPT_MODEL,
AZURE_OPENAI_GPT4V_MODEL,
AZURE_OPENAI_CHATGPT_DEPLOYMENT,
AZURE_OPENAI_GPT4V_DEPLOYMENT,
]
):
raise ValueError(
"AZURE_OPENAI_CHATGPT_MODEL and AZURE_OPENAI_GPT4V_MODEL must not be a reasoning model when USE_GPT4V is true"
)
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
current_app.config[CONFIG_ASK_VISION_APPROACH] = RetrieveThenReadVisionApproach(
search_client=search_client,
openai_client=openai_client,
blob_container_client=blob_container_client,
auth_helper=auth_helper,
vision_endpoint=AZURE_VISION_ENDPOINT,
vision_token_provider=token_provider,
gpt4v_deployment=AZURE_OPENAI_GPT4V_DEPLOYMENT,
gpt4v_model=AZURE_OPENAI_GPT4V_MODEL,
embedding_model=OPENAI_EMB_MODEL,
embedding_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
embedding_dimensions=OPENAI_EMB_DIMENSIONS,
embedding_field=AZURE_SEARCH_FIELD_NAME_EMBEDDING,
sourcepage_field=KB_FIELDS_SOURCEPAGE,
content_field=KB_FIELDS_CONTENT,
query_language=AZURE_SEARCH_QUERY_LANGUAGE,
query_speller=AZURE_SEARCH_QUERY_SPELLER,
prompt_manager=prompt_manager,
)
current_app.config[CONFIG_CHAT_VISION_APPROACH] = ChatReadRetrieveReadVisionApproach(
search_client=search_client,
openai_client=openai_client,
blob_container_client=blob_container_client,
auth_helper=auth_helper,
vision_endpoint=AZURE_VISION_ENDPOINT,
vision_token_provider=token_provider,
chatgpt_model=OPENAI_CHATGPT_MODEL,
chatgpt_deployment=AZURE_OPENAI_CHATGPT_DEPLOYMENT,
gpt4v_deployment=AZURE_OPENAI_GPT4V_DEPLOYMENT,
gpt4v_model=AZURE_OPENAI_GPT4V_MODEL,
embedding_model=OPENAI_EMB_MODEL,
embedding_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
embedding_dimensions=OPENAI_EMB_DIMENSIONS,
embedding_field=AZURE_SEARCH_FIELD_NAME_EMBEDDING,
sourcepage_field=KB_FIELDS_SOURCEPAGE,
content_field=KB_FIELDS_CONTENT,
query_language=AZURE_SEARCH_QUERY_LANGUAGE,
query_speller=AZURE_SEARCH_QUERY_SPELLER,
prompt_manager=prompt_manager,
)
@bp.after_app_serving
async def close_clients():
await current_app.config[CONFIG_SEARCH_CLIENT].close()
await current_app.config[CONFIG_BLOB_CONTAINER_CLIENT].close()
if current_app.config.get(CONFIG_USER_BLOB_CONTAINER_CLIENT):
await current_app.config[CONFIG_USER_BLOB_CONTAINER_CLIENT].close()
def create_app():
app = Quart(__name__)
app.register_blueprint(bp)
app.register_blueprint(chat_history_cosmosdb_bp)
if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
app.logger.info("APPLICATIONINSIGHTS_CONNECTION_STRING is set, enabling Azure Monitor")
configure_azure_monitor()
# This tracks HTTP requests made by aiohttp:
AioHttpClientInstrumentor().instrument()
# This tracks HTTP requests made by httpx:
HTTPXClientInstrumentor().instrument()
# This tracks OpenAI SDK requests:
OpenAIInstrumentor().instrument()
# This middleware tracks app route requests:
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app) # type: ignore[assignment]
# Log levels should be one of https://docs.python.org/3/library/logging.html#logging-levels
# Set root level to WARNING to avoid seeing overly verbose logs from SDKS
logging.basicConfig(level=logging.WARNING)
# Set our own logger levels to INFO by default
app_level = os.getenv("APP_LOG_LEVEL", "INFO")
app.logger.setLevel(os.getenv("APP_LOG_LEVEL", app_level))
logging.getLogger("scripts").setLevel(app_level)
if allowed_origin := os.getenv("ALLOWED_ORIGIN"):
allowed_origins = allowed_origin.split(";")
if len(allowed_origins) > 0:
app.logger.info("CORS enabled for %s", allowed_origins)
cors(app, allow_origin=allowed_origins, allow_methods=["GET", "POST"])
return app