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.

Fix f-string issues in repository.py

hashedviking025f17f8964c
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 dae4ca91b1..ae6cc7bf0b 100644
--- a/src/local_deep_research/advanced_search_system/findings/repository.py
+++ b/src/local_deep_research/advanced_search_system/findings/repository.py
@@ -239,6 +239,23 @@ class FindingsRepository(BaseFindingsRepository):
# Use finding_texts for the prompt
current_knowledge = "\n\n".join(finding_texts) if finding_texts else ""
+ # Check if knowledge exceeds a reasonable token limit (rough estimate based on characters)
+ # 1 token ≈ 4 characters in English
+ estimated_tokens = len(current_knowledge) / 4
+ max_safe_tokens = 12000 # Adjust based on your model's context window
+
+ if estimated_tokens > max_safe_tokens:
+ logger.warning(
+ f"Knowledge size may exceed model's capacity: ~{int(estimated_tokens)} tokens"
+ )
+ # Truncate if needed (keeping the beginning and end which are often most important)
+ # This is a simple approach - a more sophisticated chunking might be better
+ if len(current_knowledge) > 24000: # ~6000 tokens
+ first_part = current_knowledge[:12000] # ~3000 tokens from start
+ last_part = current_knowledge[-12000:] # ~3000 tokens from end
+ current_knowledge = f"{first_part}\n\n[...content truncated due to length...]\n\n{last_part}"
+ logger.info("Knowledge truncated to fit within token limits")
+
prompt = f"""Synthesize the following accumulated knowledge into a comprehensive answer for the original query.
Format the response with clear sections, citations, and a concise summary, following the structure provided below.
@@ -270,20 +287,164 @@ Use numbered citations [1], [2], etc. for important sources - these will be link
logger.debug(f"Synthesis prompt (first 500 chars): {prompt[:500]}...")
try:
- response = self.model.invoke(prompt)
- synthesized_content = response.content
- logger.info(
- f"Successfully synthesized final answer for query: '{query}'"
- )
- # Return only the synthesized content from the LLM
- return synthesized_content
+ # Add timeout handling
+ import platform
+ import signal
+ import threading
+ from contextlib import contextmanager
+
+ # Check if we're on Windows
+ if platform.system() == "Windows":
+ # Windows-compatible timeout using threading
+ class TimeoutError(Exception):
+ pass
+
+ def timeout_handler(timeout_seconds, callback, args):
+ def handler():
+ callback(*args)
+
+ timer = threading.Timer(timeout_seconds, handler)
+ timer.daemon = True
+ return timer
+
+ def invoke_with_timeout(timeout_seconds, func, *args, **kwargs):
+ """
+ Function for implementing timeouts on Windows
+ """
+ result = None
+ exception = None
+ completed = False
+
+ def target():
+ nonlocal result, exception, completed
+ try:
+ result = func(*args, **kwargs)
+ completed = True
+ except Exception as e:
+ exception = e
+
+ thread = threading.Thread(target=target)
+ thread.daemon = True
+
+ try:
+ thread.start()
+ thread.join(timeout_seconds)
+ if not completed and thread.is_alive():
+ raise TimeoutError(
+ f"Operation timed out after {timeout_seconds} seconds"
+ )
+ if exception:
+ raise exception
+ return result
+ finally:
+ # Nothing to clean up
+ pass
+
+ # Use Windows-compatible timeout
+ try:
+ logger.info(
+ "Using Windows-compatible timeout for LLM invocation"
+ )
+ response = invoke_with_timeout(120, self.model.invoke, prompt)
+
+ # Handle different response types (string or object with content attribute)
+ if hasattr(response, "content"):
+ synthesized_content = response.content
+ else:
+ # Handle string responses
+ synthesized_content = str(response)
+
+ logger.info(
+ f"Successfully synthesized final answer for query: '{query}'"
+ )
+ # Return only the synthesized content from the LLM
+ return synthesized_content
+ except TimeoutError as timeout_error:
+ logger.error(
+ f"LLM invocation timed out during synthesis for query '{query}': {timeout_error}",
+ exc_info=True,
+ )
+ # Return more specific error about timeout
+ return "Error: Final answer synthesis failed due to LLM timeout. Please check your LLM service or try with a smaller query scope."
+
+ else:
+ # Unix-compatible timeout using SIGALRM
+ @contextmanager
+ def timeout(seconds, message="Operation timed out"):
+ def signal_handler(signum, frame):
+ raise TimeoutError(message)
+
+ signal.signal(signal.SIGALRM, signal_handler)
+ signal.alarm(seconds)
+ try:
+ yield
+ finally:
+ signal.alarm(0)
+
+ # Try with a timeout (adjust seconds as needed)
+ try:
+ with timeout(120, "LLM invocation timed out after 120 seconds"):
+ response = self.model.invoke(prompt)
+
+ # Handle different response types (string or object with content attribute)
+ if hasattr(response, "content"):
+ synthesized_content = response.content
+ else:
+ # Handle string responses
+ synthesized_content = str(response)
+
+ logger.info(
+ f"Successfully synthesized final answer for query: '{query}'"
+ )
+ # Return only the synthesized content from the LLM
+ return synthesized_content
+ except TimeoutError as timeout_error:
+ logger.error(
+ f"LLM invocation timed out during synthesis for query '{query}': {timeout_error}",
+ exc_info=True,
+ )
+ # Return more specific error about timeout
+ return "Error: Final answer synthesis failed due to LLM timeout. Please check your LLM service or try with a smaller query scope."
+
except Exception as invoke_error:
logger.error(
f"LLM invocation failed during synthesis for query '{query}': {invoke_error}",
exc_info=True,
)
- # Re-raise or return specific error
- return "Error: Failed to synthesize final answer due to LLM error."
+
+ # Attempt to determine the type of error
+ error_message = str(invoke_error).lower()
+ error_type = "unknown"
+
+ if "timeout" in error_message or "timed out" in error_message:
+ error_type = "timeout"
+ elif (
+ "too many tokens" in error_message
+ or "context length" in error_message
+ or "token limit" in error_message
+ ):
+ error_type = "token_limit"
+ elif "rate limit" in error_message or "rate_limit" in error_message:
+ error_type = "rate_limit"
+ elif "connection" in error_message or "network" in error_message:
+ error_type = "connection"
+ elif "api key" in error_message or "authentication" in error_message:
+ error_type = "authentication"
+
+ # Return more detailed error message based on type
+ if error_type == "timeout":
+ return "Error: Failed to synthesize final answer due to LLM timeout. Please check your connection or try again later."
+ elif error_type == "token_limit":
+ return "Error: Failed to synthesize final answer due to token limit exceeded. Try reducing the scope of your query."
+ elif error_type == "rate_limit":
+ return "Error: Failed to synthesize final answer due to LLM rate limit. Please try again in a few minutes."
+ elif error_type == "connection":
+ return "Error: Failed to synthesize final answer due to connection issues. Please check your internet connection and LLM service status."
+ elif error_type == "authentication":
+ return "Error: Failed to synthesize final answer due to authentication issues. Please check your API keys."
+ else:
+ # Generic error with details
+ return f"Error: Failed to synthesize final answer. LLM error: {str(invoke_error)}"
except Exception as e:
# Catch potential errors during prompt construction or logging itself
@@ -292,4 +453,4 @@ Use numbered citations [1], [2], etc. for important sources - these will be link
exc_info=True,
)
# Return a specific error message for synthesis failure
- return "Error: Failed to synthesize final answer from knowledge."
+ return f"Error: Failed to synthesize final answer from knowledge. Details: {str(e)}"