Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1955,34 +1955,31 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1955 |
return
|
1956 |
|
1957 |
else:
|
1958 |
-
#
|
1959 |
-
|
1960 |
-
"β *Product Not Found*\n\n"
|
1961 |
-
f"π *We couldn't find '{message_body}' in our veterinary database.*\n\n"
|
1962 |
-
"π‘ *Try these alternatives:*\n"
|
1963 |
-
"β’ Check spelling (e.g., 'Hydropex' not 'Hydro pex')\n"
|
1964 |
-
"β’ Search by symptoms (e.g., 'respiratory', 'liver support')\n"
|
1965 |
-
"β’ Search by category (e.g., 'antibiotic', 'vitamin')\n"
|
1966 |
-
"β’ Search by species (e.g., 'poultry', 'livestock')\n\n"
|
1967 |
-
"π₯ *Popular Veterinary Products:*\n"
|
1968 |
-
"β’ Hydropex (Electrolyte supplement)\n"
|
1969 |
-
"β’ Heposel (Liver tonic)\n"
|
1970 |
-
"β’ Bromacid (Respiratory support)\n"
|
1971 |
-
"β’ Tribiotic (Antibiotic)\n"
|
1972 |
-
"β’ Symodex (Multivitamin)\n\n"
|
1973 |
-
"π¬ *Type 'main' to return to main menu or try another search.*"
|
1974 |
-
)
|
1975 |
|
1976 |
-
#
|
1977 |
-
if
|
1978 |
-
|
1979 |
-
|
1980 |
-
|
1981 |
-
|
1982 |
-
|
1983 |
-
|
1984 |
-
|
1985 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1986 |
|
1987 |
# π― PRIORITY 5: Default: treat as general query with intelligent product inquiry
|
1988 |
await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
|
@@ -2006,17 +2003,29 @@ async def handle_general_query_with_ai(from_number: str, query: str, user_contex
|
|
2006 |
current_state = user_context.get('current_state', 'main_menu')
|
2007 |
current_product = user_context.get('current_product')
|
2008 |
prompt = f"""
|
2009 |
-
You are a professional veterinary product assistant for Apex Biotical, helping users on WhatsApp.
|
2010 |
-
Always answer in a clear, accurate, and helpful manner.
|
2011 |
|
2012 |
User Query: "{query}"
|
2013 |
Current State: {current_state}
|
2014 |
Current Product: {current_product.get('Product Name', 'None') if current_product else 'None'}
|
2015 |
|
2016 |
-
|
2017 |
-
If the user asks a
|
2018 |
-
|
2019 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020 |
"""
|
2021 |
response = openai.ChatCompletion.create(
|
2022 |
model="gpt-4o",
|
@@ -3729,7 +3738,31 @@ async def handle_intelligent_product_inquiry(from_number: str, query: str, user_
|
|
3729 |
# The actual sending of product details should be handled by the caller
|
3730 |
return selected_product
|
3731 |
else:
|
3732 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3733 |
except Exception as e:
|
3734 |
logger.error(f"Error in handle_intelligent_product_inquiry: {e}")
|
3735 |
send_whatsjet_message(from_number, "β Error processing your request. Type 'main' to return to the main menu.")
|
|
|
1955 |
return
|
1956 |
|
1957 |
else:
|
1958 |
+
# Check for specific query types before falling back to generic response
|
1959 |
+
query_lower = message_body.lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1960 |
|
1961 |
+
# Check for company/about queries
|
1962 |
+
if any(keyword in query_lower for keyword in ['apex', 'company', 'about', 'who', 'what is']):
|
1963 |
+
# Use OpenAI for company information
|
1964 |
+
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1965 |
+
return
|
1966 |
+
|
1967 |
+
# Check for product-specific questions (mode of action, dosage, etc.)
|
1968 |
+
product_question_keywords = ['mode of action', 'dosage', 'administration', 'composition', 'indications', 'precautions', 'storage', 'how to use', 'side effects']
|
1969 |
+
if any(keyword in query_lower for keyword in product_question_keywords):
|
1970 |
+
# Use OpenAI for product-specific questions
|
1971 |
+
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1972 |
+
return
|
1973 |
+
|
1974 |
+
# Check for general veterinary questions
|
1975 |
+
veterinary_keywords = ['weather', 'temperature', 'disease', 'symptoms', 'treatment', 'prevention', 'vaccination', 'nutrition', 'health']
|
1976 |
+
if any(keyword in query_lower for keyword in veterinary_keywords):
|
1977 |
+
# Use OpenAI for general veterinary questions
|
1978 |
+
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1979 |
+
return
|
1980 |
+
|
1981 |
+
# Simple one-liner for wrong queries
|
1982 |
+
send_whatsjet_message(from_number, "β Please correct your question or type 'main' to go to main menu.")
|
1983 |
|
1984 |
# π― PRIORITY 5: Default: treat as general query with intelligent product inquiry
|
1985 |
await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
|
|
|
2003 |
current_state = user_context.get('current_state', 'main_menu')
|
2004 |
current_product = user_context.get('current_product')
|
2005 |
prompt = f"""
|
2006 |
+
You are a professional veterinary product assistant for Apex Biotical Solutions, helping users on WhatsApp.
|
2007 |
+
Always answer in a clear, accurate, and helpful manner with proper formatting and emojis.
|
2008 |
|
2009 |
User Query: "{query}"
|
2010 |
Current State: {current_state}
|
2011 |
Current Product: {current_product.get('Product Name', 'None') if current_product else 'None'}
|
2012 |
|
2013 |
+
IMPORTANT INSTRUCTIONS:
|
2014 |
+
1. If the user asks about "Apex" or "Apex Biotical" - provide a comprehensive, professional overview of Apex Biotical Solutions as a veterinary pharmaceutical company, including their expertise, product range, and commitment to animal health.
|
2015 |
+
|
2016 |
+
2. If the user asks about specific product details (mode of action, dosage, administration, composition, etc.) - search through the veterinary products database and provide detailed, accurate information about the specific product mentioned.
|
2017 |
+
|
2018 |
+
3. If the user asks about products (e.g., 'poultry products', 'respiratory medicine'), list ALL relevant products from the database with a short description for each.
|
2019 |
+
|
2020 |
+
4. If the user asks a general veterinary question, provide a concise, expert answer.
|
2021 |
+
|
2022 |
+
5. If the query is about weather or non-veterinary topics, politely redirect to veterinary-related questions.
|
2023 |
+
|
2024 |
+
6. Always keep responses professional, concise, and user-friendly with proper formatting.
|
2025 |
+
7. Use emojis and bullet points for better readability.
|
2026 |
+
8. If you don't have specific information, say so clearly and suggest alternatives.
|
2027 |
+
|
2028 |
+
Available Products Database: {products_df.to_dict('records') if products_df is not None else 'No products loaded'}
|
2029 |
"""
|
2030 |
response = openai.ChatCompletion.create(
|
2031 |
model="gpt-4o",
|
|
|
3738 |
# The actual sending of product details should be handled by the caller
|
3739 |
return selected_product
|
3740 |
else:
|
3741 |
+
# Check for specific query types before falling back to generic response
|
3742 |
+
query_lower = query.lower().strip()
|
3743 |
+
|
3744 |
+
# Check for company/about queries
|
3745 |
+
if any(keyword in query_lower for keyword in ['apex', 'company', 'about', 'who', 'what is']):
|
3746 |
+
# Use OpenAI for company information
|
3747 |
+
await handle_general_query_with_ai(from_number, query, user_context, reply_language)
|
3748 |
+
return
|
3749 |
+
|
3750 |
+
# Check for product-specific questions (mode of action, dosage, etc.)
|
3751 |
+
product_question_keywords = ['mode of action', 'dosage', 'administration', 'composition', 'indications', 'precautions', 'storage', 'how to use', 'side effects']
|
3752 |
+
if any(keyword in query_lower for keyword in product_question_keywords):
|
3753 |
+
# Use OpenAI for product-specific questions
|
3754 |
+
await handle_general_query_with_ai(from_number, query, user_context, reply_language)
|
3755 |
+
return
|
3756 |
+
|
3757 |
+
# Check for general veterinary questions
|
3758 |
+
veterinary_keywords = ['weather', 'temperature', 'disease', 'symptoms', 'treatment', 'prevention', 'vaccination', 'nutrition', 'health']
|
3759 |
+
if any(keyword in query_lower for keyword in veterinary_keywords):
|
3760 |
+
# Use OpenAI for general veterinary questions
|
3761 |
+
await handle_general_query_with_ai(from_number, query, user_context, reply_language)
|
3762 |
+
return
|
3763 |
+
|
3764 |
+
# Simple one-liner for wrong queries
|
3765 |
+
send_whatsjet_message(from_number, "β Please correct your question or type 'main' to go to main menu.")
|
3766 |
except Exception as e:
|
3767 |
logger.error(f"Error in handle_intelligent_product_inquiry: {e}")
|
3768 |
send_whatsjet_message(from_number, "β Error processing your request. Type 'main' to return to the main menu.")
|