Skip to content

Commit 7210cf8

Browse files
committed
Revert "Fix agent error handling for 404 nutrition service responses (#229)"
This reverts commit 297e341.
1 parent a0fa462 commit 7210cf8

File tree

2 files changed

+11
-53
lines changed

2 files changed

+11
-53
lines changed

pet_clinic_ai_agents/nutrition_agent/nutrition_agent.py

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,22 @@
1616
def get_nutrition_data(pet_type):
1717
"""Helper function to get nutrition data from the API"""
1818
if not NUTRITION_SERVICE_URL:
19-
return {"error": "service_unavailable", "facts": "", "products": ""}
19+
return {"facts": "Error: Nutrition service not found", "products": ""}
2020

2121
try:
2222
response = requests.get(f"{NUTRITION_SERVICE_URL}/{pet_type.lower()}", timeout=5)
2323

2424
if response.status_code == 200:
2525
data = response.json()
26-
return {"error": None, "facts": data.get('facts', ''), "products": data.get('products', '')}
27-
elif response.status_code == 404:
28-
return {"error": "pet_type_not_found", "facts": "", "products": ""}
29-
else:
30-
return {"error": "service_error", "facts": "", "products": ""}
26+
return {"facts": data.get('facts', ''), "products": data.get('products', '')}
27+
return {"facts": f"Error: Nutrition service could not find information for pet: {pet_type.lower()}", "products": ""}
3128
except requests.RequestException:
32-
return {"error": "service_unavailable", "facts": "", "products": ""}
29+
return {"facts": "Error: Nutrition service down", "products": ""}
3330

3431
@tool
3532
def get_feeding_guidelines(pet_type):
3633
"""Get feeding guidelines based on pet type"""
3734
data = get_nutrition_data(pet_type)
38-
39-
if data["error"] == "pet_type_not_found":
40-
return f"I don't have nutrition information for {pet_type} pets. Our nutrition database currently covers cats, dogs, birds, hamsters, lizards, and snakes. Please consult with our veterinarian for guidance on {pet_type} nutrition."
41-
elif data["error"] == "service_unavailable":
42-
return "Our nutrition service is currently unavailable. Please call our clinic at (555) 123-PETS for nutrition guidance."
43-
elif data["error"] == "service_error":
44-
return "There was an error accessing nutrition information. Please call our clinic at (555) 123-PETS for assistance."
45-
4635
result = f"Nutrition info for {pet_type}: {data['facts']}"
4736
if data['products']:
4837
result += f" Recommended products available at our clinic: {data['products']}"
@@ -52,14 +41,6 @@ def get_feeding_guidelines(pet_type):
5241
def get_dietary_restrictions(pet_type):
5342
"""Get dietary recommendations for specific health conditions by animal type"""
5443
data = get_nutrition_data(pet_type)
55-
56-
if data["error"] == "pet_type_not_found":
57-
return f"I don't have dietary restriction information for {pet_type} pets. Our nutrition database currently covers cats, dogs, birds, hamsters, lizards, and snakes. Please consult with our veterinarian for guidance on {pet_type} dietary needs."
58-
elif data["error"] == "service_unavailable":
59-
return "Our nutrition service is currently unavailable. Please call our clinic at (555) 123-PETS for dietary guidance."
60-
elif data["error"] == "service_error":
61-
return "There was an error accessing dietary information. Please call our clinic at (555) 123-PETS for assistance."
62-
6344
result = f"Dietary info for {pet_type}: {data['facts']}. Consult veterinarian for condition-specific advice."
6445
if data['products']:
6546
result += f" Recommended products available at our clinic: {data['products']}"
@@ -69,14 +50,6 @@ def get_dietary_restrictions(pet_type):
6950
def get_nutritional_supplements(pet_type):
7051
"""Get supplement recommendations by animal type"""
7152
data = get_nutrition_data(pet_type)
72-
73-
if data["error"] == "pet_type_not_found":
74-
return f"I don't have supplement information for {pet_type} pets. Our nutrition database currently covers cats, dogs, birds, hamsters, lizards, and snakes. Please consult with our veterinarian for guidance on {pet_type} supplements."
75-
elif data["error"] == "service_unavailable":
76-
return "Our nutrition service is currently unavailable. Please call our clinic at (555) 123-PETS for supplement guidance."
77-
elif data["error"] == "service_error":
78-
return "There was an error accessing supplement information. Please call our clinic at (555) 123-PETS for assistance."
79-
8053
result = f"Supplement info for {pet_type}: {data['facts']}. Consult veterinarian for supplements."
8154
if data['products']:
8255
result += f" Recommended products available at our clinic: {data['products']}"
@@ -85,15 +58,8 @@ def get_nutritional_supplements(pet_type):
8558
@tool
8659
def create_order(product_name, pet_type, quantity=1):
8760
"""Create an order for a recommended product. Requires product_name, pet_type, and optional quantity (default 1)."""
61+
product_lower = product_name.lower()
8862
data = get_nutrition_data(pet_type)
89-
90-
if data["error"] == "pet_type_not_found":
91-
return f"I cannot create an order for {pet_type} products as we don't have nutrition information for this pet type. Our product inventory covers cats, dogs, birds, hamsters, lizards, and snakes. Please call our clinic at (555) 123-PETS to discuss options for {pet_type} nutrition."
92-
elif data["error"] == "service_unavailable":
93-
return "Our nutrition service is currently unavailable. Please call our clinic at (555) 123-PETS to place your order."
94-
elif data["error"] == "service_error":
95-
return "There was an error accessing our product inventory. Please call our clinic at (555) 123-PETS to place your order."
96-
9763
if data['products'] and product_name.lower() in data['products'].lower():
9864
order_id = f"ORD-{uuid.uuid4().hex[:8].upper()}"
9965
return f"Order {order_id} created for {quantity}x {product_name}. Total: ${quantity * 29.99:.2f}. Expected delivery: 3-5 business days. You can pick it up at our clinic or we'll ship it to you."
@@ -110,14 +76,8 @@ def create_nutrition_agent():
11076
system_prompt = (
11177
"You are a specialized pet nutrition expert at our veterinary clinic, providing accurate, evidence-based dietary guidance for pets. "
11278
"Never mention using any API, tools, or external services - present all advice as your own expert knowledge.\n\n"
113-
"CRITICAL: NEVER fabricate or invent product names, nutrition facts, or recommendations when you don't have information for a specific pet type. "
114-
"If you don't have nutrition information for a pet type, clearly state this limitation and refer to our veterinarian.\n\n"
11579
"When providing nutrition guidance:\n"
116-
"- ONLY provide specific product recommendations when you have verified product information for that pet type\n"
117-
"- If nutrition information is unavailable for a pet type, clearly state: 'I don't have nutrition information for [pet type] pets'\n"
118-
"- List the pet types you DO have information for: cats, dogs, birds, hamsters, lizards, and snakes\n"
119-
"- For unsupported pet types, always recommend consulting with our veterinarian\n"
120-
"- When you DO have information, use the specific nutrition information available to you as the foundation for your recommendations\n"
80+
"- Use the specific nutrition information available to you as the foundation for your recommendations\n"
12181
"- Always recommend the SPECIFIC PRODUCT NAMES provided to you that pet owners should buy FROM OUR PET CLINIC\n"
12282
"- Mention our branded products by name (like PurrfectChoice, BarkBite, FeatherFeast, etc.) when recommending food\n"
12383
"- Emphasize that we carry high-quality, veterinarian-recommended food brands at our clinic\n"

pet_clinic_ai_agents/primary_agent/pet_clinic_agent.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def consult_nutrition_specialist(query):
4242

4343
agent_arn = os.environ.get('NUTRITION_AGENT_ARN')
4444
if not agent_arn:
45-
return "Our nutrition specialist is currently unavailable. Please call (555) 123-PETS ext. 201 for nutrition guidance."
45+
return "Nutrition specialist configuration error. Please call (555) 123-PETS ext. 201."
4646

4747
try:
4848
region = os.environ.get('AWS_REGION') or os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
@@ -61,9 +61,9 @@ def consult_nutrition_specialist(query):
6161
else:
6262
return "Our nutrition specialist is experiencing high demand. Please try again in a few moments or call (555) 123-PETS ext. 201."
6363
except ClientError as e:
64-
return "Our nutrition specialist is currently unavailable. Please call (555) 123-PETS ext. 201 for nutrition guidance."
64+
return str(e)
6565
except Exception as e:
66-
return "Our nutrition specialist is currently unavailable. Please call (555) 123-PETS ext. 201 for nutrition guidance."
66+
return "Unable to reach our nutrition specialist. Please call (555) 123-PETS ext. 201."
6767

6868
agent = None
6969
agent_app = BedrockAgentCoreApp()
@@ -85,10 +85,8 @@ def consult_nutrition_specialist(query):
8585
"- NEVER expose or mention agent ARNs, tools, APIs, or any technical details in your responses to users\n"
8686
"- NEVER say things like 'I'm using a tool' or 'Let me look that up' - just respond naturally\n"
8787
"- When consulting the nutrition specialist, ONLY say 'Let me consult our nutrition specialist' - nothing else about the process\n"
88-
"- If the specialist indicates they don't have information for a specific pet type, relay this information clearly to the customer\n"
89-
"- Our nutrition database currently covers cats, dogs, birds, hamsters, lizards, and snakes - for other pet types, recommend consulting with our veterinarian\n"
90-
"- If the specialist returns an error or indicates unavailability, inform the customer that our specialist is currently unavailable and provide the phone number\n"
91-
"- For nutrition questions, provide 2-3 product recommendations in a brief bulleted list when available, then suggest monitoring and consultation if needed\n"
88+
"- If the specialist returns an error or indicates unavailability, inform the customer that our specialist is currently unavailable\n"
89+
"- For nutrition questions, provide 2-3 product recommendations in a brief bulleted list, then suggest monitoring and consultation if needed\n"
9290
"- Always recommend purchasing products from our pet clinic\n"
9391
"- For medical concerns, provide general guidance and recommend scheduling a veterinary appointment\n"
9492
"- For emergencies, immediately provide emergency contact information"

0 commit comments

Comments
 (0)