~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
new search strategy that is much more streamlined
LearningCircuit017a65d92817
diff --git a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.pynew file mode 100644index 0000000000..dac1e3ad56--- /dev/null+++ b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py@@ -0,0 +1,372 @@+import concurrent.futures+import logging+from typing import Dict++from ...citation_handler import CitationHandler+from ...config.llm_config import get_llm+from ...config.search_config import get_search+from ...utilities.db_utils import get_db_setting+from ...utilities.search_utilities import extract_links_from_search_results+from ..filters.cross_engine_filter import CrossEngineFilter+from ..findings.repository import FindingsRepository+from ..questions.standard_question import StandardQuestionGenerator+from .base_strategy import BaseSearchStrategy++logger = logging.getLogger(__name__)+++class SourceBasedSearchStrategy(BaseSearchStrategy):+ """+ Source-based search strategy that generates questions based on search results and+ defers content analysis until final synthesis.+ """++ def __init__(+ self,+ search=None,+ model=None,+ citation_handler=None,+ include_text_content: bool = True,+ use_cross_engine_filter: bool = True,+ filter_reorder: bool = True,+ filter_reindex: bool = True,+ filter_max_results: int = 20,+ ):+ """Initialize with optional dependency injection for testing."""+ super().__init__()+ self.search = search or get_search()+ self.model = model or get_llm()+ self.progress_callback = None+ self.all_links_of_system = list()+ self.all_search_results = []+ self.questions_by_iteration = {}+ self.include_text_content = include_text_content+ self.use_cross_engine_filter = use_cross_engine_filter+ self.filter_reorder = filter_reorder+ self.filter_reindex = filter_reindex++ # Initialize the cross-engine filter+ self.cross_engine_filter = CrossEngineFilter(+ model=self.model,+ max_results=filter_max_results,+ default_reorder=filter_reorder,+ default_reindex=filter_reindex,+ )++ # Set include_full_content on the search engine if it supports it+ if hasattr(self.search, "include_full_content"):+ self.search.include_full_content = include_text_content++ # Use provided citation_handler or create one+ self.citation_handler = citation_handler or CitationHandler(self.model)++ # Initialize components+ self.question_generator = StandardQuestionGenerator(self.model)+ self.findings_repository = FindingsRepository(self.model)++ def _format_search_results_as_context(self, search_results):+ """Format search results into context for question generation."""+ context_snippets = []++ for i, result in enumerate(+ search_results[:10]+ ): # Limit to prevent context overflow+ title = result.get("title", "Untitled")+ snippet = result.get("snippet", "")+ url = result.get("link", "")++ if snippet:+ context_snippets.append(+ f"Source {i + 1}: {title}\nURL: {url}\nSnippet: {snippet}"+ )++ return "\n\n".join(context_snippets)++ def analyze_topic(self, query: str) -> Dict:+ """+ Analyze a topic using source-based search strategy.+ """+ logger.info(f"Starting source-based research on topic: {query}")++ findings = []+ self.all_search_results = []++ # Track all search results across iterations+ self.all_links_of_system = list()+ self.questions_by_iteration = {}++ self._update_progress(+ "Initializing source-based research",+ 5,+ {+ "phase": "init",+ "strategy": "source-based",+ "include_text_content": self.include_text_content,+ },+ )++ # Check search engine+ if not self._validate_search_engine():+ return {+ "findings": [],+ "iterations": 0,+ "questions_by_iteration": {},+ "formatted_findings": "Error: Unable to conduct research without a search engine.",+ "current_knowledge": "",+ "error": "No search engine available",+ }++ # Determine number of iterations to run+ iterations_to_run = get_db_setting("search.iterations")+ logger.debug("Selected amount of iterations: " + iterations_to_run)+ iterations_to_run = int(iterations_to_run)+ try:+ # Run each iteration+ for iteration in range(1, iterations_to_run + 1):+ iteration_progress_base = 5 + (iteration - 1) * (70 / iterations_to_run)++ self._update_progress(+ f"Starting iteration {iteration}/{iterations_to_run}",+ iteration_progress_base,+ {"phase": f"iteration_{iteration}", "iteration": iteration},+ )++ # Step 1: Generate or use questions+ self._update_progress(+ f"Generating search questions for iteration {iteration}",+ iteration_progress_base + 5,+ {"phase": "question_generation", "iteration": iteration},+ )++ # For first iteration, use initial query+ if iteration == 1:+ # First round is simply the initial query+ all_questions = [query]+ self.questions_by_iteration[iteration] = [query]+ logger.info(+ f"Using initial query for iteration {iteration}: {query}"+ )+ else:+ # For subsequent iterations, generate questions based on previous search results+ source_context = self._format_search_results_as_context(+ self.all_search_results+ )+ context = f"""Previous search results:\n{source_context}\n\nIteration: {iteration} of {iterations_to_run}"""++ # Use standard question generator with search results as context+ questions = self.question_generator.generate_questions(+ current_knowledge=context,+ query=query,+ questions_per_iteration=int(+ get_db_setting("search.questions_per_iteration")+ ),+ questions_by_iteration=self.questions_by_iteration,+ )++ # Use only the new questions for this iteration's searches+ all_questions = questions++ # Store in questions_by_iteration+ self.questions_by_iteration[iteration] = questions+ logger.info(+ f"Generated questions for iteration {iteration}: {questions}"+ )++ # Step 2: Run all searches in parallel for this iteration+ self._update_progress(+ f"Running parallel searches for iteration {iteration}",+ iteration_progress_base + 10,+ {"phase": "parallel_search", "iteration": iteration},+ )++ # Function for thread pool+ def search_question(q):+ try:+ result = self.search.run(q)+ return {"question": q, "results": result or []}+ except Exception as e:+ logger.error(f"Error searching for '{q}': {str(e)}")+ return {"question": q, "results": [], "error": str(e)}++ # Run searches in parallel+ with concurrent.futures.ThreadPoolExecutor(+ max_workers=len(all_questions)+ ) as executor:+ futures = [+ executor.submit(search_question, q) for q in all_questions+ ]+ iteration_search_dict = {}+ iteration_search_results = []++ # Process results as they complete+ for i, future in enumerate(+ concurrent.futures.as_completed(futures)+ ):+ result_dict = future.result()+ question = result_dict["question"]+ search_results = result_dict["results"]+ iteration_search_dict[question] = search_results++ self._update_progress(+ f"Completed search {i + 1} of {len(all_questions)}: {question[:30]}...",+ iteration_progress_base+ + 10+ + ((i + 1) / len(all_questions) * 30),+ {+ "phase": "search_complete",+ "iteration": iteration,+ "result_count": len(search_results),+ "question": question,+ },+ )++ # Collect all search results for this iteration+ iteration_search_results.extend(search_results)++ # Step 3: Apply cross-engine filtering if enabled+ if self.use_cross_engine_filter:+ self._update_progress(+ f"Filtering search results for iteration {iteration}",+ iteration_progress_base + 45,+ {"phase": "cross_engine_filtering", "iteration": iteration},+ )++ # Get the current link count (for indexing)+ existing_link_count = len(self.all_links_of_system)++ # Filter the search results+ filtered_search_results = self.cross_engine_filter.filter_results(+ iteration_search_results,+ query,+ reorder=self.filter_reorder,+ reindex=self.filter_reindex,+ start_index=existing_link_count, # Start indexing after existing links+ )++ links = extract_links_from_search_results(filtered_search_results)+ self.all_links_of_system.extend(links)++ self._update_progress(+ f"Filtered from {len(iteration_search_results)} to {len(filtered_search_results)} results",+ iteration_progress_base + 50,+ {+ "phase": "filtering_complete",+ "iteration": iteration,+ "links_count": len(self.all_links_of_system),+ },+ )++ # Use filtered results+ iteration_search_results = filtered_search_results+ else:+ # Just extract links without filtering+ links = extract_links_from_search_results(iteration_search_results)+ self.all_links_of_system.extend(links)++ # Add to all search results+ self.all_search_results.extend(iteration_search_results)++ # Create a lightweight finding for this iteration's search metadata (no text content)+ finding = {+ "phase": f"Iteration {iteration}",+ "content": f"Searched with {len(all_questions)} questions, found {len(iteration_search_results)} results.",+ "question": query,+ "search_results": iteration_search_results,+ "documents": [],+ }+ findings.append(finding)++ # Mark iteration as complete+ iteration_progress = 5 + iteration * (70 / iterations_to_run)+ self._update_progress(+ f"Completed iteration {iteration}/{iterations_to_run}",+ iteration_progress,+ {"phase": "iteration_complete", "iteration": iteration},+ )++ # Final filtering of all accumulated search results+ self._update_progress(+ "Performing final filtering of all results",+ 80,+ {"phase": "final_filtering"},+ )++ # Apply final cross-engine filtering to all accumulated results if enabled+ if self.use_cross_engine_filter:+ final_filtered_results = self.cross_engine_filter.filter_results(+ self.all_search_results,+ query,+ reorder=True, # Always reorder in final filtering+ reindex=True, # Always reindex in final filtering+ max_results=int(get_db_setting("search.final_max_results") or 30),+ )+ else:+ final_filtered_results = self.all_search_results++ # Final synthesis after all iterations+ self._update_progress(+ "Generating final synthesis", 90, {"phase": "synthesis"}+ )++ # Generate final synthesis from filtered search results+ final_citation_result = self.citation_handler.analyze_initial(+ query, final_filtered_results+ )++ # Add null check for final_citation_result+ if final_citation_result:+ synthesized_content = final_citation_result["content"]+ documents = final_citation_result.get("documents", [])+ else:+ synthesized_content = "No relevant results found in final synthesis."+ documents = []++ # Add a final synthesis finding+ final_finding = {+ "phase": "Final synthesis",+ "content": synthesized_content,+ "question": query,+ "search_results": final_filtered_results,+ "documents": documents,+ }+ findings.append(final_finding)++ # Add documents to repository+ self.findings_repository.add_documents(documents)++ # Transfer questions to repository+ self.findings_repository.set_questions_by_iteration(+ self.questions_by_iteration+ )++ # Format findings+ formatted_findings = self.findings_repository.format_findings_to_text(+ findings, synthesized_content+ )++ except Exception as e:+ import traceback++ error_msg = f"Error in research process: {str(e)}"+ logger.error(error_msg)+ logger.error(traceback.format_exc())+ synthesized_content = f"Error: {str(e)}"+ formatted_findings = f"Error: {str(e)}"+ finding = {+ "phase": "Error",+ "content": synthesized_content,+ "question": query,+ "search_results": [],+ "documents": [],+ }+ findings.append(finding)++ self._update_progress("Research complete", 100, {"phase": "complete"})++ return {+ "findings": findings,+ "iterations": iterations_to_run,+ "questions_by_iteration": self.questions_by_iteration,+ "formatted_findings": formatted_findings,+ "current_knowledge": synthesized_content,+ }diff --git a/src/local_deep_research/citation_handler.py b/src/local_deep_research/citation_handler.pyindex 8bb978c340..3ee19abf3a 100644--- a/src/local_deep_research/citation_handler.py+++ b/src/local_deep_research/citation_handler.py@@ -82,18 +82,18 @@ Provide a detailed analysis with citations. Do not create the bibliography, it wformatted_sources = self._format_sources(documents)# Add fact-checking stepfact_check_prompt = f"""Analyze these sources for factual consistency:- 1. Cross-reference major claims between sources- 2. Identify and flag any contradictions- 3. Verify basic facts (dates, company names, ownership)- 4. Note when sources disagree+1. Cross-reference major claims between sources+2. Identify and flag any contradictions+3. Verify basic facts (dates, company names, ownership)+4. Note when sources disagree- Previous Knowledge:- {previous_knowledge}+Previous Knowledge:+{previous_knowledge}- New Sources:- {formatted_sources}+New Sources:+{formatted_sources}- Return any inconsistencies or conflicts found."""+Return any inconsistencies or conflicts found."""if get_db_setting("general.enable_fact_checking", settings.general.enable_fact_checking):@@ -104,16 +104,15 @@ Provide a detailed analysis with citations. Do not create the bibliography, it wprompt = f"""Using the previous knowledge and new sources, answer the question. Include citations using numbers in square brackets [1], [2], etc. When citing, use the source number provided at the start of each source. Reflect information from sources critically.- Previous Knowledge:- {previous_knowledge}+Previous Knowledge:+{previous_knowledge}- Question: {question}+Question: {question}- New Sources:- {formatted_sources}- Reflect information from sources critically based on: {fact_check_response}. Never invent sources.- Provide a detailed answer with citations. Example format: "According to [1], ..."- """+New Sources:+{formatted_sources}+Reflect information from sources critically based on: {fact_check_response}. Never invent sources.+Provide a detailed answer with citations. Example format: "According to [1], ..." """response = self.llm.invoke(prompt)diff --git a/src/local_deep_research/search_system.py b/src/local_deep_research/search_system.pyindex 3068ecea11..af7df078bd 100644--- a/src/local_deep_research/search_system.py+++ b/src/local_deep_research/search_system.py@@ -13,6 +13,9 @@ from .advanced_search_system.strategies.parallel_search_strategy import (ParallelSearchStrategy,)from .advanced_search_system.strategies.rapid_search_strategy import RapidSearchStrategy+from .advanced_search_system.strategies.source_based_strategy import (+ SourceBasedSearchStrategy,+)from .advanced_search_system.strategies.standard_strategy import StandardSearchStrategyfrom .citation_handler import CitationHandlerfrom .config.config_files import settings@@ -30,7 +33,7 @@ class AdvancedSearchSystem:def __init__(self,- strategy_name: str = "parallel",+ strategy_name: str = "source-based",include_text_content: bool = True,use_cross_engine_filter: bool = True,llm: BaseChatModel | None = None,@@ -70,6 +73,14 @@ class AdvancedSearchSystem:if strategy_name.lower() == "iterdrag":logger.info("Creating IterDRAGStrategy instance")self.strategy = IterDRAGStrategy(model=self.model, search=self.search)+ elif strategy_name.lower() == "source-based":+ logger.info("Creating SourceBasedSearchStrategy instance")+ self.strategy = SourceBasedSearchStrategy(+ model=self.model,+ search=self.search,+ include_text_content=include_text_content,+ use_cross_engine_filter=use_cross_engine_filter,+ )elif strategy_name.lower() == "parallel":logger.info("Creating ParallelSearchStrategy instance")self.strategy = ParallelSearchStrategy(