Skip to content

Commit 3170e01

Browse files
Update Application Insights configuration and refactor response format handling in agent classes
1 parent ac6240b commit 3170e01

File tree

4 files changed

+52
-53
lines changed

4 files changed

+52
-53
lines changed

src/backend/.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ AZURE_AI_RESOURCE_GROUP=
1414
AZURE_AI_PROJECT_NAME=
1515
AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4o
1616
AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME=gpt-4o
17-
#APPLICATIONINSIGHTS_CONNECTION_STRING=
17+
APPLICATIONINSIGHTS_CONNECTION_STRING=
1818
AZURE_AI_AGENT_ENDPOINT=
1919

2020
BACKEND_API_URL=http://localhost:8000

src/backend/app_kernel.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# app_kernel.py
22
import asyncio
33
import logging
4+
import os
45
import uuid
56
from typing import Dict, List, Optional
67

@@ -9,7 +10,7 @@
910
from auth.auth_utils import get_authenticated_user_details
1011

1112
# Azure monitoring
12-
# from azure.monitor.opentelemetry import configure_azure_monitor
13+
from azure.monitor.opentelemetry import configure_azure_monitor
1314
from config_kernel import Config
1415
from event_utils import track_event_if_configured
1516

@@ -33,19 +34,19 @@
3334
# Updated import for KernelArguments
3435
from utils_kernel import initialize_runtime_and_context, rai_success
3536

36-
# Check if the Application Insights Instrumentation Key is set in the environment variables
37-
# connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
38-
# if connection_string:
39-
# # Configure Application Insights if the Instrumentation Key is found
40-
# configure_azure_monitor(connection_string=connection_string)
41-
# logging.info(
42-
# "Application Insights configured with the provided Instrumentation Key"
43-
# )
44-
# else:
45-
# # Log a warning if the Instrumentation Key is not found
46-
# logging.warning(
47-
# "No Application Insights Instrumentation Key found. Skipping configuration"
48-
# )
37+
#Check if the Application Insights Instrumentation Key is set in the environment variables
38+
connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
39+
if connection_string:
40+
# Configure Application Insights if the Instrumentation Key is found
41+
configure_azure_monitor(connection_string=connection_string)
42+
logging.info(
43+
"Application Insights configured with the provided Instrumentation Key"
44+
)
45+
else:
46+
# Log a warning if the Instrumentation Key is not found
47+
logging.warning(
48+
"No Application Insights Instrumentation Key found. Skipping configuration"
49+
)
4950

5051
# Configure logging
5152
logging.basicConfig(level=logging.INFO)

src/backend/kernel_agents/agent_factory.py

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

77
# Import the new AppConfig instance
88
from app_config import config
9-
9+
from azure.ai.agents.models import (ResponseFormatJsonSchema,
10+
ResponseFormatJsonSchemaType)
1011
from context.cosmos_memory_kernel import CosmosMemoryContext
1112
from kernel_agents.agent_base import BaseAgent
1213
from kernel_agents.generic_agent import GenericAgent
@@ -264,14 +265,13 @@ async def create_all_agents(
264265
temperature=temperature,
265266
agent_instances=agent_instances, # Pass agent instances to the planner
266267
client=client,
267-
response_format={
268-
"type": "json_schema",
269-
"json_schema": {
270-
"name": PlannerResponsePlan.__name__,
271-
"description": f"respond with {PlannerResponsePlan.__name__.lower()}",
272-
"schema": PlannerResponsePlan.model_json_schema()
273-
}
274-
},
268+
response_format=ResponseFormatJsonSchemaType(
269+
json_schema=ResponseFormatJsonSchema(
270+
name=PlannerResponsePlan.__name__,
271+
description=f"respond with {PlannerResponsePlan.__name__.lower()}",
272+
schema=PlannerResponsePlan.model_json_schema(),
273+
)
274+
),
275275
)
276276
agent_instances[AgentType.PLANNER.value] = (
277277
planner_agent # to pass it to group chat manager
@@ -326,4 +326,4 @@ def clear_cache(cls, session_id: Optional[str] = None) -> None:
326326
else:
327327
cls._agent_cache.clear()
328328
cls._azure_ai_agent_cache.clear()
329-
logger.info("Cleared all agent caches")
329+
logger.info("Cleared all agent caches")

src/backend/kernel_agents/planner_agent.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import uuid
44
from typing import Any, Dict, List, Optional, Tuple
55

6+
from azure.ai.agents.models import (ResponseFormatJsonSchema,
7+
ResponseFormatJsonSchemaType)
68
from context.cosmos_memory_kernel import CosmosMemoryContext
79
from event_utils import track_event_if_configured
810
from kernel_agents.agent_base import BaseAgent
@@ -131,40 +133,36 @@ async def create(
131133
try:
132134
logging.info("Initializing PlannerAgent from async init azure AI Agent")
133135

134-
# Construct response_format with the required 'type' field
135-
response_format = {
136-
"type": "json_schema", # Add the missing type field
137-
"json_schema": {
138-
"name": PlannerResponsePlan.__name__,
139-
"description": f"respond with {PlannerResponsePlan.__name__.lower()}",
140-
"schema": PlannerResponsePlan.model_json_schema()
141-
}
142-
}
143-
144136
# Create the Azure AI Agent using AppConfig with string instructions
145137
agent_definition = await cls._create_azure_ai_agent_definition(
146138
agent_name=agent_name,
147-
instructions=cls._get_template(), # Pass the formatted string
139+
instructions=cls._get_template(), # Pass the formatted string, not an object
148140
temperature=0.0,
149-
response_format=response_format,
141+
response_format=ResponseFormatJsonSchemaType(
142+
json_schema=ResponseFormatJsonSchema(
143+
name=PlannerResponsePlan.__name__,
144+
description=f"respond with {PlannerResponsePlan.__name__.lower()}",
145+
schema=PlannerResponsePlan.model_json_schema(),
146+
)
147+
),
150148
)
151149

152-
except Exception as exc:
153-
logging.error("Error initializing PlannerAgent definition: %s", exc)
154-
raise
150+
return cls(
151+
session_id=session_id,
152+
user_id=user_id,
153+
memory_store=memory_store,
154+
tools=tools,
155+
system_message=system_message,
156+
agent_name=agent_name,
157+
available_agents=available_agents,
158+
agent_instances=agent_instances,
159+
client=client,
160+
definition=agent_definition,
161+
)
155162

156-
return cls(
157-
session_id=session_id,
158-
user_id=user_id,
159-
memory_store=memory_store,
160-
tools=tools,
161-
system_message=system_message,
162-
agent_name=agent_name,
163-
available_agents=available_agents,
164-
agent_instances=agent_instances,
165-
client=client,
166-
definition=agent_definition,
167-
)
163+
except Exception as e:
164+
logging.error(f"Failed to create Azure AI Agent for PlannerAgent: {e}")
165+
raise
168166

169167
async def handle_input_task(self, input_task: InputTask) -> str:
170168
"""Handle the initial input task from the user.
@@ -598,4 +596,4 @@ def _get_template():
598596
Choose from {{$agents_str}} ONLY for planning your steps.
599597
600598
"""
601-
return instruction_template
599+
return instruction_template

0 commit comments

Comments
 (0)