~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: Implement follow-up query as question generator
LearningCircuit8a83bbee3fec
diff --git a/src/local_deep_research/advanced_search_system/questions/followup/__init__.py b/src/local_deep_research/advanced_search_system/questions/followup/__init__.pynew file mode 100644index 0000000000..e29c8f3e90--- /dev/null+++ b/src/local_deep_research/advanced_search_system/questions/followup/__init__.py@@ -0,0 +1,14 @@+"""+Follow-up Question Generators Package++This package contains specialized question generators for follow-up research+that builds upon previous research context.+"""++from .base_followup_question import BaseFollowUpQuestionGenerator+from .simple_followup_question import SimpleFollowUpQuestionGenerator++__all__ = [+ "BaseFollowUpQuestionGenerator",+ "SimpleFollowUpQuestionGenerator",+]diff --git a/src/local_deep_research/advanced_search_system/questions/followup/base_followup_question.py b/src/local_deep_research/advanced_search_system/questions/followup/base_followup_question.pynew file mode 100644index 0000000000..da4717849b--- /dev/null+++ b/src/local_deep_research/advanced_search_system/questions/followup/base_followup_question.py@@ -0,0 +1,91 @@+"""+Base class for follow-up question generators.++This extends the standard question generator interface to handle+follow-up research that includes previous research context.+"""++from abc import abstractmethod+from typing import Dict, List+from ..base_question import BaseQuestionGenerator+++class BaseFollowUpQuestionGenerator(BaseQuestionGenerator):+ """+ Abstract base class for follow-up question generators.++ These generators create contextualized queries that incorporate+ previous research findings and context.+ """++ def __init__(self, model):+ """+ Initialize the follow-up question generator.++ Args:+ model: The language model to use for question generation+ """+ super().__init__(model)+ self.follow_up_context = {}++ def set_follow_up_context(self, context: Dict):+ """+ Set the follow-up research context.++ Args:+ context: Dictionary containing:+ - past_findings: Previous research findings+ - original_query: The original research query+ - follow_up_query: The follow-up question from user+ - past_sources: Sources from previous research+ - key_entities: Key entities identified+ """+ self.follow_up_context = context++ @abstractmethod+ def generate_contextualized_query(+ self,+ follow_up_query: str,+ original_query: str,+ past_findings: str,+ **kwargs,+ ) -> str:+ """+ Generate a contextualized query for follow-up research.++ Args:+ follow_up_query: The user's follow-up question+ original_query: The original research query+ past_findings: The findings from previous research+ **kwargs: Additional context parameters++ Returns:+ str: A contextualized query that includes previous context+ """+ pass++ def generate_questions(+ self,+ current_knowledge: str,+ query: str,+ questions_per_iteration: int,+ questions_by_iteration: Dict[int, List[str]],+ ) -> List[str]:+ """+ Generate questions for follow-up research.++ For follow-up research, we typically return a single contextualized+ query rather than multiple questions, as the context is already rich.++ Args:+ current_knowledge: The accumulated knowledge so far+ query: The research query (already contextualized)+ questions_per_iteration: Number of questions to generate+ questions_by_iteration: Previous questions++ Returns:+ List[str]: List containing the contextualized query+ """+ # For follow-up research, the query is already contextualized+ # Just return it as a single-item list+ return [query]diff --git a/src/local_deep_research/advanced_search_system/questions/followup/llm_followup_question.py b/src/local_deep_research/advanced_search_system/questions/followup/llm_followup_question.pynew file mode 100644index 0000000000..46ec68c7c6--- /dev/null+++ b/src/local_deep_research/advanced_search_system/questions/followup/llm_followup_question.py@@ -0,0 +1,93 @@+"""+LLM-based follow-up question generator.++This implementation uses an LLM to intelligently reformulate follow-up+questions based on the previous research context.+"""++from typing import Dict, List+from loguru import logger+from .base_followup_question import BaseFollowUpQuestionGenerator+++class LLMFollowUpQuestionGenerator(BaseFollowUpQuestionGenerator):+ """+ LLM-based follow-up question generator.++ This generator uses an LLM to reformulate follow-up questions+ based on the previous research context, creating more targeted+ and effective search queries.++ NOTE: This is a placeholder for future implementation.+ Currently falls back to simple concatenation.+ """++ def generate_contextualized_query(+ self,+ follow_up_query: str,+ original_query: str,+ past_findings: str,+ **kwargs,+ ) -> str:+ """+ Generate a contextualized query using LLM reformulation.++ Future implementation will:+ 1. Analyze the follow-up query in context of past findings+ 2. Identify information gaps+ 3. Reformulate for more effective searching+ 4. Generate multiple targeted search questions++ Args:+ follow_up_query: The user's follow-up question+ original_query: The original research query+ past_findings: The findings from previous research+ **kwargs: Additional context parameters++ Returns:+ str: An LLM-reformulated contextualized query+ """+ # TODO: Implement LLM-based reformulation+ # For now, fall back to simple concatenation+ logger.warning(+ "LLM-based follow-up question generation not yet implemented, "+ "falling back to simple concatenation"+ )++ from .simple_followup_question import SimpleFollowUpQuestionGenerator++ simple_generator = SimpleFollowUpQuestionGenerator(self.model)+ return simple_generator.generate_contextualized_query(+ follow_up_query, original_query, past_findings, **kwargs+ )++ def generate_questions(+ self,+ current_knowledge: str,+ query: str,+ questions_per_iteration: int,+ questions_by_iteration: Dict[int, List[str]],+ ) -> List[str]:+ """+ Generate multiple targeted questions for follow-up research.++ Future implementation will generate multiple specific questions+ based on the follow-up query and context.++ Args:+ current_knowledge: The accumulated knowledge so far+ query: The research query+ questions_per_iteration: Number of questions to generate+ questions_by_iteration: Previous questions++ Returns:+ List[str]: List of targeted follow-up questions+ """+ # TODO: Implement multi-question generation+ # For now, return single contextualized query+ return super().generate_questions(+ current_knowledge,+ query,+ questions_per_iteration,+ questions_by_iteration,+ )diff --git a/src/local_deep_research/advanced_search_system/questions/followup/simple_followup_question.py b/src/local_deep_research/advanced_search_system/questions/followup/simple_followup_question.pynew file mode 100644index 0000000000..c3e2e9319a--- /dev/null+++ b/src/local_deep_research/advanced_search_system/questions/followup/simple_followup_question.py@@ -0,0 +1,65 @@+"""+Simple concatenation-based follow-up question generator.++This implementation preserves the current behavior of concatenating+the previous research context with the follow-up query without using+an LLM to reformulate.+"""++from loguru import logger+from .base_followup_question import BaseFollowUpQuestionGenerator+++class SimpleFollowUpQuestionGenerator(BaseFollowUpQuestionGenerator):+ """+ Simple follow-up question generator that concatenates context.++ This generator creates a contextualized query by directly concatenating+ the previous research findings with the follow-up question, without+ any LLM-based reformulation. This ensures the follow-up query is+ understood in the context of previous research.+ """++ def generate_contextualized_query(+ self,+ follow_up_query: str,+ original_query: str,+ past_findings: str,+ **kwargs,+ ) -> str:+ """+ Generate a contextualized query by simple concatenation.++ This method preserves the exact user query while providing full+ context from previous research. This ensures queries like+ "provide data in a table" are understood as referring to the+ previous findings, not as new searches.++ Args:+ follow_up_query: The user's follow-up question+ original_query: The original research query+ past_findings: The findings from previous research+ **kwargs: Additional context parameters (unused)++ Returns:+ str: A contextualized query with previous research embedded+ """+ # Simply concatenate the context with the query - no LLM interpretation needed+ # Highlight importance at top, actual request at bottom+ contextualized = f"""IMPORTANT: This is a follow-up request. Focus on addressing the specific user request at the bottom of this prompt using the previous research context provided below.++Previous research query: {original_query}++Previous findings:+{past_findings}++---+USER'S FOLLOW-UP REQUEST: {follow_up_query}+Please address this specific request using the context and findings above.+---"""++ logger.info(+ f"Created contextualized query with {len(past_findings)} chars of context"+ )++ return contextualizeddiff --git a/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py b/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.pyindex beb423e6cf..00cffdec47 100644--- a/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py+++ b/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py@@ -11,6 +11,9 @@ from loguru import loggerfrom ..base_strategy import BaseSearchStrategyfrom ...filters.followup_relevance_filter import FollowUpRelevanceFilterfrom ...knowledge.followup_context_manager import FollowUpContextHandler+from ...questions.followup.simple_followup_question import (+ SimpleFollowUpQuestionGenerator,+)class EnhancedContextualFollowUpStrategy(BaseSearchStrategy):@@ -56,6 +59,9 @@ class EnhancedContextualFollowUpStrategy(BaseSearchStrategy):self.relevance_filter = FollowUpRelevanceFilter(model)self.context_manager = FollowUpContextHandler(model)+ # Initialize question generator for creating contextualized queries+ self.question_generator = SimpleFollowUpQuestionGenerator(model)+# For follow-up research, we ALWAYS want to combine sources# This is the whole point of follow-up - building on previous researchself.combine_sources = True@@ -185,8 +191,17 @@ class EnhancedContextualFollowUpStrategy(BaseSearchStrategy):)# Step 4: Create a query that includes FULL context from previous research- # Use original query directly without reformulation- contextualized_query = self._create_contextualized_query(query, query)+ # Use question generator to create contextualized query+ past_findings = self.full_context.get("past_findings", "")+ original_research_query = self.full_context.get("original_query", "")++ contextualized_query = (+ self.question_generator.generate_contextualized_query(+ follow_up_query=query,+ original_query=original_research_query,+ past_findings=past_findings,+ )+ )# Let the delegate strategy (from user's settings) do the actual research# with the contextualized query and pre-injected sources@@ -269,47 +284,6 @@ class EnhancedContextualFollowUpStrategy(BaseSearchStrategy):return result- def _create_contextualized_query(- self, reformulated_query: str, original_query: str- ) -> str:- """- Create a query that includes full context from previous research.-- This ensures the LLM understands that queries like "provide data in a table"- refer to the previous research findings, not a new search about tables.-- Args:- reformulated_query: The reformulated search query- original_query: The original user query-- Returns:- Query with full context embedded- """- # Get the past findings - the complete text, no truncation- past_findings = self.full_context.get("past_findings", "")-- # Get the original research query- original_research_query = self.full_context.get("original_query", "")-- # Simply concatenate the context with the query - no LLM interpretation needed- # Highlight importance at top, actual request at bottom- contextualized = f"""IMPORTANT: This is a follow-up request. Focus on addressing the specific user request at the bottom of this prompt using the previous research context provided below.--Previous research query: {original_research_query}--Previous findings:-{past_findings}------USER'S FOLLOW-UP REQUEST: {original_query}-Please address this specific request using the context and findings above.----"""-- logger.info(- f"Created contextualized query with {len(past_findings)} chars of context"- )- return contextualized-def _filter_relevant_sources(self, query: str) -> List[Dict]:"""Filter past sources for relevance to the follow-up query.