Spaces:
Sleeping
Sleeping
Update scenario_handlers/event_recommendation.py
Browse files
scenario_handlers/event_recommendation.py
CHANGED
|
@@ -63,18 +63,18 @@ class EventRecommendationHandler(BaseScenarioHandler):
|
|
| 63 |
query = f"sự kiện phù hợp với {scenario_data.get('interest_tag', 'mọi người')}"
|
| 64 |
print(f"🔍 RAG Search: {query}")
|
| 65 |
|
| 66 |
-
results = self._search_rag(query, limit=
|
| 67 |
formatted_events = self._format_event_list(results)
|
| 68 |
|
| 69 |
# Save results to scenario data
|
| 70 |
scenario_data['rag_results'] = formatted_events
|
| 71 |
scenario_data['available_events'] = [
|
| 72 |
-
r['metadata'].get('
|
| 73 |
-
for i, r in enumerate(results
|
| 74 |
]
|
| 75 |
|
| 76 |
return {
|
| 77 |
-
"message": f"Đây là
|
| 78 |
"new_state": {
|
| 79 |
"active_scenario": "event_recommendation",
|
| 80 |
"scenario_step": 3,
|
|
@@ -215,34 +215,83 @@ class EventRecommendationHandler(BaseScenarioHandler):
|
|
| 215 |
return type_map.get(step, 'text')
|
| 216 |
|
| 217 |
def _format_event_list(self, results: list) -> str:
|
| 218 |
-
"""Format event search results
|
| 219 |
print(f"🔍 DEBUG: RAG returned {len(results)} results")
|
| 220 |
|
| 221 |
if not results or len(results) == 0:
|
|
|
|
| 222 |
return "Hiện tại chưa có event phù hợp 😢\nBạn thử vibe khác nhé!"
|
| 223 |
|
| 224 |
# Debug: Print first result to see structure
|
| 225 |
-
|
| 226 |
-
print(f"🔍 DEBUG: First result metadata: {results[0].get('metadata', {})}")
|
| 227 |
|
| 228 |
events = []
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
metadata = r.get('metadata', {})
|
| 231 |
-
name = metadata.get('event_name', f'Event {i}')
|
| 232 |
-
date = metadata.get('date', 'TBA')
|
| 233 |
-
location = metadata.get('location', '')
|
| 234 |
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
|
|
|
| 237 |
event_str = f"{i}. **{name}**"
|
| 238 |
-
if date != 'TBA':
|
| 239 |
-
event_str += f" ({date})"
|
| 240 |
-
if location:
|
| 241 |
-
event_str += f" - {location}"
|
| 242 |
|
| 243 |
events.append(event_str)
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
def _detect_choice(self, user_input: str) -> str:
|
| 248 |
"""Detect what info user wants to see"""
|
|
|
|
| 63 |
query = f"sự kiện phù hợp với {scenario_data.get('interest_tag', 'mọi người')}"
|
| 64 |
print(f"🔍 RAG Search: {query}")
|
| 65 |
|
| 66 |
+
results = self._search_rag(query, limit=5) # Get up to 5
|
| 67 |
formatted_events = self._format_event_list(results)
|
| 68 |
|
| 69 |
# Save results to scenario data
|
| 70 |
scenario_data['rag_results'] = formatted_events
|
| 71 |
scenario_data['available_events'] = [
|
| 72 |
+
r['metadata'].get('id_use', metadata.get('original_id', f'event_{i+1}'))
|
| 73 |
+
for i, r in enumerate(results)
|
| 74 |
]
|
| 75 |
|
| 76 |
return {
|
| 77 |
+
"message": f"Đây là các event hợp với bạn nè:\n{formatted_events}\n\nBạn có muốn xem chi tiết event nào không?",
|
| 78 |
"new_state": {
|
| 79 |
"active_scenario": "event_recommendation",
|
| 80 |
"scenario_step": 3,
|
|
|
|
| 215 |
return type_map.get(step, 'text')
|
| 216 |
|
| 217 |
def _format_event_list(self, results: list) -> str:
|
| 218 |
+
"""Format event search results - IMPROVED to show actual event count"""
|
| 219 |
print(f"🔍 DEBUG: RAG returned {len(results)} results")
|
| 220 |
|
| 221 |
if not results or len(results) == 0:
|
| 222 |
+
print("⚠️ DEBUG: No results found")
|
| 223 |
return "Hiện tại chưa có event phù hợp 😢\nBạn thử vibe khác nhé!"
|
| 224 |
|
| 225 |
# Debug: Print first result to see structure
|
| 226 |
+
print(f"🔍 DEBUG: First result metadata: {results[0].get('metadata', {})}")
|
|
|
|
| 227 |
|
| 228 |
events = []
|
| 229 |
+
# FIXED: Use actual length instead of hardcoded [:3]
|
| 230 |
+
num_events = min(len(results), 5) # Show max 5 events
|
| 231 |
+
print(f"🔍 DEBUG: Formatting {num_events} events")
|
| 232 |
+
|
| 233 |
+
for i, r in enumerate(results[:num_events], 1):
|
| 234 |
metadata = r.get('metadata', {})
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
# Extract event info from Qdrant metadata
|
| 237 |
+
event_id = metadata.get('id_use', metadata.get('original_id'))
|
| 238 |
+
texts = metadata.get('texts', [])
|
| 239 |
+
text = texts[0] if texts and len(texts) > 0 else metadata.get('text', '')
|
| 240 |
+
|
| 241 |
+
print(f"🔍 DEBUG Event {i}:")
|
| 242 |
+
print(f" - ID: {event_id}")
|
| 243 |
+
print(f" - Text length: {len(text)} chars")
|
| 244 |
+
print(f" - Text preview: {text[:100]}")
|
| 245 |
+
|
| 246 |
+
# Extract event name from text
|
| 247 |
+
# Try to find event name pattern in text
|
| 248 |
+
name = self._extract_event_name_from_text(text)
|
| 249 |
+
|
| 250 |
+
if not name:
|
| 251 |
+
# Fallback to shortened text
|
| 252 |
+
name = text[:60].strip()
|
| 253 |
+
if len(text) > 60:
|
| 254 |
+
name += "..."
|
| 255 |
+
|
| 256 |
+
print(f" - Extracted name: {name}")
|
| 257 |
|
| 258 |
+
# Format event string
|
| 259 |
event_str = f"{i}. **{name}**"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
events.append(event_str)
|
| 262 |
|
| 263 |
+
result = "\n".join(events)
|
| 264 |
+
print(f"✅ DEBUG: Formatted {len(events)} events successfully")
|
| 265 |
+
return result
|
| 266 |
+
|
| 267 |
+
def _extract_event_name_from_text(self, text: str) -> str:
|
| 268 |
+
"""
|
| 269 |
+
Try to extract event name from description text
|
| 270 |
+
|
| 271 |
+
Patterns to match:
|
| 272 |
+
- "Sự kiện văn hóa hài kịch" → "Văn hóa hài kịch"
|
| 273 |
+
- "Stand-up Comedy Night - ..." → "Stand-up Comedy Night"
|
| 274 |
+
"""
|
| 275 |
+
if not text:
|
| 276 |
+
return ""
|
| 277 |
+
|
| 278 |
+
# Remove "Sự kiện" prefix if exists
|
| 279 |
+
clean_text = text
|
| 280 |
+
if clean_text.startswith("Sự kiện "):
|
| 281 |
+
clean_text = clean_text[8:].strip()
|
| 282 |
+
|
| 283 |
+
# Take first sentence or first 80 chars
|
| 284 |
+
sentences = clean_text.split('.')
|
| 285 |
+
if sentences and len(sentences) > 0:
|
| 286 |
+
first_sentence = sentences[0].strip()
|
| 287 |
+
if len(first_sentence) > 80:
|
| 288 |
+
return first_sentence[:77] + "..."
|
| 289 |
+
return first_sentence
|
| 290 |
+
|
| 291 |
+
# Fallback
|
| 292 |
+
if len(clean_text) > 80:
|
| 293 |
+
return clean_text[:77] + "..."
|
| 294 |
+
return clean_text
|
| 295 |
|
| 296 |
def _detect_choice(self, user_input: str) -> str:
|
| 297 |
"""Detect what info user wants to see"""
|