Skip to content

Commit 39d5932

Browse files
committed
Example using AWSBedrockAgentCoreProcessor
1 parent b1fb255 commit 39d5932

File tree

9 files changed

+443
-0
lines changed

9 files changed

+443
-0
lines changed

aws-agentcore/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.bedrock_agentcore*

aws-agentcore/README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Amazon Bedrock AgentCore Example
2+
3+
This example demonstrates how to integrate an AgentCore-hosted agent into a Pipecat pipeline.
4+
5+
The pipeline looks like a standard Pipecat bot pipeline, but with an AgentCore agent taking the place of an LLM. User audio gets converted to text and sent to the AgentCore agent, which will try to do work on the user's behalf. Responses from the agent are streamed back and spoken. User and agent messages are recorded in a context object.
6+
7+
Note that unlike an LLM service found in a traditional Pipecat bot pipeline, the AgentCore agent by default does not receive the full conversation context after each user turn, only the last user message. It is up to the AgentCore agent to decide whether and how to manage its own memory (AgentCore includes memory capabilities).
8+
9+
## Prerequisites
10+
11+
- Accounts with:
12+
- AWS
13+
- OpenAI
14+
- Deepgram
15+
- Cartesia
16+
- Daily (optional)
17+
- Python 3.10 or higher
18+
- `uv` package manager
19+
20+
## Setup
21+
22+
### Install Dependencies
23+
24+
Install dependencies needed to run the Pipecat bot as well as the AgentCore CLI.
25+
26+
```bash
27+
uv sync
28+
```
29+
30+
### Set Environment Variables
31+
32+
Copy `env.example` to `.env` and fill in the values in `.env`.
33+
34+
```bash
35+
cp env.example .env
36+
```
37+
38+
**Do not worry** about `AWS_AGENT_ARN` yet. You'll obtain an agent ARN as part of the following steps, when you deploy your agent to AgentCore Runtime.
39+
40+
## Deploying Your Agent to AgentCore Runtime
41+
42+
Before you can run the Pipecat bot file, you need to deploy an agent to AgentCore Runtime. This example includes two agents:
43+
44+
- A dummy agent that reports progress while pretending to carry out a relatively long-running task
45+
- An algorithmic-problem-solving agent that can write code to answer questions
46+
47+
Below we'll do a barebones walkthrough of deploying an agent to AgentCore Runtime. For a comprehensive guide to getting started with Amazon Bedrock AgentCore, including detailed setup instructions, see the [Amazon Bedrock AgentCore Developer Guide](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html).
48+
49+
### IAM Setup
50+
51+
Configure your IAM user with the necessary policies for AgentCore usage. Start with these:
52+
53+
- `BedrockAgentCoreFullAccess`
54+
- A new policy (maybe named `BedrockAgentCoreCLI`) configured [like this](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-permissions.html#runtime-permissions-starter-toolkit)
55+
56+
You can also choose to specify more granular permissions; see [Amazon Bedrock AgentCore docs](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-permissions.html) for more information.
57+
58+
### Environment Setup
59+
60+
To simplify the remaining AgentCore deployment steps in this README, it's a good idea to export some AWS-specific environment variables:
61+
62+
```bash
63+
export AWS_SECRET_ACCESS_KEY=...
64+
export AWS_ACCESS_KEY_ID=...
65+
export AWS_REGION=...
66+
```
67+
68+
### Agent Configuration
69+
70+
Create a new AgentCore configuration.
71+
72+
```bash
73+
cd agents
74+
uv run agentcore configure -e code_agent.py
75+
```
76+
77+
Follow the interactive prompts to complete the configuration. It's OK to just accept all defaults.
78+
79+
### Agent Deployment
80+
81+
Deploy your agent to AgentCore Runtime.
82+
83+
```bash
84+
uv run agentcore launch
85+
```
86+
87+
This step will spit out the agent ARN. Copy it and paste it in your `.env` file as your `AWS_AGENT_ARN` value.
88+
89+
The above is also the command you need to run after you've updated your agent code and need to redeploy.
90+
91+
### Validation
92+
93+
Try running your agent on AgentCore Runtime.
94+
95+
```bash
96+
uv run agentcore invoke '{"prompt": "What is the meaning of life?"}'
97+
```
98+
99+
### Obtaining Your Agent ARN at Any Point
100+
101+
Your agent status will include its ARN.
102+
103+
```bash
104+
uv run agentcore status
105+
```
106+
107+
## Running The Example
108+
109+
With your agent deployed to AgentCore, you can now run the example.
110+
111+
```bash
112+
# Using SmallWebRTC transport
113+
uv run python bot.py
114+
115+
# Using Daily transport
116+
uv run python bot.py -t daily -d
117+
```

aws-agentcore/agents/.dockerignore

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Build artifacts
2+
build/
3+
dist/
4+
*.egg-info/
5+
*.egg
6+
7+
# Python cache
8+
__pycache__/
9+
__pycache__*
10+
*.py[cod]
11+
*$py.class
12+
*.so
13+
.Python
14+
15+
# Virtual environments
16+
.venv/
17+
.env
18+
venv/
19+
env/
20+
ENV/
21+
22+
# Testing
23+
.pytest_cache/
24+
.coverage
25+
.coverage*
26+
htmlcov/
27+
.tox/
28+
*.cover
29+
.hypothesis/
30+
.mypy_cache/
31+
.ruff_cache/
32+
33+
# Development
34+
*.log
35+
*.bak
36+
*.swp
37+
*.swo
38+
*~
39+
.DS_Store
40+
41+
# IDEs
42+
.vscode/
43+
.idea/
44+
45+
# Version control
46+
.git/
47+
.gitignore
48+
.gitattributes
49+
50+
# Documentation
51+
docs/
52+
*.md
53+
!README.md
54+
55+
# CI/CD
56+
.github/
57+
.gitlab-ci.yml
58+
.travis.yml
59+
60+
# Project specific
61+
tests/
62+
63+
# Bedrock AgentCore specific - keep config but exclude runtime files
64+
.bedrock_agentcore.yaml
65+
.dockerignore
66+
.bedrock_agentcore/
67+
68+
# Keep wheelhouse for offline installations
69+
# wheelhouse/

aws-agentcore/agents/code_agent.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
from bedrock_agentcore.memory.integrations.strands.config import (
4+
AgentCoreMemoryConfig,
5+
RetrievalConfig,
6+
)
7+
from bedrock_agentcore.memory.integrations.strands.session_manager import (
8+
AgentCoreMemorySessionManager,
9+
)
10+
from bedrock_agentcore.runtime import BedrockAgentCoreApp
11+
from strands import Agent
12+
from strands_tools.code_interpreter import AgentCoreCodeInterpreter
13+
14+
app = BedrockAgentCoreApp()
15+
16+
MEMORY_ID = os.getenv("BEDROCK_AGENTCORE_MEMORY_ID")
17+
REGION = os.getenv("AWS_REGION")
18+
MODEL_ID = "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
19+
20+
21+
@app.entrypoint
22+
async def invoke(payload, context):
23+
actor_id = "quickstart-user"
24+
25+
# Get runtime session ID for isolation
26+
session_id = getattr(context, "session_id", None)
27+
28+
# Create Code Interpreter with runtime session binding
29+
code_interpreter = AgentCoreCodeInterpreter(region=REGION, auto_create=True)
30+
31+
agent = Agent(
32+
model=MODEL_ID,
33+
system_prompt="""You are a helpful assistant specializing in solving algorithmic problems with code.
34+
35+
Your output will be spoken aloud by text-to-speech, so use plain language without special formatting or characters (for instance, **AVOID NUMBERED OR BULLETED LISTS**).
36+
37+
Think aloud as you work: explain your approach before coding, describe what you're doing as you write code, and analyze the results after execution. Narrate your reasoning throughout to make your process transparent and educational.
38+
39+
Also, try to be as succinct as possible. Avoid unnecessary verbosity.
40+
""",
41+
tools=[code_interpreter.code_interpreter],
42+
)
43+
44+
# Stream the response
45+
async for event in agent.stream_async(payload.get("prompt", "")):
46+
if "data" in event:
47+
chunk = event["data"]
48+
# Yield chunks as they arrive for real-time streaming
49+
yield {"response": chunk}
50+
elif "result" in event:
51+
# Final result with stop reason
52+
yield {"done": True}
53+
54+
55+
if __name__ == "__main__":
56+
app.run()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import asyncio
2+
3+
from bedrock_agentcore import BedrockAgentCoreApp
4+
5+
app = BedrockAgentCoreApp()
6+
7+
8+
@app.entrypoint
9+
async def invoke(payload, context):
10+
prompt = payload.get("prompt")
11+
12+
yield {"response": f"Handling your request: {prompt}"}
13+
14+
# Simulate some processing
15+
await asyncio.sleep(5)
16+
17+
yield {"response": f"Still working on it..."}
18+
19+
# Simulate more processing
20+
await asyncio.sleep(5)
21+
22+
yield {"response": f"Finished! The answer, as always, is 'who knows?'."}
23+
24+
# Remove yields above and uncomment the below to test non-streamed response
25+
# return {"response": f"Finished! The answer, as always, is 'who knows?'."}
26+
27+
28+
if __name__ == "__main__":
29+
app.run()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bedrock-agentcore
2+
strands-agents
3+
strands-agents-tools

0 commit comments

Comments
 (0)