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.

Remove unused ContextualFollowUpStrategy and related files

LearningCircuitc8ea732f2b18
diff --git a/src/local_deep_research/advanced_search_system/strategies/contextual_followup_strategy.py b/src/local_deep_research/advanced_search_system/strategies/contextual_followup_strategy.py
deleted file mode 100644
index 2aa8c2fd36..0000000000
--- a/src/local_deep_research/advanced_search_system/strategies/contextual_followup_strategy.py
+++ /dev/null
@@ -1,312 +0,0 @@
-"""
-Contextual Follow-Up Strategy
-
-This strategy acts as a wrapper that:
-1. Retrieves past research context (links and findings)
-2. Filters relevant links from previous research
-3. Passes the query with filtered context to a delegate strategy
-4. Enables efficient follow-up questions with link reuse
-"""
-
-from typing import Dict, List, Optional
-from loguru import logger
-from .base_strategy import BaseSearchStrategy
-
-
-class ContextualFollowUpStrategy(BaseSearchStrategy):
- """
- A wrapper strategy that enhances follow-up queries with past research context.
-
- This strategy:
- 1. Summarizes past research findings
- 2. Filters past links relevant to the current question
- 3. Delegates to another strategy with enriched context
- 4. Avoids redundant searches by reusing relevant past links
- """
-
- def __init__(
- self,
- model,
- search,
- delegate_strategy: BaseSearchStrategy,
- all_links_of_system=None,
- settings_snapshot=None,
- research_context: Optional[Dict] = None,
- **kwargs,
- ):
- """
- Initialize the contextual follow-up strategy.
-
- Args:
- model: The LLM model to use
- search: The search engine
- delegate_strategy: The strategy to delegate actual search to
- all_links_of_system: Accumulated links from past searches
- settings_snapshot: Settings configuration
- research_context: Context from previous research (past links, findings, etc.)
- """
- super().__init__(all_links_of_system, settings_snapshot)
-
- self.model = model
- self.search = search
- self.delegate_strategy = delegate_strategy
- self.research_context = research_context or {}
-
- # Extract past research data if available
- self.past_links = self._extract_past_links()
- self.past_findings = self._extract_past_findings()
-
- logger.info(
- f"ContextualFollowUpStrategy initialized with {len(self.past_links)} past links"
- )
-
- def _extract_past_links(self) -> List[Dict]:
- """Extract links from previous research context."""
- links = []
-
- # Check various sources of past links
- if "past_links" in self.research_context:
- links.extend(self.research_context["past_links"])
-
- if "resources" in self.research_context:
- # Extract from ResearchResource format
- for resource in self.research_context["resources"]:
- links.append(
- {
- "url": resource.get("url", ""),
- "title": resource.get("title", ""),
- "snippet": resource.get("content_preview", ""),
- "source_type": resource.get("source_type", "web"),
- }
- )
-
- if "all_links_of_system" in self.research_context:
- links.extend(self.research_context["all_links_of_system"])
-
- # Also check if we have links from a previous search in the same session
- if self.all_links_of_system:
- links.extend(self.all_links_of_system)
-
- # Deduplicate by URL
- unique_links = {}
- for link in links:
- url = link.get("url", "")
- if url and url not in unique_links:
- unique_links[url] = link
-
- return list(unique_links.values())
-
- def _extract_past_findings(self) -> str:
- """Extract and summarize past research findings."""
- findings_parts = []
-
- # Extract from various possible sources
- if "past_findings" in self.research_context:
- findings_parts.append(self.research_context["past_findings"])
-
- if "report_content" in self.research_context:
- findings_parts.append(self.research_context["report_content"])
-
- if "formatted_findings" in self.research_context:
- findings_parts.append(self.research_context["formatted_findings"])
-
- if "current_knowledge" in self.research_context:
- findings_parts.append(self.research_context["current_knowledge"])
-
- # Combine all findings
- combined_findings = "\n\n".join(filter(None, findings_parts))
-
- # If too long, summarize
- if len(combined_findings) > 5000:
- return self._summarize_findings(combined_findings)
-
- return combined_findings
-
- def _summarize_findings(self, findings: str) -> str:
- """Create a concise summary of past findings."""
- try:
- prompt = f"""Summarize these research findings in 3-5 key points:
-
-{findings[:8000]}
-
-Provide a concise summary focusing on the most important facts and conclusions."""
-
- response = self.model.invoke(prompt)
- return (
- response.content
- if hasattr(response, "content")
- else str(response)
- )
- except Exception as e:
- logger.warning(f"Failed to summarize findings: {e}")
- return findings[:2000] + "..."
-
- def _filter_relevant_links(self, query: str) -> List[Dict]:
- """Filter past links that are relevant to the current query."""
- if not self.past_links:
- return []
-
- try:
- # Prepare links for filtering
- links_text = "\n".join(
- [
- f"{i + 1}. {link.get('title', 'Untitled')} - {link.get('url', '')}"
- for i, link in enumerate(
- self.past_links[:50]
- ) # Limit to 50 for efficiency
- ]
- )
-
- prompt = f"""Given this follow-up question: "{query}"
-
-And these links from previous research:
-{links_text}
-
-Which links are most relevant to answering the follow-up question?
-Return the numbers of the relevant links (comma-separated).
-Only include links that would help answer the specific question.
-If none are relevant, return "none".
-
-Relevant link numbers:"""
-
- response = self.model.invoke(prompt)
- content = (
- response.content
- if hasattr(response, "content")
- else str(response)
- )
-
- # Parse response
- if "none" in content.lower():
- return []
-
- # Extract numbers
- import re
-
- numbers = re.findall(r"\d+", content)
- relevant_indices = [
- int(n) - 1
- for n in numbers
- if 0 <= int(n) - 1 < len(self.past_links)
- ]
-
- # Return filtered links
- filtered_links = [self.past_links[i] for i in relevant_indices]
-
- logger.info(
- f"Filtered {len(filtered_links)} relevant links from {len(self.past_links)} past links"
- )
- return filtered_links
-
- except Exception as e:
- logger.warning(f"Error filtering links: {e}")
- # On error, return top 5 links as fallback
- return self.past_links[:5]
-
- def _create_enriched_query(
- self, query: str, relevant_links: List[Dict]
- ) -> str:
- """Create an enriched query with context from past research."""
- parts = [query]
-
- # Add summary of past findings if available
- if self.past_findings:
- summary = self.past_findings[:1000] # Limit context size
- parts.append(f"\n\nContext from previous research:\n{summary}")
-
- # Add relevant links if any
- if relevant_links:
- links_context = "\n".join(
- [
- f"- {link.get('title', 'Untitled')}: {(link.get('snippet') or '')[:200]}"
- for link in relevant_links[:5]
- ]
- )
- parts.append(
- f"\n\nRelevant sources already found:\n{links_context}"
- )
-
- return "\n".join(parts)
-
- def analyze_topic(self, query: str) -> Dict:
- """
- Analyze a follow-up topic with context from past research.
-
- Args:
- query: The follow-up question to research
-
- Returns:
- Dict containing research findings enhanced with past context
- """
- logger.info(f"Starting contextual follow-up search for: {query}")
-
- self._update_progress(
- "Analyzing past research context", 10, {"phase": "context_analysis"}
- )
-
- # Filter relevant links from past research
- relevant_links = self._filter_relevant_links(query)
-
- self._update_progress(
- f"Found {len(relevant_links)} relevant past sources",
- 20,
- {
- "phase": "link_filtering",
- "relevant_links": len(relevant_links),
- "total_past_links": len(self.past_links),
- },
- )
-
- # Create enriched query with context
- enriched_query = self._create_enriched_query(query, relevant_links)
-
- # Inject relevant links into the delegate strategy's all_links_of_system
- if relevant_links:
- if not self.delegate_strategy.all_links_of_system:
- self.delegate_strategy.all_links_of_system = []
-
- # Add relevant past links to the beginning
- # This ensures they're considered but can be supplemented with new searches
- existing_urls = {
- link.get("url")
- for link in self.delegate_strategy.all_links_of_system
- }
-
- for link in relevant_links:
- if link.get("url") not in existing_urls:
- self.delegate_strategy.all_links_of_system.insert(0, link)
-
- self._update_progress(
- "Delegating to search strategy with context",
- 30,
- {"phase": "delegation"},
- )
-
- # Delegate to the wrapped strategy with enriched context
- # Pass the original query but with pre-loaded relevant links
- result = self.delegate_strategy.analyze_topic(query)
-
- # Enhance result with context information
- result["contextual_search"] = True
- result["past_links_used"] = len(relevant_links)
- result["total_past_links"] = len(self.past_links)
-
- # Add a note about context usage in the findings
- if relevant_links and "formatted_findings" in result:
- context_note = f"\n\n**Note:** This follow-up search reused {len(relevant_links)} relevant sources from previous research."
- result["formatted_findings"] += context_note
-
- # Store the enriched query for reference
- result["enriched_query"] = enriched_query
-
- self._update_progress(
- "Contextual follow-up search complete", 100, {"phase": "complete"}
- )
-
- return result
-
- def set_progress_callback(self, callback):
- """Set progress callback for both wrapper and delegate."""
- super().set_progress_callback(callback)
- if self.delegate_strategy:
- self.delegate_strategy.set_progress_callback(callback)
diff --git a/src/local_deep_research/followup_research/README.md b/src/local_deep_research/followup_research/README.md
deleted file mode 100644
index adc3bfd33e..0000000000
--- a/src/local_deep_research/followup_research/README.md
+++ /dev/null
@@ -1,115 +0,0 @@
-# Follow-up Research Module
-
-This module provides functionality for asking follow-up questions on existing research, reusing previous findings and sources to provide contextual answers.
-
-## Architecture
-
-The follow-up research feature is organized as a self-contained module:
-
-```
-followup_research/
-├── __init__.py # Module exports
-├── models.py # Data models (FollowUpRequest, FollowUpResponse)
-├── service.py # Business logic for follow-up research
-├── routes.py # Flask API endpoints
-├── static/
-│ └── followup.js # Frontend JavaScript
-└── README.md # This file
-```
-
-## How It Works
-
-1. **User initiates follow-up**: Clicks "Ask Follow-up Question" on a research result
-2. **Context loading**: Service loads parent research data and resources from database
-3. **Strategy execution**: Uses `ContextualFollowUpStrategy` which:
- - Filters relevant links from parent research
- - Summarizes relevant findings
- - Delegates to `source-based` strategy for quick summary
-4. **Results**: Returns focused answer building on previous research
-
-## Configuration
-
-The follow-up research uses these default settings:
-- **Strategy**: `contextual-followup` (wrapper) → `source-based` (delegate)
-- **Iterations**: 1 (quick summary mode)
-- **Questions per iteration**: 3
-
-## API Endpoints
-
-### POST `/api/followup/prepare`
-Loads parent research context for preview.
-
-Request:
-```json
-{
- "parent_research_id": "uuid",
- "question": "follow-up question"
-}
-```
-
-### POST `/api/followup/start`
-Starts a follow-up research task.
-
-Request:
-```json
-{
- "parent_research_id": "uuid",
- "question": "follow-up question",
- "strategy": "source-based",
- "max_iterations": 1,
- "questions_per_iteration": 3
-}
-```
-
-## Frontend Integration
-
-The JavaScript module is automatically included in the results page (`results.html`):
-- Located at: `web/static/js/followup.js`
-- Included via: `{{ url_for('research.serve_static', path='js/followup.js') }}`
-
-The script automatically:
-- Adds "Ask Follow-up Question" button to research results
-- Creates modal for entering follow-up questions
-- Handles API communication
-- Redirects to new research when started
-
-## Database Requirements
-
-The module requires these database tables:
-- `ResearchHistory`: Stores parent research data
-- `ResearchResource`: Stores research links and sources
-
-## Usage Example
-
-```python
-from local_deep_research.followup_research import FollowUpResearchService, FollowUpRequest
-
-# Initialize service
-service = FollowUpResearchService(username="user123")
-
-# Create follow-up request
-request = FollowUpRequest(
- parent_research_id="abc-123",
- question="What are the implications of the findings?",
- strategy="source-based",
- max_iterations=1
-)
-
-# Perform follow-up
-params = service.perform_followup(request)
-```
-
-## Benefits
-
-- **Efficiency**: Reuses already-fetched content, reducing API calls
-- **Context Preservation**: Answers build coherently on original research
-- **Clean Architecture**: Self-contained module, doesn't pollute main codebase
-- **Flexibility**: Can use any strategy as the delegate
-
-## Future Enhancements
-
-- [ ] Support multiple delegate strategies
-- [ ] Add conversation threading for multi-turn follow-ups
-- [ ] Implement relevance scoring visualization
-- [ ] Add export of follow-up chains
-- [ ] Support batch follow-up questions
diff --git a/tests/test_followup_api.py b/tests/test_followup_api.py
index 2dc72f3214..3901377c4b 100644
--- a/tests/test_followup_api.py
+++ b/tests/test_followup_api.py
@@ -305,38 +305,3 @@ class TestFollowUpAPI:
assert data["strategy"] == "source-based"
assert data["max_iterations"] == 2
assert data["questions_per_iteration"] == 5
-
- def test_contextual_strategy_registration(self):
- """Test that contextual-followup strategy is properly registered."""
- from local_deep_research.search_system import AdvancedSearchSystem
-
- with (
- patch("local_deep_research.search_system.get_llm") as mock_llm,
- patch(
- "local_deep_research.search_system.get_search"
- ) as mock_search,
- ):
- # Setup mocks
- mock_llm.return_value = MagicMock()
- mock_search.return_value = MagicMock()
-
- # Create system with contextual-followup strategy
- research_context = {
- "delegate_strategy": "source-based",
- "past_links": [],
- "past_findings": "",
- }
-
- system = AdvancedSearchSystem(
- llm=mock_llm.return_value,
- search=mock_search.return_value,
- strategy_name="contextual-followup",
- research_context=research_context,
- )
-
- # Verify strategy type
- from local_deep_research.advanced_search_system.strategies.contextual_followup_strategy import (
- ContextualFollowUpStrategy,
- )
-
- assert isinstance(system.strategy, ContextualFollowUpStrategy)