|
| 1 | +--- |
| 2 | +title: "SearxNG Search" |
| 3 | +description: "Search the web using your local SearxNG instance for privacy-focused web searches" |
| 4 | +--- |
| 5 | + |
| 6 | +# SearxNG Search Tool |
| 7 | + |
| 8 | +The SearxNG search tool allows you to perform web searches using your local SearxNG instance, providing privacy-focused search capabilities as an alternative to traditional search engines like Google or DuckDuckGo. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +SearxNG is a privacy-respecting metasearch engine that aggregates results from multiple search engines without storing your data. This tool integrates with your local SearxNG instance to provide secure, private web searches for your AI agents. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +First, install the required dependency: |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install requests |
| 20 | +``` |
| 21 | + |
| 22 | +You'll also need a running SearxNG instance. You can set it up using Docker: |
| 23 | + |
| 24 | +```bash |
| 25 | +# Run SearxNG locally on port 32768 |
| 26 | +docker run -d --name searxng -p 32768:8080 searxng/searxng |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Basic Usage |
| 32 | + |
| 33 | +```python |
| 34 | +from praisonaiagents.tools import searxng_search |
| 35 | + |
| 36 | +# Basic search |
| 37 | +results = searxng_search("AI news") |
| 38 | + |
| 39 | +# With custom parameters |
| 40 | +results = searxng_search( |
| 41 | + query="Python programming", |
| 42 | + max_results=10, |
| 43 | + searxng_url="http://localhost:32768/search" |
| 44 | +) |
| 45 | + |
| 46 | +# Display results |
| 47 | +for result in results: |
| 48 | + print(f"Title: {result['title']}") |
| 49 | + print(f"URL: {result['url']}") |
| 50 | + print(f"Snippet: {result['snippet']}") |
| 51 | + print("-" * 50) |
| 52 | +``` |
| 53 | + |
| 54 | +### Using the Alias |
| 55 | + |
| 56 | +```python |
| 57 | +from praisonaiagents.tools import searxng |
| 58 | + |
| 59 | +# Same functionality as searxng_search |
| 60 | +results = searxng("machine learning tutorials") |
| 61 | +``` |
| 62 | + |
| 63 | +### Integration with Agents |
| 64 | + |
| 65 | +```python |
| 66 | +from praisonaiagents import Agent, Task |
| 67 | +from praisonaiagents.tools import searxng_search |
| 68 | + |
| 69 | +# Create an agent with SearxNG search capability |
| 70 | +search_agent = Agent( |
| 71 | + name="Search Agent", |
| 72 | + instructions="You are a search assistant that helps users find information using SearxNG.", |
| 73 | + tools=[searxng_search] |
| 74 | +) |
| 75 | + |
| 76 | +# Create a task |
| 77 | +task = Task( |
| 78 | + description="Search for the latest developments in artificial intelligence", |
| 79 | + agent=search_agent |
| 80 | +) |
| 81 | + |
| 82 | +# Execute the task |
| 83 | +result = task.execute() |
| 84 | +print(result) |
| 85 | +``` |
| 86 | + |
| 87 | +## Function Parameters |
| 88 | + |
| 89 | +### `searxng_search(query, max_results=5, searxng_url=None)` |
| 90 | + |
| 91 | +**Parameters:** |
| 92 | + |
| 93 | +- `query` (str, required): The search query string |
| 94 | +- `max_results` (int, optional): Maximum number of results to return. Default: 5 |
| 95 | +- `searxng_url` (str, optional): URL of your SearxNG instance. Default: "http://localhost:32768/search" |
| 96 | + |
| 97 | +**Returns:** |
| 98 | + |
| 99 | +- `List[Dict]`: List of search results, each containing: |
| 100 | + - `title`: The title of the search result |
| 101 | + - `url`: The URL of the search result |
| 102 | + - `snippet`: A brief snippet/description of the content |
| 103 | + - `error`: Error message if the search fails |
| 104 | + |
| 105 | +## Configuration |
| 106 | + |
| 107 | +### Environment Variables |
| 108 | + |
| 109 | +You can set a default SearxNG URL using environment variables: |
| 110 | + |
| 111 | +```python |
| 112 | +import os |
| 113 | + |
| 114 | +# Set default SearxNG URL |
| 115 | +os.environ['SEARXNG_URL'] = 'http://my-searxng-server:8080/search' |
| 116 | + |
| 117 | +# The tool will use this URL if none is provided |
| 118 | +results = searxng_search("AI research") |
| 119 | +``` |
| 120 | + |
| 121 | +### SearxNG Setup |
| 122 | + |
| 123 | +For production use, consider: |
| 124 | + |
| 125 | +1. **Custom SearxNG Configuration**: Configure engines, categories, and settings |
| 126 | +2. **Authentication**: Set up authentication if needed |
| 127 | +3. **Rate Limiting**: Configure appropriate rate limits |
| 128 | +4. **SSL/TLS**: Use HTTPS for secure connections |
| 129 | + |
| 130 | +## Advanced Usage |
| 131 | + |
| 132 | +### Multi-Agent Search System |
| 133 | + |
| 134 | +```python |
| 135 | +from praisonaiagents import Agent, Task, Process |
| 136 | +from praisonaiagents.tools import searxng_search |
| 137 | + |
| 138 | +# Research agent |
| 139 | +researcher = Agent( |
| 140 | + name="Researcher", |
| 141 | + instructions="Search for academic and technical information", |
| 142 | + tools=[searxng_search] |
| 143 | +) |
| 144 | + |
| 145 | +# News agent |
| 146 | +news_agent = Agent( |
| 147 | + name="News Agent", |
| 148 | + instructions="Search for latest news and current events", |
| 149 | + tools=[searxng_search] |
| 150 | +) |
| 151 | + |
| 152 | +# Tasks |
| 153 | +research_task = Task( |
| 154 | + description="Find recent research papers on quantum computing", |
| 155 | + agent=researcher |
| 156 | +) |
| 157 | + |
| 158 | +news_task = Task( |
| 159 | + description="Find latest news about AI developments", |
| 160 | + agent=news_agent |
| 161 | +) |
| 162 | + |
| 163 | +# Process |
| 164 | +process = Process( |
| 165 | + agents=[researcher, news_agent], |
| 166 | + tasks=[research_task, news_task] |
| 167 | +) |
| 168 | + |
| 169 | +result = process.run() |
| 170 | +``` |
| 171 | + |
| 172 | +### Custom SearxNG Configuration |
| 173 | + |
| 174 | +```python |
| 175 | +from praisonaiagents.tools import searxng_search |
| 176 | + |
| 177 | +# Search with specific SearxNG instance |
| 178 | +results = searxng_search( |
| 179 | + query="sustainable energy", |
| 180 | + max_results=15, |
| 181 | + searxng_url="https://my-private-searxng.com/search" |
| 182 | +) |
| 183 | +``` |
| 184 | + |
| 185 | +## Error Handling |
| 186 | + |
| 187 | +The tool includes comprehensive error handling: |
| 188 | + |
| 189 | +```python |
| 190 | +results = searxng_search("test query") |
| 191 | + |
| 192 | +for result in results: |
| 193 | + if "error" in result: |
| 194 | + print(f"Search error: {result['error']}") |
| 195 | + else: |
| 196 | + print(f"Found: {result['title']}") |
| 197 | +``` |
| 198 | + |
| 199 | +## Common Error Messages |
| 200 | + |
| 201 | +- **Connection Error**: "Could not connect to SearxNG at {url}. Ensure SearxNG is running." |
| 202 | +- **Timeout Error**: "SearxNG search request timed out" |
| 203 | +- **Missing Dependency**: "SearxNG search requires requests package. Install with: pip install requests" |
| 204 | +- **Parsing Error**: "Error parsing SearxNG response: {error}" |
| 205 | + |
| 206 | +## Best Practices |
| 207 | + |
| 208 | +1. **Local Instance**: Use a local SearxNG instance for better privacy and performance |
| 209 | +2. **Rate Limiting**: Be mindful of search frequency to avoid overwhelming your instance |
| 210 | +3. **Error Handling**: Always check for errors in the response |
| 211 | +4. **Custom URLs**: Use environment variables for different deployment environments |
| 212 | +5. **Result Validation**: Validate search results before using them in your application |
| 213 | + |
| 214 | +## Comparison with Other Search Tools |
| 215 | + |
| 216 | +| Feature | SearxNG | DuckDuckGo | Google | |
| 217 | +|---------|---------|------------|--------| |
| 218 | +| Privacy | ✅ High | ⚡ Medium | ❌ Low | |
| 219 | +| Local Control | ✅ Yes | ❌ No | ❌ No | |
| 220 | +| Multi-Engine | ✅ Yes | ❌ No | ❌ No | |
| 221 | +| Customization | ✅ High | ❌ Limited | ❌ None | |
| 222 | + |
| 223 | +## Troubleshooting |
| 224 | + |
| 225 | +### SearxNG Not Responding |
| 226 | + |
| 227 | +```bash |
| 228 | +# Check if SearxNG is running |
| 229 | +curl http://localhost:32768/search?q=test&format=json |
| 230 | + |
| 231 | +# Restart SearxNG container |
| 232 | +docker restart searxng |
| 233 | +``` |
| 234 | + |
| 235 | +### Connection Issues |
| 236 | + |
| 237 | +```python |
| 238 | +# Test connection before using |
| 239 | +import requests |
| 240 | + |
| 241 | +try: |
| 242 | + response = requests.get("http://localhost:32768/search", timeout=5) |
| 243 | + print("SearxNG is accessible") |
| 244 | +except requests.exceptions.ConnectionError: |
| 245 | + print("Cannot connect to SearxNG") |
| 246 | +``` |
| 247 | + |
| 248 | +For more information about SearxNG setup and configuration, visit the [official SearxNG documentation](https://docs.searxng.org/). |
0 commit comments