Skip to content

Commit 3968bcd

Browse files
Added custom event (microsoft#24)
* feat: added custom event * Logs updated * modify code * added exception logs * added exception logs for cosmos memory --------- Co-authored-by: Roopan P M <[email protected]>
1 parent fb0b278 commit 3968bcd

File tree

6 files changed

+311
-15
lines changed

6 files changed

+311
-15
lines changed

src/backend/agents/base_agent.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from context.cosmos_memory import CosmosBufferedChatCompletionContext
1414
from models.messages import (ActionRequest, ActionResponse,
1515
AgentMessage, Step, StepStatus)
16+
from azure.monitor.events.extension import track_event
1617

1718
class BaseAgent(RoutedAgent):
1819
def __init__(
@@ -94,14 +95,53 @@ async def handle_action_request(
9495
step_id=message.step_id,
9596
)
9697
)
98+
99+
track_event(
100+
"Base agent - Added into the cosmos",
101+
{
102+
"session_id": message.session_id,
103+
"user_id": self._user_id,
104+
"plan_id": message.plan_id,
105+
"content": f"{result}",
106+
"source": self._agent_name,
107+
"step_id": message.step_id,
108+
},
109+
)
110+
97111
except Exception as e:
98-
print(f"Error during LLM call: {e}")
112+
logging.exception(f"Error during LLM call: {e}")
113+
track_event(
114+
"Base agent - Error during llm call, captured into the cosmos",
115+
{
116+
"session_id": message.session_id,
117+
"user_id": self._user_id,
118+
"plan_id": message.plan_id,
119+
"content": f"{e}",
120+
"source": self._agent_name,
121+
"step_id": message.step_id,
122+
},
123+
)
124+
99125
return
100126
print(f"Task completed: {result}")
101127

102128
step.status = StepStatus.completed
103129
step.agent_reply = result
104130
await self._model_context.update_step(step)
131+
132+
track_event(
133+
"Base agent - Updated step and updated into the cosmos",
134+
{
135+
"status": StepStatus.completed,
136+
"session_id": message.session_id,
137+
"agent_reply": f"{result}",
138+
"user_id": self._user_id,
139+
"plan_id": message.plan_id,
140+
"content": f"{result}",
141+
"source": self._agent_name,
142+
"step_id": message.step_id,
143+
},
144+
)
105145

106146
action_response = ActionResponse(
107147
step_id=step.id,

src/backend/agents/group_chat_manager.py

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from typing import Dict, List
77

88
from autogen_core.base import AgentId, MessageContext
9-
from autogen_core.components import (RoutedAgent, default_subscription,
10-
message_handler)
9+
from autogen_core.components import RoutedAgent, default_subscription, message_handler
1110
from autogen_core.components.models import AzureOpenAIChatCompletionClient
1211

1312
from context.cosmos_memory import CosmosBufferedChatCompletionContext
@@ -28,6 +27,7 @@
2827

2928
from datetime import datetime
3029
from typing import List
30+
from azure.monitor.events.extension import track_event
3131

3232

3333
@default_subscription
@@ -36,7 +36,7 @@ def __init__(
3636
self,
3737
model_client: AzureOpenAIChatCompletionClient,
3838
session_id: str,
39-
user_id:str,
39+
user_id: str,
4040
memory: CosmosBufferedChatCompletionContext,
4141
agent_ids: Dict[BAgentType, AgentId],
4242
):
@@ -66,6 +66,17 @@ async def handle_input_task(
6666
step_id="",
6767
)
6868
)
69+
70+
track_event(
71+
"Group Chat Manager - Received and added input task into the cosmos",
72+
{
73+
"session_id": message.session_id,
74+
"user_id": self._user_id,
75+
"content": message.description,
76+
"source": "HumanAgent",
77+
},
78+
)
79+
6980
# Send the InputTask to the PlannerAgent
7081
planner_agent_id = self._agent_ids.get(BAgentType.planner_agent)
7182
plan: Plan = await self.send_message(message, planner_agent_id)
@@ -158,6 +169,16 @@ class Step(BaseDataModel):
158169
step.status = StepStatus.rejected
159170
step.human_approval_status = HumanFeedbackStatus.rejected
160171
self._memory.update_step(step)
172+
track_event(
173+
"Group Chat Manager - Steps has been rejected and updated into the cosmos",
174+
{
175+
"status": StepStatus.rejected,
176+
"session_id": message.session_id,
177+
"user_id": self._user_id,
178+
"human_approval_status": HumanFeedbackStatus.rejected,
179+
"source": step.agent,
180+
},
181+
)
161182
else:
162183
# Update and execute all steps if no specific step_id is provided
163184
for step in steps:
@@ -172,6 +193,16 @@ class Step(BaseDataModel):
172193
step.status = StepStatus.rejected
173194
step.human_approval_status = HumanFeedbackStatus.rejected
174195
self._memory.update_step(step)
196+
track_event(
197+
"Group Chat Manager - Step has been rejected and updated into the cosmos",
198+
{
199+
"status": StepStatus.rejected,
200+
"session_id": message.session_id,
201+
"user_id": self._user_id,
202+
"human_approval_status": HumanFeedbackStatus.rejected,
203+
"source": step.agent,
204+
},
205+
)
175206

176207
# Function to update step status and add feedback
177208
async def _update_step_status(
@@ -187,6 +218,16 @@ async def _update_step_status(
187218
step.human_feedback = received_human_feedback
188219
step.status = StepStatus.completed
189220
await self._memory.update_step(step)
221+
track_event(
222+
"Group Chat Manager - Received human feedback, Updating step and updated into the cosmos",
223+
{
224+
"status": StepStatus.completed,
225+
"session_id": step.session_id,
226+
"user_id": self._user_id,
227+
"human_feedback": received_human_feedback,
228+
"source": step.agent,
229+
},
230+
)
190231
# TODO: Agent verbosity
191232
# await self._memory.add_item(
192233
# AgentMessage(
@@ -205,6 +246,15 @@ async def _execute_step(self, session_id: str, step: Step):
205246
# Update step status to 'action_requested'
206247
step.status = StepStatus.action_requested
207248
await self._memory.update_step(step)
249+
track_event(
250+
"Group Chat Manager - Update step to action_requested and updated into the cosmos",
251+
{
252+
"status": StepStatus.action_requested,
253+
"session_id": step.session_id,
254+
"user_id": self._user_id,
255+
"source": step.agent,
256+
},
257+
)
208258

209259
# generate conversation history for the invoked agent
210260
plan = await self._memory.get_plan_by_session(session_id=session_id)
@@ -261,6 +311,18 @@ async def _execute_step(self, session_id: str, step: Step):
261311
)
262312
)
263313

314+
track_event(
315+
f"Group Chat Manager - Requesting {step.agent.value.title()} to perform the action and added into the cosmos",
316+
{
317+
"session_id": session_id,
318+
"user_id": self._user_id,
319+
"plan_id": step.plan_id,
320+
"content": f"Requesting {step.agent.value.title()} to perform action: {step.action}",
321+
"source": "GroupChatManager",
322+
"step_id": step.id,
323+
},
324+
)
325+
264326
agent_id = self._agent_ids.get(step.agent)
265327
# If the agent_id is not found, send the request to the PlannerAgent for re-planning
266328
# TODO: re-think for the demo scenario
@@ -283,6 +345,17 @@ async def _execute_step(self, session_id: str, step: Step):
283345
logging.info(
284346
"Marking the step as complete - Since we have received the human feedback"
285347
)
348+
track_event(
349+
"Group Chat Manager - Steps completed - Received the human feedback and updated into the cosmos",
350+
{
351+
"session_id": session_id,
352+
"user_id": self._user_id,
353+
"plan_id": step.plan_id,
354+
"content": "Marking the step as complete - Since we have received the human feedback",
355+
"source": step.agent,
356+
"step_id": step.id,
357+
},
358+
)
286359
else:
287360
await self.send_message(action_request, agent_id)
288361
logging.info(f"Sent ActionRequest to {step.agent.value}")

src/backend/agents/human.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
AgentMessage,
1616
Step,
1717
)
18+
from azure.monitor.events.extension import track_event
1819

1920

2021
@default_subscription
@@ -59,6 +60,17 @@ async def handle_step_feedback(
5960
)
6061
)
6162
logging.info(f"HumanAgent received feedback for step: {step}")
63+
track_event(
64+
f"Human Agent - Received feedback for step: {step} and added into the cosmos",
65+
{
66+
"session_id": message.session_id,
67+
"user_id": self.user_id,
68+
"plan_id": step.plan_id,
69+
"content": f"Received feedback for step: {step.action}",
70+
"source": "HumanAgent",
71+
"step_id": message.step_id,
72+
},
73+
)
6274

6375
# Notify the GroupChatManager that the step has been completed
6476
await self._memory.add_item(
@@ -71,3 +83,14 @@ async def handle_step_feedback(
7183
)
7284
)
7385
logging.info(f"HumanAgent sent approval request for step: {step}")
86+
87+
track_event(
88+
f"Human Agent - Approval request sent for step {step} and added into the cosmos",
89+
{
90+
"session_id": message.session_id,
91+
"user_id": self.user_id,
92+
"plan_id": step.plan_id,
93+
"step_id": message.step_id,
94+
"agent_id": self.group_chat_manager_id,
95+
},
96+
)

src/backend/agents/planner.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
HumanFeedbackStatus,
2727
)
2828
from typing import Optional
29+
from azure.monitor.events.extension import track_event
2930

3031
@default_subscription
3132
class PlannerAgent(RoutedAgent):
@@ -70,6 +71,17 @@ async def handle_input_task(self, message: InputTask, ctx: MessageContext) -> Pl
7071
)
7172
)
7273
logging.info(f"Plan generated: {plan.summary}")
74+
75+
track_event(
76+
f"Planner - Generated a plan with {len(steps)} steps and added plan into the cosmos",
77+
{
78+
"session_id": message.session_id,
79+
"user_id": self._user_id,
80+
"plan_id": plan.id,
81+
"content": f"Generated a plan with {len(steps)} steps. Click the blue check box beside each step to complete it, click the x to remove this step.",
82+
"source": "PlannerAgent",
83+
},
84+
)
7385

7486
if plan.human_clarification_request is not None:
7587
# if the plan identified that user information was required, send a message asking the user for it
@@ -86,6 +98,17 @@ async def handle_input_task(self, message: InputTask, ctx: MessageContext) -> Pl
8698
logging.info(
8799
f"Additional information requested: {plan.human_clarification_request}"
88100
)
101+
102+
track_event(
103+
"Planner - Additional information requested and added into the cosmos",
104+
{
105+
"session_id": message.session_id,
106+
"user_id": self._user_id,
107+
"plan_id": plan.id,
108+
"content": f"I require additional information before we can proceed: {plan.human_clarification_request}",
109+
"source": "PlannerAgent",
110+
},
111+
)
89112

90113
return plan
91114

@@ -112,6 +135,17 @@ async def handle_plan_clarification(
112135
step_id="",
113136
)
114137
)
138+
139+
track_event(
140+
"Planner - Store HumanAgent clarification and added into the cosmos",
141+
{
142+
"session_id": message.session_id,
143+
"user_id": self._user_id,
144+
"content": f"{message.human_clarification}",
145+
"source": "HumanAgent",
146+
},
147+
)
148+
115149
await self._memory.add_item(
116150
AgentMessage(
117151
session_id=message.session_id,
@@ -123,6 +157,16 @@ async def handle_plan_clarification(
123157
)
124158
)
125159
logging.info("Plan updated with HumanClarification.")
160+
161+
track_event(
162+
"Planner - Updated with HumanClarification and added into the cosmos",
163+
{
164+
"session_id": message.session_id,
165+
"user_id": self._user_id,
166+
"content": "Thanks. The plan has been updated.",
167+
"source": "PlannerAgent",
168+
},
169+
)
126170

127171
def _generate_instruction(self, objective: str) -> str:
128172

@@ -221,6 +265,19 @@ class StructuredOutputPlan(BaseModel):
221265
)
222266
# Store the plan in memory
223267
await self._memory.add_plan(plan)
268+
269+
track_event(
270+
"Planner - Initial plan and added into the cosmos",
271+
{
272+
"session_id": self._session_id,
273+
"user_id": self._user_id,
274+
"initial_goal": structured_plan.initial_goal,
275+
"overall_status": PlanStatus.in_progress,
276+
"source": "PlannerAgent",
277+
"summary": structured_plan.summary_plan_and_steps,
278+
"human_clarification_request": structured_plan.human_clarification_request,
279+
},
280+
)
224281

225282
# Create the Step instances and store them in memory
226283
steps = []
@@ -235,12 +292,35 @@ class StructuredOutputPlan(BaseModel):
235292
human_approval_status=HumanFeedbackStatus.requested,
236293
)
237294
await self._memory.add_step(step)
295+
track_event(
296+
"Planner - Added planned individual step into the cosmos",
297+
{
298+
"plan_id": plan.id,
299+
"action": step_data.action,
300+
"agent": step_data.agent,
301+
"status": StepStatus.planned,
302+
"session_id": self._session_id,
303+
"user_id": self._user_id,
304+
"human_approval_status": HumanFeedbackStatus.requested,
305+
},
306+
)
238307
steps.append(step)
239308

240309
return plan, steps
241310

242311
except Exception as e:
243-
logging.error(f"Error in create_structured_plan: {e}")
312+
logging.exception(f"Error in create_structured_plan: {e}")
313+
track_event(
314+
f"Planner - Error in create_structured_plan: {e} into the cosmos",
315+
{
316+
"session_id": self._session_id,
317+
"user_id": self._user_id,
318+
"initial_goal": "Error generating plan",
319+
"overall_status": PlanStatus.failed,
320+
"source": "PlannerAgent",
321+
"summary": "Error generating plan",
322+
},
323+
)
244324
# Handle the error, possibly by creating a plan with an error step
245325
plan = Plan(
246326
id=str(uuid.uuid4()),

0 commit comments

Comments
 (0)