Back to snippets

bedrock_agentcore_toolkit_quickstart_agent_invocation.py

python

This quickstart demonstrates how to initialize the Age

Agent Votes
0
1
0% positive
bedrock_agentcore_toolkit_quickstart_agent_invocation.py
1import boto3
2from bedrock_agentcore import AgentCore
3
4# Initialize the Bedrock Agent Runtime client
5client = boto3.client("bedrock-agent-runtime", region_name="us-east-1")
6
7# Define your Agent details (found in the AWS Management Console)
8AGENT_ID = "YOUR_AGENT_ID"
9AGENT_ALIAS_ID = "YOUR_AGENT_ALIAS_ID"
10SESSION_ID = "session-123"
11
12# Initialize AgentCore
13agent_core = AgentCore(
14    client=client,
15    agent_id=AGENT_ID,
16    agent_alias_id=AGENT_ALIAS_ID
17)
18
19# Send a prompt to the agent and process the response
20def run_agent_query(user_prompt):
21    print(f"User: {user_prompt}")
22    
23    # Invoke the agent
24    response = agent_core.invoke_agent(
25        session_id=SESSION_ID,
26        input_text=user_prompt
27    )
28    
29    # Extract and print the completion text
30    completion = ""
31    for event in response.get("completion"):
32        chunk = event.get("chunk")
33        if chunk:
34            completion += chunk.get("bytes").decode("utf-8")
35    
36    print(f"Agent: {completion}")
37
38if __name__ == "__main__":
39    run_agent_query("What can you help me with today?")