|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +--> |
| 17 | + |
| 18 | +# Strands Example |
| 19 | + |
| 20 | +A minimal example showcasing a Strands agent that answers questions about Strands documentation using a curated URL knowledge base and the native Strands `http_request` tool. |
| 21 | + |
| 22 | +## Table of Contents |
| 23 | + |
| 24 | +- [Key Features](#key-features) |
| 25 | +- [Prerequisites](#prerequisites) |
| 26 | +- [Installation and Setup](#installation-and-setup) |
| 27 | + - [Install this Workflow](#install-this-workflow) |
| 28 | + - [Set Up API Keys](#set-up-api-keys) |
| 29 | +- [Run the Workflow](#run-the-workflow) |
| 30 | + - [Run the workflow (config.yml)](#1-run-the-workflow-configyml) |
| 31 | + - [Serve AgentCore-compatible endpoints (agentcore_config.yml)](#2-serve-agentcore-compatible-endpoints-agentcore_configyml) |
| 32 | + - [Evaluate accuracy and performance (eval_config.yml)](#3-evaluate-accuracy-and-performance-eval_configyml) |
| 33 | + |
| 34 | +## Key Features |
| 35 | + |
| 36 | +- **Strands framework integration**: Demonstrates support for Strands Agents in the NeMo Agent toolkit. |
| 37 | +- **AgentCore Integration**: Demonstrates an agent that can be run on Amazon Bedrock AgentCore runtime. |
| 38 | +- **Evaluation and Performance Metrics**: Runs dataset-driven evaluation and performance analysis via `nat eval`. |
| 39 | +- **Support for Model Providers**: Configuration includes NIM, OpenAI, and AWS Bedrock options. |
| 40 | + |
| 41 | +## Prerequisites |
| 42 | + |
| 43 | +- NeMo Agent toolkit installed. See the [Install Guide](../../../docs/source/quick-start/installing.md#install-from-source). |
| 44 | +- API keys as required by your chosen models. |
| 45 | + |
| 46 | +## Installation and Setup |
| 47 | + |
| 48 | +### Install this Workflow |
| 49 | + |
| 50 | +```bash |
| 51 | +uv pip install -e examples/frameworks/strands_demo |
| 52 | +``` |
| 53 | + |
| 54 | +### Set Up API Keys |
| 55 | + |
| 56 | +```bash |
| 57 | +export NVIDIA_API_KEY=<YOUR_NVIDIA_API_KEY> |
| 58 | +# Optional if you switch models in the config |
| 59 | +export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> |
| 60 | +export AWS_ACCESS_KEY_ID=<YOUR_AWS_ACCESS_KEY_ID> |
| 61 | +export AWS_SECRET_ACCESS_KEY=<YOUR_AWS_SECRET_ACCESS_KEY> |
| 62 | +export AWS_DEFAULT_REGION=us-east-1 |
| 63 | +``` |
| 64 | + |
| 65 | +## Run the Workflow |
| 66 | + |
| 67 | +The `configs/` directory contains four ready-to-use configurations. Use the commands below. |
| 68 | + |
| 69 | +### 1) Run the workflow (config.yml) |
| 70 | + |
| 71 | +```bash |
| 72 | +nat run --config_file examples/frameworks/strands_demo/configs/config.yml \ |
| 73 | + --input "How do I use the Strands Agents API?" |
| 74 | +``` |
| 75 | + |
| 76 | +**Expected Workflow Output** |
| 77 | +The workflow produces a large amount of output, the end of the output should contain something similar to the following: |
| 78 | + |
| 79 | +```console |
| 80 | +Workflow Result: |
| 81 | +['To answer your question about using the Strands Agents API, I\'ll need to search for the relevant documentation. Let me do that for you.Thank you for providing that information. To get the most relevant information about using the Strands Agents API, I\'ll fetch the content from the "strands_agent_loop" URL, as it seems to be the most relevant to your question about using the API.Based on the information from the Strands Agents documentation, I can provide you with an overview of how to use the Strands Agents API. Here\'s a summary of the key points:\n\n1. Initialization:\n To use the Strands Agents API, you start by initializing an agent with the necessary components:\n\n ```python\n from strands import Agent\n from strands_tools import calculator\n\n agent = Agent(\n tools=[calculator],\n system_prompt="You are a helpful assistant."\n )\n ```\n\n This sets up the agent with tools (like a calculator in this example) and a system prompt.\n\n2. Processing User Input:\n You can then use the agent to process user input:\n\n ```python\n result = agent("Calculate 25 * 48")\n ```\n\n3. Agent Loop:\n The Strands Agents API uses an "agent loop" to process requests. This loop includes:\n - Receiving user input and context\n - Processing the input using a language model (LLM)\n - Deciding whether to use tools to gather information or perform actions\n - Executing tools and receiving results\n - Continuing reasoning with new information\n - Producing a final response or iterating through the loop again\n\n4. Tool Execution:\n The agent can use tools as part of its processing. When the model decides to use a tool, it will format a request like this:\n\n ```json\n {\n "role": "assistant",\n "content": [\n {\n "toolUse": {\n "toolUseId": "tool_123",\n "name": "calculator",\n "input": {\n "expression": "25 * 48"\n }\n }\n }\n ]\n }\n ```\n\n The API then executes the tool and returns the result to the model for further processing.\n\n5. Recursive Processing:\n The agent loop can continue recursively if more tool executions or multi-step reasoning is required.\n\n6. Completion:\n The loop completes when the model generates a final text response or when an unhandled exception occurs.\n\nTo effectively use the Strands Agents API, you should:\n- Initialize your agent with appropriate tools and system prompts\n- Design your tools carefully, considering token limits and complexity\n- Handle potential exceptions, such as the MaxTokensReachedException\n\nRemember that the API is designed to support complex, multi-step reasoning and actions with seamless integration of tools and language models. It\'s flexible enough to handle a wide range of tasks and can be customized to your specific needs.'] |
| 82 | +``` |
| 83 | + |
| 84 | +### 2) Serve AgentCore-compatible endpoints (agentcore_config.yml) |
| 85 | + |
| 86 | +To run the agent on Amazon Bedrock AgentCore [runtime](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/getting-started-custom.html). Note that `agentcore_config.yml` defines two required endpoints for AgentCore. This configuration is a general requirement for any agent, regardless of whether it uses the Strands integration. |
| 87 | + |
| 88 | +```bash |
| 89 | +nat serve --config_file examples/frameworks/strands_demo/configs/agentcore_config.yml |
| 90 | +``` |
| 91 | + |
| 92 | +### 3) Evaluate accuracy and performance (eval_config.yml) |
| 93 | + |
| 94 | +Runs the workflow over a dataset and computes evaluation and performance metrics. See the evaluation guide and profiling guides in `docs/source/workflows/` for more information. |
| 95 | + |
| 96 | +```bash |
| 97 | +nat eval --config_file examples/frameworks/strands_demo/configs/eval_config.yml |
| 98 | +``` |
| 99 | +> Tip: If you hit rate limits, lower concurrency: `--override eval.general.max_concurrency 1`. |
0 commit comments