minhvtt commited on
Commit
b222c75
verified
1 Parent(s): 163baa3

Update agent_service.py

Browse files
Files changed (1) hide show
  1. agent_service.py +19 -11
agent_service.py CHANGED
@@ -411,30 +411,35 @@ class AgentService:
411
 
412
  async def _execute_rag_search(self, query_params: Dict) -> str:
413
  """
414
- Execute RAG search with Multi-Stage Fallback Strategy
415
  Called when LLM wants to search_events
416
  """
417
  query = query_params.get("query", "").strip()
418
  vibe = query_params.get("vibe", "").strip()
419
  time = query_params.get("time", "").strip()
420
 
421
- # Strategy: Try specific -> broad
 
 
 
422
  search_strategies = []
 
423
 
424
  # 1. Full combination (Specific)
425
- if query or vibe:
426
- full_query = f"{query} {vibe} {time}".strip()
427
  search_strategies.append(("Full Context", full_query))
428
 
429
  # 2. Main keyword only (Broad) - Critical for terms like "r瓢峄"
430
- if query and len(search_strategies) > 0 and query != full_query:
431
  search_strategies.append(("Keyword Only", query))
432
 
433
- # 3. Vibe only (Fallback)
434
- if vibe and len(search_strategies) > 0 and vibe != full_query:
435
  search_strategies.append(("Vibe Only", vibe))
436
 
437
- print(f"馃攳 RAG Search Plan: {[s[0] for s in search_strategies]}")
 
438
 
439
  final_results = []
440
  seen_ids = set()
@@ -443,23 +448,26 @@ class AgentService:
443
  if not search_text:
444
  continue
445
 
446
- print(f"馃憠 Trying strategy: {strategy_name} ('{search_text}')")
447
 
448
- # Use embedding + qdrant
449
  embedding = self.embedding_service.encode_text(search_text)
450
  results = self.qdrant_service.search(
451
  query_embedding=embedding,
452
- limit=5
 
453
  )
454
 
455
  # Deduplicate and add results
456
  count = 0
457
  for res in results:
458
  doc_id = res['id']
 
459
  if doc_id not in seen_ids:
460
  seen_ids.add(doc_id)
461
  final_results.append(res)
462
  count += 1
 
463
 
464
  print(f" Found {count} new results (Total: {len(final_results)})")
465
 
 
411
 
412
  async def _execute_rag_search(self, query_params: Dict) -> str:
413
  """
414
+ Execute RAG search with Strict Relevance Filtering
415
  Called when LLM wants to search_events
416
  """
417
  query = query_params.get("query", "").strip()
418
  vibe = query_params.get("vibe", "").strip()
419
  time = query_params.get("time", "").strip()
420
 
421
+ # Score threshold to filter weak/irrelevant matches
422
+ SCORE_THRESHOLD = 0.5 # Tune this value (0.4-0.6)
423
+
424
+ # Build search strategies
425
  search_strategies = []
426
+ user_gave_specific_keyword = bool(query) # If user gave a keyword, don't fallback to vibe
427
 
428
  # 1. Full combination (Specific)
429
+ full_query = f"{query} {vibe} {time}".strip()
430
+ if full_query:
431
  search_strategies.append(("Full Context", full_query))
432
 
433
  # 2. Main keyword only (Broad) - Critical for terms like "r瓢峄"
434
+ if query and query != full_query:
435
  search_strategies.append(("Keyword Only", query))
436
 
437
+ # 3. Vibe only (Fallback) - ONLY if user didn't provide specific keyword
438
+ if vibe and not user_gave_specific_keyword and vibe != full_query:
439
  search_strategies.append(("Vibe Only", vibe))
440
 
441
+ print(f"[SEARCH] Plan: {[s[0] for s in search_strategies]}")
442
+ print(f"[SEARCH] User gave specific keyword: {user_gave_specific_keyword}")
443
 
444
  final_results = []
445
  seen_ids = set()
 
448
  if not search_text:
449
  continue
450
 
451
+ print(f"[SEARCH] Trying: {strategy_name} ('{search_text}')")
452
 
453
+ # Use embedding + qdrant with score threshold
454
  embedding = self.embedding_service.encode_text(search_text)
455
  results = self.qdrant_service.search(
456
  query_embedding=embedding,
457
+ limit=5,
458
+ score_threshold=SCORE_THRESHOLD # Filter weak matches
459
  )
460
 
461
  # Deduplicate and add results
462
  count = 0
463
  for res in results:
464
  doc_id = res['id']
465
+ score = res.get('confidence', 0)
466
  if doc_id not in seen_ids:
467
  seen_ids.add(doc_id)
468
  final_results.append(res)
469
  count += 1
470
+ print(f" + Added: {doc_id[:20]}... (score: {score:.3f})")
471
 
472
  print(f" Found {count} new results (Total: {len(final_results)})")
473