Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Remove datetime on files to reduce files

LearningCircuite537016671d7
diff --git a/search_system.py b/search_system.py
index 63d11e4fb6..d6fce60968 100644
--- a/search_system.py
+++ b/search_system.py
@@ -2,34 +2,38 @@ from langchain_community.tools import DuckDuckGoSearchResults
from langchain_ollama import ChatOllama
from typing import Dict, List
from datetime import datetime
-from utilities import remove_think_tags, format_findings_to_text
-import os
+
+from utilities import remove_think_tags
class AdvancedSearchSystem:
def __init__(self):
self.search = DuckDuckGoSearchResults(max_results=40)
self.model = ChatOllama(model="deepseek-r1:14b", temperature=0.7)
+
self.max_iterations = 2
- self.context_limit = 5000
- self.questions_by_iteration = {}
-
- def _get_initial_questions(self, query: str) -> List[str]:
- prompt = f"Generate 3 essential research questions to investigate: {query}\nFormat: One question per line starting with Q:"
- response = self.model.invoke(prompt)
- return [q.replace('Q:', '').strip()
- for q in remove_think_tags(response.content).split('\n')
- if q.strip().startswith('Q:')][:3]
+ self.context_limit = 5000 # Maximum characters to keep as context
+ self.questions_by_iteration = {} # New: track questions
def _get_follow_up_questions(self, current_knowledge: str, query: str) -> List[str]:
+ # Limit the context for the model
limited_knowledge = current_knowledge[-self.context_limit:] if len(current_knowledge) > self.context_limit else current_knowledge
- prompt = f"""Based on current knowledge, what critical questions remain unanswered?
-Query: {query}
-Past questions: {str(self.questions_by_iteration)}
-Knowledge: {limited_knowledge}
-Format: One question per line starting with Q:"""
+
+ prompt = f"""
+ Based on our current knowledge, what are the most important questions we still need to answer concerning the user query?
+ query: {query}
+ Past questions:
+ {str(self.questions_by_iteration)}
+ Recent Knowledge:
+ {limited_knowledge}
+
+ List only the top 3 most critical questions that would help fill in gaps or resolve uncertainties. Do not repeat questions.
+ Format: One question per line, starting with 'Q: '
+ """
response = self.model.invoke(prompt)
- return [q.replace('Q:', '').strip()
- for q in remove_think_tags(response.content).split('\n')
- if q.strip().startswith('Q:')][:3]
+ questions = [q.replace('Q: ', '').strip()
+ for q in remove_think_tags(response.content).split('\n')
+ if q.strip().startswith('Q: ')]
+ print(questions)
+ return questions[:3]
def analyze_topic(self, query: str) -> Dict:
findings = []
@@ -38,63 +42,60 @@ Format: One question per line starting with Q:"""
while iteration < self.max_iterations:
if iteration == 0:
- initial_questions = self._get_initial_questions(query)
- self.questions_by_iteration[0] = initial_questions
-
- # Store results differently for initial analysis
- all_search_results = ""
- for question in initial_questions:
- search_results = self.search.run(question)
- all_search_results += f"Question: {question}\n{search_results}\n\n"
-
+ search_results = " - Ask questions about this topic."
initial_analysis = self.model.invoke(
- f"Analyze findings for: {query}\n{all_search_results}"
+ f"Analyze these results about: {query}\n\nResults: {search_results}"
)
findings.append({
"phase": "Initial Analysis",
- "content": remove_think_tags(initial_analysis.content),
- "questions": initial_questions,
- "search_results": all_search_results
+ "content": remove_think_tags(initial_analysis.content)
})
current_knowledge = remove_think_tags(initial_analysis.content)
+
else:
questions = self._get_follow_up_questions(current_knowledge, query)
- self.questions_by_iteration[iteration] = questions
+ self.questions_by_iteration[iteration] = questions # Store questions
for question in questions:
search_results = self.search.run(question)
+ print(search_results)
+
limited_knowledge = current_knowledge[-self.context_limit:] if len(current_knowledge) > self.context_limit else current_knowledge
- analysis = self.model.invoke(
- f"Question: {question}\nResults: {search_results}\nKnowledge: {limited_knowledge}\nExtend knowledge and conclude on question."
- )
+ analysis_prompt = f"""
+ Question: {question}
+
+ Results: {search_results}
+
+ Knowledge:
+ {limited_knowledge}
+
+ Extend current knowledge on question and provide a conclusion on question.
+ """
+ analysis = self.model.invoke(analysis_prompt)
findings.append({
"phase": f"Follow-up {iteration}.{questions.index(question) + 1}",
"content": remove_think_tags(analysis.content),
- "question": question,
+ "question": question, # New: store question
"search_results": search_results
})
- current_knowledge += f"\n\n{remove_think_tags(analysis.content)}"
-
+ current_knowledge += f"\n\n\n{remove_think_tags(analysis.content)}"
iteration += 1
- self._save_findings(findings, current_knowledge, query)
+ print("Research iteration ", iteration)
+ from utilities import format_findings_to_text
+ formatted_findings = format_findings_to_text(findings, current_knowledge, self.questions_by_iteration)
+ # Create a safe filename from the query
+ safe_query = "".join(x for x in query if x.isalnum() or x in [' ', '-', '_'])[:50] # Limit length
+ safe_query = safe_query.replace(' ', '_').lower()
+ filename = f"formatted_output_{safe_query}.txt"
+
+ with open(filename, "w", encoding='utf-8') as text_file:
+ text_file.write(formatted_findings)
+
+
+
return {
"findings": findings,
"iterations": iteration,
- "questions": self.questions_by_iteration
+ "questions": self.questions_by_iteration # New: return questions
}
-
- def _save_findings(self, findings: List[Dict], current_knowledge: str, query: str):
- formatted_findings = format_findings_to_text(findings, current_knowledge, self.questions_by_iteration)
- safe_query = "".join(x for x in query if x.isalnum() or x in [' ', '-', '_'])[:50]
- safe_query = safe_query.replace(' ', '_').lower()
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
-
- output_dir = "research_outputs"
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- filename = os.path.join(output_dir, f"formatted_output_{safe_query}_{timestamp}.txt")
-
- with open(filename, "w", encoding='utf-8') as text_file:
- text_file.write(formatted_findings)