~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
Update search_system.py
LearningCircuit049088f792b9
diff --git a/search_system.py b/search_system.pyindex 7ac0626a22..63d11e4fb6 100644--- a/search_system.py+++ b/search_system.py@@ -2,38 +2,34 @@ from langchain_community.tools import DuckDuckGoSearchResultsfrom langchain_ollama import ChatOllamafrom typing import Dict, Listfrom datetime import datetime--from utilities import remove_think_tags+from utilities import remove_think_tags, format_findings_to_text+import osclass 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 # Maximum characters to keep as context- self.questions_by_iteration = {} # New: track questions+ 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]def _get_follow_up_questions(self, current_knowledge: str, query: str) -> List[str]:- # Limit the context for the modellimited_knowledge = current_knowledge[-self.context_limit:] if len(current_knowledge) > self.context_limit else current_knowledge-- 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: '- """+ 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:"""response = self.model.invoke(prompt)- 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]+ return [q.replace('Q:', '').strip()+ for q in remove_think_tags(response.content).split('\n')+ if q.strip().startswith('Q:')][:3]def analyze_topic(self, query: str) -> Dict:findings = []@@ -42,61 +38,63 @@ class AdvancedSearchSystem:while iteration < self.max_iterations:if iteration == 0:- search_results = " - Ask questions about this topic."+ 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"+initial_analysis = self.model.invoke(- f"Analyze these results about: {query}\n\nResults: {search_results}"+ f"Analyze findings for: {query}\n{all_search_results}")findings.append({"phase": "Initial Analysis",- "content": remove_think_tags(initial_analysis.content)+ "content": remove_think_tags(initial_analysis.content),+ "questions": initial_questions,+ "search_results": all_search_results})current_knowledge = remove_think_tags(initial_analysis.content)-else:questions = self._get_follow_up_questions(current_knowledge, query)- self.questions_by_iteration[iteration] = questions # Store questions+ self.questions_by_iteration[iteration] = questionsfor 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_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)+ analysis = self.model.invoke(+ f"Question: {question}\nResults: {search_results}\nKnowledge: {limited_knowledge}\nExtend knowledge and conclude on question."+ )findings.append({"phase": f"Follow-up {iteration}.{questions.index(question) + 1}","content": remove_think_tags(analysis.content),- "question": question, # New: store question+ "question": question,"search_results": search_results})- current_knowledge += f"\n\n\n{remove_think_tags(analysis.content)}"- iteration += 1- 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()- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")- filename = f"formatted_output_{safe_query}_{timestamp}.txt"+ current_knowledge += f"\n\n{remove_think_tags(analysis.content)}"- with open(filename, "w", encoding='utf-8') as text_file:- text_file.write(formatted_findings)-+ iteration += 1+ self._save_findings(findings, current_knowledge, query)--return {"findings": findings,"iterations": iteration,- "questions": self.questions_by_iteration # New: return questions+ "questions": self.questions_by_iteration}++ 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)