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.

fixed many issues

LearningCircuit04562193f604
diff --git a/src/local_deep_research/advanced_search_system/findings/base_findings.py b/src/local_deep_research/advanced_search_system/findings/base_findings.py
index 13e19ed1b4..e1150fd8b4 100644
--- a/src/local_deep_research/advanced_search_system/findings/base_findings.py
+++ b/src/local_deep_research/advanced_search_system/findings/base_findings.py
@@ -7,13 +7,15 @@ import logging
from abc import ABC, abstractmethod
from typing import Dict, List
+from langchain_core.language_models import BaseLLM
+
logger = logging.getLogger(__name__)
class BaseFindingsRepository(ABC):
"""Abstract base class for all findings repositories."""
- def __init__(self, model):
+ def __init__(self, model: BaseLLM):
"""
Initialize the findings repository.
@@ -24,7 +26,7 @@ class BaseFindingsRepository(ABC):
self.findings: Dict[str, List[str]] = {}
@abstractmethod
- def add_finding(self, query: str, finding: str) -> None:
+ def add_finding(self, query: str, finding: Dict | str) -> None:
"""
Add a finding to the repository.
@@ -72,7 +74,7 @@ class BaseFindingsRepository(ABC):
query: The original query
sub_queries: List of sub-queries
findings: List of findings for each sub-query
-
+ accumulated_knowledge: Accumulated knowledge from previous findings
Returns:
str: Synthesized final answer
"""
diff --git a/src/local_deep_research/advanced_search_system/findings/repository.py b/src/local_deep_research/advanced_search_system/findings/repository.py
index 647c4e0038..dae4ca91b1 100644
--- a/src/local_deep_research/advanced_search_system/findings/repository.py
+++ b/src/local_deep_research/advanced_search_system/findings/repository.py
@@ -6,11 +6,9 @@ import logging
from typing import Dict, List, Union
from langchain_core.documents import Document
+from langchain_core.language_models import BaseLLM
-from ...utilties.search_utilities import (
- format_findings,
- remove_think_tags,
-)
+from ...utilties.search_utilities import format_findings
from .base_findings import BaseFindingsRepository
logger = logging.getLogger(__name__)
@@ -34,7 +32,7 @@ def format_links(links: List[Dict]) -> str:
class FindingsRepository(BaseFindingsRepository):
"""Repository for managing research findings."""
- def __init__(self, model):
+ def __init__(self, model: BaseLLM):
"""Initialize the repository.
Args:
@@ -45,10 +43,9 @@ class FindingsRepository(BaseFindingsRepository):
self.documents: List[Document] = []
self.questions_by_iteration: Dict[int, List[str]] = {}
- def add_finding(self, query: str, finding: Union[Dict, str]) -> None:
+ def add_finding(self, query: str, finding: Dict | str) -> None:
"""Add a finding for a query."""
- if query not in self.findings:
- self.findings[query] = []
+ self.findings.setdefault(query, [])
# Convert to dictionary if it's a string
if isinstance(finding, str):
@@ -119,7 +116,7 @@ class FindingsRepository(BaseFindingsRepository):
Args:
questions_by_iteration: Dictionary mapping iteration numbers to lists of questions
"""
- self.questions_by_iteration = questions_by_iteration
+ self.questions_by_iteration = questions_by_iteration.copy()
logger.info(f"Set questions for {len(questions_by_iteration)} iterations")
def format_findings_to_text(
@@ -200,7 +197,7 @@ class FindingsRepository(BaseFindingsRepository):
finding_texts.append(item["content"])
elif isinstance(item, str):
finding_texts.append(item)
- accumulated_knowledge = "\n\n".join(finding_texts) if finding_texts else ""
+ accumulated_knowledge = "\n\n".join(finding_texts)
if findings:
logger.info(f"first finding type: {type(findings[0])}")
@@ -274,7 +271,7 @@ Use numbered citations [1], [2], etc. for important sources - these will be link
try:
response = self.model.invoke(prompt)
- synthesized_content = remove_think_tags(response.content)
+ synthesized_content = response.content
logger.info(
f"Successfully synthesized final answer for query: '{query}'"
)
diff --git a/src/local_deep_research/advanced_search_system/knowledge/base_knowledge.py b/src/local_deep_research/advanced_search_system/knowledge/base_knowledge.py
index f1e88a0f1e..0cfe36fa2f 100644
--- a/src/local_deep_research/advanced_search_system/knowledge/base_knowledge.py
+++ b/src/local_deep_research/advanced_search_system/knowledge/base_knowledge.py
@@ -25,7 +25,15 @@ class BaseKnowledgeGenerator(ABC):
@abstractmethod
def generate(self, query: str, context: str) -> str:
- """Generate knowledge from the given query and context."""
+ """
+ Generate knowledge from the given query and context.
+
+ Args:
+ query: The query to generate knowledge for
+ context: Additional context for knowledge generation
+
+ Returns:
+ """
pass
@abstractmethod
diff --git a/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py b/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
index ce3eaf7ee8..b961dfa50a 100644
--- a/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
+++ b/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
@@ -6,7 +6,6 @@ import logging
from datetime import datetime
from typing import List
-from ...utilties.search_utilities import remove_think_tags
from .base_knowledge import BaseKnowledgeGenerator
logger = logging.getLogger(__name__)
@@ -62,7 +61,7 @@ Generate detailed knowledge that:
Format your response as a well-structured paragraph."""
response = self.model.invoke(prompt)
- knowledge = remove_think_tags(response.content)
+ knowledge = response.content
logger.info("Generated knowledge successfully")
return knowledge
@@ -94,7 +93,7 @@ Format your response as a well-structured paragraph."""
try:
response = self.model.invoke(prompt)
- return remove_think_tags(response.content)
+ return response.content
except Exception as e:
logger.error(f"Error generating sub-knowledge: {str(e)}")
return ""
@@ -132,7 +131,7 @@ Compressed Knowledge:"""
try:
response = self.model.invoke(prompt)
- compressed_knowledge = remove_think_tags(response.content)
+ compressed_knowledge = response.content
logger.info(f"Compressed knowledge length: {len(compressed_knowledge)}")
return compressed_knowledge
except Exception as e:
diff --git a/src/local_deep_research/advanced_search_system/questions/base_question.py b/src/local_deep_research/advanced_search_system/questions/base_question.py
index 5c80e6adb6..80d2896b9f 100644
--- a/src/local_deep_research/advanced_search_system/questions/base_question.py
+++ b/src/local_deep_research/advanced_search_system/questions/base_question.py
@@ -44,36 +44,6 @@ class BaseQuestionGenerator(ABC):
"""
pass
- def _validate_inputs(
- self,
- current_knowledge: str,
- query: str,
- questions_per_iteration: int,
- questions_by_iteration: Dict[int, List[str]],
- ) -> bool:
- """
- Validate the input parameters for question generation.
-
- Args:
- current_knowledge: The accumulated knowledge so far
- query: The original research query
- questions_per_iteration: Number of questions to generate per iteration
- questions_by_iteration: Questions generated in previous iterations
-
- Returns:
- bool: True if inputs are valid, False otherwise
- """
- if not query or not isinstance(query, str):
- logger.error("Invalid query provided")
- return False
- if questions_per_iteration < 1:
- logger.error("Invalid questions_per_iteration value")
- return False
- if not isinstance(questions_by_iteration, dict):
- logger.error("Invalid questions_by_iteration format")
- return False
- return True
-
def _format_previous_questions(
self, questions_by_iteration: Dict[int, List[str]]
) -> str:
diff --git a/src/local_deep_research/advanced_search_system/questions/decomposition_question.py b/src/local_deep_research/advanced_search_system/questions/decomposition_question.py
index ab02c8a99c..0d22a2be83 100644
--- a/src/local_deep_research/advanced_search_system/questions/decomposition_question.py
+++ b/src/local_deep_research/advanced_search_system/questions/decomposition_question.py
@@ -1,6 +1,8 @@
import logging
from typing import Dict, List
+from langchain_core.language_models import BaseLLM
+
from .base_question import BaseQuestionGenerator
logger = logging.getLogger(__name__)
@@ -9,7 +11,7 @@ logger = logging.getLogger(__name__)
class DecompositionQuestionGenerator(BaseQuestionGenerator):
"""Question generator for decomposing complex queries into sub-queries."""
- def __init__(self, model, max_subqueries=5):
+ def __init__(self, model: BaseLLM, max_subqueries: int = 5):
"""
Initialize the question generator.
@@ -24,7 +26,7 @@ class DecompositionQuestionGenerator(BaseQuestionGenerator):
self,
current_knowledge: str,
query: str,
- initial_results: List[Dict] = None,
+ initial_results: List[Dict] | None = None,
**kwargs,
) -> List[str]:
"""Generate sub-queries by decomposing the original query."""
diff --git a/src/local_deep_research/advanced_search_system/strategies/base_strategy.py b/src/local_deep_research/advanced_search_system/strategies/base_strategy.py
index ac1af0f8c5..969a5e7a59 100644
--- a/src/local_deep_research/advanced_search_system/strategies/base_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/base_strategy.py
@@ -90,7 +90,7 @@ class BaseSearchStrategy(ABC):
List: Empty list to continue processing
"""
error_msg = f"Error during search: {str(error)}"
- logger.info(f"SEARCH ERROR: {error_msg}")
+ logger.error(f"SEARCH ERROR: {error_msg}")
self._update_progress(
error_msg,
progress_base + 2,
diff --git a/src/local_deep_research/advanced_search_system/strategies/iterdrag_strategy.py b/src/local_deep_research/advanced_search_system/strategies/iterdrag_strategy.py
index e8c535c496..de2e6157e3 100644
--- a/src/local_deep_research/advanced_search_system/strategies/iterdrag_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/iterdrag_strategy.py
@@ -5,7 +5,9 @@ IterDRAG strategy implementation.
import json
import logging
from datetime import datetime
-from typing import Callable, Dict, List
+from typing import Dict, List
+
+from langchain_core.language_models import BaseLLM
from ...citation_handler import CitationHandler
from ...config.config_files import settings
@@ -25,7 +27,9 @@ logger = logging.getLogger(__name__)
class IterDRAGStrategy(BaseSearchStrategy):
"""IterDRAG strategy that breaks queries into sub-queries."""
- def __init__(self, model=None, search=None, citation_handler=None):
+ def __init__(
+ self, model: BaseLLM | None = None, search=None, citation_handler=None
+ ):
"""Initialize the strategy with optional dependency injection for testing."""
super().__init__()
self.model = model or get_llm()
@@ -42,10 +46,6 @@ class IterDRAGStrategy(BaseSearchStrategy):
self.knowledge_generator = StandardKnowledge(self.model)
self.findings_repository = FindingsRepository(self.model)
- def set_progress_callback(self, callback: Callable[[str, int, dict], None]) -> None:
- """Set a callback function to receive progress updates."""
- self.progress_callback = callback
-
def _update_progress(
self, message: str, progress_percent: int = None, metadata: dict = None
) -> None:
diff --git a/src/local_deep_research/advanced_search_system/strategies/standard_strategy.py b/src/local_deep_research/advanced_search_system/strategies/standard_strategy.py
index 09c0ee5af2..816f8044a7 100644
--- a/src/local_deep_research/advanced_search_system/strategies/standard_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/standard_strategy.py
@@ -1,5 +1,5 @@
import logging
-from typing import Callable, Dict
+from typing import Dict
from ...citation_handler import CitationHandler
from ...config.config_files import settings
@@ -41,10 +41,6 @@ class StandardSearchStrategy(BaseSearchStrategy):
self.progress_callback = None
self.all_links_of_system = list()
- def set_progress_callback(self, callback: Callable[[str, int, dict], None]) -> None:
- """Set a callback function to receive progress updates."""
- self.progress_callback = callback
-
def _update_progress(
self, message: str, progress_percent: int = None, metadata: dict = None
) -> None:
@@ -141,12 +137,8 @@ class StandardSearchStrategy(BaseSearchStrategy):
search_results = self.search.run(question)
except Exception as e:
error_msg = f"Error during search: {str(e)}"
- logger.info(f"SEARCH ERROR: {error_msg}")
- self._update_progress(
- error_msg,
- int(question_progress_base + 2),
- {"phase": "search_error", "error": str(e)},
- )
+ logger.error(f"SEARCH ERROR: {error_msg}")
+ self._handle_search_error(error_msg, question_progress_base + 10)
search_results = []
if search_results is None:
@@ -230,11 +222,8 @@ class StandardSearchStrategy(BaseSearchStrategy):
except Exception as e:
error_msg = f"Error analyzing results: {str(e)}"
logger.info(f"ANALYSIS ERROR: {error_msg}")
- self._update_progress(
- error_msg,
- int(question_progress_base + 10),
- {"phase": "analysis_error", "error": str(e)},
- )
+ self._handle_search_error(error_msg, question_progress_base + 10)
+
iteration += 1
self._update_progress(
@@ -256,10 +245,8 @@ class StandardSearchStrategy(BaseSearchStrategy):
except Exception as e:
error_msg = f"Error compressing knowledge: {str(e)}"
logger.info(f"COMPRESSION ERROR: {error_msg}")
- self._update_progress(
- error_msg,
- int((iteration / total_iterations) * 100 - 3),
- {"phase": "compression_error", "error": str(e)},
+ self._handle_search_error(
+ error_msg, int((iteration / total_iterations) * 100 - 3)
)
self._update_progress(
diff --git a/src/local_deep_research/web/services/research_service.py.bak b/src/local_deep_research/web/services/research_service.py.bak
deleted file mode 100644
index 1a6048a452..0000000000
--- a/src/local_deep_research/web/services/research_service.py.bak
+++ /dev/null
@@ -1,811 +0,0 @@
-import json
-import logging
-import os
-import threading
-import traceback
-from datetime import datetime
-
-from ...config.config_files import settings
-from ...config.llm_config import get_llm
-from ...config.search_config import get_search
-from ...report_generator import IntegratedReportGenerator
-from ...search_system import AdvancedSearchSystem
-from ..models.database import add_log_to_db, calculate_duration, get_db_connection
-from .socket_service import emit_to_subscribers # Keep if needed directly
-
-# Initialize logger
-logger = logging.getLogger(__name__)
-
-# Output directory for research results
-OUTPUT_DIR = "research_outputs"
-
-
-def start_research_process(
- research_id,
- query,
- mode,
- active_research,
- termination_flags,
- run_research_callback,
- **kwargs,
-):
- """
- Start a research process in a background thread.
-
- Args:
- research_id: The ID of the research
- query: The research query
- mode: The research mode (quick/detailed)
- active_research: Dictionary of active research processes
- termination_flags: Dictionary of termination flags
- run_research_callback: The callback function to run the research
- **kwargs: Additional parameters to pass to the research process (model, search_engine, etc.)
-
- Returns:
- threading.Thread: The thread running the research
- """
- # Start research process in a background thread
- thread = threading.Thread(
- target=run_research_callback,
- args=(research_id, query, mode, active_research, termination_flags),
- kwargs=kwargs,
- )
- thread.daemon = True
- thread.start()
-
- active_research[research_id] = {
- "thread": thread,
- "progress": 0,
- "status": "in_progress",
- "log": [
- {
- "time": datetime.utcnow().isoformat(),
- "message": "Research started",
- "progress": 0,
- }
- ],
- "settings": kwargs, # Store settings for reference
- }
-
- return thread
-
-
-def run_research_process(
- research_id, query, mode, active_research, termination_flags, **kwargs
-):
- """
- Run the research process in the background for a given research ID.
-
- Args:
- research_id: The ID of the research
- query: The research query
- mode: The research mode (quick/detailed)
- active_research: Dictionary of active research processes
- termination_flags: Dictionary of termination flags
- **kwargs: Additional parameters for the research (model, search_engine, etc.)
- """
- try:
- # Check if this research has been terminated before we even start
- if research_id in termination_flags and termination_flags[research_id]:
- logger.info(f"Research {research_id} was terminated before starting")
- cleanup_research_resources(research_id, active_research, termination_flags)
- return
-
- logger.info(f"Starting research process for ID {research_id}, query: {query}")
-
- # Extract key parameters
- model = kwargs.get("model")
- search_engine = kwargs.get("search_engine")
- max_results = kwargs.get("max_results")
- time_period = kwargs.get("time_period")
- iterations = kwargs.get("iterations")
- questions_per_iteration = kwargs.get("questions_per_iteration")
-
- # Log all parameters for debugging
- logger.info(
- f"Research parameters: model={model}, search_engine={search_engine}, max_results={max_results}, time_period={time_period}, iterations={iterations}, questions_per_iteration={questions_per_iteration}"
- )
-
- # Set up the AI Context Manager
- output_dir = os.path.join(OUTPUT_DIR, f"research_{research_id}")
- os.makedirs(output_dir, exist_ok=True)
-
- # Set up progress callback
- def progress_callback(message, progress_percent, metadata):
- # Frequent termination check
- if research_id in termination_flags and termination_flags[research_id]:
- handle_termination(research_id, active_research, termination_flags)
- raise Exception("Research was terminated by user")
-
- timestamp = datetime.utcnow().isoformat()
-
- # Adjust progress based on research mode
- adjusted_progress = progress_percent
- if mode == "detailed" and metadata.get("phase") == "output_generation":
- # For detailed mode, adjust the progress range for output generation
- adjusted_progress = min(80, progress_percent)
- elif mode == "detailed" and metadata.get("phase") == "report_generation":
- # Scale the progress from 80% to 95% for the report generation phase
- if progress_percent is not None:
- normalized = progress_percent / 100
- adjusted_progress = 80 + (normalized * 15)
- elif mode == "quick" and metadata.get("phase") == "output_generation":
- # For quick mode, ensure we're at least at 85% during output generation
- adjusted_progress = max(85, progress_percent)
- # Map any further progress within output_generation to 85-95% range
- if progress_percent is not None and progress_percent > 0:
- normalized = progress_percent / 100
- adjusted_progress = 85 + (normalized * 10)
-
- # Don't let progress go backwards
- if research_id in active_research and adjusted_progress is not None:
- current_progress = active_research[research_id].get("progress", 0)
- adjusted_progress = max(current_progress, adjusted_progress)
-
- log_entry = {
- "time": timestamp,
- "message": message,
- "progress": adjusted_progress,
- "metadata": metadata,
- }
-
- # Update active research record
- if research_id in active_research:
- active_research[research_id]["log"].append(log_entry)
- if adjusted_progress is not None:
- active_research[research_id]["progress"] = adjusted_progress
-
- # Determine log type for database storage
- log_type = "info"
- if metadata and metadata.get("phase"):
- phase = metadata.get("phase")
- if phase in ["complete", "iteration_complete"]:
- log_type = "milestone"
- elif phase == "error" or "error" in message.lower():
- log_type = "error"
-
- # Save logs to the database
- add_log_to_db(
- research_id,
- message,
- log_type=log_type,
- progress=adjusted_progress,
- metadata=metadata,
- )
-
- # Update progress in the research_history table (for backward compatibility)
- conn = get_db_connection()
- cursor = conn.cursor()
-
- # Update the progress and log separately to avoid race conditions
- if adjusted_progress is not None:
- cursor.execute(
- "UPDATE research_history SET progress = ? WHERE id = ?",
- (adjusted_progress, research_id),
- )
-
- # Add the log entry to the progress_log
- cursor.execute(
- "SELECT progress_log FROM research_history WHERE id = ?",
- (research_id,),
- )
- log_result = cursor.fetchone()
-
- if log_result:
- try:
- current_log = json.loads(log_result[0])
- except Exception:
- current_log = []
-
- current_log.append(log_entry)
- cursor.execute(
- "UPDATE research_history SET progress_log = ? WHERE id = ?",
- (json.dumps(current_log), research_id),
- )
-
- conn.commit()
- conn.close()
-
- # Emit a socket event
- try:
- # Basic event data
- event_data = {"message": message, "progress": adjusted_progress}
-
- # Add log entry in full format for detailed logging on client
- if metadata:
- event_data["log_entry"] = log_entry
-
- emit_to_subscribers("research_progress", research_id, event_data)
- except Exception as e:
- logger.error(f"Socket emit error (non-critical): {str(e)}")
-
- # Function to check termination during long-running operations
- def check_termination():
- if research_id in termination_flags and termination_flags[research_id]:
- handle_termination(research_id, active_research, termination_flags)
- raise Exception(
- "Research was terminated by user during long-running operation"
- )
- return False # Not terminated
-
- # Set the progress callback in the system
- system = AdvancedSearchSystem(strategy_name="standard")
- system.set_progress_callback(progress_callback)
-
- # Configure the system with the specified parameters
- if model or search_engine:
- # Log that we're overriding system settings
- logger.info(
- f"Overriding system settings with: model={model}, search_engine={search_engine}"
- )
-
- # Override LLM if specified
- if model:
- try:
- # Temporarily override the model setting
- original_model = settings.llm.model
- settings.llm.model = model
- system.model = get_llm()
- # Restore original setting
- settings.llm.model = original_model
- logger.info(f"Successfully set LLM to: {model}")
- except Exception as e:
- logger.error(f"Error setting LLM model to {model}: {str(e)}")
-
- # Override search engine if specified
- if search_engine:
- try:
- # Temporarily override the search setting
- original_search = settings.search.tool
- settings.search.tool = search_engine
-
- # Set other search parameters if provided
- if max_results:
- original_max_results = settings.search.max_results
- settings.search.max_results = int(max_results)
-
- if time_period:
- original_time_period = settings.search.time_period
- settings.search.time_period = time_period
-
- if iterations:
- original_iterations = settings.search.iterations
- settings.search.iterations = int(iterations)
- system.max_iterations = int(iterations)
-
- if questions_per_iteration:
- original_questions = settings.search.questions_per_iteration
- settings.search.questions_per_iteration = int(
- questions_per_iteration
- )
- system.questions_per_iteration = int(questions_per_iteration)
-
- # Create a new search object with these settings
- system.search = get_search(
- search_tool=search_engine, llm_instance=system.model
- )
-
- # Restore original settings
- settings.search.tool = original_search
- if max_results:
- settings.search.max_results = original_max_results
- if time_period:
- settings.search.time_period = original_time_period
- if iterations:
- settings.search.iterations = original_iterations
- if questions_per_iteration:
- settings.search.questions_per_iteration = original_questions
-
- logger.info(f"Successfully set search engine to: {search_engine}")
- except Exception as e:
- logger.error(
- f"Error setting search engine to {search_engine}: {str(e)}"
- )
-
- # Run the search
- progress_callback("Starting research process", 5, {"phase": "init"})
-
- try:
- results = system.analyze_topic(query)
- if mode == "quick":
- progress_callback(
- "Search complete, preparing to generate summary...",
- 85,
- {"phase": "output_generation"},
- )
- else:
- progress_callback(
- "Search complete, generating output",
- 80,
- {"phase": "output_generation"},
- )
- except Exception as search_error:
- # Better handling of specific search errors
- error_message = str(search_error)
- error_type = "unknown"
-
- # Extract error details for common issues
- if "status code: 503" in error_message:
- error_message = "Ollama AI service is unavailable (HTTP 503). Please check that Ollama is running properly on your system."
- error_type = "ollama_unavailable"
- elif "status code: 404" in error_message:
- error_message = "Ollama model not found (HTTP 404). Please check that you have pulled the required model."
- error_type = "model_not_found"
- elif "status code:" in error_message:
- # Extract the status code for other HTTP errors
- status_code = error_message.split("status code:")[1].strip()
- error_message = f"API request failed with status code {status_code}. Please check your configuration."
- error_type = "api_error"
- elif "connection" in error_message.lower():
- error_message = "Connection error. Please check that your LLM service (Ollama/API) is running and accessible."
- error_type = "connection_error"
-
- # Raise with improved error message
- raise Exception(f"{error_message} (Error type: {error_type})")
-
- # Generate output based on mode
- if mode == "quick":
- # Quick Summary
- if results.get("findings") or results.get("formatted_findings"):
- raw_formatted_findings = results["formatted_findings"]
-
- # Check if formatted_findings contains an error message
- if isinstance(
- raw_formatted_findings, str
- ) and raw_formatted_findings.startswith("Error:"):
- # Extract synthesized content from findings if available
- synthesized_content = ""
- for finding in results.get("findings", []):
- if finding.get("phase") == "Final synthesis":
- synthesized_content = finding.get("content", "")
- break
-
- # Use synthesized content as fallback
- if synthesized_content:
- raw_formatted_findings = synthesized_content
- # Or use current_knowledge as another fallback
- elif results.get("current_knowledge"):
- raw_formatted_findings = results["current_knowledge"]
- # Or combine all finding contents as last resort
- elif results.get("findings"):
- raw_formatted_findings = "\n\n".join(
- f"## {finding.get('phase', 'Finding')}\n\n{finding.get('content', '')}"
- for finding in results.get("findings", [])
- if finding.get("content")
- )
-
- logger.info(
- f"Found formatted_findings of length: {len(str(raw_formatted_findings))}"
- )
-
- try:
- clean_markdown = raw_formatted_findings
- logger.info(
- f"Successfully converted to clean markdown of length: {len(clean_markdown)}"
- )
-
- # First send a progress update for generating the summary
- progress_callback(
- "Generating clean summary from research data...",
- 90,
- {"phase": "output_generation"},
- )
-
- # Save as markdown file
- if not os.path.exists(OUTPUT_DIR):
- os.makedirs(OUTPUT_DIR)
-
- safe_query = "".join(
- x for x in query if x.isalnum() or x in [" ", "-", "_"]
- )[:50]
- safe_query = safe_query.replace(" ", "_").lower()
- report_path = os.path.join(
- OUTPUT_DIR, f"quick_summary_{safe_query}.md"
- )
-
- # Send progress update for writing to file
- progress_callback(
- "Writing research report to file...",
- 95,
- {"phase": "report_complete"},
- )
-
- logger.info(f"Writing report to: {report_path}")
- with open(report_path, "w", encoding="utf-8") as f:
- f.write("# Quick Research Summary\n\n")
- f.write(f"Query: {query}\n\n")
- f.write(clean_markdown)
- f.write("\n\n## Research Metrics\n")
- f.write(f"- Search Iterations: {results['iterations']}\n")
- f.write(f"- Generated at: {datetime.utcnow().isoformat()}\n")
-
- # Update database
- metadata = {
- "iterations": results["iterations"],
- "generated_at": datetime.utcnow().isoformat(),
- }
-
- # Calculate duration in seconds - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- logger.info(f"Updating database for research_id: {research_id}")
- # Get the start time from the database
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?",
- (research_id,),
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- duration_seconds = calculate_duration(result[0])
-
- # Update the record
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, report_path = ?, metadata = ? WHERE id = ?",
- (
- "completed",
- completed_at,
- duration_seconds,
- report_path,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
- logger.info(
- f"Database updated successfully for research_id: {research_id}"
- )
-
- # Send the final completion message
- progress_callback(
- "Research completed successfully",
- 100,
- {"phase": "complete", "report_path": report_path},
- )
-
- # Clean up resources
- logger.info(f"Cleaning up resources for research_id: {research_id}")
- cleanup_research_resources(
- research_id, active_research, termination_flags
- )
- logger.info(f"Resources cleaned up for research_id: {research_id}")
-
- except Exception as inner_e:
- logger.error(
- f"Error during quick summary generation: {str(inner_e)}"
- )
- logger.error(traceback.format_exc())
- raise Exception(f"Error generating quick summary: {str(inner_e)}")
- else:
- raise Exception(
- "No research findings were generated. Please try again."
- )
- else:
- # Full Report
- progress_callback(
- "Generating detailed report...", 85, {"phase": "report_generation"}
- )
-
- report_generator = IntegratedReportGenerator()
- final_report = report_generator.generate_report(results, query)
-
- progress_callback(
- "Report generation complete", 95, {"phase": "report_complete"}
- )
-
- # Save as markdown file
- if not os.path.exists(OUTPUT_DIR):
- os.makedirs(OUTPUT_DIR)
-
- safe_query = "".join(
- x for x in query if x.isalnum() or x in [" ", "-", "_"]
- )[:50]
- safe_query = safe_query.replace(" ", "_").lower()
- report_path = os.path.join(OUTPUT_DIR, f"detailed_report_{safe_query}.md")
-
- with open(report_path, "w", encoding="utf-8") as f:
- f.write(final_report["content"])
-
- # Update database
- metadata = final_report["metadata"]
- metadata["iterations"] = results["iterations"]
-
- # Calculate duration in seconds - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- # Get the start time from the database
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- duration_seconds = calculate_duration(result[0])
-
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, report_path = ?, metadata = ? WHERE id = ?",
- (
- "completed",
- completed_at,
- duration_seconds,
- report_path,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
-
- progress_callback(
- "Research completed successfully",
- 100,
- {"phase": "complete", "report_path": report_path},
- )
-
- # Clean up resources
- cleanup_research_resources(research_id, active_research, termination_flags)
-
- except Exception as e:
- # Handle error
- error_message = f"Research failed: {str(e)}"
- logger.error(error_message)
- try:
- # Check for common Ollama error patterns in the exception and provide more user-friendly errors
- user_friendly_error = str(e)
- error_context = {}
-
- if "Error type: ollama_unavailable" in user_friendly_error:
- user_friendly_error = "Ollama AI service is unavailable. Please check that Ollama is running properly on your system."
- error_context = {
- "solution": "Start Ollama with 'ollama serve' or check if it's installed correctly."
- }
- elif "Error type: model_not_found" in user_friendly_error:
- user_friendly_error = (
- "Required Ollama model not found. Please pull the model first."
- )
- error_context = {
- "solution": "Run 'ollama pull mistral' to download the required model."
- }
- elif "Error type: connection_error" in user_friendly_error:
- user_friendly_error = "Connection error with LLM service. Please check that your AI service is running."
- error_context = {
- "solution": "Ensure Ollama or your API service is running and accessible."
- }
- elif "Error type: api_error" in user_friendly_error:
- # Keep the original error message as it's already improved
- error_context = {"solution": "Check API configuration and credentials."}
-
- # Update metadata with more context about the error
- metadata = {"phase": "error", "error": user_friendly_error}
- if error_context:
- metadata.update(error_context)
-
- # If we still have an active research record, update its log
- if research_id in active_research:
- progress_callback(user_friendly_error, None, metadata)
-
- conn = get_db_connection()
- cursor = conn.cursor()
-
- # If termination was requested, mark as suspended instead of failed
- status = (
- "suspended"
- if (research_id in termination_flags and termination_flags[research_id])
- else "failed"
- )
- message = (
- "Research was terminated by user"
- if status == "suspended"
- else user_friendly_error
- )
-
- # Calculate duration up to termination point - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- # Get the start time from the database
- duration_seconds = None
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- # Use the helper function for consistent duration calculation
- if result and result[0]:
- duration_seconds = calculate_duration(result[0])
-
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ?, metadata = ? WHERE id = ?",
- (
- status,
- completed_at,
- duration_seconds,
- json.dumps(metadata),
- research_id,
- ),
- )
- conn.commit()
- conn.close()
-
- try:
- emit_to_subscribers(
- "research_progress",
- research_id,
- {"status": status, "error": message},
- )
- except Exception as socket_error:
- logger.error(f"Failed to emit error via socket: {str(socket_error)}")
-
- except Exception as inner_e:
- logger.error(f"Error in error handler: {str(inner_e)}")
- logger.error(traceback.format_exc())
-
- # Clean up resources
- cleanup_research_resources(research_id, active_research, termination_flags)
-
-
-def cleanup_research_resources(research_id, active_research, termination_flags):
- """
- Clean up resources for a completed research.
-
- Args:
- research_id: The ID of the research
- active_research: Dictionary of active research processes
- termination_flags: Dictionary of termination flags
- """
- logger.info(f"Cleaning up resources for research {research_id}")
-
- # Get the current status from the database to determine the final status message
- current_status = "completed" # Default
- try:
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute(
- "SELECT status FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
- if result and result[0]:
- current_status = result[0]
- conn.close()
- except Exception as e:
- logger.error(f"Error retrieving research status during cleanup: {e}")
-
- # Remove from active research
- if research_id in active_research:
- del active_research[research_id]
-
- # Remove from termination flags
- if research_id in termination_flags:
- del termination_flags[research_id]
-
- # Send a final message to subscribers
- try:
- # Import here to avoid circular imports
- from ..routes.research_routes import get_globals
-
- globals_dict = get_globals()
- socket_subscriptions = globals_dict.get("socket_subscriptions", {})
-
- # Send a final message to any remaining subscribers with explicit status
- if research_id in socket_subscriptions and socket_subscriptions[research_id]:
- # Use the proper status message based on database status
- if current_status == "suspended" or current_status == "failed":
- final_message = {
- "status": current_status,
- "message": f"Research was {current_status}",
- "progress": 0, # For suspended research, show 0% not 100%
- }
- else:
- final_message = {
- "status": "completed",
- "message": "Research process has ended and resources have been cleaned up",
- "progress": 100,
- }
-
- logger.info(
- f"Sending final {current_status} socket message for research {research_id}"
- )
-
- emit_to_subscribers("research_progress", research_id, final_message)
-
- except Exception as e:
- logger.error(f"Error sending final cleanup message: {e}")
-
-
-def handle_termination(research_id, active_research, termination_flags):
- """
- Handle the termination of a research process.
-
- Args:
- research_id: The ID of the research
- active_research: Dictionary of active research processes
- termination_flags: Dictionary of termination flags
- """
- # Explicitly set the status to suspended in the database
- conn = get_db_connection()
- cursor = conn.cursor()
-
- # Calculate duration up to termination point - using UTC consistently
- now = datetime.utcnow()
- completed_at = now.isoformat()
-
- # Get the start time from the database
- cursor.execute(
- "SELECT created_at FROM research_history WHERE id = ?",
- (research_id,),
- )
- result = cursor.fetchone()
-
- # Calculate the duration
- duration_seconds = calculate_duration(result[0]) if result and result[0] else None
-
- # Update the database with suspended status
- cursor.execute(
- "UPDATE research_history SET status = ?, completed_at = ?, duration_seconds = ? WHERE id = ?",
- ("suspended", completed_at, duration_seconds, research_id),
- )
- conn.commit()
- conn.close()
-
- # Clean up resources
- cleanup_research_resources(research_id, active_research, termination_flags)
-
-
-def cancel_research(research_id):
- """
- Cancel/terminate a research process
-
- Args:
- research_id: The ID of the research to cancel
-
- Returns:
- bool: True if the research was found and cancelled, False otherwise
- """
- # Import globals from research routes
- from ..routes.research_routes import get_globals
-
- globals_dict = get_globals()
- active_research = globals_dict["active_research"]
- termination_flags = globals_dict["termination_flags"]
-
- # Set termination flag
- termination_flags[research_id] = True
-
- # Check if the research is active
- if research_id in active_research:
- # Call handle_termination to update database
- handle_termination(research_id, active_research, termination_flags)
- return True
- else:
- # Update database directly if not found in active_research
- from ..models.database import get_db_connection
-
- conn = get_db_connection()
- cursor = conn.cursor()
-
- # First check if the research exists
- cursor.execute(
- "SELECT status FROM research_history WHERE id = ?", (research_id,)
- )
- result = cursor.fetchone()
-
- if not result:
- conn.close()
- return False
-
- # If it exists but isn't in active_research, still update status
- cursor.execute(
- "UPDATE research_history SET status = ? WHERE id = ?",
- ("suspended", research_id),
- )
- conn.commit()
- conn.close()
-
- return True