zenaight commited on
Commit Β·
e9ce725
1
Parent(s): 5fa177b
Enhance AI chat functionality for property inquiries
Browse files- Updated the system message to clarify the AI's capabilities, emphasizing access to a real property database and the ability to search for properties.
- Implemented logic to detect user interest in properties and automatically search the database for relevant listings, providing detailed responses with property information.
- Improved the response structure to include a summary of available properties, enhancing user engagement and interaction flow.
- ai_chat.py +46 -2
ai_chat.py
CHANGED
|
@@ -58,18 +58,26 @@ async def chat_with_session_memory(state):
|
|
| 58 |
context += f"Persona: {persona}\n"
|
| 59 |
|
| 60 |
# Add system message with context
|
| 61 |
-
system_message = f"""You are a friendly, professional industrial property agent assistant. Be conversational and human-like in your responses.
|
| 62 |
|
| 63 |
Context:
|
| 64 |
{context}
|
| 65 |
|
| 66 |
Key guidelines:
|
| 67 |
-
- You are a property agent with access to real property listings
|
|
|
|
|
|
|
|
|
|
| 68 |
- Be helpful and informative about industrial properties
|
| 69 |
- Keep responses conversational and not robotic
|
| 70 |
- Always respond naturally and conversationally
|
| 71 |
- Use the conversation context to understand what the user needs
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
Respond naturally to the user's message: {user_message}"""
|
| 74 |
|
| 75 |
# Use LLM to generate response
|
|
@@ -85,6 +93,42 @@ Respond naturally to the user's message: {user_message}"""
|
|
| 85 |
response = llm.invoke(messages)
|
| 86 |
ai_response = response.content.strip()
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return {
|
| 89 |
"response": ai_response,
|
| 90 |
"user_message": user_message,
|
|
|
|
| 58 |
context += f"Persona: {persona}\n"
|
| 59 |
|
| 60 |
# Add system message with context
|
| 61 |
+
system_message = f"""You are a friendly, professional industrial property agent assistant with access to a real property database. Be conversational and human-like in your responses.
|
| 62 |
|
| 63 |
Context:
|
| 64 |
{context}
|
| 65 |
|
| 66 |
Key guidelines:
|
| 67 |
+
- You are a property agent with access to real property listings in a database
|
| 68 |
+
- You can search the database for properties using the search_properties_for_ai() function
|
| 69 |
+
- When users ask about properties, search the database and provide real property information
|
| 70 |
+
- NEVER say you can't help or suggest other websites - you have real property data
|
| 71 |
- Be helpful and informative about industrial properties
|
| 72 |
- Keep responses conversational and not robotic
|
| 73 |
- Always respond naturally and conversationally
|
| 74 |
- Use the conversation context to understand what the user needs
|
| 75 |
|
| 76 |
+
Available functions:
|
| 77 |
+
- search_properties_for_ai(city=None, min_size=None, features=None, price_type=None, limit=5) - Search for properties in the database
|
| 78 |
+
|
| 79 |
+
When users ask about properties, search the database and provide the actual property listings with details like title, location, size, price, and features.
|
| 80 |
+
|
| 81 |
Respond naturally to the user's message: {user_message}"""
|
| 82 |
|
| 83 |
# Use LLM to generate response
|
|
|
|
| 93 |
response = llm.invoke(messages)
|
| 94 |
ai_response = response.content.strip()
|
| 95 |
|
| 96 |
+
# If the AI response suggests it needs property information, search the database
|
| 97 |
+
if any(keyword in user_message.lower() for keyword in ["property", "properties", "warehouse", "office", "space", "johannesburg", "cape town", "pretoria", "durban"]):
|
| 98 |
+
# Extract city from persona or message
|
| 99 |
+
city = None
|
| 100 |
+
if persona.get("location_preference"):
|
| 101 |
+
city = persona["location_preference"]
|
| 102 |
+
|
| 103 |
+
# Search for properties
|
| 104 |
+
properties = await search_properties_for_ai(city=city, limit=5)
|
| 105 |
+
|
| 106 |
+
if properties:
|
| 107 |
+
# Create a detailed property response
|
| 108 |
+
property_response = "π’ **Available Properties:**\n\n"
|
| 109 |
+
for i, prop in enumerate(properties[:3], 1):
|
| 110 |
+
property_response += f"**{i}. {prop['title']}**\n"
|
| 111 |
+
property_response += f"π {prop['location']}, {prop['city']}\n"
|
| 112 |
+
property_response += f"π {prop['size_sqm']} sqm β’ π° R{prop['price']:,.0f}/month\n"
|
| 113 |
+
if prop.get('listing_url'):
|
| 114 |
+
property_response += f"π {prop['listing_url']}\n"
|
| 115 |
+
if prop.get('features'):
|
| 116 |
+
property_response += f"β¨ {', '.join(prop.get('features', [])[:3])}\n"
|
| 117 |
+
property_response += "\n"
|
| 118 |
+
|
| 119 |
+
property_response += "Which property interests you? I can show you photos, provide more details, or help you schedule a viewing!"
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
"response": property_response,
|
| 123 |
+
"user_message": user_message,
|
| 124 |
+
"ai_response": property_response,
|
| 125 |
+
"session_id": session_id,
|
| 126 |
+
"wa_id": wa_id,
|
| 127 |
+
"wamid": wamid,
|
| 128 |
+
"current_property": None,
|
| 129 |
+
"property_details": {}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
return {
|
| 133 |
"response": ai_response,
|
| 134 |
"user_message": user_message,
|