Skip to content

Commit

Permalink
fix some format issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zedy committed Feb 7, 2024
1 parent 5186832 commit 7ac0727
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 48 deletions.
60 changes: 31 additions & 29 deletions code/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand All @@ -369,4 +371,4 @@ def conversation_custom():


if __name__ == "__main__":
app.run()
app.run()
27 changes: 16 additions & 11 deletions code/backend/batch/utilities/helpers/EnvHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions infra/app/adminweb.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions infra/app/function.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions infra/app/storekeys.bicep
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions infra/app/web.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ac0727

Please sign in to comment.