Skip to content

Search Tools

enhancedtoolkits.searxng.EnhancedSearxngTools

EnhancedSearxngTools(host: str, max_results: Optional[int] = 10, timeout: int = 30, enable_content_fetching: Optional[bool] = False, enable_file_downloads: Optional[bool] = True, max_file_size_mb: int = 10, file_download_timeout: int = 60, byparr_enabled: Optional[bool] = True, add_instructions: bool = True, **kwargs)

Bases: StrictToolkit

Enhanced SearxNG Tools v2.0

A production-ready search toolkit with optional content fetching, robust error handling, and comprehensive search capabilities.

Parameters:

Name Type Description Default
host str

SearxNG instance URL

required
max_results Optional[int]

Maximum number of search results to return

10
timeout int

Request timeout in seconds

30
enable_content_fetching Optional[bool]

Whether to fetch full content from URLs

False
enable_file_downloads Optional[bool]

Whether to download and process files (PDF, TXT, MD)

True
max_file_size_mb int

Maximum file size to download in MB

10
file_download_timeout int

Timeout for file downloads in seconds

60
byparr_enabled Optional[bool]

Override for Byparr usage (None = auto-detect)

True
Source code in src/enhancedtoolkits/searxng.py
def __init__(
    self,
    host: str,
    max_results: Optional[int] = 10,
    timeout: int = 30,
    enable_content_fetching: Optional[bool] = False,
    enable_file_downloads: Optional[bool] = True,
    max_file_size_mb: int = 10,
    file_download_timeout: int = 60,
    byparr_enabled: Optional[bool] = True,
    add_instructions: bool = True,
    **kwargs,
):
    """
    Initialize Enhanced SearxNG Tools.

    Args:
        host: SearxNG instance URL
        max_results: Maximum number of search results to return
        timeout: Request timeout in seconds
        enable_content_fetching: Whether to fetch full content from URLs
        enable_file_downloads: Whether to download and process files (PDF, TXT, MD)
        max_file_size_mb: Maximum file size to download in MB
        file_download_timeout: Timeout for file downloads in seconds
        byparr_enabled: Override for Byparr usage (None = auto-detect)
    """
    if not host:
        raise ValueError("Invalid SearxNG host URL")

    # Configuration
    self.host = self._validate_host(host)
    self.max_results = max(1, min(30, max_results or 10))  # Limit between 1-30
    self.timeout = max(5, min(120, timeout))  # Limit between 5-120 seconds
    self.enable_content_fetching = enable_content_fetching
    self.enable_file_downloads = enable_file_downloads
    self.supported_file_types = ["pdf", "txt", "md"]
    self.max_file_size_mb = max(
        1, min(500, max_file_size_mb)
    )  # Limit between 1-500 MB
    self.file_download_timeout = max(
        10, min(300, file_download_timeout)
    )  # 10-300 seconds
    self.add_instructions = add_instructions
    self.instructions = EnhancedSearxngTools.get_llm_usage_instructions()

    super().__init__(name="enhanced_searxng_tools", **kwargs)

    # Byparr configuration (optional)
    if byparr_enabled is not None:
        self.byparr_enabled = byparr_enabled
    else:
        self.byparr_enabled = BYPARR_ENABLED and self.enable_content_fetching

    # HTTP client configuration
    self.client = httpx.Client(
        timeout=httpx.Timeout(self.timeout),
        follow_redirects=True,
        headers={"User-Agent": "Enhanced-SearxNG-Tools/2.0 (Python/httpx)"},
    )

    # File download client configuration (separate timeout)
    if self.enable_file_downloads:
        self.file_client = httpx.Client(
            timeout=httpx.Timeout(self.file_download_timeout),
            follow_redirects=True,
            headers=self._get_file_download_headers(),
        )
    else:
        self.file_client = None

    # Register search methods
    self.register(self.perform_web_search)
    self.register(self.perform_news_search)
    self.register(self.perform_image_search)
    self.register(self.perform_video_search)
    self.register(self.perform_category_search)

    log_info(
        f"Enhanced SearxNG Tools initialized - Host: {self.host}, Max Results: {self.max_results}, "
        f"Content Fetching: {self.enable_content_fetching}, File Downloads: {self.enable_file_downloads}, "
        f"Supported Files: {self.supported_file_types}, Byparr: {self.byparr_enabled}"
    )

Functions

perform_web_search(query: str, max_results: Optional[int] = None) -> str

Perform a web search using SearxNG.

Parameters:

Name Type Description Default
query str

Search query string

required
max_results Optional[int]

Maximum number of results (overrides default)

None

Returns:

Type Description
str

JSON string containing search results

Raises:

Type Description
SearxngSearchError

If search fails

Source code in src/enhancedtoolkits/searxng.py
def perform_web_search(self, query: str, max_results: Optional[int] = None) -> str:
    """
    Perform a web search using SearxNG.

    Args:
        query: Search query string
        max_results: Maximum number of results (overrides default)

    Returns:
        JSON string containing search results

    Raises:
        SearxngSearchError: If search fails
    """
    return self._search(query, category="general", max_results=max_results)
perform_news_search(query: str, max_results: Optional[int] = None) -> str

Perform a news search using SearxNG.

Parameters:

Name Type Description Default
query str

Search query string

required
max_results Optional[int]

Maximum number of results (overrides default)

None

Returns:

Type Description
str

JSON string containing news search results

Raises:

Type Description
SearxngSearchError

If search fails

Source code in src/enhancedtoolkits/searxng.py
def perform_news_search(self, query: str, max_results: Optional[int] = None) -> str:
    """
    Perform a news search using SearxNG.

    Args:
        query: Search query string
        max_results: Maximum number of results (overrides default)

    Returns:
        JSON string containing news search results

    Raises:
        SearxngSearchError: If search fails
    """
    return self._search(query, category="news", max_results=max_results)
perform_image_search(query: str, max_results: Optional[int] = None) -> str

Perform an image search using SearxNG.

Parameters:

Name Type Description Default
query str

Search query string

required
max_results Optional[int]

Maximum number of results (overrides default)

None

Returns:

Type Description
str

JSON string containing image search results

Raises:

Type Description
SearxngSearchError

If search fails

Source code in src/enhancedtoolkits/searxng.py
def perform_image_search(self, query: str, max_results: Optional[int] = None) -> str:
    """
    Perform an image search using SearxNG.

    Args:
        query: Search query string
        max_results: Maximum number of results (overrides default)

    Returns:
        JSON string containing image search results

    Raises:
        SearxngSearchError: If search fails
    """
    return self._search(query, category="images", max_results=max_results)
perform_video_search(query: str, max_results: Optional[int] = None) -> str

Perform a video search using SearxNG.

Parameters:

Name Type Description Default
query str

Search query string

required
max_results Optional[int]

Maximum number of results (overrides default)

None

Returns:

Type Description
str

JSON string containing video search results

Raises:

Type Description
SearxngSearchError

If search fails

Source code in src/enhancedtoolkits/searxng.py
def perform_video_search(self, query: str, max_results: Optional[int] = None) -> str:
    """
    Perform a video search using SearxNG.

    Args:
        query: Search query string
        max_results: Maximum number of results (overrides default)

    Returns:
        JSON string containing video search results

    Raises:
        SearxngSearchError: If search fails
    """
    return self._search(query, category="videos", max_results=max_results)
perform_category_search(query: str, category: str, max_results: Optional[int] = None) -> str

Perform a search in a specific category using SearxNG.

Parameters:

Name Type Description Default
query str

Search query string

required
category str

Search category (general, news, images, videos, etc.)

required
max_results Optional[int]

Maximum number of results (overrides default)

None

Returns:

Type Description
str

JSON string containing search results

Raises:

Type Description
SearxngSearchError

If search fails or category is invalid

Source code in src/enhancedtoolkits/searxng.py
def perform_category_search(
    self, query: str, category: str, max_results: Optional[int] = None
) -> str:
    """
    Perform a search in a specific category using SearxNG.

    Args:
        query: Search query string
        category: Search category (general, news, images, videos, etc.)
        max_results: Maximum number of results (overrides default)

    Returns:
        JSON string containing search results

    Raises:
        SearxngSearchError: If search fails or category is invalid
    """
    if category not in self.SUPPORTED_CATEGORIES:
        available_categories = ", ".join(self.SUPPORTED_CATEGORIES.keys())
        raise SearxngSearchError(
            f"Unsupported category '{category}'. Available categories: {available_categories}"
        )

    return self._search(query, category=category, max_results=max_results)

get_llm_usage_instructions staticmethod

get_llm_usage_instructions() -> str

Returns a set of detailed instructions for LLMs on how to use each tool in EnhancedSearxngTools. Each instruction includes the method name, description, parameters, types, and example values.

Source code in src/enhancedtoolkits/searxng.py
    @staticmethod
    def get_llm_usage_instructions() -> str:
        """
        Returns a set of detailed instructions for LLMs on how to use each tool in EnhancedSearxngTools.
        Each instruction includes the method name, description, parameters, types, and example values.
        """
        instructions = """
<internet_search_tools_instructions>
*** Web Search Tools Instructions using SearxNG ***

These tools enable you to perform various web searches and manage the results. They provide accurate, real-time information with improved file handling capabilities.

**General Instructions:**
- All methods return a dictionary with 'results' (list) and 'status' (str) keys.
- Each result is a dictionary containing relevant data based on the search type.
- The 'file_type' field will be present if supported file types are detected in URLs.
- If errors occur, an error message will be returned under the 'error' key.

### Functions Tools

1. **perform_web_search**: Perform a web search.
   - Parameters:
     - `query` (str): Search term or phrase, e.g., "climate change impacts".
     - `max_results` (int, optional): Maximum results to return (default: 5, range: 1-30).
   - *Example:* `perform_web_search("python best practices", max_results=7)`

2. **perform_news_search**: Perform a news search.
   - Parameters:
     - `query` (str): Search term or phrase, e.g., "AI in healthcare".
     - `max_results` (int, optional): Maximum results to return (default: 5, range: 1-30).
   - *Example:* `perform_news_search("space exploration", max_results=5)`

3. **perform_image_search**: Perform an image search.
   - Parameters:
     - `query` (str): Search term or phrase, e.g., "coffee shop interior".
     - `max_results` (int, optional): Maximum results to return (default: 5, range: 1-30).
   - *Example:* `perform_image_search("dog breeds")`

4. **perform_video_search**: Perform a video search.
   - Parameters:
     - `query` (str): Search term or phrase, e.g., "language learning".
     - `max_results` (int, optional): Maximum results to return (default: 5, range: 1-30).
   - *Example:* `perform_video_search("language learning", max_results=8)`

5. **perform_category_search**: Search within a specific category.
   - Parameters:
     - `query` (str): Search term or phrase, e.g., "quantum computing advancements".
     - `category` (str): One of: general, news, images, videos, music, files, science, social, or it.
     - `max_results` (int, optional): Maximum results to return (default: 5, range: 1-30).
   - *Example:* `perform_category_search("cybersecurity trends", category="it")`

**File Processing Features:**
- Automatically detects PDF, TXT, and Markdown files in search results.
- Extracts readable content from PDFs using MarkItDown.
- Preserves formatting when downloading Markdown files.
- Handles TXT file downloads with proper encoding detection.

**Configuration Options:**
- Max file download size (default: 50MB)
- Download timeout protection
- Enable/disable URL content fetching and file downloads

**Additional Notes:**
- Ensure the category parameter in `perform_category_search` matches one of the accepted values.
- File processing is automatically applied when supported file types are detected in URLs.

**Error Handling:**
If any errors occur during execution (e.g., invalid parameters, connectivity issues), appropriate error messages will be returned under the 'error' key to guide troubleshooting.

</internet_search_tools_instructions>
"""
        return instructions