Skip to content

Commit ed9fe72

Browse files
committed
Refactor Azure config and token handling
Centralizes Azure configuration and access token retrieval in AppConfig, removing duplicate logic from JsonService and v3/config/settings.py. Updates environment sample and settings to use new config properties and methods for improved maintainability and consistency.
1 parent 3dc5394 commit ed9fe72

File tree

5 files changed

+41
-43
lines changed

5 files changed

+41
-43
lines changed

src/backend/.env.sample

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ AZURE_AI_PROJECT_NAME=
1515
AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4o
1616
APPLICATIONINSIGHTS_CONNECTION_STRING=
1717
AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME=gpt-4o
18+
AZURE_COGNITIVE_SERVICES="https://cognitiveservices.azure.com/.default"
1819
AZURE_AI_AGENT_ENDPOINT=
19-
APP_ENV="dev"
20+
AZURE_BING_CONNECTION_NAME=
21+
REASONING_MODEL_NAME=o3
22+
APP_ENV=dev
2023

2124
BACKEND_API_URL=http://localhost:8000
22-
FRONTEND_SITE_NAME=http://127.0.0.1:3000
25+
FRONTEND_SITE_NAME=*

src/backend/common/config/app_config.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AppConfig:
1717

1818
def __init__(self):
1919
"""Initialize the application configuration with environment variables."""
20+
self.logger = logging.getLogger(__name__)
2021
# Azure authentication settings
2122
self.AZURE_TENANT_ID = self._get_optional("AZURE_TENANT_ID")
2223
self.AZURE_CLIENT_ID = self._get_optional("AZURE_CLIENT_ID")
@@ -31,6 +32,14 @@ def __init__(self):
3132
"APPLICATIONINSIGHTS_CONNECTION_STRING"
3233
)
3334
self.APP_ENV = self._get_required("APP_ENV", "prod")
35+
self.AZURE_AI_MODEL_DEPLOYMENT_NAME = self._get_required(
36+
"AZURE_AI_MODEL_DEPLOYMENT_NAME", "gpt-4o"
37+
)
38+
39+
self.AZURE_COGNITIVE_SERVICES = self._get_optional(
40+
"AZURE_COGNITIVE_SERVICES", "https://cognitiveservices.azure.com/.default"
41+
)
42+
3443
# Azure OpenAI settings
3544
self.AZURE_OPENAI_DEPLOYMENT_NAME = self._get_required(
3645
"AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4o"
@@ -39,9 +48,10 @@ def __init__(self):
3948
"AZURE_OPENAI_API_VERSION", "2024-11-20"
4049
)
4150
self.AZURE_OPENAI_ENDPOINT = self._get_required("AZURE_OPENAI_ENDPOINT")
42-
self.AZURE_OPENAI_SCOPES = [
43-
f"{self._get_optional('AZURE_OPENAI_SCOPE', 'https://cognitiveservices.azure.com/.default')}"
44-
]
51+
self.REASONING_MODEL_NAME = self._get_optional("REASONING_MODEL_NAME", "o3")
52+
self.AZURE_BING_CONNECTION_NAME = self._get_optional(
53+
"AZURE_BING_CONNECTION_NAME"
54+
)
4555

4656
# Frontend settings
4757
self.FRONTEND_SITE_NAME = self._get_optional(
@@ -70,6 +80,16 @@ def get_azure_credentials(self):
7080
self._azure_credentials = get_azure_credential()
7181
return self._azure_credentials
7282

83+
async def get_access_token(self) -> str:
84+
"""Get Azure access token for API calls."""
85+
try:
86+
credential = get_azure_credential()
87+
token = credential.get_token(self.AZURE_COGNITIVE_SERVICES)
88+
return token.token
89+
except Exception as e:
90+
self.logger.error(f"Failed to get access token: {e}")
91+
raise
92+
7393
def _get_required(self, name: str, default: Optional[str] = None) -> str:
7494
"""Get a required configuration value from environment variables.
7595

src/backend/common/services/json_service.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,6 @@ async def delete_team_configuration(self, team_id: str, user_id: str) -> bool:
283283
# Model validation methods
284284
# -----------------------
285285

286-
async def get_access_token(self) -> str:
287-
"""Get Azure access token for API calls."""
288-
try:
289-
credential = get_azure_credential()
290-
token = credential.get_token("https://management.azure.com/.default")
291-
return token.token
292-
except Exception as e:
293-
self.logger.error(f"Failed to get access token: {e}")
294-
raise
295-
296286
async def list_model_deployments(self) -> List[Dict[str, Any]]:
297287
"""
298288
List all model deployments in the Azure AI project using the REST API.

src/backend/common/utils/utils_kernel.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Import the credential utility
1313
from common.auth.azure_credential_utils import get_azure_credential
14+
from common.config.app_config import config
1415

1516
# Import agent factory and the new AppConfig
1617
from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent
@@ -34,14 +35,11 @@ async def rai_success(description: str, is_task_creation: bool) -> bool:
3435
"""
3536
try:
3637
# Use managed identity for authentication to Azure OpenAI
37-
credential = get_azure_credential()
38-
access_token = credential.get_token(
39-
"https://cognitiveservices.azure.com/.default"
40-
).token
41-
42-
CHECK_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
43-
API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
44-
DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_MODEL_NAME")
38+
access_token = await config.get_access_token()
39+
40+
CHECK_ENDPOINT = config.AZURE_OPENAI_ENDPOINT
41+
API_VERSION = config.AZURE_OPENAI_API_VERSION
42+
DEPLOYMENT_NAME = config.AZURE_AI_MODEL_DEPLOYMENT_NAME
4543

4644
if not all([CHECK_ENDPOINT, API_VERSION, DEPLOYMENT_NAME]):
4745
logging.error("Missing required environment variables for RAI check")

src/backend/v3/config/settings.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,20 @@
1010
AzureChatCompletion,
1111
OpenAIChatPromptExecutionSettings,
1212
)
13-
14-
# Load environment variables
15-
load_dotenv()
16-
17-
# Azure configuration
18-
TENANT_ID = ""
19-
CLIENT_ID = ""
13+
from common.config.app_config import config
2014

2115

2216
class AzureConfig:
2317
"""Azure OpenAI and authentication configuration."""
2418

2519
def __init__(self):
26-
self.endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
27-
self.reasoning_model = os.getenv("REASONING_MODEL_NAME", "o3")
28-
self.standard_model = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4")
29-
self.bing_connection_name = os.getenv("BING_CONNECTION_NAME")
20+
self.endpoint = config.AZURE_OPENAI_ENDPOINT
21+
self.reasoning_model = config.REASONING_MODEL_NAME
22+
self.standard_model = config.AZURE_OPENAI_DEPLOYMENT_NAME
23+
self.bing_connection_name = config.AZURE_BING_CONNECTION_NAME
3024

3125
# Create credential
32-
self.credential = SyncDefaultAzureCredential()
33-
34-
def get_azure_token(self):
35-
"""Get Azure token for authentication."""
36-
token = self.credential.get_token(
37-
"https://cognitiveservices.azure.com/.default"
38-
)
39-
return token.token
26+
self.credential = config.get_azure_credentials()
4027

4128
def create_chat_completion_service(self, use_reasoning_model=False):
4229
"""Create Azure Chat Completion service."""
@@ -47,7 +34,7 @@ def create_chat_completion_service(self, use_reasoning_model=False):
4734
return AzureChatCompletion(
4835
deployment_name=model_name,
4936
endpoint=self.endpoint,
50-
ad_token_provider=self.get_azure_token,
37+
ad_token_provider=config.get_access_token(),
5138
)
5239

5340
def create_execution_settings(self):

0 commit comments

Comments
 (0)