|
83 | 83 | "metadata": {}, |
84 | 84 | "outputs": [], |
85 | 85 | "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 " |
87 | 97 | ] |
88 | 98 | }, |
89 | 99 | { |
|
98 | 108 | { |
99 | 109 | "cell_type": "code", |
100 | 110 | "execution_count": null, |
101 | | - "id": "5545c2d3", |
| 111 | + "id": "becbe73a-dd49-42c9-b79a-a41bcb25216b", |
102 | 112 | "metadata": {}, |
103 | 113 | "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": [], |
104 | 126 | "source": [ |
105 | 127 | "!sh deploy_prereqs.sh" |
106 | 128 | ] |
|
125 | 147 | "outputs": [], |
126 | 148 | "source": [ |
127 | 149 | "import os\n", |
| 150 | + "\n", |
| 151 | + "#Set Arize Endpoint, API and Space ID keys as env variables\n", |
128 | 152 | "API_KEY = \"your-api-key\"\n", |
129 | 153 | "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", |
130 | 156 | "ENDPOINT = \"otlp.arize.com:443\"\n", |
131 | 157 | "os.environ[\"ARIZE_SPACE_ID\"] = SPACE_ID\n", |
132 | 158 | "os.environ[\"ARIZE_API_KEY\"] = API_KEY\n", |
|
152 | 178 | { |
153 | 179 | "cell_type": "code", |
154 | 180 | "execution_count": null, |
155 | | - "id": "11716548", |
| 181 | + "id": "0941c213-9466-450c-9bba-a9ed37415f8d", |
156 | 182 | "metadata": {}, |
157 | 183 | "outputs": [], |
158 | 184 | "source": [ |
| 185 | + "from opentelemetry import trace\n", |
| 186 | + "from opentelemetry.sdk.trace import TracerProvider\n", |
159 | 187 | "from opentelemetry.sdk.trace.export import BatchSpanProcessor\n", |
| 188 | + "from opentelemetry.sdk.resources import Resource\n", |
160 | 189 | "from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter\n", |
161 | 190 | "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", |
165 | 191 | "\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", |
172 | 200 | "\n", |
173 | | - "provider.add_span_processor(StrandsToOpenInferenceProcessor(debug=True))\n", |
| 201 | + "provider = TracerProvider(resource=resource)\n", |
| 202 | + "provider.add_span_processor(strands_processor)\n", |
174 | 203 | "\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", |
175 | 211 | "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", |
189 | 213 | ")\n", |
190 | 214 | "\n", |
191 | 215 | "trace.set_tracer_provider(provider)" |
|
221 | 245 | "from strands import Agent, tool\n", |
222 | 246 | "from strands.models.bedrock import BedrockModel\n", |
223 | 247 | "import boto3\n", |
| 248 | + "import os\n", |
224 | 249 | "\n", |
225 | 250 | "system_prompt = \"\"\"You are \"Restaurant Helper\", a restaurant assistant helping customers reserving tables in \n", |
226 | 251 | " different restaurants. You can talk about the menus, create new bookings, get the details of an existing booking \n", |
|
267 | 292 | " create_booking, delete_booking\n", |
268 | 293 | " ],\n", |
269 | 294 | " trace_attributes={\n", |
270 | | - " \"session.id\": \"abc-1234\",\n", |
| 295 | + " \"session.id\": SESSION_ID ,\n", |
271 | 296 | " \"user.id\": \"[email protected]\",\n", |
272 | 297 | " \"arize.tags\": [\n", |
273 | 298 | " \"Agent-SDK\",\n", |
274 | 299 | " \"Arize-Project\",\n", |
275 | 300 | " \"OpenInference-Integration\",\n", |
276 | 301 | " ]\n", |
277 | 302 | " }\n", |
278 | | - ")" |
| 303 | + ")\n", |
| 304 | + "\n", |
| 305 | + "os.environ['STRANDS_AGENT_SYSTEM_PROMPT'] = agent.system_prompt" |
279 | 306 | ] |
280 | 307 | }, |
281 | 308 | { |
|
298 | 325 | "metadata": {}, |
299 | 326 | "outputs": [], |
300 | 327 | "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", |
303 | 330 | "print(results)" |
304 | 331 | ] |
305 | 332 | }, |
|
319 | 346 | "metadata": {}, |
320 | 347 | "outputs": [], |
321 | 348 | "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", |
324 | 447 | "print(results)" |
325 | 448 | ] |
326 | 449 | }, |
|
489 | 612 | ], |
490 | 613 | "metadata": { |
491 | 614 | "kernelspec": { |
492 | | - "display_name": "3.11.11", |
| 615 | + "display_name": "conda_python3", |
493 | 616 | "language": "python", |
494 | | - "name": "python3" |
| 617 | + "name": "conda_python3" |
495 | 618 | }, |
496 | 619 | "language_info": { |
497 | 620 | "codemirror_mode": { |
|
503 | 626 | "name": "python", |
504 | 627 | "nbconvert_exporter": "python", |
505 | 628 | "pygments_lexer": "ipython3", |
506 | | - "version": "3.11.11" |
| 629 | + "version": "3.10.18" |
507 | 630 | } |
508 | 631 | }, |
509 | 632 | "nbformat": 4, |
|
0 commit comments