From 7ac072791d537a79b891906b92d8b63b4e01edfa Mon Sep 17 00:00:00 2001 From: zedy Date: Wed, 7 Feb 2024 20:31:49 +0800 Subject: [PATCH] fix some format issue --- code/app.py | 60 ++++++++++--------- .../batch/utilities/helpers/EnvHelper.py | 27 +++++---- infra/app/adminweb.bicep | 4 +- infra/app/function.bicep | 4 +- infra/app/storekeys.bicep | 4 +- infra/app/web.bicep | 4 +- 6 files changed, 55 insertions(+), 48 deletions(-) diff --git a/code/app.py b/code/app.py index 7cb570349..61a7518a3 100644 --- a/code/app.py +++ b/code/app.py @@ -28,40 +28,33 @@ def static_file(path): return app.send_static_file(path) -AZURE_AUTH_TYPE = os.environ.get("AZURE_AUTH_TYPE", "keys") -# Initialize Azure keys based on authentication type and environment settings. -# When AZURE_AUTH_TYPE is not 'rbac' and USE_KEY_VAULT environment variable is set, -# Azure keys are securely fetched from Azure Key Vault using DefaultAzureCredential. -# Otherwise, keys are obtained from environment variables, with fallbacks to None or -# an empty string depending on the AZURE_AUTH_TYPE. -if not AZURE_AUTH_TYPE == 'rbac' and os.environ.get("USE_KEY_VAULT"): - credential = DefaultAzureCredential() - secret_client = SecretClient(os.environ.get("AZURE_KEY_VAULT_ENDPOINT"), credential) - AZURE_SEARCH_KEY = secret_client.get_secret(os.environ.get("AZURE_SEARCH_KEY")).value - AZURE_OPENAI_KEY = secret_client.get_secret(os.environ.get("AZURE_OPENAI_KEY")).value - AZURE_SPEECH_KEY = secret_client.get_secret(os.environ.get("AZURE_SPEECH_SERVICE_KEY")).value -else: - AZURE_SEARCH_KEY = None if AZURE_AUTH_TYPE == 'rbac' else os.environ.get("AZURE_SEARCH_KEY") - AZURE_OPENAI_KEY = "" if AZURE_AUTH_TYPE == 'rbac' else os.environ.get("AZURE_OPENAI_KEY") - AZURE_SPEECH_KEY = None if AZURE_AUTH_TYPE == 'rbac' else os.environ.get("AZURE_SPEECH_SERVICE_KEY") - +from backend.batch.utilities.helpers.EnvHelper import EnvHelper +env_helper: EnvHelper = EnvHelper() +AZURE_AUTH_TYPE = env_helper.AZURE_AUTH_TYPE +AZURE_SEARCH_KEY = env_helper.AZURE_SEARCH_KEY +AZURE_OPENAI_KEY = env_helper.AZURE_OPENAI_KEY +AZURE_SPEECH_KEY = env_helper.AZURE_SPEECH_KEY @app.route("/api/config", methods=["GET"]) def get_config(): # Retrieve the environment variables or other configuration data - azure_speech_region = os.getenv('AZURE_SPEECH_SERVICE_REGION') + azure_speech_region = os.getenv("AZURE_SPEECH_SERVICE_REGION") # Return the configuration data as JSON - return jsonify({ - 'azureSpeechKey': AZURE_SPEECH_KEY, - 'azureSpeechRegion': azure_speech_region - }) + return jsonify( + {"azureSpeechKey": AZURE_SPEECH_KEY, "azureSpeechRegion": azure_speech_region} + ) + # ACS Integration Settings AZURE_SEARCH_SERVICE = os.environ.get("AZURE_SEARCH_SERVICE") AZURE_SEARCH_INDEX = os.environ.get("AZURE_SEARCH_INDEX") -AZURE_SEARCH_USE_SEMANTIC_SEARCH = os.environ.get("AZURE_SEARCH_USE_SEMANTIC_SEARCH", "False") -AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG = os.environ.get("AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG", "default") +AZURE_SEARCH_USE_SEMANTIC_SEARCH = os.environ.get( + "AZURE_SEARCH_USE_SEMANTIC_SEARCH", "False" +) +AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG = os.environ.get( + "AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG", "default" +) AZURE_SEARCH_TOP_K = os.environ.get("AZURE_SEARCH_TOP_K", 5) AZURE_SEARCH_ENABLE_IN_DOMAIN = os.environ.get("AZURE_SEARCH_ENABLE_IN_DOMAIN", "true") AZURE_SEARCH_CONTENT_COLUMNS = os.environ.get("AZURE_SEARCH_CONTENT_COLUMNS") @@ -84,7 +77,9 @@ def get_config(): "AZURE_OPENAI_API_VERSION", "2023-06-01-preview" ) AZURE_OPENAI_STREAM = os.environ.get("AZURE_OPENAI_STREAM", "true") -AZURE_OPENAI_MODEL_NAME = os.environ.get("AZURE_OPENAI_MODEL_NAME", "gpt-35-turbo") # Name of the model, e.g. 'gpt-35-turbo' or 'gpt-4' +AZURE_OPENAI_MODEL_NAME = os.environ.get( + "AZURE_OPENAI_MODEL_NAME", "gpt-35-turbo" +) # Name of the model, e.g. 'gpt-35-turbo' or 'gpt-4' AZURE_TOKEN_PROVIDER = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") SHOULD_STREAM = True if AZURE_OPENAI_STREAM.lower() == "true" else False @@ -263,9 +258,10 @@ def conversation_without_data(request): for message in request_messages: messages.append({"role": message["role"], "content": message["content"]}) + # Azure Open AI takes the deployment name as the model name, "AZURE_OPENAI_MODEL" means deployment name. response = openai_client.chat.completions.create( model=AZURE_OPENAI_MODEL, - messages = messages, + messages=messages, temperature=float(AZURE_OPENAI_TEMPERATURE), max_tokens=int(AZURE_OPENAI_MAX_TOKENS), top_p=float(AZURE_OPENAI_TOP_P), @@ -341,9 +337,15 @@ def conversation_custom(): chat_history = [] for i, k in enumerate(user_assistant_messages): if i % 2 == 0: - chat_history.append((user_assistant_messages[i]['content'],user_assistant_messages[i+1]['content'])) + chat_history.append((user_assistant_messages[i]["content"],user_assistant_messages[i+1]["content"])) from backend.batch.utilities.helpers.ConfigHelper import ConfigHelper - messages = message_orchestrator.handle_message(user_message=user_message, chat_history=chat_history, conversation_id=conversation_id, orchestrator=ConfigHelper.get_active_config_or_default().orchestrator) + + messages = message_orchestrator.handle_message( + user_message=user_message, + chat_history=chat_history, + conversation_id=conversation_id, + orchestrator=ConfigHelper.get_active_config_or_default().orchestrator, + ) response_obj = { "id": "response.id", @@ -369,4 +371,4 @@ def conversation_custom(): if __name__ == "__main__": - app.run() \ No newline at end of file + app.run() diff --git a/code/backend/batch/utilities/helpers/EnvHelper.py b/code/backend/batch/utilities/helpers/EnvHelper.py index fd400f17d..7b11ea06c 100644 --- a/code/backend/batch/utilities/helpers/EnvHelper.py +++ b/code/backend/batch/utilities/helpers/EnvHelper.py @@ -44,18 +44,23 @@ def __init__(self, **kwargs) -> None: self.AZURE_OPENAI_EMBEDDING_MODEL = os.getenv('AZURE_OPENAI_EMBEDDING_MODEL', '') self.AZURE_TOKEN_PROVIDER = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") # Initialize Azure keys based on authentication type and environment settings. - # When AZURE_AUTH_TYPE is not 'rbac' and USE_KEY_VAULT environment variable is set, - # Azure keys are securely fetched from Azure Key Vault using DefaultAzureCredential. - # Otherwise, keys are obtained from environment variables, with fallbacks to None or - # an empty string depending on the AZURE_AUTH_TYPE. - if not self.AZURE_AUTH_TYPE == 'rbac' and os.environ.get("USE_KEY_VAULT"): - self.credential = DefaultAzureCredential() - self.secret_client = SecretClient(os.environ.get("AZURE_KEY_VAULT_ENDPOINT"), self.credential) - self.AZURE_SEARCH_KEY = self.secret_client.get_secret(os.environ.get("AZURE_SEARCH_KEY")).value - self.AZURE_OPENAI_KEY = self.secret_client.get_secret(os.environ.get("AZURE_OPENAI_KEY")).value + # When AZURE_AUTH_TYPE is "rbac", azure keys are None or an empty string. + # When USE_KEY_VAULT environment variable is set, keys are securely fetched from Azure Key Vault using DefaultAzureCredential. + # Otherwise, keys are obtained from environment variables. + if self.AZURE_AUTH_TYPE == "rbac": + self.AZURE_SEARCH_KEY = None + self.AZURE_OPENAI_KEY = "" + self.AZURE_SPEECH_KEY = None + elif os.environ.get("USE_KEY_VAULT"): + credential = DefaultAzureCredential() + secret_client = SecretClient(os.environ.get("AZURE_KEY_VAULT_ENDPOINT"), credential) + self.AZURE_SEARCH_KEY = secret_client.get_secret(os.environ.get("AZURE_SEARCH_KEY")).value + self.AZURE_OPENAI_KEY = secret_client.get_secret(os.environ.get("AZURE_OPENAI_KEY")).value + self.AZURE_SPEECH_KEY = secret_client.get_secret(os.environ.get("AZURE_SPEECH_SERVICE_KEY")).value else: - self.AZURE_SEARCH_KEY = None if self.AZURE_AUTH_TYPE == 'rbac' else os.environ.get("AZURE_SEARCH_KEY") - self.AZURE_OPENAI_KEY = "" if self.AZURE_AUTH_TYPE == 'rbac' else os.environ.get("AZURE_OPENAI_KEY") + self.AZURE_SEARCH_KEY = os.environ.get("AZURE_SEARCH_KEY") + self.AZURE_OPENAI_KEY = os.environ.get("AZURE_OPENAI_KEY") + self.AZURE_SPEECH_KEY = os.environ.get("AZURE_SPEECH_SERVICE_KEY") # Set env for OpenAI SDK self.OPENAI_API_BASE = f"https://{os.getenv('AZURE_OPENAI_RESOURCE')}.openai.azure.com/" self.OPENAI_API_TYPE = "azure" if self.AZURE_AUTH_TYPE == "keys" else "azure_ad" diff --git a/infra/app/adminweb.bicep b/infra/app/adminweb.bicep index b4e533ff3..9a89975a1 100644 --- a/infra/app/adminweb.bicep +++ b/infra/app/adminweb.bicep @@ -10,7 +10,7 @@ param appCommandLine string = 'python -m streamlit run Admin.py --server.port 80 param applicationInsightsName string = '' param keyVaultName string = '' param azureOpenAIName string = '' -param azureCognitiveSearchName string = '' +param azureAISearchName string = '' @secure() param appSettings object = {} param serviceName string = 'adminweb' @@ -38,7 +38,7 @@ module adminweb '../core/host/appservice.bicep' = { USE_KEY_VAULT: useKeyVault ? useKeyVault : '' AZURE_KEY_VAULT_ENDPOINT: useKeyVault ? keyVaultEndpoint : '' AZURE_OPENAI_KEY: useKeyVault ? openAIKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', azureOpenAIName), '2023-05-01').key1 - AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureCognitiveSearchName), '2021-04-01-preview').primaryKey + AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureAISearchName), '2021-04-01-preview').primaryKey AZURE_BLOB_ACCOUNT_KEY: useKeyVault ? storageAccountKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Storage/storageAccounts', storageAccountName), '2021-09-01').keys[0].value AZURE_FORM_RECOGNIZER_KEY: useKeyVault ? formRecognizerKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', formRecognizerName), '2023-05-01').key1 AZURE_CONTENT_SAFETY_KEY: useKeyVault ? contentSafetyKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', contentSafetyName), '2023-05-01').key1 diff --git a/infra/app/function.bicep b/infra/app/function.bicep index b54a4c057..322198601 100644 --- a/infra/app/function.bicep +++ b/infra/app/function.bicep @@ -12,7 +12,7 @@ param runtimeVersion string = '3.11' param clientKey string param keyVaultName string = '' param azureOpenAIName string = '' -param azureCognitiveSearchName string = '' +param azureAISearchName string = '' param formRecognizerName string = '' param contentSafetyName string = '' param useKeyVault bool @@ -40,7 +40,7 @@ module function '../core/host/functions.bicep' = { USE_KEY_VAULT: useKeyVault ? useKeyVault : '' AZURE_KEY_VAULT_ENDPOINT: useKeyVault ? keyVaultEndpoint : '' AZURE_OPENAI_KEY: useKeyVault ? openAIKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', azureOpenAIName), '2023-05-01').key1 - AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureCognitiveSearchName), '2021-04-01-preview').primaryKey + AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureAISearchName), '2021-04-01-preview').primaryKey AZURE_BLOB_ACCOUNT_KEY: useKeyVault ? storageAccountKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Storage/storageAccounts', storageAccountName), '2021-09-01').keys[0].value AZURE_FORM_RECOGNIZER_KEY: useKeyVault ? formRecognizerKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', formRecognizerName), '2023-05-01').key1 AZURE_CONTENT_SAFETY_KEY: useKeyVault ? contentSafetyKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', contentSafetyName), '2023-05-01').key1 diff --git a/infra/app/storekeys.bicep b/infra/app/storekeys.bicep index 115680c9f..f149417ed 100644 --- a/infra/app/storekeys.bicep +++ b/infra/app/storekeys.bicep @@ -1,7 +1,7 @@ param keyVaultName string = '' param storageAccountName string = '' param azureOpenAIName string = '' -param azureCognitiveSearchName string = '' +param azureAISearchName string = '' param rgName string = '' param formRecognizerName string = '' param contentSafetyName string = '' @@ -34,7 +34,7 @@ resource searchKeySecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = { parent: keyVault name: searchKeyName properties: { - value: listAdminKeys(resourceId(subscription().subscriptionId, rgName, 'Microsoft.Search/searchServices', azureCognitiveSearchName), '2021-04-01-preview').primaryKey + value: listAdminKeys(resourceId(subscription().subscriptionId, rgName, 'Microsoft.Search/searchServices', azureAISearchName), '2021-04-01-preview').primaryKey } } diff --git a/infra/app/web.bicep b/infra/app/web.bicep index a2115005e..c4f7a1560 100644 --- a/infra/app/web.bicep +++ b/infra/app/web.bicep @@ -7,7 +7,7 @@ param appServicePlanId string param applicationInsightsName string = '' param keyVaultName string = '' param azureOpenAIName string = '' -param azureCognitiveSearchName string = '' +param azureAISearchName string = '' param storageAccountName string = '' param formRecognizerName string = '' param contentSafetyName string = '' @@ -40,7 +40,7 @@ module web '../core/host/appservice.bicep' = { USE_KEY_VAULT: useKeyVault ? useKeyVault : '' AZURE_KEY_VAULT_ENDPOINT: useKeyVault ? keyVaultEndpoint : '' AZURE_OPENAI_KEY: useKeyVault ? openAIKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', azureOpenAIName), '2023-05-01').key1 - AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureCognitiveSearchName), '2021-04-01-preview').primaryKey + AZURE_SEARCH_KEY: useKeyVault ? searchKeyName : listAdminKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Search/searchServices', azureAISearchName), '2021-04-01-preview').primaryKey AZURE_BLOB_ACCOUNT_KEY: useKeyVault ? storageAccountKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.Storage/storageAccounts', storageAccountName), '2021-09-01').keys[0].value AZURE_FORM_RECOGNIZER_KEY: useKeyVault ? formRecognizerKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', formRecognizerName), '2023-05-01').key1 AZURE_CONTENT_SAFETY_KEY: useKeyVault ? contentSafetyKeyName : listKeys(resourceId(subscription().subscriptionId, resourceGroup().name, 'Microsoft.CognitiveServices/accounts', contentSafetyName), '2023-05-01').key1