|
60 | 60 | "* AWS account\n", |
61 | 61 | "* Anthropic Claude Sonnet 4.5 enabled on Amazon Bedrock\n", |
62 | 62 | "\n", |
63 | | - "Let's now install the requirement packages for our Strands Agent Agent" |
| 63 | + "Let's now install the required packages for our Strands Agent" |
64 | 64 | ] |
65 | 65 | }, |
66 | 66 | { |
|
107 | 107 | "source": [ |
108 | 108 | "### Creating and invoking agent with stream_async\n", |
109 | 109 | "\n", |
110 | | - "Let's now create our agent with a built-in calculator tool and no `callback_handler`. We will use the `stream_async` method to iteract over the streamed agent events" |
| 110 | + "Let's now create our agent with a built-in calculator tool and no `callback_handler`. We will use the `stream_async` method to iterate over the streamed agent events" |
111 | 111 | ] |
112 | 112 | }, |
113 | 113 | { |
114 | 114 | "cell_type": "code", |
115 | 115 | "execution_count": null, |
116 | 116 | "metadata": {}, |
117 | 117 | "outputs": [], |
118 | | - "source": "# Initialize our agent without a callback handler\nagent = Agent(\n model=\"us.anthropic.claude-sonnet-4-5-20250929-v1:0\", # Optional: Specify the model ID\n tools=[calculator], \n callback_handler=None)\n\n# Async function that iterators over streamed agent events\n\n\nasync def process_streaming_response():\n agent_stream = agent.stream_async(\"Calculate 2+2\")\n async for event in agent_stream:\n print(event)\n\n\n# Run the agent\nawait process_streaming_response()" |
| 118 | + "source": [ |
| 119 | + "# Initialize our agent without a callback handler\n", |
| 120 | + "agent = Agent(\n", |
| 121 | + " model=\"us.anthropic.claude-sonnet-4-5-20250929-v1:0\", # Optional: Specify the model ID\n", |
| 122 | + " tools=[calculator], \n", |
| 123 | + " callback_handler=None)\n", |
| 124 | + "\n", |
| 125 | + "# Async function that iterates over streamed agent events\n", |
| 126 | + "\n", |
| 127 | + "\n", |
| 128 | + "async def process_streaming_response():\n", |
| 129 | + " agent_stream = agent.stream_async(\"Calculate 2+2\")\n", |
| 130 | + " async for event in agent_stream:\n", |
| 131 | + " print(event)\n", |
| 132 | + "\n", |
| 133 | + "\n", |
| 134 | + "# Run the agent\n", |
| 135 | + "await process_streaming_response()" |
| 136 | + ] |
119 | 137 | }, |
120 | 138 | { |
121 | 139 | "cell_type": "markdown", |
|
146 | 164 | } |
147 | 165 | }, |
148 | 166 | "outputs": [], |
149 | | - "source": "# Async function that iterators over streamed agent events\n\n\nasync def process_streaming_response():\n agent_stream = agent.stream_async(\"What is the capital of France and what is 42+7?\")\n async for event in agent_stream:\n # Track event loop lifecycle\n if event.get(\"init_event_loop\", False):\n print(\"🔄 Event loop initialized\")\n elif event.get(\"start_event_loop\", False):\n print(\"▶️ Event loop cycle starting\")\n elif event.get(\"start\", False):\n print(\"📝 New cycle started\")\n elif \"message\" in event:\n print(f\"📬 New message created: {event['message']['role']}\")\n elif event.get(\"force_stop\", False):\n print(\n f\"🛑 Event loop force-stopped: {event.get('force_stop_reason', 'unknown reason')}\"\n )\n\n # Track tool usage\n if \"current_tool_use\" in event and event[\"current_tool_use\"].get(\"name\"):\n tool_name = event[\"current_tool_use\"][\"name\"]\n print(f\"🔧 Using tool: {tool_name}\")\n\n # Show only a snippet of text to keep output clean\n if \"data\" in event:\n # Only show first 20 chars of each chunk for demo purposes\n data_snippet = event[\"data\"][:20] + (\n \"...\" if len(event[\"data\"]) > 20 else \"\"\n )\n print(f\"📟 Text: {data_snippet}\")\n\n return event[\"result\"]\n\n\n# Run the agent\nawait process_streaming_response()" |
| 167 | + "source": [ |
| 168 | + "# Async function that iterates over streamed agent events\n", |
| 169 | + "\n", |
| 170 | + "\n", |
| 171 | + "async def process_streaming_response():\n", |
| 172 | + " agent_stream = agent.stream_async(\"What is the capital of France and what is 42+7?\")\n", |
| 173 | + " async for event in agent_stream:\n", |
| 174 | + " # Track event loop lifecycle\n", |
| 175 | + " if event.get(\"init_event_loop\", False):\n", |
| 176 | + " print(\"🔄 Event loop initialized\")\n", |
| 177 | + " elif event.get(\"start_event_loop\", False):\n", |
| 178 | + " print(\"▶️ Event loop cycle starting\")\n", |
| 179 | + " elif event.get(\"start\", False):\n", |
| 180 | + " print(\"📝 New cycle started\")\n", |
| 181 | + " elif \"message\" in event:\n", |
| 182 | + " print(f\"📬 New message created: {event['message']['role']}\")\n", |
| 183 | + " elif event.get(\"force_stop\", False):\n", |
| 184 | + " print(\n", |
| 185 | + " f\"🛑 Event loop force-stopped: {event.get('force_stop_reason', 'unknown reason')}\"\n", |
| 186 | + " )\n", |
| 187 | + "\n", |
| 188 | + " # Track tool usage\n", |
| 189 | + " if \"current_tool_use\" in event and event[\"current_tool_use\"].get(\"name\"):\n", |
| 190 | + " tool_name = event[\"current_tool_use\"][\"name\"]\n", |
| 191 | + " print(f\"🔧 Using tool: {tool_name}\")\n", |
| 192 | + "\n", |
| 193 | + " # Show only a snippet of text to keep output clean\n", |
| 194 | + " if \"data\" in event:\n", |
| 195 | + " # Only show first 20 chars of each chunk for demo purposes\n", |
| 196 | + " data_snippet = event[\"data\"][:20] + (\n", |
| 197 | + " \"...\" if len(event[\"data\"]) > 20 else \"\"\n", |
| 198 | + " )\n", |
| 199 | + " print(f\"📟 Text: {data_snippet}\")\n", |
| 200 | + "\n", |
| 201 | + " return event[\"result\"]\n", |
| 202 | + "\n", |
| 203 | + "\n", |
| 204 | + "# Run the agent\n", |
| 205 | + "await process_streaming_response()" |
| 206 | + ] |
150 | 207 | }, |
151 | 208 | { |
152 | 209 | "cell_type": "markdown", |
|
186 | 243 | "@app.post(\"/stream\")\n", |
187 | 244 | "async def stream_response(request: PromptRequest):\n", |
188 | 245 | " async def generate():\n", |
189 | | - " agent = Agent(tools=[calculator, weather_forecast], callback_handler=None)\n", |
| 246 | + " agent = Agent(\n", |
| 247 | + " model=\"us.anthropic.claude-sonnet-4-5-20250929-v1:0\", # Specify the model ID; do not rely on the SDK default, which can reach end of life\n", |
| 248 | + " tools=[calculator, weather_forecast],\n", |
| 249 | + " callback_handler=None,\n", |
| 250 | + " )\n", |
190 | 251 | " try:\n", |
191 | 252 | " async for event in agent.stream_async(request.prompt):\n", |
192 | 253 | " if \"data\" in event:\n", |
|
292 | 353 | "source": [ |
293 | 354 | "### Congratulations!\n", |
294 | 355 | "\n", |
295 | | - "In this notebook you learned how to stream your agents outputs using async iteractors and callback handlers. " |
| 356 | + "In this notebook you learned how to stream your agents outputs using async iterators and callback handlers. " |
296 | 357 | ] |
297 | 358 | } |
298 | 359 | ], |
|
0 commit comments