~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
searxng draft
LearningCircuit3be40474d246
diff --git a/config.py b/config.pyindex 72c35581e8..b779487409 100644--- a/config.py+++ b/config.py@@ -41,7 +41,7 @@ import os# Load environment variablesload_dotenv()# Choose search tool: "serp" or "duckduckgo" (serp requires API key)-search_tool = "auto" # Change this variable to switch between search tools; for only local search "local-all"+search_tool = "searxng" # Change this variable to switch between search tools; for only local search "local-all"# LLM Configurationdiff --git a/docs/SearXNG-Setup.md b/docs/SearXNG-Setup.mdnew file mode 100644index 0000000000..1feafda738--- /dev/null+++ b/docs/SearXNG-Setup.md@@ -0,0 +1,115 @@+# SearXNG Integration for Local Deep Research++This document explains how to configure and use the SearXNG integration with Local Deep Research.++## Configuring SearXNG Access++The SearXNG search engine is **disabled by default** until you provide an instance URL. This ensures the system doesn't attempt to use public instances without explicit configuration.++### Setting Up Access++You have two ways to enable the SearXNG search engine:++1. **Environment Variable (Recommended)**:+ ```bash+ # Add to your .env file or set in your environment+ SEARXNG_INSTANCE=http://localhost:8080++ # Optional: Set custom delay between requests (in seconds)+ SEARXNG_DELAY=2.0+ ```++2. **Configuration Parameter**: Add to your `config.py`:+ ```python+ # In config.py+ SEARXNG_CONFIG = {+ "instance_url": "http://localhost:8080",+ "delay_between_requests": 2.0+ }+ ```++## Self-Hosting SearXNG (Recommended)++For the most ethical usage, we strongly recommend self-hosting your own SearXNG instance:++### Using Docker (easiest method)++```bash+# Pull the SearXNG Docker image+docker pull searxng/searxng++# Run SearXNG (will be available at http://localhost:8080)+docker run -d -p 8080:8080 --name searxng searxng/searxng+```++### Using Docker Compose (recommended for production)++1. Create a file named `docker-compose.yml` with the following content:++```yaml+version: '3'+services:+ searxng:+ container_name: searxng+ image: searxng/searxng+ ports:+ - "8080:8080"+ volumes:+ - ./searxng:/etc/searxng+ environment:+ - SEARXNG_BASE_URL=http://localhost:8080/+ restart: unless-stopped+```++2. Run with Docker Compose:++```bash+docker-compose up -d+```++## Using Public Instances++If you must use a public instance:++1. **Get Permission**: Always contact the administrator of any public instance+2. **Respect Resources**: Use a longer delay (4-5 seconds minimum) between requests+3. **Limited Usage**: Keep your research volume reasonable++Example configuration for a public instance:+```bash+SEARXNG_INSTANCE=https://instance.example.com+SEARXNG_DELAY=5.0+```++## Checking Configuration++To verify if SearXNG is properly configured:++```python+from web_search_engines.search_engine_factory import create_search_engine++# Create the engine+engine = create_search_engine("searxng")++# Check if available+if engine and hasattr(engine, 'is_available') and engine.is_available:+ print(f"SearXNG configured with instance: {engine.instance_url}")+ print(f"Delay between requests: {engine.delay_between_requests} seconds")+else:+ print("SearXNG is not properly configured or is disabled")+```++## Troubleshooting++If you encounter errors:++1. Check that your instance is running+2. Verify the URL is correct in your environment variables+3. Ensure you can access the instance in your browser+4. Check firewall settings and network connectivity++## Resources++- [SearXNG Documentation](https://searxng.github.io/searxng/)+- [SearXNG GitHub Repository](https://github.com/searxng/searxng)+- [SearXNG Docker Hub](https://hub.docker.com/r/searxng/searxng)\ No newline at end of filediff --git a/web_search_engines/engines/search_engine_searxng.py b/web_search_engines/engines/search_engine_searxng.pynew file mode 100644index 0000000000..90119445af--- /dev/null+++ b/web_search_engines/engines/search_engine_searxng.py@@ -0,0 +1,365 @@+import requests+import logging+import os+from typing import Dict, List, Any, Optional+from langchain_core.language_models import BaseLLM+import time+import json++from web_search_engines.search_engine_base import BaseSearchEngine+from web_search_engines.engines.full_search import FullSearchResults+import config++# Setup logging+logging.basicConfig(level=logging.INFO)+logger = logging.getLogger(__name__)++class SearXNGSearchEngine(BaseSearchEngine):+ """+ SearXNG search engine implementation that requires an instance URL provided via+ environment variable or configuration. Designed for ethical usage with proper+ rate limiting and single-instance approach.+ """++ def __init__(self,+ max_results: int = 15,+ instance_url: Optional[str] = None, # Can be None if using env var+ categories: Optional[List[str]] = None,+ engines: Optional[List[str]] = None,+ language: str = "en",+ safe_search: int = 1,+ time_range: Optional[str] = None,+ delay_between_requests: float = 2.0,+ llm: Optional[BaseLLM] = None,+ max_filtered_results: Optional[int] = None,+ include_full_content: bool = True,+ api_key: Optional[str] = None): # API key is actually the instance URL+ """+ Initialize the SearXNG search engine with ethical usage patterns.++ Args:+ max_results: Maximum number of search results+ instance_url: URL of your SearXNG instance (preferably self-hosted)+ categories: List of SearXNG categories to search in (general, images, videos, news, etc.)+ engines: List of engines to use (google, bing, duckduckgo, etc.)+ language: Language code for search results+ safe_search: Safe search level (0=off, 1=moderate, 2=strict)+ time_range: Time range for results (day, week, month, year)+ delay_between_requests: Seconds to wait between requests+ llm: Language model for relevance filtering+ max_filtered_results: Maximum number of results to keep after filtering+ include_full_content: Whether to include full webpage content in results+ api_key: Alternative way to provide instance URL (takes precedence over instance_url)+ """+ # Initialize the BaseSearchEngine with the LLM and max_filtered_results+ super().__init__(llm=llm, max_filtered_results=max_filtered_results)++ # Get instance URL from various sources in priority order:+ # 1. api_key parameter (which is actually the instance URL)+ # 2. SEARXNG_INSTANCE environment variable+ # 3. instance_url parameter+ # 4. Default to None, which will disable the engine+ self.instance_url = api_key or os.getenv("SEARXNG_INSTANCE") or instance_url++ # Validate and normalize the instance URL if provided+ if self.instance_url:+ self.instance_url = self.instance_url.rstrip('/')+ self.is_available = True+ else:+ self.is_available = False+ logger.warning("No SearXNG instance URL provided. The engine is disabled. "+ "Set SEARXNG_INSTANCE environment variable or provide instance_url parameter.")++ self.max_results = max_results+ self.categories = categories or ["general"]+ self.engines = engines+ self.language = language+ self.safe_search = safe_search+ self.time_range = time_range++ # Get delay from env var if provided, otherwise use parameter+ self.delay_between_requests = float(os.getenv("SEARXNG_DELAY", delay_between_requests))++ self.include_full_content = include_full_content++ # Construct search URL if instance is available+ if self.is_available:+ self.search_url = f"{self.instance_url}/search"+ logger.info(f"SearXNG engine initialized with instance: {self.instance_url}")+ logger.info(f"Rate limiting set to {self.delay_between_requests} seconds between requests")++ # Initialize FullSearchResults for content retrieval+ self.full_search = FullSearchResults(+ llm=llm,+ web_search=self,+ language=language,+ max_results=max_results,+ region="wt-wt",+ time="y",+ safesearch="Moderate" if safe_search == 1 else "Off" if safe_search == 0 else "Strict"+ )++ # Track last request time for rate limiting+ self.last_request_time = 0++ def _respect_rate_limit(self):+ """Apply self-imposed rate limiting between requests"""+ current_time = time.time()+ time_since_last_request = current_time - self.last_request_time++ # If we haven't waited long enough since the last request+ if time_since_last_request < self.delay_between_requests:+ # Calculate how much longer we need to wait+ wait_time = self.delay_between_requests - time_since_last_request+ logger.info(f"Rate limiting: waiting {wait_time:.2f} seconds")+ time.sleep(wait_time)++ # Update the last request time+ self.last_request_time = time.time()++ def _get_search_results(self, query: str) -> List[Dict[str, Any]]:+ """+ Get search results from SearXNG with ethical rate limiting.++ Args:+ query: The search query++ Returns:+ List of search results from SearXNG+ """+ # If the engine is disabled, return empty results+ if not self.is_available:+ logger.warning("SearXNG engine is disabled (no instance URL provided)")+ return []++ try:+ # Respect rate limits+ self._respect_rate_limit()++ # Basic parameters+ params = {+ "q": query,+ "categories": ",".join(self.categories),+ "language": self.language,+ "format": "json",+ "pageno": 1,+ "safesearch": self.safe_search,+ "count": self.max_results+ }++ # Add optional parameters if provided+ if self.engines:+ params["engines"] = ",".join(self.engines)++ if self.time_range:+ params["time_range"] = self.time_range++ # Basic browser-like headers+ headers = {+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",+ "Accept": "application/json",+ }++ # Send request+ logger.info(f"Sending request to SearXNG instance at {self.instance_url}")+ response = requests.get(+ self.search_url,+ params=params,+ headers=headers,+ timeout=10+ )++ # Check response+ if response.status_code == 200:+ try:+ data = response.json()+ results = data.get("results", [])+ logger.info(f"SearXNG returned {len(results)} results")+ return results+ except json.JSONDecodeError:+ logger.error("Failed to decode JSON response from SearXNG")+ return []+ else:+ logger.error(f"SearXNG returned status code {response.status_code}")+ return []++ except Exception as e:+ logger.error(f"Error getting SearXNG results: {e}")+ return []++ def _get_previews(self, query: str) -> List[Dict[str, Any]]:+ """+ Get preview information for SearXNG search results.++ Args:+ query: The search query++ Returns:+ List of preview dictionaries+ """+ # If the engine is disabled, return empty results+ if not self.is_available:+ logger.warning("SearXNG engine is disabled (no instance URL provided)")+ return []++ logger.info(f"Getting SearXNG previews for query: {query}")++ # Get raw search results+ results = self._get_search_results(query)++ if not results:+ logger.warning(f"No SearXNG results found for query: {query}")+ return []++ # Format results as previews+ previews = []+ for i, result in enumerate(results):+ # Extract relevant fields+ title = result.get("title", "")+ url = result.get("url", "")+ content = result.get("content", "")++ preview = {+ "id": url or f"searxng-result-{i}",+ "title": title,+ "link": url,+ "snippet": content,+ "engine": result.get("engine", ""),+ "category": result.get("category", "")+ }++ previews.append(preview)++ return previews++ def _get_full_content(self, relevant_items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """+ Get full content for the relevant search results.++ Args:+ relevant_items: List of relevant preview dictionaries++ Returns:+ List of result dictionaries with full content+ """+ # If the engine is disabled, return input items unchanged+ if not self.is_available:+ return relevant_items++ # Check if we should get full content+ if hasattr(config, 'SEARCH_SNIPPETS_ONLY') and config.SEARCH_SNIPPETS_ONLY:+ logger.info("Snippet-only mode, skipping full content retrieval")+ return relevant_items++ # Get full content using FullSearchResults+ logger.info("Retrieving full webpage content")++ try:+ # Use FullSearchResults to get full content+ results_with_content = self.full_search._get_full_content(relevant_items)+ return results_with_content++ except Exception as e:+ logger.error(f"Error retrieving full content: {e}")+ # Fall back to returning the items without full content+ return relevant_items++ def invoke(self, query: str) -> List[Dict[str, Any]]:+ """Compatibility method for LangChain tools"""+ return self.run(query)++ def results(self, query: str, max_results: Optional[int] = None) -> List[Dict[str, Any]]:+ """+ Get search results in a format compatible with other search engines.++ Args:+ query: The search query+ max_results: Optional override for maximum results++ Returns:+ List of search result dictionaries+ """+ # If the engine is disabled, return empty results+ if not self.is_available:+ return []++ # Save current max_results+ original_max_results = self.max_results++ try:+ # Override max_results if provided+ if max_results is not None:+ self.max_results = max_results++ # Get raw search results+ results = self._get_search_results(query)++ # Format results+ formatted_results = []+ for result in results:+ formatted_results.append({+ "title": result.get("title", ""),+ "link": result.get("url", ""),+ "snippet": result.get("content", "")+ })++ return formatted_results++ finally:+ # Restore original max_results+ self.max_results = original_max_results++ @staticmethod+ def get_self_hosting_instructions() -> str:+ """+ Get instructions for self-hosting a SearXNG instance.++ Returns:+ String with installation instructions+ """+ return """+# SearXNG Self-Hosting Instructions++The most ethical way to use SearXNG is to host your own instance. Here's how:++## Using Docker (easiest method)++1. Install Docker if you don't have it already+2. Run these commands:++```bash+# Pull the SearXNG Docker image+docker pull searxng/searxng++# Run SearXNG (will be available at http://localhost:8080)+docker run -d -p 8080:8080 --name searxng searxng/searxng+```++## Using Docker Compose (recommended for production)++1. Create a file named `docker-compose.yml` with the following content:++```yaml+version: '3'+services:+ searxng:+ container_name: searxng+ image: searxng/searxng+ ports:+ - "8080:8080"+ volumes:+ - ./searxng:/etc/searxng+ environment:+ - SEARXNG_BASE_URL=http://localhost:8080/+ restart: unless-stopped+```++2. Run with Docker Compose:++```bash+docker-compose up -d+```++For more detailed instructions and configuration options, visit:+https://searxng.github.io/searxng/admin/installation.html+"""\ No newline at end of filediff --git a/web_search_engines/search_engines_config.py b/web_search_engines/search_engines_config.pyindex 1dae43f3f1..aee7ea9fcc 100644--- a/web_search_engines/search_engines_config.py+++ b/web_search_engines/search_engines_config.py@@ -33,7 +33,30 @@ SEARCH_ENGINES = {"sort_order": "descending"}},-++ # SearXNG search engine (using "API key" for instance URL)+ "searxng": {+ "module_path": "web_search_engines.engines.search_engine_searxng",+ "class_name": "SearXNGSearchEngine",+ "requires_api_key": True, # Changed to True to indicate requirement+ "api_key_env": "SEARXNG_INSTANCE", # Environment variable contains instance URL+ "reliability": 0.7,+ "strengths": ["privacy-focused", "metasearch capability", "no tracking",+ "combines multiple engines", "ethical usage", "respects rate limits"],+ "weaknesses": ["requires self-hosted instance", "disabled without configuration"],+ "default_params": {+ "max_results": 15,+ "instance_url": None, # Default to None, will use env var or be disabled+ "categories": ["general"],+ "engines": None,+ "language": "en",+ "safe_search": 1,+ "time_range": None,+ "delay_between_requests": 2.0, # Respectful rate limiting+ "include_full_content": True+ },+ "supports_full_search": True+ },# GitHub search engine"github": {"module_path": "web_search_engines.engines.search_engine_github",