-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_with_kbase.py
More file actions
84 lines (73 loc) · 2.46 KB
/
agent_with_kbase.py
File metadata and controls
84 lines (73 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Example: AgentCore runtime with a Knowledge Base attached.
Usage:
cdk synth # Verify CloudFormation templates
cdk deploy --all # Deploy both stacks to AWS
After deployment:
1. Upload documents to the KB data bucket (see DataBucketName output)
2. Wait for auto-sync to ingest (default 5 min batch window)
3. Invoke the AgentCore runtime and ask questions about your documents
Deploy/Test with:
cdk deploy --all --require-approval broadening \
--app "python agent_with_kb.app.py"
Testing it:
1) Set up a small knowledge base file in terminal for the dev account:
echo "The team standup is every day at 9:30am." > test-doc.txt
aws s3 cp test-doc.txt s3://my-agent-kb1-kb-data-development/
2) Run from the terminal with bedrock-agentcore cli
(assuming deployed in dev account, runtime arn from deploy output):
aws bedrock-agentcore invoke-agent-runtime \
--agent-runtime-arn <RUNTIME-ARN> \
--payload "$(echo -n '{"prompt":"When is standup?"}' | base64)" \
--region eu-west-2 \
outfile.json
"""
import aws_cdk as cdk
from gds_idea_cdk_constructs import DeploymentConfig
from gds_idea_cdk_constructs.agent_core import (
DEFAULT_AGENT_CODE_DIR,
AgentCore,
AgentCoreProperties,
CustomAgent,
MemoryConfig,
)
from gds_idea_cdk_constructs.knowledge_base import (
ChunkingConfig,
KnowledgeBase,
KnowledgeBaseProps,
)
app = cdk.App()
# --- Environment ---
cdk_env = cdk.Environment(
account="992382722318",
region="eu-west-2",
)
# --- Shared config (resolves from Secrets Manager) ---
config = DeploymentConfig(cdk_env)
# --- Knowledge Base ---
kb = KnowledgeBase(
app,
deployment_config=config,
app_config="my-agent-kb1",
kb_props=KnowledgeBaseProps(
chunking=ChunkingConfig.fixed_size(max_tokens=500, overlap_percentage=10),
description="Knowledge base for the agent demo",
retain_on_delete=False,
),
)
# --- AgentCore Runtime (with KB attached) ---
agent = AgentCore(
app,
"MyAgentStack",
props=AgentCoreProperties(
runtime_name="my_kb_agent",
memory=MemoryConfig(name="my_kb_agent_session_store"),
agent=CustomAgent(
agent_code_directory=DEFAULT_AGENT_CODE_DIR,
environment_variables=kb.environment_variables,
),
),
env=cdk_env,
)
# --- Cross-stack: grant the agent permission to query the KB ---
kb.grant_retrieve(agent.runtime_role)
app.synth()