Skip to content

~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.

Fix: Improve search engine base class with better error handling and response processing

hashedviking4e8c63a7e3ef
diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.py
index 2e1267bd6f..a93083ebc9 100644
--- a/src/local_deep_research/web_search_engines/search_engine_base.py
+++ b/src/local_deep_research/web_search_engines/search_engine_base.py
@@ -1,7 +1,6 @@
import json
import logging
from abc import ABC, abstractmethod
-from datetime import datetime
from typing import Any, Dict, List, Optional
from langchain_core.language_models import BaseLLM
@@ -100,60 +99,60 @@ class BaseSearchEngine(ABC):
self, previews: List[Dict[str, Any]], query: str
) -> List[Dict[str, Any]]:
"""
- Filter search results for relevance to the query using an LLM.
-
- Checks config.SKIP_RELEVANCE_FILTER to determine whether to perform filtering.
+ Filter search results by relevance to the query using the LLM.
Args:
- previews: List of search result dictionaries with preview information
+ previews: List of preview dictionaries
query: The original search query
Returns:
- Filtered list of the most relevant search results
+ Filtered list of preview dictionaries
"""
- # Import config inside the method to avoid circular import
-
- # Skip filtering if configured to do so or if no LLM is available
- if (
- hasattr(search_config, "SKIP_RELEVANCE_FILTER")
- and search_config.SKIP_RELEVANCE_FILTER
- ):
- # Return all previews up to max_filtered_results if no filtering is performed
- limit = self.max_filtered_results or 5
- return previews[:limit]
-
- # Default implementation uses LLM if available
- if not self.llm or not previews:
- # If no LLM available, return all previews as relevant
- if self.max_filtered_results and len(previews) > self.max_filtered_results:
- return previews[: self.max_filtered_results]
+ # If no LLM or too few previews, return all
+ if not self.llm or len(previews) <= 1:
return previews
- now = datetime.now()
- current_time = now.strftime("%Y-%m-%d")
- prompt = f"""Analyze these search results and provide a ranked list of the most relevant ones.
+ # Create a simple context for LLM
+ preview_context = []
+ for i, preview in enumerate(previews):
+ title = preview.get("title", "Untitled").strip()
+ snippet = preview.get("snippet", "").strip()
+
+ # Clean up snippet if too long
+ if len(snippet) > 300:
+ snippet = snippet[:300] + "..."
+
+ preview_context.append(f"[{i}] Title: {title}\nSnippet: {snippet}")
+
+ # Set a reasonable limit on context length
+ max_context_items = min(10, len(preview_context))
+ context = "\n\n".join(preview_context[:max_context_items])
-IMPORTANT: Evaluate and rank based on these criteria (in order of importance):
-1. Timeliness - current/recent information as of {current_time}
-2. Direct relevance to query: "{query}"
-3. Source reliability (prefer official sources, established websites)
-4. Factual accuracy (cross-reference major claims)
+ prompt = f"""You are a search result filter. Your task is to rank search results by relevance to a query.
-Search results to evaluate:
-{json.dumps(previews, indent=2)}
+Query: "{query}"
-Return ONLY a JSON array of indices (0-based) ranked from most to least relevant.
-Include ONLY indices that meet ALL criteria, with the most relevant first.
-Example response: [4, 0, 2]
+Search Results:
+{context}
-Respond with ONLY the JSON array, no other text."""
+Return the search results as a JSON array of indices, ranked from most to least relevant to the query.
+Only include indices of results that are actually relevant to the query.
+For example: [3, 0, 7, 1]
+
+If no results seem relevant to the query, return an empty array: []"""
try:
# Get LLM's evaluation
response = self.llm.invoke(prompt)
- # Extract JSON array from response
- response_text = remove_think_tags(response.content)
+ # Handle different response formats (string or object with content attribute)
+ response_text = ""
+ if hasattr(response, "content"):
+ response_text = remove_think_tags(response.content)
+ else:
+ # Handle string responses
+ response_text = remove_think_tags(str(response))
+
# Clean up response to handle potential formatting issues
response_text = response_text.strip()
@@ -171,6 +170,13 @@ Respond with ONLY the JSON array, no other text."""
if idx < len(previews):
ranked_results.append(previews[idx])
+ # If we filtered out all results, return at least some of the originals
+ if not ranked_results and previews:
+ logger.info(
+ "Filtering removed all results, returning top 3 originals instead"
+ )
+ return previews[: min(3, len(previews))]
+
# Limit to max_filtered_results if specified
if (
self.max_filtered_results
@@ -184,14 +190,17 @@ Respond with ONLY the JSON array, no other text."""
return ranked_results
else:
logger.info(
- "Could not find JSON array in response, returning no previews"
+ "Could not find JSON array in response, returning original previews"
)
- return []
+ # Return at least the top few results instead of nothing
+ max_results = min(5, len(previews))
+ return previews[:max_results]
except Exception as e:
logger.info(f"Relevance filtering error: {e}")
- # Fall back to returning all previews (or top N) on error
- return []
+ # Fall back to returning top results on error
+ max_results = min(5, len(previews))
+ return previews[:max_results]
@abstractmethod
def _get_previews(self, query: str) -> List[Dict[str, Any]]: