~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
fix: improve TopicOrganizationStrategy and context overflow metrics
LearningCircuitff570d01d478
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.pyindex 589dde0bd1..cf74f5c482 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@@ -6,6 +6,7 @@ from loguru import loggerfrom .base_strategy import BaseSearchStrategyfrom .source_based_strategy import SourceBasedSearchStrategyfrom ..findings.topic import Topic, TopicGraph+from ...utilities.thread_context import preserve_research_contextclass TopicOrganizationStrategy(BaseSearchStrategy):@@ -34,6 +35,7 @@ class TopicOrganizationStrategy(BaseSearchStrategy):similarity_threshold: float = 0.7,enable_refinement: bool = False,max_refinement_iterations: int = 2,+ generate_text: bool = True,):"""Initialize the topic organization strategy."""super().__init__(@@ -44,7 +46,16 @@ class TopicOrganizationStrategy(BaseSearchStrategy):self.model = modelself.search = search- self.citation_handler = citation_handler++ # Create citation handler if not provided (needed for text generation)+ if citation_handler is None and generate_text:+ from local_deep_research.citation_handler import CitationHandler++ self.citation_handler = CitationHandler(+ model, settings_snapshot=settings_snapshot+ )+ else:+ self.citation_handler = citation_handler# Topic organization parametersself.min_sources_per_topic = min_sources_per_topic@@ -53,6 +64,7 @@ class TopicOrganizationStrategy(BaseSearchStrategy):self.enable_refinement = enable_refinementself.max_refinement_iterations = max_refinement_iterationsself.refinement_questions = [] # Track all questions asked+ self.generate_text = generate_text# Create a no-op citation handler to skip text generationclass NoTextCitationHandler:@@ -62,6 +74,7 @@ class TopicOrganizationStrategy(BaseSearchStrategy):return {"content": "", "documents": []}# Initialize source-based strategy without text generation+ # Pass our own all_links_of_system so it accumulates properlyself.source_strategy = SourceBasedSearchStrategy(search=search,model=model,@@ -71,7 +84,7 @@ class TopicOrganizationStrategy(BaseSearchStrategy):filter_reorder=filter_reorder,filter_reindex=filter_reindex,cross_engine_max_results=cross_engine_max_results,- all_links_of_system=all_links_of_system,+ all_links_of_system=self.all_links_of_system, # Share our list with source strategysettings_snapshot=settings_snapshot,search_original_query=search_original_query,)@@ -685,11 +698,21 @@ Otherwise, respond with only the follow-up question, nothing else.)# Use source-based strategy to gather sources- source_results = self.source_strategy.analyze_topic(query)+ # Wrap in preserve_research_context to maintain context+ @preserve_research_context+ def _call_source_strategy(q):+ return self.source_strategy.analyze_topic(q)++ source_results = _call_source_strategy(query)- # Extract sources from results - the strategy returns all sources in all_links_of_system+ # The source_based_strategy already added sources to its all_links_of_system with proper indices+ # We need to use those sources which already have sequential indicesall_sources = source_results.get("all_links_of_system", [])+ # Update our all_links_of_system to match what source_based_strategy collected+ # This ensures continuity of numbering+ self.all_links_of_system = source_results.get("all_links_of_system", [])+if not all_sources:logger.warning("No sources found to organize into topics")return {@@ -779,8 +802,8 @@ Otherwise, respond with only the follow-up question, nothing else.f"Filtered from {len(validated_topics)} to {len(relevant_topics)} relevant topics")- # Check if we should do refinement- if (+ # Refinement loop - can do multiple iterations+ while (self.enable_refinementand len(self.refinement_questions) < self.max_refinement_iterations):@@ -788,60 +811,115 @@ Otherwise, respond with only the follow-up question, nothing else.relevant_topics, query)- if refinement_question:+ if not refinement_question:+ # No more refinements neededlogger.info(- f"Starting refinement iteration {len(self.refinement_questions)} with question: {refinement_question}"+ "No further refinement needed - stopping refinement loop")+ break++ logger.info(+ f"Starting refinement iteration {len(self.refinement_questions)} with question: {refinement_question}"+ )++ self._update_progress(+ f"Refining topics with follow-up search (iteration {len(self.refinement_questions) + 1}/{self.max_refinement_iterations})",+ 85+ + (+ 10+ * len(self.refinement_questions)+ / self.max_refinement_iterations+ ),+ {"phase": "refinement", "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}"++ # Wrap in preserve_research_context to maintain context+ @preserve_research_context+ def _call_refinement_search(q):+ return self.source_strategy.analyze_topic(q)++ refinement_results = _call_refinement_search(combined_query)++ # Get ALL sources from the refinement search (includes previous + new)+ all_sources_after_refinement = refinement_results.get(+ "all_links_of_system", []+ )++ # Extract only the NEW sources added in this refinement+ # The new sources are those beyond what we already had+ existing_count = len(self.all_links_of_system)+ new_sources = (+ all_sources_after_refinement[existing_count:]+ if len(all_sources_after_refinement) > existing_count+ else []+ )++ # Update our all_links_of_system to include the new sources+ self.all_links_of_system = all_sources_after_refinement++ if new_sources:+ logger.info(+ f"Found {len(new_sources)} new sources from refinement iteration {len(self.refinement_questions)}")- # Search for more sources using the refinement question- combined_query = f"{query} {refinement_question}"- refinement_results = self.source_strategy.analyze_topic(- combined_query+ # Add new sources to existing topics or create new ones+ updated_topics = self._extract_topics_from_sources(+ new_sources, query, existing_topics=relevant_topics)- new_sources = refinement_results.get("all_links_of_system", [])- if new_sources:- logger.info(- f"Found {len(new_sources)} new sources from refinement"- )+ # Re-select lead sources after adding new sources+ logger.info("Re-evaluating lead sources for all topics")+ self._reselect_lead_sources(updated_topics)- # Add new sources to existing topics or create new ones- updated_topics = self._extract_topics_from_sources(- new_sources, query, existing_topics=relevant_topics- )+ # Reorganize sources across topics based on new lead sources+ logger.info("Reorganizing sources across topics")+ updated_topics = self._reorganize_topics(updated_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"- )+ # 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 iteration {len(self.refinement_questions)}: {len(relevant_topics)} relevant topics"+ )+ else:+ logger.info(+ f"No new sources found in refinement iteration {len(self.refinement_questions)}"+ )- # Format findings- formatted_findings = self._format_topic_findings(relevant_topics, query)+ # Generate text if enabled+ if self.generate_text and self.citation_handler:+ self._update_progress(+ "Generating text synthesis from topics",+ 95,+ {"phase": "text_generation"},+ )+ generated_text = self._generate_topic_based_text(+ relevant_topics, query+ )+ else:+ generated_text = ""++ # Format findings - include both generated text and topic structure+ if generated_text:+ # Combine generated text with topic organization details+ formatted_findings = f"{generated_text}\n\n---\n\n{self._format_topic_findings(relevant_topics, query)}"+ else:+ # Just the topic organization+ formatted_findings = self._format_topic_findings(+ relevant_topics, query+ )self._update_progress("Topic organization complete",@@ -850,15 +928,12 @@ Otherwise, respond with only the follow-up question, nothing else.)# Collect all sources from relevant topics only+ # Note: We don't need to add to all_links_of_system since we're already+ # maintaining it from source_based_strategy resultstopic_sources = []for topic in relevant_topics:topic_sources.extend(topic.get_all_sources())- # Add all topic sources to all_links_of_system (deduplicated)- for source in topic_sources:- if source not in self.all_links_of_system:- self.all_links_of_system.append(source)-# Format the topic graph as a structured knowledge representationtopic_graph_str = self._format_topic_graph_as_knowledge(relevant_topics, query@@ -867,15 +942,25 @@ Otherwise, respond with only the follow-up question, nothing else.# Create findings with sources included in contentfindings = []- # Add a main finding with the topic graph- main_finding = {- "phase": "Topic Organization Overview",+ # If text generation is enabled, add the generated answer first+ if generated_text:+ answer_finding = {+ "phase": "Research Answer",+ "content": generated_text,+ "question": query,+ "documents": topic_sources, # All sources from validated topics+ }+ findings.append(answer_finding)++ # Always add the topic organization overview+ topic_finding = {+ "phase": "Topic Organization","content": topic_graph_str,"question": query,- "documents": topic_sources, # All sources from validated topics+ "documents": topic_sources,"topics": [topic.to_dict() for topic in relevant_topics],}- findings.append(main_finding)+ findings.append(topic_finding)# Add individual findings for each topic with their sourcesfor i, topic in enumerate(relevant_topics, 1):@@ -1053,3 +1138,113 @@ Otherwise, respond with only the follow-up question, nothing else.lines.append(f"\n---\n**Total Sources Organized:** {total_sources}")return "\n".join(lines)++ def _generate_topic_based_text(+ self, topics: List[Topic], query: str+ ) -> str:+ """+ Generate text synthesis from topics using citation handler with custom prompts.+ Two-part generation: lead sources synthesis + individual topic paragraphs.+ """+ if not topics or not self.citation_handler:+ return ""++ generated_texts = []++ # Part 1: Generate comprehensive paragraph from lead sources+ lead_sources = []+ for topic in topics:+ # Use the existing index from the source (already set by source_based_strategy)+ lead_sources.append(topic.lead_source)++ # Create documents for citation handler using existing indices+ # Pass nr_of_links=0 since sources already have correct indices+ lead_documents = self.citation_handler._create_documents(+ lead_sources, nr_of_links=0+ )+ formatted_lead_sources = self.citation_handler._format_sources(+ lead_documents+ )++ lead_prompt = f"""Answer this research question using ONLY the lead sources provided:+QUESTION: {query}++LEAD SOURCES:+{formatted_lead_sources}++Write ONE comprehensive paragraph that synthesizes the main findings from these lead sources.+Focus on directly answering the research question.+Use citations [1], [2], etc. for specific claims.+Make the paragraph substantive and informative."""++ try:+ lead_response = self.model.invoke(lead_prompt)+ lead_text = (+ lead_response.content+ if hasattr(lead_response, "content")+ else str(lead_response)+ )+ generated_texts.append(lead_text)+ except Exception:+ logger.exception("Error generating lead synthesis")+ lead_text = ""++ # Part 2: Generate individual topic paragraphs with context awareness+ for i, topic in enumerate(topics):+ # Get lead sources from OTHER topics for context+ other_leads_info = []+ for j, other_topic in enumerate(topics):+ if i != j:+ other_lead = other_topic.lead_source+ other_leads_info.append(+ f"- Topic {j + 1} ({other_topic.title}): {other_lead.get('snippet', '')[:150]}"+ )++ # Get ALL sources from this topic (they already have indices from source_based_strategy)+ topic_sources = []+ topic_sources.append(topic.lead_source)+ topic_sources.extend(topic.supporting_sources)++ # Create documents and format using existing indices+ # Pass nr_of_links=0 since sources already have correct indices+ topic_documents = self.citation_handler._create_documents(+ topic_sources, nr_of_links=0+ )+ formatted_topic_sources = self.citation_handler._format_sources(+ topic_documents+ )++ topic_prompt = f"""RESEARCH QUESTION: {query}++CURRENT TOPIC: {topic.title}+SOURCES FOR THIS TOPIC:+{formatted_topic_sources}++OTHER TOPICS BEING COVERED (for context - avoid repeating):+{chr(10).join(other_leads_info) if other_leads_info else "None"}++Write a SHORT paragraph (2-3 sentences) that:+1. Focuses on what's UNIQUE about this topic's contribution to answering the research question+2. Avoids repeating information covered by other topics+3. Synthesizes information from all sources in this topic+4. Uses citations for specific claims++Stay anchored to the research question and highlight this topic's specific insights."""++ try:+ topic_response = self.model.invoke(topic_prompt)+ topic_text = (+ topic_response.content+ if hasattr(topic_response, "content")+ else str(topic_response)+ )++ # Add topic header+ generated_texts.append(f"\n**{topic.title}:**")+ generated_texts.append(topic_text)+ except Exception:+ logger.exception(+ f"Error generating text for topic '{topic.title}'"+ )++ return "\n".join(generated_texts)diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.pyindex 3434294840..7f54360204 100644--- a/src/local_deep_research/metrics/token_counter.py+++ b/src/local_deep_research/metrics/token_counter.py@@ -541,8 +541,8 @@ class TokenCountingCallback(BaseCallbackHandler):f"context_truncated: {self.context_truncated}, "f"tokens_truncated: {self.tokens_truncated}, "f"ollama_prompt_eval_count: {self.ollama_metrics.get('prompt_eval_count')}, "- f"prompt_tokens: {self.estimated_prompt_tokens}, "- f"completion_tokens: {self.estimated_completion_tokens}"+ f"prompt_tokens: {prompt_tokens}, "+ f"completion_tokens: {completion_tokens}")# Add token usage record with enhanced fieldsdiff --git a/src/local_deep_research/search_system_factory.py b/src/local_deep_research/search_system_factory.pyindex 351425c5a8..b10a559163 100644--- a/src/local_deep_research/search_system_factory.py+++ b/src/local_deep_research/search_system_factory.py@@ -642,8 +642,9 @@ def create_strategy(similarity_threshold=kwargs.get("similarity_threshold", 0.3),enable_refinement=kwargs.get("enable_refinement", True),max_refinement_iterations=kwargs.get(- "max_refinement_iterations", 3+ "max_refinement_iterations", 5),+ generate_text=kwargs.get("generate_text", True),)# Iterative refinement strategydiff --git a/src/local_deep_research/web/routes/context_overflow_api.py b/src/local_deep_research/web/routes/context_overflow_api.pyindex 68337c43c1..5bdf5ccd5e 100644--- a/src/local_deep_research/web/routes/context_overflow_api.py+++ b/src/local_deep_research/web/routes/context_overflow_api.py@@ -120,16 +120,28 @@ def get_context_overflow_metrics():# Format time series for chartchart_data = []for usage in time_series_data:+ # Calculate original tokens (before truncation)+ ollama_used = (+ usage.ollama_prompt_eval_count+ ) # What Ollama actually processed+ actual_prompt = ollama_used or usage.prompt_tokens+ tokens_truncated = usage.tokens_truncated or 0+ original_tokens = (+ actual_prompt + tokens_truncated+ if usage.context_truncated+ else actual_prompt+ )+chart_data.append({"timestamp": usage.timestamp.isoformat(),"research_id": usage.research_id,- "prompt_tokens": usage.prompt_tokens,- "actual_prompt_tokens": usage.ollama_prompt_eval_count- or usage.prompt_tokens,+ "prompt_tokens": usage.prompt_tokens, # From our standard token counting+ "ollama_prompt_tokens": ollama_used, # What Ollama actually used (may be capped)+ "original_prompt_tokens": original_tokens, # What was originally requested (before truncation)"context_limit": usage.context_limit,"truncated": bool(usage.context_truncated),- "tokens_truncated": usage.tokens_truncated or 0,+ "tokens_truncated": tokens_truncated,"model": usage.model_name,})@@ -193,8 +205,12 @@ def get_context_overflow_metrics():"timestamp": req.timestamp.isoformat(),"research_id": req.research_id,"model": req.model_name,- "prompt_tokens": req.prompt_tokens,- "actual_tokens": req.ollama_prompt_eval_count,+ "prompt_tokens": req.prompt_tokens, # Standard token count+ "ollama_tokens": req.ollama_prompt_eval_count, # What Ollama actually used+ "original_tokens": (+ req.ollama_prompt_eval_count or req.prompt_tokens+ )+ + (req.tokens_truncated or 0), # What was requested"context_limit": req.context_limit,"tokens_truncated": req.tokens_truncated,"truncation_ratio": req.truncation_ratio,diff --git a/src/local_deep_research/web/routes/metrics_routes.py b/src/local_deep_research/web/routes/metrics_routes.pyindex 880f2fd23b..2096d66afb 100644--- a/src/local_deep_research/web/routes/metrics_routes.py+++ b/src/local_deep_research/web/routes/metrics_routes.py@@ -861,7 +861,7 @@ def context_overflow_page():@login_requireddef api_metrics():"""Get overall metrics data."""- logger.info("DEBUG: api_metrics endpoint called")+ logger.debug("api_metrics endpoint called")try:# Get username from sessionusername = flask_session.get("username")@@ -914,13 +914,13 @@ def api_metrics():# Get strategy analyticsstrategy_data = get_strategy_analytics(period, username)- logger.info(f"DEBUG: strategy_data keys: {list(strategy_data.keys())}")+ logger.debug(f"strategy_data keys: {list(strategy_data.keys())}")# Get rate limiting analyticsrate_limiting_data = get_rate_limiting_analytics(period, username)- logger.info(f"DEBUG: rate_limiting_data: {rate_limiting_data}")- logger.info(- f"DEBUG: rate_limiting_data keys: {list(rate_limiting_data.keys())}"+ logger.debug(f"rate_limiting_data: {rate_limiting_data}")+ logger.debug(+ f"rate_limiting_data keys: {list(rate_limiting_data.keys())}")# Combine metrics@@ -932,11 +932,9 @@ def api_metrics():"user_satisfaction": user_satisfaction,}- logger.info(- f"DEBUG: combined_metrics keys: {list(combined_metrics.keys())}"- )- logger.info(- f"DEBUG: combined_metrics['rate_limiting']: {combined_metrics.get('rate_limiting', 'NOT FOUND')}"+ logger.debug(f"combined_metrics keys: {list(combined_metrics.keys())}")+ logger.debug(+ f"combined_metrics['rate_limiting']: {combined_metrics.get('rate_limiting', 'NOT FOUND')}")return jsonify(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.pyindex 821a135f20..73302c0b66 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@@ -468,17 +468,17 @@ Query: "{query}"Current date: {current_date}Criteria for selection (in order of importance):-1. Relevance - directly addresses the query topic-2. Quality - from reputable sources or contains substantive information+1. Direct relevance - MUST directly address the specific query topic, not just mention keywords+2. Quality - from reputable sources with substantive information about the query3. Recency - prefer recent information when relevantSearch results to evaluate:{json.dumps(previews, indent=2)}-Return a JSON array of indices (0-based) for results that are relevant to the query.-Include results that are reasonably relevant, not just perfect matches.-Aim to include diverse perspectives if available.-Example response: [4, 0, 2, 7, 11]+Return a JSON array of indices (0-based) for results that are highly relevant to the query.+Only include results that directly help answer the specific question asked.+Be selective - it's better to return fewer high-quality results than many mediocre ones.+Example response: [4, 0, 2, 7]Respond with ONLY the JSON array, no other text."""