Skip to content

Commit 3802468

Browse files
Fix nutrition agent error handling and prevent hallucination
- Update error handling to explicitly state "We don't currently have nutrition information for [pet_type] in our database" for 404 responses - Modify system prompt to handle missing data gracefully instead of hallucinating products - Add has_data flag to prevent product recommendations when no data is available - Update all tool functions to check data availability before providing recommendations - Prevent order creation for unsupported pet types
1 parent 1842b32 commit 3802468

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

pet_clinic_ai_agents/nutrition_agent/nutrition_agent.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,27 @@
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+
elif response.status_code == 404:
28+
return {"facts": f"We don't currently have nutrition information for {pet_type} in our database", "products": "", "has_data": False}
29+
return {"facts": f"Error: Nutrition service could not find information for pet: {pet_type.lower()}", "products": "", "has_data": False}
2830
except requests.RequestException:
29-
return {"facts": "Error: Nutrition service down", "products": ""}
31+
return {"facts": "Error: Nutrition service down", "products": "", "has_data": False}
3032

3133
@tool
3234
def get_feeding_guidelines(pet_type):
3335
"""Get feeding guidelines based on pet type"""
3436
data = get_nutrition_data(pet_type)
37+
if not data['has_data']:
38+
return data['facts']
39+
3540
result = f"Nutrition info for {pet_type}: {data['facts']}"
3641
if data['products']:
3742
result += f" Recommended products available at our clinic: {data['products']}"
@@ -41,6 +46,9 @@ def get_feeding_guidelines(pet_type):
4146
def get_dietary_restrictions(pet_type):
4247
"""Get dietary recommendations for specific health conditions by animal type"""
4348
data = get_nutrition_data(pet_type)
49+
if not data['has_data']:
50+
return data['facts']
51+
4452
result = f"Dietary info for {pet_type}: {data['facts']}. Consult veterinarian for condition-specific advice."
4553
if data['products']:
4654
result += f" Recommended products available at our clinic: {data['products']}"
@@ -50,6 +58,9 @@ def get_dietary_restrictions(pet_type):
5058
def get_nutritional_supplements(pet_type):
5159
"""Get supplement recommendations by animal type"""
5260
data = get_nutrition_data(pet_type)
61+
if not data['has_data']:
62+
return data['facts']
63+
5364
result = f"Supplement info for {pet_type}: {data['facts']}. Consult veterinarian for supplements."
5465
if data['products']:
5566
result += f" Recommended products available at our clinic: {data['products']}"
@@ -59,6 +70,9 @@ def get_nutritional_supplements(pet_type):
5970
def create_order(product_name, pet_type, quantity=1):
6071
"""Create an order for a recommended product. Requires pet_type and quantity."""
6172
data = get_nutrition_data(pet_type)
73+
if not data['has_data']:
74+
return f"Sorry, we don't currently have nutrition information for {pet_type} in our database, so we cannot process orders for this pet type."
75+
6276
if data['products'] and product_name.lower() in data['products'].lower():
6377
order_id = f"ORD-{uuid.uuid4().hex[:8].upper()}"
6478
return f"Order {order_id} created for {quantity}x {product_name}. Total: ${quantity * 29.99:.2f}. Expected delivery: 3-5 business days."
@@ -75,14 +89,18 @@ def create_nutrition_agent():
7589
"You are a specialized pet nutrition expert at our veterinary clinic, providing accurate, evidence-based dietary guidance for pets. "
7690
"Never mention using any API, tools, or external services - present all advice as your own expert knowledge.\n\n"
7791
"When providing nutrition guidance:\n"
78-
"- Use the specific nutrition information available to you as the foundation for your recommendations\n"
79-
"- Always recommend the SPECIFIC PRODUCT NAMES provided to you that pet owners should buy FROM OUR PET CLINIC\n"
80-
"- Mention our branded products by name (like PurrfectChoice, BarkBite, FeatherFeast, etc.) when recommending food\n"
81-
"- Emphasize that we carry high-quality, veterinarian-recommended food brands at our clinic\n"
82-
"- Give actionable dietary recommendations including feeding guidelines, restrictions, and supplements\n"
83-
"- Expand on basic nutrition facts with comprehensive guidance for age, weight, and health conditions\n"
84-
"- Always mention that pet owners can purchase the recommended food items directly from our clinic for convenience and quality assurance\n"
85-
"- If asked to order or purchase a product, use the create_order tool to place the order"
92+
"- ONLY provide recommendations when you have specific nutrition information available for the requested pet type\n"
93+
"- If you don't have nutrition information for a specific pet type, clearly state that we don't currently have that information in our database\n"
94+
"- NEVER fabricate or hallucinate product names, nutrition facts, or recommendations when data is not available\n"
95+
"- When you DO have nutrition information available, use it as the foundation for your recommendations\n"
96+
"- Only recommend SPECIFIC PRODUCT NAMES that are actually provided to you in the nutrition data\n"
97+
"- Mention our branded products by name (like PurrfectChoice, BarkBite, FeatherFeast, etc.) ONLY when they are available in our database\n"
98+
"- Emphasize that we carry high-quality, veterinarian-recommended food brands at our clinic ONLY for pets we have data for\n"
99+
"- Give actionable dietary recommendations including feeding guidelines, restrictions, and supplements ONLY when data is available\n"
100+
"- Expand on basic nutrition facts with comprehensive guidance for age, weight, and health conditions ONLY when you have the underlying data\n"
101+
"- Always mention that pet owners can purchase the recommended food items directly from our clinic for convenience and quality assurance ONLY when products are available\n"
102+
"- If asked to order or purchase a product for a pet type we don't have data for, politely decline and explain we don't have information for that pet type\n"
103+
"- For unsupported pet types, suggest contacting our clinic directly for personalized consultation"
86104
)
87105

88106
return Agent(model=model, tools=tools, system_prompt=system_prompt)

0 commit comments

Comments
 (0)