Skip to content

Commit 16784e3

Browse files
Update Arize integration with latest notebook changes and improvements (#163)
Update to the integration mapper between OTEL from Strands and OpenInference for Arize.
1 parent 0df5eae commit 16784e3

File tree

3 files changed

+1170
-417
lines changed

3 files changed

+1170
-417
lines changed

03-integrations/Openinference-Arize/Arize-Observability-openinference-strands.ipynb

Lines changed: 158 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,17 @@
8383
"metadata": {},
8484
"outputs": [],
8585
"source": [
86-
"!pip install -r requirements.txt"
86+
"!pip install -q -r requirements.txt"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"id": "4a7b19e1-344a-4342-94d5-9a165cb00941",
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"!pip install --upgrade strands-agents strands-agents-tools "
8797
]
8898
},
8999
{
@@ -98,9 +108,21 @@
98108
{
99109
"cell_type": "code",
100110
"execution_count": null,
101-
"id": "5545c2d3",
111+
"id": "becbe73a-dd49-42c9-b79a-a41bcb25216b",
102112
"metadata": {},
103113
"outputs": [],
114+
"source": [
115+
"pip show strands-agents strands-agents-tools"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "5545c2d3",
122+
"metadata": {
123+
"scrolled": true
124+
},
125+
"outputs": [],
104126
"source": [
105127
"!sh deploy_prereqs.sh"
106128
]
@@ -125,8 +147,12 @@
125147
"outputs": [],
126148
"source": [
127149
"import os\n",
150+
"\n",
151+
"#Set Arize Endpoint, API and Space ID keys as env variables\n",
128152
"API_KEY = \"your-api-key\"\n",
129153
"SPACE_ID = \"your-space-id\"\n",
154+
"SESSION_ID = \"session-abc-4\" # <---We'll use this to group our trace conversations to simulate sessions\n",
155+
"\n",
130156
"ENDPOINT = \"otlp.arize.com:443\"\n",
131157
"os.environ[\"ARIZE_SPACE_ID\"] = SPACE_ID\n",
132158
"os.environ[\"ARIZE_API_KEY\"] = API_KEY\n",
@@ -152,40 +178,38 @@
152178
{
153179
"cell_type": "code",
154180
"execution_count": null,
155-
"id": "11716548",
181+
"id": "0941c213-9466-450c-9bba-a9ed37415f8d",
156182
"metadata": {},
157183
"outputs": [],
158184
"source": [
185+
"from opentelemetry import trace\n",
186+
"from opentelemetry.sdk.trace import TracerProvider\n",
159187
"from opentelemetry.sdk.trace.export import BatchSpanProcessor\n",
188+
"from opentelemetry.sdk.resources import Resource\n",
160189
"from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter\n",
161190
"from strands_to_openinference_mapping import StrandsToOpenInferenceProcessor\n",
162-
"from arize.otel import register\n",
163-
"from opentelemetry import trace\n",
164-
"import grpc\n",
165191
"\n",
166-
"provider = register(\n",
167-
" space_id=SPACE_ID,\n",
168-
" api_key=API_KEY,\n",
169-
" project_name=\"strands-agent-integration2\",\n",
170-
" set_global_tracer_provider=True,\n",
171-
")\n",
192+
"#strands_processor = StrandsToOpenInferenceProcessor(debug=True)\n",
193+
"strands_processor = StrandsToOpenInferenceProcessor()\n",
194+
"\n",
195+
"# Create resource with model_id \n",
196+
"resource = Resource.create({\n",
197+
" \"model_id\": \"venu-kanamatareddy-strands-agent\", ### <-- Update with your Arize Project Name <last name>-<first name>-strands-agent\n",
198+
" \"service.name\": \"strands-agent-integration\",\n",
199+
"})\n",
172200
"\n",
173-
"provider.add_span_processor(StrandsToOpenInferenceProcessor(debug=True))\n",
201+
"provider = TracerProvider(resource=resource)\n",
202+
"provider.add_span_processor(strands_processor)\n",
174203
"\n",
204+
"otlp_exporter = OTLPSpanExporter(\n",
205+
" endpoint=ENDPOINT,\n",
206+
" headers={\n",
207+
" \"space_id\": SPACE_ID,\n",
208+
" \"api_key\": API_KEY\n",
209+
" }\n",
210+
")\n",
175211
"provider.add_span_processor(\n",
176-
" BatchSpanProcessor(\n",
177-
" OTLPSpanExporter(\n",
178-
" endpoint=ENDPOINT,\n",
179-
" headers={\n",
180-
" \"authorization\": f\"Bearer {API_KEY}\",\n",
181-
" \"api_key\": API_KEY,\n",
182-
" \"arize-space-id\": SPACE_ID,\n",
183-
" \"arize-interface\": \"python\",\n",
184-
" \"user-agent\": \"arize-python\",\n",
185-
" },\n",
186-
" compression=grpc.Compression.Gzip,\n",
187-
" )\n",
188-
" )\n",
212+
" BatchSpanProcessor(otlp_exporter)\n",
189213
")\n",
190214
"\n",
191215
"trace.set_tracer_provider(provider)"
@@ -221,6 +245,7 @@
221245
"from strands import Agent, tool\n",
222246
"from strands.models.bedrock import BedrockModel\n",
223247
"import boto3\n",
248+
"import os\n",
224249
"\n",
225250
"system_prompt = \"\"\"You are \"Restaurant Helper\", a restaurant assistant helping customers reserving tables in \n",
226251
" different restaurants. You can talk about the menus, create new bookings, get the details of an existing booking \n",
@@ -267,15 +292,17 @@
267292
" create_booking, delete_booking\n",
268293
" ],\n",
269294
" trace_attributes={\n",
270-
" \"session.id\": \"abc-1234\",\n",
295+
" \"session.id\": SESSION_ID ,\n",
271296
" \"user.id\": \"[email protected]\",\n",
272297
" \"arize.tags\": [\n",
273298
" \"Agent-SDK\",\n",
274299
" \"Arize-Project\",\n",
275300
" \"OpenInference-Integration\",\n",
276301
" ]\n",
277302
" }\n",
278-
")"
303+
")\n",
304+
"\n",
305+
"os.environ['STRANDS_AGENT_SYSTEM_PROMPT'] = agent.system_prompt"
279306
]
280307
},
281308
{
@@ -298,8 +325,8 @@
298325
"metadata": {},
299326
"outputs": [],
300327
"source": [
301-
"# Test with a question about restaurants\n",
302-
"results = agent(\"Hi, where can I eat in New York?\")\n",
328+
"# Find restaurants in a locale\n",
329+
"results = agent(\"Hi, where can I eat in Napa?\")\n",
303330
"print(results)"
304331
]
305332
},
@@ -319,8 +346,104 @@
319346
"metadata": {},
320347
"outputs": [],
321348
"source": [
322-
"# Test with a reservation request\n",
323-
"results = agent(\"Make a reservation for tonight at Rice & Spice. At 8pm, for 2 people in the name of Anna\")\n",
349+
"agent = Agent(\n",
350+
" model=model,\n",
351+
" system_prompt=system_prompt,\n",
352+
" tools=[\n",
353+
" retrieve, current_time, get_booking_details,\n",
354+
" create_booking, delete_booking\n",
355+
" ],\n",
356+
" trace_attributes={\n",
357+
" \"session.id\": SESSION_ID, \n",
358+
" \"user.id\": \"[email protected]\",\n",
359+
" \"arize.tags\": [\n",
360+
" \"Agent-SDK\",\n",
361+
" \"Arize-Project\",\n",
362+
" \"OpenInference-Integration\",\n",
363+
" ]\n",
364+
" }\n",
365+
")\n",
366+
"\n",
367+
"# Make a reservation request\n",
368+
"results = agent(\"Make a reservation for tonight at Ember and Vine. At 8pm, for 2 people in the name of Ricardo\")\n",
369+
"print(results)"
370+
]
371+
},
372+
{
373+
"cell_type": "markdown",
374+
"id": "a6403109-65e0-4b59-b2b0-f98825494c60",
375+
"metadata": {},
376+
"source": [
377+
"### Test Case 3: Cancel reservation and Rebook \n",
378+
"Now, let's cancel our reservation and make a new reservation at a different restaurant. This will trigger the delete_booking and create_booking tools."
379+
]
380+
},
381+
{
382+
"cell_type": "code",
383+
"execution_count": null,
384+
"id": "7a386d86-20b3-4536-a591-415a4e49037a",
385+
"metadata": {},
386+
"outputs": [],
387+
"source": [
388+
"agent = Agent(\n",
389+
" model=model,\n",
390+
" system_prompt=system_prompt,\n",
391+
" tools=[\n",
392+
" retrieve, current_time, get_booking_details,\n",
393+
" create_booking, delete_booking\n",
394+
" ],\n",
395+
" trace_attributes={\n",
396+
" \"session.id\": SESSION_ID, \n",
397+
" \"user.id\": \"[email protected]\",\n",
398+
" \"arize.tags\": [\n",
399+
" \"Agent-SDK\",\n",
400+
" \"Arize-Project\",\n",
401+
" \"OpenInference-Integration\",\n",
402+
" ]\n",
403+
" }\n",
404+
")\n",
405+
"#Cancel booking and rebook at another restaurant\n",
406+
"results = agent(\"I change my mind. Cancel my reservation at Ember and Vine with booking id c8118e24. Instead, book a reservation for Rice & Spice for party of 2 under Ricardo at 8pm tonight\")\n",
407+
"print(results)"
408+
]
409+
},
410+
{
411+
"cell_type": "markdown",
412+
"id": "c0fa93c4-ae15-41f6-9522-3c29ebd37585",
413+
"metadata": {},
414+
"source": [
415+
"### Test Case 4: Ask for movie recommendation (out of scope skill)\n",
416+
"Now, ask to suggest a movie purposely to see how the agent handles this question."
417+
]
418+
},
419+
{
420+
"cell_type": "code",
421+
"execution_count": null,
422+
"id": "ef472cf6-f1f2-46b5-aefc-c8a0d8b85e06",
423+
"metadata": {},
424+
"outputs": [],
425+
"source": [
426+
"agent = Agent(\n",
427+
" model=model,\n",
428+
" system_prompt=system_prompt,\n",
429+
" tools=[\n",
430+
" retrieve, current_time, get_booking_details,\n",
431+
" create_booking, delete_booking\n",
432+
" ],\n",
433+
" trace_attributes={\n",
434+
" \"session.id\": SESSION_ID, \n",
435+
" \"user.id\": \"[email protected]\",\n",
436+
" \"arize.tags\": [\n",
437+
" \"Agent-SDK\",\n",
438+
" \"Arize-Project\",\n",
439+
" \"OpenInference-Integration\",\n",
440+
" ]\n",
441+
" }\n",
442+
")\n",
443+
"# \n",
444+
"\n",
445+
"# Ask the agent for something out of scope.\n",
446+
"results = agent(\"Ok now find me a good movie that I can watch tonight after our dinner.\")\n",
324447
"print(results)"
325448
]
326449
},
@@ -489,9 +612,9 @@
489612
],
490613
"metadata": {
491614
"kernelspec": {
492-
"display_name": "3.11.11",
615+
"display_name": "conda_python3",
493616
"language": "python",
494-
"name": "python3"
617+
"name": "conda_python3"
495618
},
496619
"language_info": {
497620
"codemirror_mode": {
@@ -503,7 +626,7 @@
503626
"name": "python",
504627
"nbconvert_exporter": "python",
505628
"pygments_lexer": "ipython3",
506-
"version": "3.11.11"
629+
"version": "3.10.18"
507630
}
508631
},
509632
"nbformat": 4,

03-integrations/Openinference-Arize/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
strands-agents
2-
strands-agents-tools
1+
strands-agents==1.8.0
2+
strands-agents-tools==0.2.0
33
arize-otel
44
arize-toolkit
55
opentelemetry-api

0 commit comments

Comments
 (0)