Skip to content

Commit 8b7bcfc

Browse files
feat: Add SearxNG search tool for privacy-focused web searches
- Implement SearxNG search tool following PraisonAI patterns - Add comprehensive documentation with setup instructions - Include working examples for both tools and agents - Support configurable SearxNG URL and search parameters - Integrate with existing tools registry and documentation - Provide privacy-focused alternative to traditional search engines Addresses issue #337 Co-authored-by: MervinPraison <MervinPraison@users.noreply.github.com>
1 parent 41fa293 commit 8b7bcfc

7 files changed

Lines changed: 633 additions & 1 deletion

File tree

docs/tools/searxng.mdx

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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/).

docs/tools/tools.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ praisonai agents.yaml
404404
<Card title="DuckDuckGo Tools" icon="duck" href="/tools/duckduckgo_tools">
405405
Web search functionality using DuckDuckGo's API
406406
</Card>
407+
<Card title="SearxNG Tools" icon="search" href="/tools/searxng">
408+
Privacy-focused web search using local SearxNG instance
409+
</Card>
407410
<Card title="Calculator Tools" icon="calculator" href="/tools/calculator_tools">
408411
Perform mathematical calculations and conversions
409412
</Card>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
"""
3+
SearxNG Search Agent Example
4+
5+
A simple example showing how to create an agent that uses SearxNG for web searches.
6+
This provides privacy-focused search capabilities as an alternative to traditional search engines.
7+
8+
Prerequisites:
9+
1. Install dependencies: pip install requests
10+
2. Run SearxNG: docker run -d --name searxng -p 32768:8080 searxng/searxng
11+
"""
12+
13+
from praisonaiagents import Agent, Task
14+
from praisonaiagents.tools import searxng_search
15+
16+
def main():
17+
"""Create and run a SearxNG-enabled search agent"""
18+
19+
# Create a search agent with SearxNG capabilities
20+
search_agent = Agent(
21+
name="Privacy Search Agent",
22+
instructions="""You are a privacy-focused search assistant that uses SearxNG to find information.
23+
24+
Your capabilities:
25+
- Search the web using SearxNG for privacy-focused results
26+
- Analyze and summarize search findings
27+
- Provide relevant links and sources
28+
- Respect user privacy by using local SearxNG instance
29+
30+
When searching:
31+
1. Use relevant keywords to get the best results
32+
2. Analyze the search results for quality and relevance
33+
3. Provide a clear summary with key points
34+
4. Include important URLs for further reading
35+
5. If search fails, explain the issue and suggest alternatives""",
36+
tools=[searxng_search]
37+
)
38+
39+
# Create search tasks
40+
tasks = [
41+
Task(
42+
description="Search for information about renewable energy technologies and provide a comprehensive summary",
43+
agent=search_agent
44+
),
45+
Task(
46+
description="Find the latest news about artificial intelligence developments in 2024",
47+
agent=search_agent
48+
),
49+
Task(
50+
description="Research privacy-focused alternatives to mainstream social media platforms",
51+
agent=search_agent
52+
)
53+
]
54+
55+
print("Privacy Search Agent - Using SearxNG")
56+
print("=" * 50)
57+
58+
# Execute each task
59+
for i, task in enumerate(tasks, 1):
60+
print(f"\n🔍 Task {i}: {task.description}")
61+
print("-" * 50)
62+
63+
try:
64+
result = task.execute()
65+
print(f"Result:\n{result}")
66+
except Exception as e:
67+
print(f"❌ Task failed: {e}")
68+
print("Make sure SearxNG is running at http://localhost:32768")
69+
70+
print("\n" + "=" * 50)
71+
72+
print("\n✅ All search tasks completed!")
73+
print("\nSearxNG Benefits:")
74+
print("• Privacy-focused: No tracking or data collection")
75+
print("• Multi-engine: Aggregates results from multiple sources")
76+
print("• Self-hosted: Full control over your search infrastructure")
77+
print("• Customizable: Configure engines and settings as needed")
78+
79+
if __name__ == "__main__":
80+
main()

0 commit comments

Comments
 (0)