Your Name Claude commited on
Commit
c7b2943
·
1 Parent(s): c3275de

Fix: Graceful fallback when company filter finds nothing + top 30 results

Browse files

Problem 1: Company filter too strict
- Query "Sinopharm covid vaccine" found 0 results
- Filter failed silently with no fallback
- User got "No matching trials found" even though related trials exist

Fix:
- Save original keyword scores before filtering
- If company filter returns 0 results, fall back to original scores
- Add warning note to context for LLM:
"No trials found with 'Sinopharm' as sponsor. Related trials below.
Please provide general knowledge if available."
- LLM can now answer: "While I don't have Sinopharm trials in database,
here are related COVID vaccine trials..."

Problem 2: Only returned 3 trials (too few)
- Changed top_k from 3 → 30 in all search calls
- More comprehensive results for LLM to analyze
- Better for comparison queries (30 trials per treatment)

Result:
- No more silent failures on company queries
- LLM provides useful answer even when exact match not found
- 10x more trial context for better responses

🤖 Generated with Claude Code https://claude.com/claude-code

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. foundation_engine.py +27 -8
foundation_engine.py CHANGED
@@ -627,10 +627,14 @@ def retrieve_context_with_embeddings(query, top_k=10, entities=None):
627
  logger.info(f"[HYBRID] Inverted index scoring: {len(keyword_scores)} trials matched ({time.time()-t_kw:.2f}s)")
628
 
629
  # 1.5. STRICT COMPANY FILTERING (if companies specified)
 
630
  if entities and entities.get('companies'):
631
  companies = [c.lower() for c in entities['companies']]
632
  logger.info(f"[STRICT FILTER] Enforcing company filter: {companies}")
633
 
 
 
 
634
  # Filter keyword_scores to ONLY trials with these companies
635
  filtered_keyword_scores = {}
636
  sponsor_field_patterns = ['sponsor:', 'collaborator:', 'manufacturer:']
@@ -660,14 +664,17 @@ def retrieve_context_with_embeddings(query, top_k=10, entities=None):
660
  # If no company match, EXCLUDE this trial
661
 
662
  before_count = len(keyword_scores)
663
- keyword_scores = filtered_keyword_scores
664
- after_count = len(keyword_scores)
665
 
666
  logger.info(f"[STRICT FILTER] Filtered {before_count} → {after_count} trials (only those from {companies})")
667
 
668
- if len(keyword_scores) == 0:
669
- logger.warning(f"[STRICT FILTER] No trials found from companies {companies}")
670
- return "" # Early exit if no company matches
 
 
 
 
671
 
672
  # 2. SEMANTIC SCORING
673
  load_embedder()
@@ -767,6 +774,18 @@ def retrieve_context_with_embeddings(query, top_k=10, entities=None):
767
  logger.info(f"[355M RANKING] Added ranking metadata to context for final LLM")
768
 
769
  context = "\n\n---\n\n".join(context_chunks) # Use --- as separator between trials
 
 
 
 
 
 
 
 
 
 
 
 
770
  logger.info(f"[HYBRID] TOTAL TIME: {time.time()-t0:.2f}s")
771
  logger.info(f"[HYBRID] Filtered context length: {len(context)} chars (was ~{sum(len(c) for c in raw_chunks)} chars)")
772
 
@@ -1370,7 +1389,7 @@ def process_query_simple_test(conversation):
1370
 
1371
  # Try to search
1372
  start = time.time()
1373
- context = retrieve_context_with_embeddings(conversation, top_k=3)
1374
  search_time = time.time() - start
1375
 
1376
  if not context:
@@ -1539,7 +1558,7 @@ def process_query(conversation):
1539
  context_parts = []
1540
  for i, treatment in enumerate(treatments[:2], 1): # Compare first 2
1541
  logger.info(f"[COMPARE] Searching trials for {treatment}...")
1542
- treatment_trials = retrieve_context_with_embeddings(treatment, top_k=5, entities=parsed_query)
1543
 
1544
  if treatment_trials:
1545
  context_parts.append(f"=== TRIALS FOR {treatment.upper()} ===\n{treatment_trials}\n")
@@ -1571,7 +1590,7 @@ def process_query(conversation):
1571
  logger.info("Step 1: RAG search...")
1572
  output_parts.append("✓ Step 1: RAG search started...\n")
1573
  # Pass entities for STRICT company filtering
1574
- context = retrieve_context_with_embeddings(search_query, top_k=3, entities=parsed_query)
1575
 
1576
  if not context:
1577
  return "No matching trials found in RAG search."
 
627
  logger.info(f"[HYBRID] Inverted index scoring: {len(keyword_scores)} trials matched ({time.time()-t_kw:.2f}s)")
628
 
629
  # 1.5. STRICT COMPANY FILTERING (if companies specified)
630
+ company_filter_failed = False
631
  if entities and entities.get('companies'):
632
  companies = [c.lower() for c in entities['companies']]
633
  logger.info(f"[STRICT FILTER] Enforcing company filter: {companies}")
634
 
635
+ # Save original scores in case we need to fall back
636
+ original_keyword_scores = keyword_scores.copy()
637
+
638
  # Filter keyword_scores to ONLY trials with these companies
639
  filtered_keyword_scores = {}
640
  sponsor_field_patterns = ['sponsor:', 'collaborator:', 'manufacturer:']
 
664
  # If no company match, EXCLUDE this trial
665
 
666
  before_count = len(keyword_scores)
667
+ after_count = len(filtered_keyword_scores)
 
668
 
669
  logger.info(f"[STRICT FILTER] Filtered {before_count} → {after_count} trials (only those from {companies})")
670
 
671
+ # If no company matches, fall back to original search but flag it
672
+ if len(filtered_keyword_scores) == 0:
673
+ logger.warning(f"[STRICT FILTER] No trials found from companies {companies}, falling back to general search")
674
+ company_filter_failed = True
675
+ keyword_scores = original_keyword_scores # Restore original
676
+ else:
677
+ keyword_scores = filtered_keyword_scores
678
 
679
  # 2. SEMANTIC SCORING
680
  load_embedder()
 
774
  logger.info(f"[355M RANKING] Added ranking metadata to context for final LLM")
775
 
776
  context = "\n\n---\n\n".join(context_chunks) # Use --- as separator between trials
777
+
778
+ # Add note if company filter failed
779
+ if company_filter_failed and entities and entities.get('companies'):
780
+ company_names = ', '.join(entities['companies'])
781
+ warning_note = f"""
782
+ ⚠️ IMPORTANT: No trials found with '{company_names}' as the sponsor/collaborator.
783
+ The trials below are related to the topic but may not be from {company_names} specifically.
784
+ If you have general knowledge about {company_names}, please provide that information along with the context below.
785
+
786
+ """
787
+ context = warning_note + context
788
+
789
  logger.info(f"[HYBRID] TOTAL TIME: {time.time()-t0:.2f}s")
790
  logger.info(f"[HYBRID] Filtered context length: {len(context)} chars (was ~{sum(len(c) for c in raw_chunks)} chars)")
791
 
 
1389
 
1390
  # Try to search
1391
  start = time.time()
1392
+ context = retrieve_context_with_embeddings(conversation, top_k=30)
1393
  search_time = time.time() - start
1394
 
1395
  if not context:
 
1558
  context_parts = []
1559
  for i, treatment in enumerate(treatments[:2], 1): # Compare first 2
1560
  logger.info(f"[COMPARE] Searching trials for {treatment}...")
1561
+ treatment_trials = retrieve_context_with_embeddings(treatment, top_k=30, entities=parsed_query)
1562
 
1563
  if treatment_trials:
1564
  context_parts.append(f"=== TRIALS FOR {treatment.upper()} ===\n{treatment_trials}\n")
 
1590
  logger.info("Step 1: RAG search...")
1591
  output_parts.append("✓ Step 1: RAG search started...\n")
1592
  # Pass entities for STRICT company filtering
1593
+ context = retrieve_context_with_embeddings(search_query, top_k=30, entities=parsed_query)
1594
 
1595
  if not context:
1596
  return "No matching trials found in RAG search."