Skip to content

Commit 92a0673

Browse files
committed
Refactor config usage to centralize environment variables
Replaced direct os.getenv calls with values from the central AppConfig instance for Application Insights, Azure Search, and Azure AI settings. This improves maintainability and consistency in configuration management across backend modules. Removed unused kernel creation method from AppConfig.
1 parent 4ee39c0 commit 92a0673

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/backend/app_kernel.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@
3636
)
3737

3838
# Updated import for KernelArguments
39-
from common.utils.utils_kernel import (
40-
rai_success,
41-
)
39+
from common.utils.utils_kernel import rai_success
4240

4341
from common.database.database_factory import DatabaseFactory
4442
from v3.api.router import app_v3
4543

4644
# Check if the Application Insights Instrumentation Key is set in the environment variables
47-
connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
45+
connection_string = config.APPLICATIONINSIGHTS_CONNECTION_STRING
4846
if connection_string:
4947
# Configure Application Insights if the Instrumentation Key is found
5048
configure_azure_monitor(connection_string=connection_string)

src/backend/common/auth/azure_credential_utils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import os
22
from azure.identity import ManagedIdentityCredential, DefaultAzureCredential
3-
from azure.identity.aio import ManagedIdentityCredential as AioManagedIdentityCredential, DefaultAzureCredential as AioDefaultAzureCredential
3+
from azure.identity.aio import (
4+
ManagedIdentityCredential as AioManagedIdentityCredential,
5+
DefaultAzureCredential as AioDefaultAzureCredential,
6+
)
7+
from common.config.app_config import config
48

59

610
async def get_azure_credential_async(client_id=None):
@@ -16,8 +20,10 @@ async def get_azure_credential_async(client_id=None):
1620
Returns:
1721
Credential object: Either AioDefaultAzureCredential or AioManagedIdentityCredential.
1822
"""
19-
if os.getenv("APP_ENV", "prod").lower() == 'dev':
20-
return AioDefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
23+
if config.APP_ENV == "dev":
24+
return (
25+
AioDefaultAzureCredential()
26+
) # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
2127
else:
2228
return AioManagedIdentityCredential(client_id=client_id)
2329

@@ -35,7 +41,9 @@ def get_azure_credential(client_id=None):
3541
Returns:
3642
Credential object: Either DefaultAzureCredential or ManagedIdentityCredential.
3743
"""
38-
if os.getenv("APP_ENV", "prod").lower() == 'dev':
39-
return DefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
44+
if config.APP_ENV == "dev":
45+
return (
46+
DefaultAzureCredential()
47+
) # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
4048
else:
4149
return ManagedIdentityCredential(client_id=client_id)

src/backend/common/config/app_config.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from azure.cosmos.aio import CosmosClient
88
from common.auth.azure_credential_utils import get_azure_credential
99
from dotenv import load_dotenv
10-
from semantic_kernel.kernel import Kernel
1110

1211
# Load environment variables from .env file
1312
load_dotenv()
@@ -28,6 +27,10 @@ def __init__(self):
2827
self.COSMOSDB_DATABASE = self._get_optional("COSMOSDB_DATABASE")
2928
self.COSMOSDB_CONTAINER = self._get_optional("COSMOSDB_CONTAINER")
3029

30+
self.APPLICATIONINSIGHTS_CONNECTION_STRING = self._get_required(
31+
"APPLICATIONINSIGHTS_CONNECTION_STRING"
32+
)
33+
self.APP_ENV = self._get_required("APP_ENV", "prod")
3134
# Azure OpenAI settings
3235
self.AZURE_OPENAI_DEPLOYMENT_NAME = self._get_required(
3336
"AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4o"
@@ -50,6 +53,10 @@ def __init__(self):
5053
self.AZURE_AI_RESOURCE_GROUP = self._get_required("AZURE_AI_RESOURCE_GROUP")
5154
self.AZURE_AI_PROJECT_NAME = self._get_required("AZURE_AI_PROJECT_NAME")
5255
self.AZURE_AI_AGENT_ENDPOINT = self._get_required("AZURE_AI_AGENT_ENDPOINT")
56+
self.AZURE_AI_PROJECT_ENDPOINT = self._get_optional("AZURE_AI_PROJECT_ENDPOINT")
57+
58+
# Azure Search settings
59+
self.AZURE_SEARCH_ENDPOINT = self._get_optional("AZURE_SEARCH_ENDPOINT")
5360

5461
# Cached clients and resources
5562
self._azure_credentials = None
@@ -112,17 +119,6 @@ def _get_bool(self, name: str) -> bool:
112119
"""
113120
return name in os.environ and os.environ[name].lower() in ["true", "1"]
114121

115-
def create_kernel(self):
116-
"""Creates a new Semantic Kernel instance.
117-
118-
Returns:
119-
A new Semantic Kernel instance
120-
"""
121-
# Create a new kernel instance without manually configuring OpenAI services
122-
# The agents will be created using Azure AI Agent Project pattern instead
123-
kernel = Kernel()
124-
return kernel
125-
126122
def get_ai_project_client(self):
127123
"""Create and return an AIProjectClient for Azure AI Foundry using from_connection_string.
128124

src/backend/common/services/json_service.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323

2424
from common.auth.azure_credential_utils import get_azure_credential
25+
from common.config.app_config import config
2526
from common.database.database_base import DatabaseBase
2627

2728

@@ -34,18 +35,15 @@ def __init__(self, memory_context: Optional[DatabaseBase] = None):
3435
self.logger = logging.getLogger(__name__)
3536

3637
# Search validation configuration
37-
self.search_endpoint = os.getenv("AZURE_SEARCH_ENDPOINT")
38-
self.search_key = os.getenv("AZURE_SEARCH_KEY")
39-
if self.search_key:
40-
self.search_credential = AzureKeyCredential(self.search_key)
41-
else:
42-
self.search_credential = DefaultAzureCredential()
38+
self.search_endpoint = config.AZURE_SEARCH_ENDPOINT
39+
40+
self.search_credential = get_azure_credential()
4341

4442
# Model validation configuration
45-
self.subscription_id = os.getenv("AZURE_AI_SUBSCRIPTION_ID")
46-
self.resource_group = os.getenv("AZURE_AI_RESOURCE_GROUP")
47-
self.project_name = os.getenv("AZURE_AI_PROJECT_NAME")
48-
self.project_endpoint = os.getenv("AZURE_AI_PROJECT_ENDPOINT")
43+
self.subscription_id = config.AZURE_AI_SUBSCRIPTION_ID
44+
self.resource_group = config.AZURE_AI_RESOURCE_GROUP
45+
self.project_name = config.AZURE_AI_PROJECT_NAME
46+
self.project_endpoint = config.AZURE_AI_PROJECT_ENDPOINT
4947

5048
async def validate_and_parse_team_config(
5149
self, json_data: Dict[str, Any], user_id: str

src/backend/common/utils/event_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
from azure.monitor.events.extension import track_event
4+
from common.config.app_config import config
45

56

67
def track_event_if_configured(event_name: str, event_data: dict):
@@ -14,7 +15,7 @@ def track_event_if_configured(event_name: str, event_data: dict):
1415
event_data: Dictionary of event data/dimensions
1516
"""
1617
try:
17-
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
18+
instrumentation_key = config.APPLICATIONINSIGHTS_CONNECTION_STRING
1819
if instrumentation_key:
1920
track_event(event_name, event_data)
2021
else:

0 commit comments

Comments
 (0)