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.

feat: implement TopicOrganizationStrategy with iterative refinement

LearningCircuit788d6ec10601
diff --git a/src/local_deep_research/advanced_search_system/findings/topic.py b/src/local_deep_research/advanced_search_system/findings/topic.py
index 105edb8128..bc110d7333 100644
--- a/src/local_deep_research/advanced_search_system/findings/topic.py
+++ b/src/local_deep_research/advanced_search_system/findings/topic.py
@@ -19,8 +19,6 @@ class Topic:
rejected_sources: List[Dict[str, Any]] = field(default_factory=list)
# Metadata
- confidence_score: float = 0.0
- keywords: List[str] = field(default_factory=list)
created_at: datetime = field(default_factory=datetime.now)
# Relationships to other topics
@@ -64,8 +62,6 @@ class Topic:
"lead_source": self.lead_source,
"supporting_sources": self.supporting_sources,
"rejected_sources": self.rejected_sources,
- "confidence_score": self.confidence_score,
- "keywords": self.keywords,
"created_at": self.created_at.isoformat(),
"related_topic_ids": self.related_topic_ids,
"parent_topic_id": self.parent_topic_id,
@@ -157,9 +153,6 @@ class TopicGraph:
if source not in topic1.rejected_sources:
topic1.rejected_sources.append(source)
- # Merge keywords
- topic1.keywords = list(set(topic1.keywords + topic2.keywords))
-
# Update relationships
for related_id in topic2.related_topic_ids:
if (
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
index 07ce0407d7..589dde0bd1 100644
--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
@@ -1,5 +1,5 @@
import uuid
-from typing import Dict, List, Any
+from typing import Dict, List, Any, Optional
from loguru import logger
@@ -32,6 +32,8 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
min_sources_per_topic: int = 2,
max_topics: int = 5,
similarity_threshold: float = 0.7,
+ enable_refinement: bool = False,
+ max_refinement_iterations: int = 2,
):
"""Initialize the topic organization strategy."""
super().__init__(
@@ -48,6 +50,9 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
self.min_sources_per_topic = min_sources_per_topic
self.max_topics = max_topics
self.similarity_threshold = similarity_threshold
+ self.enable_refinement = enable_refinement
+ self.max_refinement_iterations = max_refinement_iterations
+ self.refinement_questions = [] # Track all questions asked
# Create a no-op citation handler to skip text generation
class NoTextCitationHandler:
@@ -75,14 +80,29 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
self.topic_graph = TopicGraph()
def _extract_topics_from_sources(
- self, sources: List[Dict[str, Any]], query: str
+ self,
+ sources: List[Dict[str, Any]],
+ query: str,
+ existing_topics: List[Topic] = None,
) -> List[Topic]:
"""
Use LLM to identify topics from search results and select lead sources.
+ Can add new sources to existing topics or create new topics.
"""
if not sources:
return []
+ existing_topics = existing_topics or []
+
+ # Format existing topics (only show lead sources to LLM)
+ existing_topics_info = []
+ if existing_topics:
+ for i, topic in enumerate(existing_topics):
+ lead = topic.lead_source
+ existing_topics_info.append(
+ f"Topic {i}: {topic.title}\nLead: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:200]}"
+ )
+
# Format sources for LLM analysis
source_texts = []
for i, source in enumerate(
@@ -97,43 +117,47 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
sources_context = "\n\n".join(source_texts)
- # Prompt for topic identification
+ # Unified prompt that works for both initial and refinement iterations
+ existing_topics_str = (
+ "\n\n".join(existing_topics_info)
+ if existing_topics_info
+ else "None yet"
+ )
+
topic_prompt = f"""
-Analyze these search results for the query: "{query}"
+Organize search results into topics for the query: "{query}"
+
+EXISTING TOPICS:
+{existing_topics_str}
-Sources:
+NEW SOURCES TO ORGANIZE:
{sources_context}
-Identify up to {self.max_topics} distinct topics from these sources.
-For each topic:
-1. Select the source number that best represents this topic (lead source)
-2. List other source numbers that support this topic
+Group these sources into topics by:
+1. Adding sources to existing topics if they match (use existing_topic_index)
+2. Creating new topics from sources that form distinct groups
Format your response as a JSON list:
[
{{
- "lead_source_index": 0,
- "supporting_source_indices": [1, 2, 3]
+ "existing_topic_index": -1, // -1 for new topic, 0+ for existing topic
+ "lead_source_index": 0, // required for new topics
+ "supporting_source_indices": [1, 2, 3] // sources to add
}}
]
Important:
- Each source should belong to at most one topic
-- Only include topics with at least {self.min_sources_per_topic} sources
-- Select the most informative source as the lead
+- New topics need at least {self.min_sources_per_topic} sources
+- For existing topics, just add supporting sources (no lead needed)
+- Select the most informative source as lead for new topics
"""
try:
- # Use LangChain's invoke method
- from langchain_core.messages import HumanMessage
-
- response = self.model.invoke([HumanMessage(content=topic_prompt)])
-
- # Extract content from response
- response_text = (
- response.content
- if hasattr(response, "content")
- else str(response)
+ # Call model directly with prompt
+ response = self.model.invoke(topic_prompt)
+ response_text = str(
+ response.content if hasattr(response, "content") else response
)
# Parse JSON response
@@ -148,42 +172,70 @@ Important:
else:
topic_data = json.loads(response_text)
- topics = []
+ # Start with existing topics or empty list
+ topics = list(existing_topics) if existing_topics else []
used_indices = set()
- for i, topic_info in enumerate(topic_data[: self.max_topics]):
- lead_idx = topic_info.get("lead_source_index")
+ for i, topic_info in enumerate(topic_data):
+ existing_topic_idx = topic_info.get("existing_topic_index", -1)
supporting_indices = topic_info.get(
"supporting_source_indices", []
)
- # Skip if lead source already used
- if lead_idx in used_indices or lead_idx >= len(sources):
- continue
+ if existing_topic_idx >= 0 and existing_topic_idx < len(
+ existing_topics
+ ):
+ # Add sources to existing topic
+ topic = existing_topics[existing_topic_idx]
+ for idx in supporting_indices:
+ if idx not in used_indices and idx < len(sources):
+ topic.add_supporting_source(sources[idx])
+ used_indices.add(idx)
+ logger.info(
+ f"Added source {idx} to existing topic '{topic.title}'"
+ )
+ else:
+ # Create new topic
+ lead_idx = topic_info.get("lead_source_index")
+
+ # Skip if lead source already used or invalid
+ if (
+ lead_idx in used_indices
+ or lead_idx >= len(sources)
+ or lead_idx is None
+ ):
+ continue
- # Use lead source title as topic title
- lead_source = sources[lead_idx]
- topic_title = lead_source.get("title", f"Topic {i + 1}")
+ # Use lead source title as topic title
+ lead_source = sources[lead_idx]
+ topic_title = lead_source.get(
+ "title", f"Topic {len(topics) + 1}"
+ )
- # Create topic
- topic = Topic(
- id=str(uuid.uuid4()),
- title=topic_title,
- lead_source=sources[lead_idx],
- keywords=[], # No keywords from LLM anymore
- )
+ # Create topic
+ topic = Topic(
+ id=str(uuid.uuid4()),
+ title=topic_title,
+ lead_source=sources[lead_idx],
+ )
- used_indices.add(lead_idx)
+ used_indices.add(lead_idx)
- # Add supporting sources
- for idx in supporting_indices:
- if idx not in used_indices and idx < len(sources):
- topic.add_supporting_source(sources[idx])
- used_indices.add(idx)
+ # Add supporting sources
+ for idx in supporting_indices:
+ if idx not in used_indices and idx < len(sources):
+ topic.add_supporting_source(sources[idx])
+ used_indices.add(idx)
- # Only add topic if it has enough sources
- if len(topic.get_all_sources()) >= self.min_sources_per_topic:
- topics.append(topic)
+ # Only add new topic if it has enough sources
+ if (
+ len(topic.get_all_sources())
+ >= self.min_sources_per_topic
+ ):
+ topics.append(topic)
+ logger.info(
+ f"Created new topic '{topic.title}' with {len(topic.get_all_sources())} sources"
+ )
return topics
@@ -191,78 +243,434 @@ Important:
logger.exception("Error extracting topics")
return []
- def _validate_topic_coherence(self, topic: Topic) -> float:
+ def _find_topic_relationships(self, topics: List[Topic]) -> None:
"""
- Validate that sources in a topic are coherent and related.
- Returns a confidence score between 0 and 1.
+ Identify relationships between topics and update the topic graph.
"""
- if len(topic.get_all_sources()) < 2:
- return 0.0
+ if len(topics) < 2:
+ return
+
+ # For now, don't auto-link topics since we don't have keywords
+ # This could be enhanced to compare source titles/snippets for similarity
+ pass
+
+ def _filter_topics_by_relevance(
+ self, topics: List[Topic], query: str
+ ) -> List[Topic]:
+ """
+ Filter topics by their relevance to the original research question.
+ For each topic, provides context from other topics and all sources within the topic.
+ """
+ if not topics:
+ return []
- # Format topic for validation
- lead_title = topic.lead_source.get("title", "")
- lead_snippet = topic.lead_source.get("snippet", "")[:500]
+ relevant_topics = []
- supporting_texts = []
- for source in topic.supporting_sources[:5]: # Limit for context
- title = source.get("title", "")
- snippet = source.get("snippet", "")[:300]
- supporting_texts.append(f"- {title}: {snippet}")
+ for topic in topics:
+ # 1. Get lead sources from OTHER topics for context
+ other_topic_leads = []
+ for other_topic in topics:
+ if other_topic.id != topic.id:
+ lead = other_topic.lead_source
+ other_topic_leads.append(
+ f"- {lead.get('title', 'Untitled')}: {lead.get('snippet', '')[:200]}"
+ )
- validation_prompt = f"""
-Evaluate the coherence of this topic cluster:
+ # 2. Get ALL sources from THIS topic
+ topic_sources = []
+ # Lead source
+ lead = topic.lead_source
+ topic_sources.append(
+ f"LEAD: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:300]}"
+ )
+ # Supporting sources
+ for source in topic.supporting_sources:
+ topic_sources.append(
+ f"- {source.get('title', 'Untitled')}: {source.get('snippet', '')[:200]}"
+ )
-Topic: {topic.title}
-Keywords: {", ".join(topic.keywords)}
+ # 3. Create relevance evaluation prompt
+ relevance_prompt = f"""
+Evaluate if this topic is relevant for answering the research question.
-Lead Source:
-Title: {lead_title}
-Snippet: {lead_snippet}
+RESEARCH QUESTION: {query}
-Supporting Sources:
-{chr(10).join(supporting_texts)}
+CURRENT TOPIC: {topic.title}
+Sources in this topic:
+{chr(10).join(topic_sources)}
-Rate the coherence from 0.0 to 1.0 based on:
-1. How well the supporting sources relate to the lead source
-2. Whether all sources discuss the same topic
-3. The relevance of the keywords
+OTHER TOPICS IDENTIFIED (for context):
+{chr(10).join(other_topic_leads) if other_topic_leads else "None"}
-Respond with just a number between 0.0 and 1.0.
+Does this topic contain information that helps answer the research question?
+Consider:
+- Does it address the main question or important sub-questions?
+- Does it provide necessary background or context?
+- Does it offer evidence, examples, or explanations relevant to the query?
+
+Respond with only "yes" or "no".
"""
- try:
- # Use LangChain's invoke method
- from langchain_core.messages import HumanMessage
+ try:
+ # Call model directly with prompt
+ response = self.model.invoke(relevance_prompt)
+ response_text = str(
+ response.content
+ if hasattr(response, "content")
+ else response
+ )
+
+ # Check if response indicates relevance
+ if "yes" in response_text.lower().strip():
+ relevant_topics.append(topic)
+ logger.info(
+ f"Topic '{topic.title}' deemed RELEVANT to query"
+ )
+ else:
+ logger.info(
+ f"Topic '{topic.title}' deemed NOT relevant to query"
+ )
+ # Remove from topic graph if not relevant
+ if topic.id in self.topic_graph.topics:
+ del self.topic_graph.topics[topic.id]
+
+ except Exception:
+ logger.exception(
+ f"Error evaluating relevance for topic '{topic.title}', keeping it"
+ )
+ relevant_topics.append(topic) # Keep on error
+
+ return relevant_topics
+
+ def _reselect_lead_sources(self, topics: List[Topic]) -> None:
+ """
+ Re-evaluate and potentially update the lead source for each topic after adding new sources.
+ For each topic, the LLM sees all sources in that topic and lead sources from other topics.
+ """
+ if not topics or len(topics) < 1:
+ return
+
+ for i, topic in enumerate(topics):
+ # Skip if topic has too few sources
+ if len(topic.get_all_sources()) < 2:
+ continue
+
+ # Get lead sources from OTHER topics for context
+ other_leads = []
+ for j, other_topic in enumerate(topics):
+ if i != j:
+ lead = other_topic.lead_source
+ other_leads.append(
+ f"Topic {j + 1}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:150]}"
+ )
+
+ # Get ALL sources from THIS topic
+ topic_sources = []
+ all_sources = topic.get_all_sources()
+ for idx, source in enumerate(all_sources):
+ source_info = f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')[:200]}"
+ if source == topic.lead_source:
+ source_info += " [CURRENT LEAD]"
+ topic_sources.append(source_info)
+
+ # Prompt to select best lead
+ selection_prompt = f"""
+Select the best lead source for this topic cluster.
+
+TOPIC: {topic.title}
+Current sources in this topic:
+{chr(10).join(topic_sources)}
+
+OTHER TOPICS (for context):
+{chr(10).join(other_leads) if other_leads else "None"}
+
+Which source number (0-{len(all_sources) - 1}) should be the lead source for this topic?
+The lead should be the most comprehensive and representative source.
+
+Respond with only the number.
+"""
+
+ try:
+ response = self.model.invoke(selection_prompt)
+ response_text = str(
+ response.content
+ if hasattr(response, "content")
+ else response
+ ).strip()
+
+ # Parse the number
+ new_lead_idx = int(response_text)
+
+ if 0 <= new_lead_idx < len(all_sources):
+ new_lead = all_sources[new_lead_idx]
+ if new_lead != topic.lead_source:
+ logger.info(
+ f"Updating lead source for topic '{topic.title}' from '{topic.lead_source.get('title')}' to '{new_lead.get('title')}'"
+ )
+ topic.update_lead_source(new_lead)
+ # Update topic title to match new lead
+ topic.title = new_lead.get("title", topic.title)
+
+ except (ValueError, IndexError):
+ logger.warning(
+ f"Could not parse lead selection response for topic '{topic.title}': {response_text}"
+ )
+ # Keep existing lead
+ except Exception:
+ logger.exception(
+ f"Error reselecting lead for topic '{topic.title}'"
+ )
+ # Keep existing lead
+
+ def _reorganize_topics(self, topics: List[Topic]) -> List[Topic]:
+ """
+ Reorganize sources across topics after lead source updates.
+ For each topic, re-evaluate if sources still belong or should move to other topics.
+ Can also create new topics if sources don't fit anywhere.
+ """
+ if not topics or len(topics) < 2:
+ return topics
+
+ # Track sources that need reassignment
+ all_reassignments = []
+
+ for i, topic in enumerate(topics):
+ if len(topic.supporting_sources) < 1:
+ continue
+
+ # Get lead sources from ALL topics (including this one)
+ all_leads = []
+ for j, t in enumerate(topics):
+ lead = t.lead_source
+ all_leads.append(
+ f"Topic {j}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:150]}"
+ )
+
+ # Get all supporting sources from THIS topic
+ sources_to_evaluate = []
+ for idx, source in enumerate(topic.supporting_sources):
+ sources_to_evaluate.append(
+ {
+ "index": idx,
+ "source": source,
+ "info": f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')[:200]}",
+ }
+ )
+
+ if not sources_to_evaluate:
+ continue
+
+ # Build prompt for reorganization
+ reorganize_prompt = f"""
+Evaluate which topic each source belongs to based on the lead sources.
- response = self.model.invoke(
- [HumanMessage(content=validation_prompt)]
+CURRENT TOPIC {i}: {topic.title}
+
+ALL TOPIC LEADS:
+{chr(10).join(all_leads)}
+
+SOURCES TO EVALUATE FROM TOPIC {i}:
+{chr(10).join([s["info"] for s in sources_to_evaluate])}
+
+For each source, determine:
+1. Which topic index (0-{len(topics) - 1}) it best fits with
+2. Or -1 if it should form a new topic
+3. Or -2 if it should be removed (not relevant)
+
+Format as JSON list:
+[
+ {{"source_index": 0, "target_topic": 1}},
+ {{"source_index": 1, "target_topic": -1}},
+]
+
+Consider similarity to lead sources when deciding.
+"""
+
+ try:
+ response = self.model.invoke(reorganize_prompt)
+ response_text = str(
+ response.content
+ if hasattr(response, "content")
+ else response
+ )
+
+ # Parse JSON
+ import json
+ import re
+
+ json_match = re.search(r"\[.*\]", response_text, re.DOTALL)
+ if json_match:
+ reassignments = json.loads(json_match.group())
+ else:
+ reassignments = json.loads(response_text)
+
+ # Process reassignments
+ for assignment in reassignments:
+ src_idx = assignment.get("source_index")
+ target = assignment.get("target_topic")
+
+ if src_idx is None or target is None:
+ continue
+
+ if 0 <= src_idx < len(sources_to_evaluate):
+ source_data = sources_to_evaluate[src_idx]
+
+ if target == i:
+ # Keep in current topic
+ continue
+ elif target == -2:
+ # Remove from topic
+ logger.info(
+ f"Removing source '{source_data['source'].get('title')}' from topic '{topic.title}'"
+ )
+ topic.reject_source(source_data["source"])
+ elif target == -1:
+ # Mark for new topic
+ all_reassignments.append(
+ {
+ "source": source_data["source"],
+ "from_topic": topic,
+ "to_new_topic": True,
+ }
+ )
+ elif 0 <= target < len(topics):
+ # Move to another topic
+ all_reassignments.append(
+ {
+ "source": source_data["source"],
+ "from_topic": topic,
+ "to_topic": topics[target],
+ }
+ )
+
+ except Exception:
+ logger.exception(
+ f"Error reorganizing sources for topic '{topic.title}'"
+ )
+ # Keep sources as is
+
+ # Apply reassignments
+ new_topic_sources = []
+ for reassignment in all_reassignments:
+ source = reassignment["source"]
+ from_topic = reassignment["from_topic"]
+
+ # Remove from original topic
+ if source in from_topic.supporting_sources:
+ from_topic.supporting_sources.remove(source)
+
+ if reassignment.get("to_new_topic"):
+ new_topic_sources.append(source)
+ elif "to_topic" in reassignment:
+ to_topic = reassignment["to_topic"]
+ to_topic.add_supporting_source(source)
+ logger.info(
+ f"Moved source '{source.get('title')}' from '{from_topic.title}' to '{to_topic.title}'"
+ )
+
+ # Create new topic if we have enough sources
+ if len(new_topic_sources) >= self.min_sources_per_topic:
+ # Select lead for new topic
+ new_topic = Topic(
+ id=str(uuid.uuid4()),
+ title=new_topic_sources[0].get("title", "New Topic"),
+ lead_source=new_topic_sources[0],
)
+ for source in new_topic_sources[1:]:
+ new_topic.add_supporting_source(source)
- # Extract content from response
- response_text = (
- response.content
- if hasattr(response, "content")
- else str(response)
+ topics.append(new_topic)
+ logger.info(
+ f"Created new topic '{new_topic.title}' with {len(new_topic_sources)} sources"
)
- score = float(response_text.strip())
- return min(max(score, 0.0), 1.0) # Clamp to [0, 1]
- except Exception as e:
- logger.warning(f"Error validating topic coherence: {e}")
- return 0.5 # Default middle score on error
- def _find_topic_relationships(self, topics: List[Topic]) -> None:
+ # Remove topics that now have too few sources
+ valid_topics = []
+ for topic in topics:
+ if len(topic.get_all_sources()) >= self.min_sources_per_topic:
+ valid_topics.append(topic)
+ else:
+ logger.info(
+ f"Removing topic '{topic.title}' - too few sources after reorganization"
+ )
+
+ return valid_topics
+
+ def _generate_refinement_question(
+ self, topics: List[Topic], original_query: str
+ ) -> Optional[str]:
"""
- Identify relationships between topics and update the topic graph.
+ Generate a follow-up question to improve confidence in answering the original query.
"""
- if len(topics) < 2:
- return
+ if not self.enable_refinement or not topics:
+ return None
+
+ # Format current topics and their source counts
+ topic_summary = []
+ for topic in topics:
+ source_count = len(topic.get_all_sources())
+ lead = topic.lead_source
+ topic_summary.append(
+ f"- {topic.title} ({source_count} sources)\n Lead: {lead.get('snippet', '')[:200]}"
+ )
+
+ # Format previous questions to avoid repetition
+ prev_questions_str = (
+ "\n".join(f"- {q}" for q in self.refinement_questions)
+ if self.refinement_questions
+ else "None"
+ )
+
+ refinement_prompt = f"""
+Based on the current research state, generate ONE follow-up question to improve confidence in answering the original query.
+
+ORIGINAL QUERY: {original_query}
+
+CURRENT TOPICS FOUND:
+{chr(10).join(topic_summary)}
+
+PREVIOUS REFINEMENT QUESTIONS ASKED:
+{prev_questions_str}
+
+CRITICAL: The follow-up question must stay focused on answering the ORIGINAL QUERY above.
- for i, topic1 in enumerate(topics):
- for topic2 in topics[i + 1 :]:
- # Check keyword overlap
- common_keywords = set(topic1.keywords) & set(topic2.keywords)
- if len(common_keywords) >= 2:
- self.topic_graph.link_topics(topic1.id, topic2.id)
+Analyze what specific aspects of the original query are NOT well covered by current topics.
+The refinement should fill gaps that directly help answer the original question better.
+
+Generate a short, specific follow-up question that:
+1. MUST directly relate to the ORIGINAL QUERY (not tangential topics)
+2. Fills a specific gap in answering the original query
+3. Is different from previous questions
+4. Stays within the scope of the original query
+5. Is concise and searchable (max 15 words)
+
+If the current topics comprehensively answer ALL aspects of the original query, respond with "NONE".
+
+Otherwise, respond with only the follow-up question, nothing else.
+"""
+
+ try:
+ response = self.model.invoke(refinement_prompt)
+ response_text = str(
+ response.content if hasattr(response, "content") else response
+ ).strip()
+
+ if "none" in response_text.lower() or not response_text:
+ logger.info("Model determined no refinement needed")
+ return None
+
+ # Clean up the question
+ question = response_text.strip('"').strip("'").strip()
+ if len(question.split()) > 20: # Sanity check
+ question = " ".join(question.split()[:15]) + "?"
+
+ logger.info(f"Generated refinement question: {question}")
+ self.refinement_questions.append(question)
+ return question
+
+ except Exception:
+ logger.exception("Error generating refinement question")
+ return None
def analyze_topic(self, query: str) -> Dict:
"""
@@ -333,22 +741,17 @@ Respond with just a number between 0.0 and 1.0.
{"phase": "validation", "topic_count": len(topics)},
)
- # Validate and score each topic
+ # Validate topics based on source count
validated_topics = []
for topic in topics:
- # Primary validation: number of sources
source_count = len(topic.get_all_sources())
- # Only use LLM validation as a secondary score, not a filter
- confidence = self._validate_topic_coherence(topic)
- topic.confidence_score = confidence
-
- # Accept topics with enough sources regardless of LLM confidence
+ # Accept topics with enough sources
if source_count >= self.min_sources_per_topic:
validated_topics.append(topic)
self.topic_graph.add_topic(topic)
logger.info(
- f"Topic '{topic.title}' accepted with {source_count} sources (confidence {confidence:.2f})"
+ f"Topic '{topic.title}' accepted with {source_count} sources"
)
else:
logger.info(
@@ -362,20 +765,93 @@ Respond with just a number between 0.0 and 1.0.
# Find relationships between topics
self._find_topic_relationships(validated_topics)
- # Format findings
- formatted_findings = self._format_topic_findings(
+ self._update_progress(
+ "Evaluating topic relevance to query",
+ 85,
+ {"phase": "relevance_filtering"},
+ )
+
+ # Filter topics by relevance to the original query
+ relevant_topics = self._filter_topics_by_relevance(
validated_topics, query
)
+ logger.info(
+ f"Filtered from {len(validated_topics)} to {len(relevant_topics)} relevant topics"
+ )
+
+ # Check if we should do refinement
+ if (
+ self.enable_refinement
+ and len(self.refinement_questions) < self.max_refinement_iterations
+ ):
+ refinement_question = self._generate_refinement_question(
+ relevant_topics, query
+ )
+
+ if refinement_question:
+ logger.info(
+ f"Starting refinement iteration {len(self.refinement_questions)} with question: {refinement_question}"
+ )
+
+ self._update_progress(
+ "Refining topics with follow-up search",
+ 90,
+ {"phase": "refinement", "question": refinement_question},
+ )
+
+ # Search for more sources using the refinement question
+ combined_query = f"{query} {refinement_question}"
+ refinement_results = self.source_strategy.analyze_topic(
+ combined_query
+ )
+ new_sources = refinement_results.get("all_links_of_system", [])
+
+ if new_sources:
+ logger.info(
+ f"Found {len(new_sources)} new sources from refinement"
+ )
+
+ # Add new sources to existing topics or create new ones
+ updated_topics = self._extract_topics_from_sources(
+ new_sources, query, existing_topics=relevant_topics
+ )
+
+ # Re-select lead sources after adding new sources
+ logger.info("Re-evaluating lead sources for all topics")
+ self._reselect_lead_sources(updated_topics)
+
+ # Reorganize sources across topics based on new lead sources
+ logger.info("Reorganizing sources across topics")
+ updated_topics = self._reorganize_topics(updated_topics)
+
+ # Re-validate and filter
+ validated_refined = []
+ for topic in updated_topics:
+ if (
+ len(topic.get_all_sources())
+ >= self.min_sources_per_topic
+ ):
+ validated_refined.append(topic)
+
+ relevant_topics = self._filter_topics_by_relevance(
+ validated_refined, query
+ )
+ logger.info(
+ f"After refinement: {len(relevant_topics)} relevant topics"
+ )
+
+ # Format findings
+ formatted_findings = self._format_topic_findings(relevant_topics, query)
self._update_progress(
"Topic organization complete",
100,
- {"phase": "complete", "final_topic_count": len(validated_topics)},
+ {"phase": "complete", "final_topic_count": len(relevant_topics)},
)
- # Collect all sources from validated topics
+ # Collect all sources from relevant topics only
topic_sources = []
- for topic in validated_topics:
+ for topic in relevant_topics:
topic_sources.extend(topic.get_all_sources())
# Add all topic sources to all_links_of_system (deduplicated)
@@ -385,7 +861,7 @@ Respond with just a number between 0.0 and 1.0.
# Format the topic graph as a structured knowledge representation
topic_graph_str = self._format_topic_graph_as_knowledge(
- validated_topics, query
+ relevant_topics, query
)
# Create findings with sources included in content
@@ -397,12 +873,12 @@ Respond with just a number between 0.0 and 1.0.
"content": topic_graph_str,
"question": query,
"documents": topic_sources, # All sources from validated topics
- "topics": [topic.to_dict() for topic in validated_topics],
+ "topics": [topic.to_dict() for topic in relevant_topics],
}
findings.append(main_finding)
# Add individual findings for each topic with their sources
- for i, topic in enumerate(validated_topics, 1):
+ for i, topic in enumerate(relevant_topics, 1):
topic_finding = {
"phase": f"Topic {i}: {topic.title}",
"content": self._format_single_topic_with_sources(topic),
@@ -422,7 +898,7 @@ Respond with just a number between 0.0 and 1.0.
"current_knowledge": topic_graph_str, # The topic graph structure as knowledge
"all_links_of_system": self.all_links_of_system,
# Topic-specific fields
- "topics": [topic.to_dict() for topic in validated_topics],
+ "topics": [topic.to_dict() for topic in relevant_topics],
"topic_graph": self.topic_graph.to_dict(),
"source_count": len(topic_sources),
}
@@ -432,8 +908,6 @@ Respond with just a number between 0.0 and 1.0.
Format a single topic with all its sources listed.
"""
lines = [
- f"**Confidence:** {topic.confidence_score:.2%}",
- f"**Keywords:** {', '.join(topic.keywords[:7])}",
f"**Total Sources:** {len(topic.get_all_sources())}",
"",
"**Lead Source:**",
@@ -479,8 +953,6 @@ Respond with just a number between 0.0 and 1.0.
# Build the graph structure
for i, topic in enumerate(topics, 1):
lines.append(f"### Topic {i}: {topic.title}")
- lines.append(f"**Confidence:** {topic.confidence_score:.2%}")
- lines.append(f"**Keywords:** {', '.join(topic.keywords[:5])}")
lines.append(f"**Sources:** {len(topic.get_all_sources())}")
# Lead source details
@@ -535,8 +1007,6 @@ Respond with just a number between 0.0 and 1.0.
for i, topic in enumerate(topics, 1):
lines.append(f"## Topic {i}: {topic.title}")
- lines.append(f"Confidence: {topic.confidence_score:.2%}")
- lines.append(f"Keywords: {', '.join(topic.keywords)}")
lines.append(
f"Sources: {len(topic.get_all_sources())} (1 lead + {len(topic.supporting_sources)} supporting)"
)
diff --git a/src/local_deep_research/search_system_factory.py b/src/local_deep_research/search_system_factory.py
index a4311ddf90..351425c5a8 100644
--- a/src/local_deep_research/search_system_factory.py
+++ b/src/local_deep_research/search_system_factory.py
@@ -640,6 +640,10 @@ def create_strategy(
min_sources_per_topic=kwargs.get("min_sources_per_topic", 2),
max_topics=kwargs.get("max_topics", 5),
similarity_threshold=kwargs.get("similarity_threshold", 0.3),
+ enable_refinement=kwargs.get("enable_refinement", True),
+ max_refinement_iterations=kwargs.get(
+ "max_refinement_iterations", 3
+ ),
)
# Iterative refinement strategy