forked from strands-agents/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke.py
More file actions
46 lines (35 loc) · 1.22 KB
/
Copy pathinvoke.py
File metadata and controls
46 lines (35 loc) · 1.22 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
"""Invoke a deployed AgentCore agent from anywhere.
Usage:
export AGENT_ARN=arn:aws:bedrock-agentcore:us-east-1:...:runtime/aim308_research_agent-XYZ
python invoke.py "what can you do?"
"""
import json
import os
import sys
import boto3
from botocore.config import Config
REGION = os.environ.get("AWS_REGION", "us-east-1")
ARN = os.environ.get("AGENT_ARN")
def main():
if not ARN:
print("⚠️ set AGENT_ARN (printed by `agentcore deploy`)")
sys.exit(1)
prompt = " ".join(sys.argv[1:]) or "Hello - introduce yourself."
session_id = os.environ.get("AIM308_SESSION", "nyc-summit-demo")
cfg = Config(read_timeout=900, connect_timeout=60)
client = boto3.client("bedrock-agentcore", region_name=REGION, config=cfg)
resp = client.invoke_agent_runtime(
agentRuntimeArn=ARN,
qualifier="DEFAULT",
runtimeSessionId=session_id,
payload=json.dumps({"prompt": prompt, "mode": "sync"}),
)
out = b""
for chunk in resp.get("response", []):
if isinstance(chunk, bytes):
out += chunk
elif isinstance(chunk, str):
out += chunk.encode()
print(out.decode("utf-8", errors="ignore"))
if __name__ == "__main__":
main()