~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
refactor: Simplify FollowUpRelevanceFilter to use LLM selection only
LearningCircuit81d63a66275c
diff --git a/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py b/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.pyindex 3238e60a24..0e064636c0 100644--- a/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py+++ b/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py@@ -5,9 +5,8 @@ Filters and ranks past research sources based on their relevanceto follow-up questions."""-from typing import Dict, List, Optional, Tuple+from typing import Dict, Listfrom loguru import logger-import refrom .base_filter import BaseFilterfrom ...utilities.search_utilities import remove_think_tags@@ -32,263 +31,93 @@ class FollowUpRelevanceFilter(BaseFilter):query: The follow-up query**kwargs: Additional parameters:- max_results: Maximum number of results to return- - threshold: Minimum relevance score (0-1)- past_findings: Summary of past findings for contextReturns:- Filtered and ranked list of relevant sources+ Filtered list of relevant sources"""if not results:return []max_results = kwargs.get("max_results", 10)- threshold = kwargs.get("threshold", 0.3)past_findings = kwargs.get("past_findings", "")- # Score each source for relevance- scored_results = []- for source in results:- score = self._calculate_relevance_score(- source, query, past_findings- )- if score >= threshold:- source_copy = source.copy()- source_copy["relevance_score"] = score- scored_results.append(source_copy)-- # Sort by relevance score- scored_results.sort(key=lambda x: x["relevance_score"], reverse=True)+ # Use LLM to select relevant sources+ relevant_indices = self._select_relevant_sources(+ results, query, past_findings, max_results+ )- # Return top results- filtered = scored_results[:max_results]+ # Return selected sources+ filtered = [results[i] for i in relevant_indices if i < len(results)]logger.info(f"Filtered {len(results)} sources to {len(filtered)} relevant ones "- f"for follow-up query"+ f"for follow-up query. Kept indices: {relevant_indices}")return filtered- def _calculate_relevance_score(- self, source: Dict, query: str, context: str- ) -> float:+ def _select_relevant_sources(+ self, sources: List[Dict], query: str, context: str, max_results: int+ ) -> List[int]:"""- Calculate relevance score for a source.+ Select relevant sources using LLM.Args:- source: Source dictionary with url, title, snippet+ sources: List of source dictionariesquery: The follow-up query- context: Additional context (past findings)-- Returns:- Relevance score between 0 and 1- """- # Quick keyword matching for initial score- keyword_score = self._keyword_relevance(source, query)-- # If keyword score is very low, skip expensive LLM call- if keyword_score < 0.1:- return keyword_score-- # Use LLM for semantic relevance if model is available- if self.model:- semantic_score = self._semantic_relevance(source, query, context)- # Weighted combination- return 0.4 * keyword_score + 0.6 * semantic_score-- return keyword_score-- def _keyword_relevance(self, source: Dict, query: str) -> float:- """- Calculate keyword-based relevance score.-- Args:- source: Source dictionary- query: Follow-up query-- Returns:- Score between 0 and 1- """- # Extract text from source- source_text = " ".join(- [- source.get("title") or "",- source.get("snippet") or "",- source.get("content_preview") or "",- ]- ).lower()-- # Extract keywords from query- query_words = set(re.findall(r"\w+", query.lower()))-- # Count matching keywords- matches = sum(1 for word in query_words if word in source_text)-- # Calculate score- if len(query_words) > 0:- return min(1.0, matches / len(query_words))- return 0.0-- def _semantic_relevance(- self, source: Dict, query: str, context: str- ) -> float:- """- Calculate semantic relevance using LLM.-- Args:- source: Source dictionary- query: Follow-up querycontext: Past findings context+ max_results: Maximum number of sources to selectReturns:- Score between 0 and 1- """- source_preview = f"""-Title: {source.get("title") or "Unknown"}-URL: {source.get("url") or ""}-Content: {(source.get("snippet") or "")[:200]}-"""+ List of indices of relevant sources+ """+ if not self.model:+ # If no model available, return first max_results+ return list(range(min(max_results, len(sources))))++ # Build source list for LLM+ source_list = []+ for i, source in enumerate(sources):+ title = source.get("title") or "Unknown"+ url = source.get("url") or ""+ snippet = (+ source.get("snippet") or source.get("content_preview") or ""+ )[:150]+ source_list.append(+ f"{i}. {title}\n URL: {url}\n Content: {snippet}"+ )++ sources_text = "\n\n".join(source_list)prompt = f"""-Rate the relevance of this source to the follow-up question on a scale of 0-10.+Select the most relevant sources for answering this follow-up question.Follow-up question: "{query}"-Source:-{source_preview}+Available sources:+{sources_text}-Consider:-1. Does the source directly address the question?-2. Does it provide useful context or background?-3. Does it contain specific information requested?+Instructions:+- Select up to {max_results} sources that are most relevant to the follow-up question+- Consider which sources directly address the question or provide essential context+- Return ONLY a comma-separated list of source numbers (e.g., "0,2,5,7")+- Do not include any explanation or other text-Return only a number from 0 to 10.-"""+Return the indices of relevant sources:"""try:response = self.model.invoke(prompt)content = remove_think_tags(response.content).strip()- # Extract number from response+ # Extract numbers from responseimport re- numbers = re.findall(r"\d+\.?\d*", content)- if numbers:- score = float(numbers[0])- return min(1.0, score / 10.0)- except Exception as e:- logger.debug(f"Semantic relevance calculation failed: {e}")-- # Fallback to keyword score- return self._keyword_relevance(source, query)-- def rank_by_relevance(- self, sources: List[Dict], query: str, context: Optional[str] = None- ) -> List[Tuple[Dict, float]]:- """- Rank sources by relevance and return with scores.-- Args:- sources: List of source documents- query: Follow-up query- context: Optional context for better ranking-- Returns:- List of (source, score) tuples sorted by relevance- """- ranked = []- for source in sources:- score = self._calculate_relevance_score(- source, query, context or ""- )- ranked.append((source, score))-- # Sort by score descending- ranked.sort(key=lambda x: x[1], reverse=True)- return ranked-- def extract_key_sources(- self, sources: List[Dict], max_count: int = 5- ) -> List[Dict]:- """- Extract the most important sources for follow-up research.-- Args:- sources: List of all sources- max_count: Maximum number of key sources to extract-- Returns:- List of key sources- """- if not sources:- return []-- # Prioritize sources with certain characteristics- prioritized = []-- for source in sources:- priority_score = 0-- # Prefer sources with content- if source.get("content_preview") or source.get("snippet"):- priority_score += 2-- # Prefer sources with clear titles- if source.get("title") and len(source.get("title", "")) > 10:- priority_score += 1-- # Prefer certain source types if specified- if source.get("source_type") in ["academic", "official", "primary"]:- priority_score += 3-- source_copy = source.copy()- source_copy["priority_score"] = priority_score- prioritized.append(source_copy)-- # Sort by priority- prioritized.sort(key=lambda x: x["priority_score"], reverse=True)-- # Return top sources- return prioritized[:max_count]+ numbers = re.findall(r"\d+", content)+ indices = [int(n) for n in numbers if int(n) < len(sources)]- def filter_by_type(- self, sources: List[Dict], source_types: List[str]- ) -> List[Dict]:- """- Filter sources by their type.-- Args:- sources: List of sources- source_types: Types to include (e.g., ["academic", "news"])-- Returns:- Filtered sources- """- if not source_types:- return sources-- return [s for s in sources if s.get("source_type") in source_types]-- def deduplicate_sources(self, sources: List[Dict]) -> List[Dict]:- """- Remove duplicate sources based on URL.-- Args:- sources: List of sources that may contain duplicates-- Returns:- Deduplicated list of sources- """- seen_urls = set()- unique_sources = []-- for source in sources:- url = source.get("url", "")- if url and url not in seen_urls:- seen_urls.add(url)- unique_sources.append(source)- elif not url:- # Keep sources without URLs (shouldn't happen but be safe)- unique_sources.append(source)-- return unique_sources+ return indices+ except Exception as e:+ logger.debug(f"LLM source selection failed: {e}")+ # Fallback to first max_results sources+ return list(range(min(max_results, len(sources))))