Skip to content

Commit fe25fd6

Browse files
authored
Merge pull request #15 from nagkumar91/main
Update the broken notebook, use azure openai for openai agents
2 parents a46e08b + 080348e commit fe25fd6

File tree

6 files changed

+288
-157
lines changed

6 files changed

+288
-157
lines changed

lab/5-Observability/01-trace-agent-session.ipynb

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,87 @@
6161
"outputs": [],
6262
"source": [
6363
"conversation_id = \"retail-session-001\"\n",
64-
"system_prompt = (\"You are Cora, a polite, factual, and helpful Zava retail assistant. \"
65-
"\"Answer with concise, markdown-friendly responses.\")\n",
64+
"system_prompt = (\n",
65+
" \"You are Cora, a polite, factual, and helpful Zava retail assistant. \"\n",
66+
" \"Answer with concise, markdown-friendly responses.\"\n",
67+
")\n",
6668
"customer_prompt = \"What's the best primer for bathroom cabinets?\"\n",
67-
"agent_reply = (\"🎨 Great choice! Zava GripLock Primer at $24 seals moisture. \"
68-
"\"Need brushes too?\")\n",
69+
"agent_reply = (\n",
70+
" \"🎨 Great choice! Zava GripLock Primer at $24 seals moisture. \"\n",
71+
" \"Need brushes too?\"\n",
72+
")\n",
73+
"tool_call_id = \"call-42\"\n",
74+
"tool_arguments = {\"sku\": \"PRIMER-4821\", \"query\": customer_prompt}\n",
75+
"tool_result = {\n",
76+
" \"name\": \"Zava GripLock Primer\",\n",
77+
" \"price\": 24.0,\n",
78+
" \"available_stock\": 67\n",
79+
"}\n",
80+
"tool_definitions = [\n",
81+
" {\n",
82+
" \"type\": \"function\",\n",
83+
" \"name\": \"product_search\",\n",
84+
" \"description\": \"Searches the catalog for matching Zava retail products.\",\n",
85+
" \"parameters\": {\n",
86+
" \"type\": \"object\",\n",
87+
" \"properties\": {\n",
88+
" \"sku\": {\n",
89+
" \"type\": \"string\",\n",
90+
" \"description\": \"Unique product SKU to search for.\"\n",
91+
" },\n",
92+
" \"query\": {\n",
93+
" \"type\": \"string\",\n",
94+
" \"description\": \"Free-form text describing the desired product.\"\n",
95+
" }\n",
96+
" },\n",
97+
" \"required\": [\"sku\"]\n",
98+
" }\n",
99+
" }\n",
100+
"]\n",
101+
"input_messages = [\n",
102+
" {\n",
103+
" \"role\": \"system\",\n",
104+
" \"parts\": [\n",
105+
" {\"type\": \"text\", \"content\": system_prompt}\n",
106+
" ],\n",
107+
" },\n",
108+
" {\n",
109+
" \"role\": \"user\",\n",
110+
" \"parts\": [\n",
111+
" {\"type\": \"text\", \"content\": customer_prompt}\n",
112+
" ],\n",
113+
" },\n",
114+
" {\n",
115+
" \"role\": \"assistant\",\n",
116+
" \"parts\": [\n",
117+
" {\n",
118+
" \"type\": \"tool_call\",\n",
119+
" \"id\": tool_call_id,\n",
120+
" \"name\": \"product_search\",\n",
121+
" \"arguments\": tool_arguments\n",
122+
" }\n",
123+
" ],\n",
124+
" },\n",
125+
" {\n",
126+
" \"role\": \"tool\",\n",
127+
" \"parts\": [\n",
128+
" {\n",
129+
" \"type\": \"tool_call_response\",\n",
130+
" \"id\": tool_call_id,\n",
131+
" \"result\": tool_result\n",
132+
" }\n",
133+
" ],\n",
134+
" },\n",
135+
"]\n",
136+
"output_messages = [\n",
137+
" {\n",
138+
" \"role\": \"assistant\",\n",
139+
" \"parts\": [\n",
140+
" {\"type\": \"text\", \"content\": agent_reply}\n",
141+
" ],\n",
142+
" \"finish_reason\": \"stop\"\n",
143+
" }\n",
144+
"]\n",
69145
"\n",
70146
"with tracer.start_as_current_span(\"create_agent cora-retail-agent\", kind=SpanKind.CLIENT) as span:\n",
71147
" span.set_attribute(\"gen_ai.provider.name\", \"azure.ai.inference\")\n",
@@ -75,7 +151,9 @@
75151
" span.set_attribute(\"gen_ai.agent.description\", \"Cora retail assistant for Zava DIY customers.\")\n",
76152
" span.set_attribute(\"gen_ai.request.model\", \"gpt-4o-mini\")\n",
77153
" span.set_attribute(\"gen_ai.system_instructions\", system_prompt)\n",
154+
" span.set_attribute(\"gen_ai.tool.definitions\", json.dumps(tool_definitions, ensure_ascii=False))\n",
78155
" span.set_attribute(\"server.address\", \"cora-agents.eastus2.inference.ai.azure.com\")\n",
156+
" span.set_attribute(\"server.port\", 443)\n",
79157
" span.set_attribute(\"azure.resource_provider.namespace\", \"Microsoft.CognitiveServices\")\n",
80158
"\n",
81159
"with tracer.start_as_current_span(\"invoke_agent cora-retail-agent\", kind=SpanKind.CLIENT) as span:\n",
@@ -96,30 +174,18 @@
96174
" span.set_attribute(\"gen_ai.usage.output_tokens\", 38)\n",
97175
" span.set_attribute(\"gen_ai.conversation.id\", conversation_id)\n",
98176
" span.set_attribute(\"gen_ai.system_instructions\", system_prompt)\n",
99-
" span.set_attribute(\"gen_ai.input.messages\", json.dumps([\n",
100-
" {\"role\": \"system\", \"content\": system_prompt},\n",
101-
" {\"role\": \"user\", \"content\": customer_prompt}\n",
102-
" ], ensure_ascii=False))\n",
103-
" span.set_attribute(\"gen_ai.output.messages\", json.dumps([\n",
104-
" {\"role\": \"assistant\", \"content\": agent_reply}\n",
105-
" ], ensure_ascii=False))\n",
177+
" span.set_attribute(\"gen_ai.input.messages\", json.dumps(input_messages, ensure_ascii=False))\n",
178+
" span.set_attribute(\"gen_ai.output.messages\", json.dumps(output_messages, ensure_ascii=False))\n",
106179
" span.set_attribute(\"server.address\", \"cora-agents.eastus2.inference.ai.azure.com\")\n",
107180
" span.set_attribute(\"server.port\", 443)\n",
108181
"\n",
109182
" with tracer.start_as_current_span(\"execute_tool product_search\", kind=SpanKind.INTERNAL) as tool_span:\n",
110183
" tool_span.set_attribute(\"gen_ai.operation.name\", \"execute_tool\")\n",
111184
" tool_span.set_attribute(\"gen_ai.tool.name\", \"product_search\")\n",
112185
" tool_span.set_attribute(\"gen_ai.tool.type\", \"function\")\n",
113-
" tool_span.set_attribute(\"gen_ai.tool.call.id\", \"call-42\")\n",
114-
" tool_span.set_attribute(\"gen_ai.tool.call.arguments\", json.dumps({\n",
115-
" \"sku\": \"PRIMER-4821\",\n",
116-
" \"query\": customer_prompt\n",
117-
" }))\n",
118-
" tool_span.set_attribute(\"gen_ai.tool.call.result\", json.dumps({\n",
119-
" \"name\": \"Zava GripLock Primer\",\n",
120-
" \"price\": 24.0,\n",
121-
" \"available_stock\": 67\n",
122-
" }))\n",
186+
" tool_span.set_attribute(\"gen_ai.tool.call.id\", tool_call_id)\n",
187+
" tool_span.set_attribute(\"gen_ai.tool.call.arguments\", json.dumps(tool_arguments))\n",
188+
" tool_span.set_attribute(\"gen_ai.tool.call.result\", json.dumps(tool_result))\n",
123189
"\n",
124190
"print(\"Done — inspect the console output to see each span and its attributes.\")"
125191
]

lab/5-Observability/02-collect-span-snapshots.ipynb

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,95 @@
5454
"outputs": [],
5555
"source": [
5656
"def record_agent_interaction():\n",
57-
" system_prompt = (\"You are Cora, a polite, factual, and helpful Zava retail assistant. \"
58-
"\"Answer with concise, markdown-friendly responses.\")\n",
57+
" system_prompt = (\n",
58+
" \"You are Cora, a polite, factual, and helpful Zava retail assistant. \"\n",
59+
" \"Answer with concise, markdown-friendly responses.\"\n",
60+
" )\n",
5961
" customer_prompt = \"Do you have a satin finish paint that dries fast?\"\n",
60-
" agent_reply = (\"⚡ Absolutely! Zava SwiftCoat Satin at $32 dries in 30 minutes. \"
61-
"\"Want rollers too?\")\n",
62+
" agent_reply = (\n",
63+
" \"⚡ Absolutely! Zava SwiftCoat Satin at $32 dries in 30 minutes. \"\n",
64+
" \"Want rollers too?\"\n",
65+
" )\n",
66+
" conversation_id = \"retail-session-002\"\n",
67+
" tool_call_id = \"call-17\"\n",
68+
" tool_arguments = {\"product_type\": \"paint\", \"finish\": \"satin\"}\n",
69+
" tool_result = {\n",
70+
" \"sku\": \"PAINT-FAST-221\",\n",
71+
" \"name\": \"Zava SwiftCoat Satin\",\n",
72+
" \"inventory\": 142\n",
73+
" }\n",
74+
" tool_definitions = [\n",
75+
" {\n",
76+
" \"type\": \"function\",\n",
77+
" \"name\": \"inventory_lookup\",\n",
78+
" \"description\": \"Fetches inventory details for a specific product category and finish.\",\n",
79+
" \"parameters\": {\n",
80+
" \"type\": \"object\",\n",
81+
" \"properties\": {\n",
82+
" \"product_type\": {\n",
83+
" \"type\": \"string\",\n",
84+
" \"description\": \"Product category name to search.\"\n",
85+
" },\n",
86+
" \"finish\": {\n",
87+
" \"type\": \"string\",\n",
88+
" \"description\": \"Desired paint finish (e.g., satin, matte).\"\n",
89+
" }\n",
90+
" },\n",
91+
" \"required\": [\"product_type\"]\n",
92+
" }\n",
93+
" }\n",
94+
" ]\n",
95+
" input_messages = [\n",
96+
" {\n",
97+
" \"role\": \"system\",\n",
98+
" \"parts\": [\n",
99+
" {\"type\": \"text\", \"content\": system_prompt}\n",
100+
" ],\n",
101+
" },\n",
102+
" {\n",
103+
" \"role\": \"user\",\n",
104+
" \"parts\": [\n",
105+
" {\"type\": \"text\", \"content\": customer_prompt}\n",
106+
" ],\n",
107+
" },\n",
108+
" {\n",
109+
" \"role\": \"assistant\",\n",
110+
" \"parts\": [\n",
111+
" {\n",
112+
" \"type\": \"tool_call\",\n",
113+
" \"id\": tool_call_id,\n",
114+
" \"name\": \"inventory_lookup\",\n",
115+
" \"arguments\": tool_arguments\n",
116+
" }\n",
117+
" ],\n",
118+
" },\n",
119+
" {\n",
120+
" \"role\": \"tool\",\n",
121+
" \"parts\": [\n",
122+
" {\n",
123+
" \"type\": \"tool_call_response\",\n",
124+
" \"id\": tool_call_id,\n",
125+
" \"result\": tool_result\n",
126+
" }\n",
127+
" ],\n",
128+
" },\n",
129+
" ]\n",
130+
" output_messages = [\n",
131+
" {\n",
132+
" \"role\": \"assistant\",\n",
133+
" \"parts\": [\n",
134+
" {\"type\": \"text\", \"content\": agent_reply}\n",
135+
" ],\n",
136+
" \"finish_reason\": \"stop\"\n",
137+
" }\n",
138+
" ]\n",
62139
"\n",
63140
" with tracer.start_as_current_span(\"invoke_agent cora-retail-agent\", kind=SpanKind.CLIENT) as span:\n",
64141
" span.set_attribute(\"gen_ai.provider.name\", \"azure.ai.inference\")\n",
65142
" span.set_attribute(\"gen_ai.operation.name\", \"invoke_agent\")\n",
66143
" span.set_attribute(\"gen_ai.agent.name\", \"cora-retail-agent\")\n",
144+
" span.set_attribute(\"gen_ai.agent.id\", \"agents/cora-retail-agent\")\n",
145+
" span.set_attribute(\"gen_ai.agent.description\", \"Cora retail assistant for Zava DIY customers.\")\n",
67146
" span.set_attribute(\"gen_ai.request.model\", \"gpt-4o-mini\")\n",
68147
" span.set_attribute(\"gen_ai.request.max_tokens\", 256)\n",
69148
" span.set_attribute(\"gen_ai.request.temperature\", 0.3)\n",
@@ -73,30 +152,21 @@
73152
" span.set_attribute(\"gen_ai.response.id\", \"resp-31f1\")\n",
74153
" span.set_attribute(\"gen_ai.usage.input_tokens\", 98)\n",
75154
" span.set_attribute(\"gen_ai.usage.output_tokens\", 41)\n",
76-
" span.set_attribute(\"gen_ai.input.messages\", json.dumps([\n",
77-
" {\"role\": \"system\", \"content\": system_prompt},\n",
78-
" {\"role\": \"user\", \"content\": customer_prompt}\n",
79-
" ], ensure_ascii=False))\n",
80-
" span.set_attribute(\"gen_ai.output.messages\", json.dumps([\n",
81-
" {\"role\": \"assistant\", \"content\": agent_reply}\n",
82-
" ], ensure_ascii=False))\n",
155+
" span.set_attribute(\"gen_ai.conversation.id\", conversation_id)\n",
156+
" span.set_attribute(\"gen_ai.system_instructions\", system_prompt)\n",
157+
" span.set_attribute(\"gen_ai.tool.definitions\", json.dumps(tool_definitions, ensure_ascii=False))\n",
158+
" span.set_attribute(\"gen_ai.input.messages\", json.dumps(input_messages, ensure_ascii=False))\n",
159+
" span.set_attribute(\"gen_ai.output.messages\", json.dumps(output_messages, ensure_ascii=False))\n",
83160
" span.set_attribute(\"server.address\", \"cora-agents.eastus2.inference.ai.azure.com\")\n",
84161
" span.set_attribute(\"server.port\", 443)\n",
85162
"\n",
86163
" with tracer.start_as_current_span(\"execute_tool inventory_lookup\", kind=SpanKind.INTERNAL) as tool_span:\n",
87164
" tool_span.set_attribute(\"gen_ai.operation.name\", \"execute_tool\")\n",
88165
" tool_span.set_attribute(\"gen_ai.tool.name\", \"inventory_lookup\")\n",
89166
" tool_span.set_attribute(\"gen_ai.tool.type\", \"function\")\n",
90-
" tool_span.set_attribute(\"gen_ai.tool.call.id\", \"call-17\")\n",
91-
" tool_span.set_attribute(\"gen_ai.tool.call.arguments\", json.dumps({\n",
92-
" \"product_type\": \"paint\",\n",
93-
" \"finish\": \"satin\"\n",
94-
" }))\n",
95-
" tool_span.set_attribute(\"gen_ai.tool.call.result\", json.dumps({\n",
96-
" \"sku\": \"PAINT-FAST-221\",\n",
97-
" \"name\": \"Zava SwiftCoat Satin\",\n",
98-
" \"inventory\": 142\n",
99-
" }))\n",
167+
" tool_span.set_attribute(\"gen_ai.tool.call.id\", tool_call_id)\n",
168+
" tool_span.set_attribute(\"gen_ai.tool.call.arguments\", json.dumps(tool_arguments))\n",
169+
" tool_span.set_attribute(\"gen_ai.tool.call.result\", json.dumps(tool_result))\n",
100170
"\n",
101171
"record_agent_interaction()\n",
102172
"print(\"Interaction recorded.\")"

0 commit comments

Comments
 (0)