Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cdk/agents/lambda/traffic-generator/traffic_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import random
import uuid
import urllib.parse as urlparse
from urllib.request import Request, urlopen
import boto3
Expand All @@ -15,6 +16,9 @@ def lambda_handler(event, context):
primary_agent_arn = os.environ.get('PRIMARY_AGENT_ARN')
nutrition_agent_arn = os.environ.get('NUTRITION_AGENT_ARN')
num_requests = int(os.environ.get('REQUESTS_PER_INVOKE', '20'))

# Use environment variable session ID or generate one
session_id = os.environ.get('SESSION_ID', f"pet-clinic-session-{str(uuid.uuid4())}")

if not primary_agent_arn:
return {
Expand All @@ -30,10 +34,10 @@ def lambda_handler(event, context):

if is_nutrition_query:
query = random.choice(prompts['nutrition-queries'])
enhanced_query = f"{query}\n\nNote: Our nutrition specialist agent ARN is {nutrition_agent_arn}" if nutrition_agent_arn else query
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}"
else:
query = random.choice(prompts['non-nutrition-queries'])
enhanced_query = query
enhanced_query = f"{query}\n\nSession ID: {session_id}"

try:
encoded_arn = urlparse.quote(primary_agent_arn, safe='')
Expand Down
4 changes: 3 additions & 1 deletion cdk/agents/lib/pet-clinic-agents-traffic-generator-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const lambda = require('aws-cdk-lib/aws-lambda');
const events = require('aws-cdk-lib/aws-events');
const targets = require('aws-cdk-lib/aws-events-targets');
const iam = require('aws-cdk-lib/aws-iam');
const crypto = require('crypto');

/**
* Lambda traffic generator for AI agents to simulate user interactions.
Expand All @@ -20,7 +21,8 @@ class PetClinicAgentsTrafficGeneratorStack extends Stack {
environment: {
PRIMARY_AGENT_ARN: props?.primaryAgentArn || '',
NUTRITION_AGENT_ARN: props?.nutritionAgentArn || '',
REQUESTS_PER_INVOKE: '20'
REQUESTS_PER_INVOKE: '20',
SESSION_ID: `pet-clinic-traffic-generator-${crypto.randomUUID()}`
}
});

Expand Down
2 changes: 1 addition & 1 deletion pet_clinic_ai_agents/nutrition_agent/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ uv
boto3
bedrock-agentcore
bedrock-agentcore-starter-toolkit
aws-opentelemetry-distro>=0.11.0
aws-opentelemetry-distro>=0.12.1
11 changes: 7 additions & 4 deletions pet_clinic_ai_agents/primary_agent/pet_clinic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):
return "Nutrition specialist configuration error. Please call (555) 123-PETS ext. 201."

try:
client = boto3.client('bedrock-agentcore')
region = os.environ.get('AWS_REGION') or os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
client = boto3.client('bedrock-agentcore', region_name=region)
response = client.invoke_agent_runtime(
agentRuntimeArn=agent_arn,
runtimeSessionId=session_id,
qualifier='DEFAULT',
payload=json.dumps({'prompt': query})
payload=json.dumps({'prompt': query}).encode('utf-8')
)
# Read the streaming response
if 'response' in response:
Expand All @@ -62,7 +63,7 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):

agent = None
agent_app = BedrockAgentCoreApp()
session_id = f"clinic-session-{str(uuid.uuid4())}"
session_id = f"pet-clinic-primary-agent-session-{str(uuid.uuid4())}"

system_prompt = (
"You are a helpful pet clinic assistant. You can help with:\n"
Expand All @@ -75,10 +76,12 @@ def consult_nutrition_specialist(query, agent_arn, session_id=None):
"- 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"
"- DO NOT use the nutrition agent for general clinic questions, appointments, hours, emergencies, or non-nutrition medical issues\n"
"- NEVER expose or mention agent ARNs in your responses to users\n"
"- If the user query contains 'session id', extract and use that session ID when calling consult_nutrition_specialist\n"
"- If no session ID is provided in the query, use the default session ID\n"
"- For medical concerns, provide general guidance and recommend scheduling a veterinary appointment\n"
"- For emergencies, immediately provide emergency contact information\n"
"- Always recommend consulting with a veterinarian for proper diagnosis and treatment\n\n"
f"Your session ID is: {session_id}. When calling consult_nutrition_specialist, use this session_id parameter."
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."
)

def create_clinic_agent():
Expand Down
2 changes: 1 addition & 1 deletion pet_clinic_ai_agents/primary_agent/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ uv
boto3
bedrock-agentcore
bedrock-agentcore-starter-toolkit
aws-opentelemetry-distro>=0.11.0
aws-opentelemetry-distro>=0.12.1