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.

added full webpages as LLM context.

LearningCircuit0127ed970cf9
diff --git a/config.py b/config.py
index ab8f06a619..1d8c9cfe6a 100644
--- a/config.py
+++ b/config.py
@@ -3,6 +3,8 @@ from langchain_anthropic import ChatAnthropic
from langchain_openai import ChatOpenAI
from langchain_ollama import ChatOllama
from dotenv import load_dotenv
+from full_duck_duck_go_search_results import FullDuckDuckGoSearchResults
+
import os
# Load environment variables
@@ -14,11 +16,11 @@ DEFAULT_TEMPERATURE = 0.7
MAX_TOKENS = 2000
# Search Configuration
-MAX_SEARCH_RESULTS = 40
+MAX_SEARCH_RESULTS = 3 # carefull if snippets are turned of it loads all full pages into your LLM context. = slow
SEARCH_REGION = "us-en"
TIME_PERIOD = "y"
SAFE_SEARCH = True
-
+SEARCH_SNIPPETS_ONLY=False # both have advantages and disadvantages
# Output Configuration
@@ -60,12 +62,17 @@ def get_llm(model_name=DEFAULT_MODEL, temperature=DEFAULT_TEMPERATURE):
)
def get_search():
- """Get search tool instance"""
- return DuckDuckGoSearchResults(
- max_results=MAX_SEARCH_RESULTS,
- region=SEARCH_REGION,
- time=TIME_PERIOD,
- safesearch=SAFE_SEARCH
- )
-
+ if SEARCH_SNIPPETS_ONLY:
+ """Get search tool instance"""
+ return DuckDuckGoSearchResults(
+ max_results=MAX_SEARCH_RESULTS,
+ region=SEARCH_REGION,
+ time=TIME_PERIOD,
+ safesearch=SAFE_SEARCH
+ )
+ else:
+ return FullDuckDuckGoSearchResults(max_results=MAX_SEARCH_RESULTS,
+ region=SEARCH_REGION,
+ time=TIME_PERIOD,
+ safesearch=SAFE_SEARCH)
diff --git a/full_duck_duck_go_search_results.py b/full_duck_duck_go_search_results.py
new file mode 100644
index 0000000000..f31607d978
--- /dev/null
+++ b/full_duck_duck_go_search_results.py
@@ -0,0 +1,105 @@
+import justext
+from langchain_community.tools import DuckDuckGoSearchResults
+from langchain.document_loaders import AsyncChromiumLoader
+from langchain.document_transformers import BeautifulSoupTransformer
+
+class FullDuckDuckGoSearchResults:
+ """
+ A wrapper class that mimics the DuckDuckGoSearchResults interface.
+
+ Internally, it:
+ 1. Uses DuckDuckGoSearchResults to get search results (with URLs).
+ 2. Downloads the full HTML content for those URLs using AsyncChromiumLoader.
+ 3. Processes the HTML using BeautifulSoupTransformer.
+ 4. Applies justext to remove boilerplate from the extracted HTML.
+
+ Additional parameters:
+ - max_results: maximum number of search results to retrieve.
+ - region: search region (e.g., "us-en").
+ - time: time period filter (e.g., "d" for daily).
+ - safesearch: safe search setting (e.g., "Moderate").
+
+ From the outside, you simply call .invoke(query) (or use the instance as a callable)
+ to get a list of search result dictionaries augmented with a "full_content" field.
+ """
+
+ def __init__(
+ self,
+ output_format: str = "list",
+ language: str = "English",
+ max_results: int = 10,
+ region: str = "us-en",
+ time: str = "d",
+ safesearch: str = "Moderate"
+ ):
+ self.output_format = output_format
+ self.language = language
+
+ # Additional parameters for DuckDuckGo search
+ self.max_results = max_results
+ self.region = region
+ self.time = time
+ self.safesearch = safesearch
+
+ # Instantiate the DuckDuckGo search tool with extra parameters.
+ self.ddg_search = DuckDuckGoSearchResults(
+ output_format=output_format,
+ max_results=self.max_results,
+ region=self.region,
+ time=self.time,
+ safesearch=self.safesearch
+ )
+ # Instantiate the BeautifulSoup transformer (no parameters needed here)
+ self.bs_transformer = BeautifulSoupTransformer()
+ # Store the tags to extract to be used later in transform_documents.
+ self.tags_to_extract = ["p", "div", "span"]
+
+ def remove_boilerplate(self, html: str) -> str:
+ """
+ Uses justext to remove boilerplate from the given HTML.
+ Returns the cleaned text content.
+ """
+ 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):
+ # Step 1: Get search results from DuckDuckGo.
+ search_results = self.ddg_search.invoke(query)
+ if not isinstance(search_results, list):
+ raise ValueError("Expected the search results in list format.")
+
+ # Extract URLs from the search results.
+ urls = [result.get("link") for result in search_results if result.get("link")]
+ if not urls:
+ print("\n === NO LINKS===\n")
+ return search_results # Return as is if no URLs are found.
+
+ # Step 2: Download the full HTML pages for these URLs.
+ loader = AsyncChromiumLoader(urls)
+ html_docs = loader.load() # Load HTML content synchronously.
+
+ # Step 3: Process the HTML using BeautifulSoupTransformer.
+ # Here we pass the tags_to_extract parameter.
+ full_docs = self.bs_transformer.transform_documents(html_docs, tags_to_extract=self.tags_to_extract)
+
+ # Step 4: Remove boilerplate from each document using justext.
+ url_to_content = {}
+ for doc in full_docs:
+ 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 search result.
+ for result in search_results:
+ link = result.get("link")
+ result["full_content"] = url_to_content.get(link, None)
+ print("FULL SEARCH")
+ return search_results
+
+ def __call__(self, query: str):
+ return self.invoke(query)
+
diff --git a/requirements.txt b/requirements.txt
index 54d0171c7f..ba01872627 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,3 +5,5 @@ langchain-ollama>=0.2.3
duckduckgo_search>=7.3.2
python-dateutil>=2.9.0
typing_extensions>=4.12.2
+justext
+playwright
diff --git a/utilities.py b/utilities.py
index d08dc960b8..bf460f71f2 100644
--- a/utilities.py
+++ b/utilities.py
@@ -5,22 +5,28 @@ def remove_think_tags(text: str) -> str:
-def extract_links_from_search_results(search_results_str: str) -> list:
- """Extract links and titles from search results string"""
- links = []
- # Split the string by 'title:' and 'link:'
- parts = search_results_str.split("title: ")
+def extract_links_from_search_results(search_results: list) -> list:
+ """
+ Extracts links and titles from a list of search result dictionaries.
+
+ Each dictionary is expected to have at least the keys "title" and "link".
- for part in parts[1:]: # Skip first part before first 'title:'
+ Returns a list of dictionaries with 'title' and 'url' keys.
+ """
+ links = []
+ for result in search_results:
try:
- title = part.split(", link: ")[0]
- link = part.split(", link: ")[1].split(", snippet:")[0]
- links.append({
- "title": title.strip(),
- "url": link.strip()
- })
- except:
+ title = result.get("title", "").strip()
+ url = result.get("link", "").strip()
+ if title and url:
+ links.append({
+ "title": title,
+ "url": url
+ })
+ except Exception:
continue
+ return links
+
return links
def format_findings_to_text(findings_list, current_knowledge, questions_by_iteration):