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

LearningCircuit5a015c8222f8
diff --git a/web_search_engines/engines/full_search.py b/web_search_engines/engines/full_search.py
index b29060616f..e597261f44 100644
--- a/web_search_engines/engines/full_search.py
+++ b/web_search_engines/engines/full_search.py
@@ -1,121 +1,128 @@
-
-from langchain_anthropic import ChatAnthropic
-from langchain_openai import ChatOpenAI
-from langchain_ollama import ChatOllama
-from dotenv import load_dotenv
-
-from web_search_engines.full_serp_search_results_old import FullSerpAPISearchResults # Added import for SerpAPI class
-from web_search_engines.full_search import FullSearchResults
-
-
-from web_search_engines.search_engine_ddg import DuckDuckGoSearchEngine
-from web_search_engines.search_engine_wikipedia import WikipediaSearchEngine
-from web_search_engines.search_engine_serpapi import SerpAPISearchEngine
-import os
-# Load environment variables
-load_dotenv()
-
-
-# LLM Configuration
-DEFAULT_MODEL = "mistral" # try to use the largest model that fits into your GPU memory
-DEFAULT_TEMPERATURE = 0.7
-MAX_TOKENS = 15000
-
-# Search System Settings
-SEARCH_ITERATIONS = 3
-QUESTIONS_PER_ITERATION = 3
-
-# Report settings
-SEARCHES_PER_SECTION = 5
-CONTEXT_CUT_OFF = 10000
-
-# citation handler
-ENABLE_FACT_CHECKING = False # comes with pros and cons. Maybe works better with larger LLMs?
-
-# URL Quality Check (applies to both DDG and SerpAPI)
-QUALITY_CHECK_DDG_URLS = True # Keep True for better quality results.
-
-# Search Configuration (applies to both DDG and SerpAPI)
-MAX_SEARCH_RESULTS = 40
-SEARCH_REGION = "us"
-TIME_PERIOD = "y"
-SAFE_SEARCH = True
-SEARCH_SNIPPETS_ONLY = False
-SEARCH_LANGUAGE = "English"
-
-# Output Configuration
-OUTPUT_DIR = "research_outputs"
-
-# Choose search tool: "serp" or "duckduckgo" (serp requires API key)
-search_tool = "wiki" # Change this variable to switch between search tools
-
-
-def get_llm(model_name=DEFAULT_MODEL, temperature=DEFAULT_TEMPERATURE):
- """Get LLM instance - easy to switch between models"""
- common_params = {
- "temperature": temperature,
- "max_tokens": MAX_TOKENS,
- }
-
- if "claude" in model_name:
- api_key = os.getenv("ANTHROPIC_API_KEY")
- if not api_key:
- raise ValueError("ANTHROPIC_API_KEY not found in environment variables")
- return ChatAnthropic(
- model=model_name, anthropic_api_key=api_key, **common_params
- )
-
- elif "gpt" in model_name:
- api_key = os.getenv("OPENAI_API_KEY")
- if not api_key:
- raise ValueError("OPENAI_API_KEY not found in environment variables")
- return ChatOpenAI(model=model_name, api_key=api_key, **common_params)
-
- else:
- return ChatOllama(model=model_name, base_url="http://localhost:11434", **common_params)
-
-
-def get_search():
- """Get search tool instance based on the chosen search_tool variable"""
- llm_instance = get_llm()
- if "serp" in search_tool.lower():
- search_engine = SerpAPISearchEngine(
- max_results=MAX_SEARCH_RESULTS,
- region=SEARCH_REGION,
- time_period=TIME_PERIOD,
- safe_search=SAFE_SEARCH,
- search_language=SEARCH_LANGUAGE
- )
- elif "wiki" in search_tool.lower():
- wiki = WikipediaSearchEngine(max_results=3, include_content=SEARCH_SNIPPETS_ONLY)
- return wiki
- else:
- search_engine = DuckDuckGoSearchEngine(
- max_results=MAX_SEARCH_RESULTS,
- region=SEARCH_REGION,
- safe_search=SAFE_SEARCH,
+import justext
+from langchain_community.document_loaders import AsyncChromiumLoader
+from langchain_community.document_transformers import BeautifulSoupTransformer
+from langchain_core.language_models import BaseLLM
+from typing import List, Dict
+import json, os
+from utilities import remove_think_tags
+from datetime import datetime
+import config
+
+class FullSearchResults:
+ def __init__(
+ self,
+ llm: BaseLLM, # Add LLM parameter
+ web_search: list,
+ output_format: str = "list",
+ language: str = "English",
+ max_results: int = 10,
+ region: str = "wt-wt",
+ time: str = "y",
+ safesearch: str = "Moderate"
+
+ ):
+ self.llm = llm
+ self.output_format = output_format
+ self.language = language
+ self.max_results = max_results
+ self.region = region
+ self.time = time
+ self.safesearch = safesearch
+ self.web_search =web_search
+ os.environ["USER_AGENT"] = "Local Deep Research/1.0"
+
+
+ self.bs_transformer = BeautifulSoupTransformer()
+ self.tags_to_extract = ["p", "div", "span"]
+
+ def check_urls(self, results: List[Dict], query: str) -> List[Dict]:
+ if not results:
+ return results
+
+ now = datetime.now()
+ current_time = now.strftime("%Y-%m-%d")
+ prompt = f"""ONLY Return a JSON array. The response contains no letters. Evaluate these URLs for:
+ 1. Timeliness (today: {current_time})
+ 2. Factual accuracy (cross-reference major claims)
+ 3. Source reliability (prefer official company websites, established news outlets)
+ 4. Direct relevance to query: {query}
+
+ URLs to evaluate:
+ {results}
+
+ Return a JSON array of indices (0-based) for sources that meet ALL criteria.
+ ONLY Return a JSON array of indices (0-based) and nothing else. No letters.
+ Example response: \n[0, 2, 4]\n\n"""
+
+ try:
+ # Get LLM's evaluation
+ response = self.llm.invoke(prompt)
+ # print(response)
+ good_indices = json.loads(remove_think_tags(response.content))
+
+ # Return only the results with good URLs
+ return [r for i, r in enumerate(results) if i in good_indices]
+ except Exception as e:
+ print(f"URL filtering error: {e}")
+ return []
+
+ def remove_boilerplate(self, html: str) -> str:
+ if not html or not html.strip():
+ return ""
+ paragraphs = justext.justext(html, justext.get_stoplist(self.language))
+ cleaned = "\n".join([p.text for p in paragraphs if not p.is_boilerplate])
+ return cleaned
+
+ def run(self, query: str):
+ nr_full_text = 0
+ # Step 1: Get search results from DuckDuckGo
+ search_results = self.web_search.invoke(query)
+ #print(type(search_results))
+ if not isinstance(search_results, list):
+ raise ValueError("Expected the search results in list format.")
+
+ # Step 2: Filter URLs using LLM
+ if config.QUALITY_CHECK_DDG_URLS:
+ filtered_results = self.check_urls(search_results, query)
+ else:
+ filtered_results = search_results
+
+ # Extract URLs from filtered results
+ urls = [result.get("link") for result in filtered_results if result.get("link")]
+ print(urls)
+ if not urls:
+ print("\n === NO VALID LINKS ===\n")
+ return []
+
+ # Step 3: Download the full HTML pages for filtered URLs
+ loader = AsyncChromiumLoader(urls)
+ html_docs = loader.load()
+
+ # Step 4: Process the HTML using BeautifulSoupTransformer
+ full_docs = self.bs_transformer.transform_documents(
+ html_docs, tags_to_extract=self.tags_to_extract
)
- if SEARCH_SNIPPETS_ONLY:
- return search_engine
-
- else :
- if "serp_old" in search_tool.lower():
- return FullSerpAPISearchResults(
- llm=llm_instance,
- serpapi_api_key=os.getenv("SERP_API_KEY"),
- max_results=MAX_SEARCH_RESULTS,
- language = SEARCH_LANGUAGE,
- region=SEARCH_REGION,
- time_period=TIME_PERIOD,
- safesearch="active" if SAFE_SEARCH else "off"
- )
- else:
- return FullSearchResults(
- web_search=search_engine,
- llm=llm_instance,
- max_results=MAX_SEARCH_RESULTS,
- region=SEARCH_REGION,
- time=TIME_PERIOD,
- safesearch="Moderate" if SAFE_SEARCH else "Off",
- )
\ No newline at end of file
+ # Step 5: Remove boilerplate from each document
+ url_to_content = {}
+ for doc in full_docs:
+ nr_full_text = nr_full_text + 1
+ source = doc.metadata.get("source")
+ if source:
+ cleaned_text = self.remove_boilerplate(doc.page_content)
+ url_to_content[source] = cleaned_text
+
+ # Attach the cleaned full content to each filtered result
+ for result in filtered_results:
+ link = result.get("link")
+ result["full_content"] = url_to_content.get(link, None)
+
+ print("FULL SEARCH WITH FILTERED URLS")
+ print("Full text retrieved: ", nr_full_text)
+ return filtered_results
+
+ def invoke(self, query: str):
+ return self.run(query)
+
+ def __call__(self, query: str):
+ return self.invoke(query)
\ No newline at end of file