44import logging
55import mimetypes
66import os
7+ import json
78import urllib .parse
89from datetime import datetime , timedelta
910
1011import openai
1112from approaches .chatreadretrieveread import ChatReadRetrieveReadApproach
1213from azure .core .credentials import AzureKeyCredential
1314from azure .identity import DefaultAzureCredential
15+ from azure .mgmt .cognitiveservices import CognitiveServicesManagementClient
1416from azure .search .documents import SearchClient
1517from azure .storage .blob import (
1618 AccountSasPermissions ,
3335AZURE_SEARCH_SERVICE_KEY = os .environ .get ("AZURE_SEARCH_SERVICE_KEY" )
3436AZURE_SEARCH_INDEX = os .environ .get ("AZURE_SEARCH_INDEX" ) or "gptkbindex"
3537AZURE_OPENAI_SERVICE = os .environ .get ("AZURE_OPENAI_SERVICE" ) or "myopenai"
38+ AZURE_OPENAI_RESOURCE_GROUP = os .environ .get ("AZURE_OPENAI_RESOURCE_GROUP" ) or ""
3639AZURE_OPENAI_CHATGPT_DEPLOYMENT = (
3740 os .environ .get ("AZURE_OPENAI_CHATGPT_DEPLOYMENT" ) or "chat"
3841)
3942AZURE_OPENAI_SERVICE_KEY = os .environ .get ("AZURE_OPENAI_SERVICE_KEY" )
43+ AZURE_SUBSCRIPTION_ID = os .environ .get ("AZURE_SUBSCRIPTION_ID" )
4044
4145KB_FIELDS_CONTENT = os .environ .get ("KB_FIELDS_CONTENT" ) or "merged_content"
4246KB_FIELDS_CATEGORY = os .environ .get ("KB_FIELDS_CATEGORY" ) or "category"
8387)
8488blob_container = blob_client .get_container_client (AZURE_BLOB_STORAGE_CONTAINER )
8589
90+ # Set up OpenAI management client
91+ openai_mgmt_client = CognitiveServicesManagementClient (
92+ credential = azure_credential ,
93+ subscription_id = AZURE_SUBSCRIPTION_ID )
94+
95+ deployment = openai_mgmt_client .deployments .get (
96+ resource_group_name = AZURE_OPENAI_RESOURCE_GROUP ,
97+ account_name = AZURE_OPENAI_SERVICE ,
98+ deployment_name = AZURE_OPENAI_CHATGPT_DEPLOYMENT )
8699
87100chat_approaches = {
88101 "rrr" : ChatReadRetrieveReadApproach (
94107 KB_FIELDS_CONTENT ,
95108 blob_client ,
96109 QUERY_TERM_LANGUAGE ,
110+ deployment .properties .model .name ,
111+ deployment .properties .model .version
97112 )
98113}
99114
105120def static_file (path ):
106121 return app .send_static_file (path )
107122
108-
109- # Return blob path with SAS token for citation access
110- @app .route ("/content/<path:path>" )
111- def content_file (path ):
112- blob = blob_container .get_blob_client (path ).download_blob ()
113- mime_type = blob .properties ["content_settings" ]["content_type" ]
114- file_extension = blob .properties ["name" ].split ("." )[- 1 :]
115- if mime_type == "application/octet-stream" :
116- mime_type = mimetypes .guess_type (path )[0 ] or "application/octet-stream"
117- if mime_type == "text/plain" and file_extension [0 ] in ["htm" , "html" ]:
118- mime_type = "text/html"
119- print (
120- "Using mime type: "
121- + mime_type
122- + "for file with extension: "
123- + file_extension [0 ]
124- )
125- return (
126- blob .readall (),
127- 200 ,
128- {
129- "Content-Type" : mime_type ,
130- "Content-Disposition" : f"inline; filename={ urllib .parse .quote (path , safe = '' )} " ,
131- },
132- )
133-
134-
135123@app .route ("/chat" , methods = ["POST" ])
136124def chat ():
137125 approach = request .json ["approach" ]
@@ -156,7 +144,6 @@ def chat():
156144 logging .exception ("Exception in /chat" )
157145 return jsonify ({"error" : str (e )}), 500
158146
159-
160147@app .route ("/getblobclienturl" )
161148def get_blob_client_url ():
162149 sas_token = generate_account_sas (
@@ -177,11 +164,6 @@ def get_blob_client_url():
177164 )
178165 return jsonify ({"url" : f"{ blob_client .url } ?{ sas_token } " })
179166
180-
181- if __name__ == "__main__" :
182- app .run ()
183-
184-
185167@app .route ("/getalluploadstatus" , methods = ["POST" ])
186168def get_all_upload_status ():
187169 timeframe = request .json ["timeframe" ]
@@ -192,3 +174,33 @@ def get_all_upload_status():
192174 logging .exception ("Exception in /getalluploadstatus" )
193175 return jsonify ({"error" : str (e )}), 500
194176 return jsonify (results )
177+
178+ # Return AZURE_OPENAI_CHATGPT_DEPLOYMENT
179+ @app .route ("/getInfoData" )
180+ def get_info_data ():
181+ response = jsonify (
182+ {
183+ "AZURE_OPENAI_CHATGPT_DEPLOYMENT" : f"{ AZURE_OPENAI_CHATGPT_DEPLOYMENT } " ,
184+ "AZURE_OPENAI_MODEL_NAME" : f"{ deployment .properties .model .name } " ,
185+ "AZURE_OPENAI_MODEL_VERSION" : f"{ deployment .properties .model .version } " ,
186+ "AZURE_OPENAI_SERVICE" : f"{ AZURE_OPENAI_SERVICE } " ,
187+ "AZURE_SEARCH_SERVICE" : f"{ AZURE_SEARCH_SERVICE } " ,
188+ "AZURE_SEARCH_INDEX" : f"{ AZURE_SEARCH_INDEX } " ,
189+ "TARGET_LANGUAGE" : f"{ QUERY_TERM_LANGUAGE } "
190+ })
191+ return response
192+
193+ @app .route ("/getcitation" , methods = ["POST" ])
194+ def get_citation ():
195+ citation = urllib .parse .unquote (request .json ["citation" ])
196+ try :
197+ blob = blob_container .get_blob_client (citation ).download_blob ()
198+ decoded_text = blob .readall ().decode ()
199+ results = jsonify (json .loads (decoded_text ))
200+ except Exception as e :
201+ logging .exception ("Exception in /getalluploadstatus" )
202+ return jsonify ({"error" : str (e )}), 500
203+ return jsonify (results .json )
204+
205+ if __name__ == "__main__" :
206+ app .run ()
0 commit comments