Skip to content

Commit 3a1e3ed

Browse files
Fix agent error handling - update system prompt for graceful missing data handling
1 parent b0a8b60 commit 3a1e3ed

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

pet_clinic_ai_agents/nutrition_agent/nutrition_agent.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
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 {"facts": "Error: Nutrition service not found", "products": ""}
19+
return {"facts": "Error: Nutrition service not found", "products": "", "has_data": False}
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 {"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": ""}
26+
return {"facts": data.get('facts', ''), "products": data.get('products', ''), "has_data": True}
27+
return {"facts": f"No specific nutrition data available for {pet_type.lower()}", "products": "", "has_data": False}
2828
except requests.RequestException:
29-
return {"facts": "Error: Nutrition service down", "products": ""}
29+
return {"facts": "Nutrition service temporarily unavailable", "products": "", "has_data": False}
3030

3131
@tool
3232
def get_feeding_guidelines(pet_type):
3333
"""Get feeding guidelines based on pet type"""
3434
data = get_nutrition_data(pet_type)
3535
result = f"Nutrition info for {pet_type}: {data['facts']}"
36-
if data['products']:
36+
if data['has_data'] and data['products']:
3737
result += f" Recommended products available at our clinic: {data['products']}"
3838
return result
3939

@@ -42,7 +42,7 @@ def get_dietary_restrictions(pet_type):
4242
"""Get dietary recommendations for specific health conditions by animal type"""
4343
data = get_nutrition_data(pet_type)
4444
result = f"Dietary info for {pet_type}: {data['facts']}. Consult veterinarian for condition-specific advice."
45-
if data['products']:
45+
if data['has_data'] and data['products']:
4646
result += f" Recommended products available at our clinic: {data['products']}"
4747
return result
4848

@@ -51,7 +51,7 @@ def get_nutritional_supplements(pet_type):
5151
"""Get supplement recommendations by animal type"""
5252
data = get_nutrition_data(pet_type)
5353
result = f"Supplement info for {pet_type}: {data['facts']}. Consult veterinarian for supplements."
54-
if data['products']:
54+
if data['has_data'] and data['products']:
5555
result += f" Recommended products available at our clinic: {data['products']}"
5656
return result
5757

@@ -60,11 +60,14 @@ def create_order(product_name, pet_type, quantity=1):
6060
"""Create an order for a recommended product. Requires product_name, pet_type, and optional quantity (default 1)."""
6161
product_lower = product_name.lower()
6262
data = get_nutrition_data(pet_type)
63-
if data['products'] and product_name.lower() in data['products'].lower():
63+
if data['has_data'] and data['products'] and product_name.lower() in data['products'].lower():
6464
order_id = f"ORD-{uuid.uuid4().hex[:8].upper()}"
6565
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."
6666

67-
return f"Sorry, {product_name} is not available in our inventory for {pet_type}. Available products: {data['products']}"
67+
if data['has_data'] and data['products']:
68+
return f"Sorry, {product_name} is not available in our inventory for {pet_type}. Available products: {data['products']}"
69+
else:
70+
return f"Sorry, we don't currently have specific product recommendations for {pet_type}. Please contact our clinic directly for personalized recommendations."
6871

6972
def create_nutrition_agent():
7073
model = BedrockModel(
@@ -78,12 +81,13 @@ def create_nutrition_agent():
7881
"Never mention using any API, tools, or external services - present all advice as your own expert knowledge.\n\n"
7982
"When providing nutrition guidance:\n"
8083
"- Use the specific nutrition information available to you as the foundation for your recommendations\n"
81-
"- Always recommend the SPECIFIC PRODUCT NAMES provided to you that pet owners should buy FROM OUR PET CLINIC\n"
82-
"- Mention our branded products by name (like PurrfectChoice, BarkBite, FeatherFeast, etc.) when recommending food\n"
83-
"- Emphasize that we carry high-quality, veterinarian-recommended food brands at our clinic\n"
84-
"- Give actionable dietary recommendations including feeding guidelines, restrictions, and supplements\n"
85-
"- Expand on basic nutrition facts with comprehensive guidance for age, weight, and health conditions\n"
86-
"- Always mention that pet owners can purchase the recommended food items directly from our clinic for convenience and quality assurance\n"
84+
"- ONLY recommend specific product names when you have confirmed product data available\n"
85+
"- If specific nutrition data is unavailable for a pet type, provide general nutrition guidance and recommend consulting our veterinarians\n"
86+
"- When you have product information, mention our branded products by name (like PurrfectChoice, BarkBite, FeatherFeast, etc.)\n"
87+
"- Emphasize that we carry high-quality, veterinarian-recommended food brands at our clinic when products are available\n"
88+
"- Give actionable dietary recommendations including feeding guidelines, restrictions, and supplements based on available data\n"
89+
"- If specific data is missing, provide general pet nutrition advice and suggest scheduling a consultation\n"
90+
"- Always mention that pet owners can contact our clinic for personalized recommendations when specific data isn't available\n"
8791
"- If asked to order or purchase a product, use the create_order tool to place the order"
8892
)
8993

0 commit comments

Comments
 (0)