Skip to content

Commit aee2473

Browse files
authored
Update Pet Clinic AI Agent ADOT version and set Region for Invoke Agent (#155)
*Description of changes:* - The latest ADOT release includes a fix to suppress noisy POST /invocations spans. Updating the requirements.txt to use this version helps reduce the volume of these spans. ref: aws-observability/aws-otel-python-instrumentation#464 - Modify the traffic generator to generate and include a SESSION_ID in the InvokeAgent call, following the best practices outlined by the Bedrock AgentCore runtime. ref: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-invoke-agent.html#runtime-invoke-session-management - Specify the region when creating the boto3 Bedrock AgentCore client, as it is a required parameter. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 64c9e39 commit aee2473

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

cdk/agents/lambda/traffic-generator/traffic_generator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import random
4+
import uuid
45
import urllib.parse as urlparse
56
from urllib.request import Request, urlopen
67
import boto3
@@ -15,6 +16,9 @@ def lambda_handler(event, context):
1516
primary_agent_arn = os.environ.get('PRIMARY_AGENT_ARN')
1617
nutrition_agent_arn = os.environ.get('NUTRITION_AGENT_ARN')
1718
num_requests = int(os.environ.get('REQUESTS_PER_INVOKE', '20'))
19+
20+
# Use environment variable session ID or generate one
21+
session_id = os.environ.get('SESSION_ID', f"pet-clinic-session-{str(uuid.uuid4())}")
1822

1923
if not primary_agent_arn:
2024
return {
@@ -30,10 +34,10 @@ def lambda_handler(event, context):
3034

3135
if is_nutrition_query:
3236
query = random.choice(prompts['nutrition-queries'])
33-
enhanced_query = f"{query}\n\nNote: Our nutrition specialist agent ARN is {nutrition_agent_arn}" if nutrition_agent_arn else query
37+
enhanced_query = f"{query}\n\nSession ID: {session_id}\nNote: Our nutrition specialist agent ARN is {nutrition_agent_arn}" if nutrition_agent_arn else f"{query}\n\nSession ID: {session_id}"
3438
else:
3539
query = random.choice(prompts['non-nutrition-queries'])
36-
enhanced_query = query
40+
enhanced_query = f"{query}\n\nSession ID: {session_id}"
3741

3842
try:
3943
encoded_arn = urlparse.quote(primary_agent_arn, safe='')

cdk/agents/lib/pet-clinic-agents-traffic-generator-stack.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const lambda = require('aws-cdk-lib/aws-lambda');
33
const events = require('aws-cdk-lib/aws-events');
44
const targets = require('aws-cdk-lib/aws-events-targets');
55
const iam = require('aws-cdk-lib/aws-iam');
6+
const crypto = require('crypto');
67

78
/**
89
* Lambda traffic generator for AI agents to simulate user interactions.
@@ -20,7 +21,8 @@ class PetClinicAgentsTrafficGeneratorStack extends Stack {
2021
environment: {
2122
PRIMARY_AGENT_ARN: props?.primaryAgentArn || '',
2223
NUTRITION_AGENT_ARN: props?.nutritionAgentArn || '',
23-
REQUESTS_PER_INVOKE: '20'
24+
REQUESTS_PER_INVOKE: '20',
25+
SESSION_ID: `pet-clinic-traffic-generator-${crypto.randomUUID()}`
2426
}
2527
});
2628

pet_clinic_ai_agents/nutrition_agent/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ uv
44
boto3
55
bedrock-agentcore
66
bedrock-agentcore-starter-toolkit
7-
aws-opentelemetry-distro>=0.11.0
7+
aws-opentelemetry-distro>=0.12.1

pet_clinic_ai_agents/primary_agent/pet_clinic_agent.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):
4343
return "Nutrition specialist configuration error. Please call (555) 123-PETS ext. 201."
4444

4545
try:
46-
client = boto3.client('bedrock-agentcore')
46+
region = os.environ.get('AWS_REGION') or os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
47+
client = boto3.client('bedrock-agentcore', region_name=region)
4748
response = client.invoke_agent_runtime(
4849
agentRuntimeArn=agent_arn,
4950
runtimeSessionId=session_id,
5051
qualifier='DEFAULT',
51-
payload=json.dumps({'prompt': query})
52+
payload=json.dumps({'prompt': query}).encode('utf-8')
5253
)
5354
# Read the streaming response
5455
if 'response' in response:
@@ -62,7 +63,7 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):
6263

6364
agent = None
6465
agent_app = BedrockAgentCoreApp()
65-
session_id = f"clinic-session-{str(uuid.uuid4())}"
66+
session_id = f"pet-clinic-primary-agent-session-{str(uuid.uuid4())}"
6667

6768
system_prompt = (
6869
"You are a helpful pet clinic assistant. You can help with:\n"
@@ -75,10 +76,12 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):
7576
"- ONLY use the consult_nutrition_specialist tool for EXPLICIT nutrition-related questions (diet, feeding, supplements, food recommendations, what to feed, can pets eat X, nutrition advice)\n"
7677
"- DO NOT use the nutrition agent for general clinic questions, appointments, hours, emergencies, or non-nutrition medical issues\n"
7778
"- NEVER expose or mention agent ARNs in your responses to users\n"
79+
"- If the user query contains 'session id', extract and use that session ID when calling consult_nutrition_specialist\n"
80+
"- If no session ID is provided in the query, use the default session ID\n"
7881
"- For medical concerns, provide general guidance and recommend scheduling a veterinary appointment\n"
7982
"- For emergencies, immediately provide emergency contact information\n"
8083
"- Always recommend consulting with a veterinarian for proper diagnosis and treatment\n\n"
81-
f"Your session ID is: {session_id}. When calling consult_nutrition_specialist, use this session_id parameter."
84+
f"Your default session ID is: {session_id}. When calling consult_nutrition_specialist, use the session ID from the query if provided, otherwise use this default session_id parameter."
8285
)
8386

8487
def create_clinic_agent():

pet_clinic_ai_agents/primary_agent/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ uv
44
boto3
55
bedrock-agentcore
66
bedrock-agentcore-starter-toolkit
7-
aws-opentelemetry-distro>=0.11.0
7+
aws-opentelemetry-distro>=0.12.1

0 commit comments

Comments
 (0)