Skip to content

Commit d3edf08

Browse files
committed
Only create agents that you need
1 parent 22f6ebe commit d3edf08

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/backend/app_kernel.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from event_utils import track_event_if_configured
4141
from models.messages_kernel import AgentType
4242
from kernel_agents.agent_factory import AgentFactory
43+
from app_config import config
4344

4445
# # Check if the Application Insights Instrumentation Key is set in the environment variables
4546
# instrumentation_key = os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
@@ -267,15 +268,24 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
267268
except Exception as client_exc:
268269
logging.error(f"Error creating AIProjectClient: {client_exc}")
269270

270-
agents = await AgentFactory.create_all_agents(
271+
human_agent = await AgentFactory.create_agent(
272+
agent_type=AgentType.HUMAN.value,
271273
session_id=human_feedback.session_id,
272274
user_id=user_id,
273275
memory_store=memory_store,
274276
client=client,
275277
)
276278

277-
# Send the feedback to the human agent
278-
human_agent = agents[AgentType.HUMAN.value]
279+
if human_agent is None:
280+
track_event_if_configured(
281+
"AgentNotFound",
282+
{
283+
"status": "Agent not found",
284+
"session_id": human_feedback.session_id,
285+
"step_id": human_feedback.step_id,
286+
},
287+
)
288+
raise HTTPException(status_code=404, detail="Agent not found")
279289

280290
# Use the human agent to handle the feedback
281291
await human_agent.handle_human_feedback(human_feedback=human_feedback)
@@ -363,15 +373,25 @@ async def human_clarification_endpoint(
363373
client = config.get_ai_project_client()
364374
except Exception as client_exc:
365375
logging.error(f"Error creating AIProjectClient: {client_exc}")
366-
agents = await AgentFactory.create_all_agents(
376+
377+
human_agent = await AgentFactory.create_agent(
378+
agent_type=AgentType.HUMAN.value,
367379
session_id=human_clarification.session_id,
368380
user_id=user_id,
369381
memory_store=memory_store,
370382
client=client,
371383
)
372384

373-
# Send the feedback to the human agent
374-
human_agent = agents[AgentType.HUMAN.value]
385+
if human_agent is None:
386+
track_event_if_configured(
387+
"AgentNotFound",
388+
{
389+
"status": "Agent not found",
390+
"session_id": human_clarification.session_id,
391+
"step_id": human_clarification.step_id,
392+
},
393+
)
394+
raise HTTPException(status_code=404, detail="Agent not found")
375395

376396
# Use the human agent to handle the feedback
377397
await human_agent.handle_human_clarification(

0 commit comments

Comments
 (0)