Spaces:
Sleeping
Sleeping
Joachim M. Vis commited on
Upload 15 files
Browse filesFiles changed (3)
- docs/enhanced_web_search.mdAdded+299
- docs/examples.md+62
- docs/index.md+2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299# Enhanced Web Search
The Enhanced Web Search system provides a comprehensive three-tier approach to web search, intelligently routing queries to the most appropriate search tools based on query type and available resources. This system is designed to handle a wide range of search needs, from simple factual queries to complex reasoning questions.
## Overview
The Enhanced Web Search system consists of three specialized search tools:
1. **BrowserSearchTool** - For direct browser-based searches of any website
2. **LibrarySearchTool** - For searches using imported libraries like arXiv and DuckDuckGo
3. **ApiSearchTool** - For searches using API services like Perplexity and Serper
These tools work together in a fallback chain, ensuring that if one tool fails or is unavailable, the system can fall back to another tool. This approach provides robust search capabilities that can adapt to different environments and query types.
## Three-Tier Search Approach
### 1. BrowserSearchTool
The BrowserSearchTool enables direct browser-based searches of any website using the browser_action capability.
**Purpose:**
- Directly interact with websites through a browser interface
- Access content that may be difficult to retrieve through APIs or libraries
- Provide visual context for search results
**Advantages:**
- Can access any website, including those without APIs
- Provides visual context and can interact with dynamic content
- Works with a wide range of websites without requiring API keys
**Use Cases:**
- Searching websites that don't have public APIs
- Accessing content behind login screens (with proper credentials)
- Interacting with dynamic web content
- Visual verification of search results
**Example:**
```python
from tools.web_tools import create_browser_search
# Create the browser search tool
browser_tool = create_browser_search()
# Search Wikipedia
results = browser_tool.search("quantum computing", source="wikipedia")
# The results include instructions for using browser_action
print(results[0]["instructions"])
```
### 2. LibrarySearchTool
The LibrarySearchTool leverages imported Python libraries (DuckDuckGo and arXiv) for search operations, intelligently routing queries to the appropriate library based on query type.
**Purpose:**
- Use established Python libraries for search operations
- Provide specialized academic search capabilities
- Offer general web search without requiring API keys
**Advantages:**
- No API keys required
- Specialized academic search through arXiv
- General web search through DuckDuckGo
- Works offline with locally installed libraries
**Use Cases:**
- Academic research and paper searches
- General web searches when API keys aren't available
- Environments with limited internet access but installed libraries
**Example:**
```python
from tools.web_tools import create_library_search
# Create the library search tool
library_tool = create_library_search()
# For academic queries, it automatically uses arXiv
academic_results = library_tool.search("recent papers on quantum computing")
# For general queries, it automatically uses DuckDuckGo
general_results = library_tool.search("weather in New York")
```
### 3. ApiSearchTool
The ApiSearchTool uses external API services (Perplexity and Serper) for search operations, intelligently selecting between them based on the query type.
**Purpose:**
- Leverage powerful commercial search APIs
- Provide high-quality results for complex queries
- Offer fast and reliable search capabilities
**Advantages:**
- High-quality results for complex queries through Perplexity
- Fast and reliable search through Serper's Google search API
- Structured and consistent results
**Use Cases:**
- Complex, reasoning-based queries that benefit from Perplexity's capabilities
- Factual and simple queries that can be handled by Serper
- Production environments with API access
**Example:**
```python
from tools.web_tools import create_api_search
# Create the API search tool
api_tool = create_api_search()
# For complex queries, it automatically uses Perplexity
complex_results = api_tool.search("Explain quantum computing in detail and its potential impact on cryptography")
# For simple queries, it automatically uses Serper
simple_results = api_tool.search("weather in New York")
```
## Fallback Chain
The three search tools work together in a fallback chain, ensuring robust search capabilities even when some tools are unavailable or fail. The EnhancedWebSearchTool manages this fallback chain.
**Fallback Process:**
1. First, try the most appropriate tool based on the query type
2. If that tool fails or is unavailable, try the next most appropriate tool
3. Continue until a successful result is found or all tools have been tried
**Example:**
```python
from tools.web_tools import create_enhanced_web_search
# Create the enhanced web search tool
enhanced_tool = create_enhanced_web_search()
# Search for information
# This will automatically select the most appropriate tool
# and fall back to others if needed
results = enhanced_tool.search("Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?")
```
## Query Type Detection
Each tool includes specialized methods to detect specific types of queries:
- **BrowserSearchTool**: Detects website-specific queries (e.g., "wikipedia article about...")
- **LibrarySearchTool**: Detects academic queries for arXiv vs. general queries for DuckDuckGo
- **ApiSearchTool**: Detects complex queries for Perplexity vs. simple queries for Serper
These detection methods ensure that queries are routed to the most appropriate search tool.
## Implementation Details
### BrowserSearchTool
The BrowserSearchTool provides search templates for common websites:
```python
self.search_templates = {
"wikipedia": "https://en.wikipedia.org/wiki/Special:Search?search={query}",
"arxiv": "https://arxiv.org/search/?query={query}&searchtype=all",
"google": "https://www.google.com/search?q={query}",
"youtube": "https://www.youtube.com/results?search_query={query}",
# Additional templates...
}
```
It can detect the most appropriate source based on the query content and provides specific instructions for using browser_action with each source.
### LibrarySearchTool
The LibrarySearchTool includes methods to detect academic queries:
```python
def _is_academic_query(self, query: str) -> bool:
# Check for academic keywords, fields, and citation patterns
# ...
return has_academic_keyword or has_academic_field or has_citation_pattern
```
It uses the DuckDuckGo library for general queries and the arXiv library for academic queries, formatting the results consistently.
### ApiSearchTool
The ApiSearchTool includes methods to detect complex queries:
```python
def _is_complex_query(self, query: str) -> bool:
# Check for question words, complex indicators, and query length
# ...
return has_question_word or has_complex_indicator or is_long_query
```
It uses the Perplexity API for complex queries and the Serper API for simple queries, formatting the results consistently.
## Configuration
Each tool can be configured through environment variables and configuration files:
### ApiSearchTool
- **PERPLEXITY_API_KEY**: API key for Perplexity
- **SERPER_API_KEY**: API key for Serper
### LibrarySearchTool
- No API keys required, but the DuckDuckGo and arXiv libraries must be installed
### BrowserSearchTool
- No API keys required, but browser_action capability must be available
## Usage
### Basic Usage
```python
from tools.web_tools import create_enhanced_web_search
# Create the enhanced web search tool
enhanced_tool = create_enhanced_web_search()
# Search for information
results = enhanced_tool.search("Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?")
# Process the results
for result in results:
print(f"Title: {result.get('title')}")
print(f"Snippet: {result.get('snippet')}")
print(f"Source: {result.get('source')}")
print(f"Relevance: {result.get('relevance_score')}")
print("---")
```
### Using with the Tool Registry
```python
from agent.tool_registry import create_default_registry, unified_search
# Create the registry
registry = create_default_registry()
# Search using unified_search
results = unified_search(registry, "How many studio albums were published by Mercedes Sosa between 2000 and 2009?")
# Process the results
for result in results["results"]:
print(f"Title: {result.get('title')}")
print(f"Snippet: {result.get('snippet')}")
print(f"Source: {result.get('source')}")
print(f"Relevance: {result.get('relevance_score')}")
print("---")
```
## Testing
You can test the Enhanced Web Search system using the provided test scripts:
```bash
# Run all tests
python tests/run_gaia_web_search_tests.py
# Run only direct tests (without registry)
python tests/run_gaia_web_search_tests.py --direct-only
# Run only registry tests
python tests/run_gaia_web_search_tests.py --registry-only
# Enable verbose logging
python tests/run_gaia_web_search_tests.py --verbose
```
## Examples
See the following example scripts for complete examples of how to use the Enhanced Web Search system:
- `examples/enhanced_web_search_example.py` - Example of using the EnhancedWebSearchTool
- `examples/multi_search_example.py` - Example of using multiple search tools together
## Requirements
- Perplexity API key (set as `PERPLEXITY_API_KEY` environment variable) for ApiSearchTool
- Serper API key (set as `SERPER_API_KEY` environment variable) for ApiSearchTool
- DuckDuckGo and arXiv libraries installed for LibrarySearchTool
- Browser_action capability for BrowserSearchTool
## Conclusion
The Enhanced Web Search system provides a robust, flexible approach to web search through its three-tier architecture. By intelligently routing queries to the most appropriate search tools and implementing a fallback chain, it ensures reliable search capabilities across a wide range of environments and query types.
This system significantly improves search performance by leveraging the strengths of each search approach:
- Direct browser-based searches for website-specific content
- Library-based searches for academic and general queries without API keys
- API-based searches for high-quality results on complex queries
Together, these tools provide a comprehensive solution for web search needs in various contexts and environments.174 unmodified lines175176177178179180174 unmodified linesresult = run_benchmark(SAMPLE_QUESTIONS, agent_config=custom_config)```## Command-Line UsageYou can also run the agent from the command line using the provided scripts:174 unmodified lines175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242174 unmodified linesresult = run_benchmark(SAMPLE_QUESTIONS, agent_config=custom_config)```## Enhanced Web Search ExamplesThe GAIA agent includes enhanced web search tools specifically designed to handle certain GAIA assessment questions. The following examples demonstrate how to use these tools directly.### Running the Enhanced Web Search Example```bash# Run all enhanced web search examplespython examples/enhanced_web_search_example.py# Run only the Wikipedia examplepython examples/enhanced_web_search_example.py --wikipedia# Run only the DuckDuckGo examplepython examples/enhanced_web_search_example.py --duckduckgo# Run only the Universe Today examplepython examples/enhanced_web_search_example.py --universe-today# Run only the Malko Competition examplepython examples/enhanced_web_search_example.py --malko```### Using the Enhanced Web Search Tools in Your Code```pythonfrom tools.web_tools import WikipediaSearchTool, DuckDuckGoSearchTool, SerperSearchTool, WebContentExtractor# Example 1: Using the WikipediaSearchTool to find information about featured articleswiki_tool = WikipediaSearchTool()results = wiki_tool.search("Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?")for result in results:print(f"Title: {result.get('title', '')}")print(f"Snippet: {result.get('snippet', '')}")print()# Example 2: Using the DuckDuckGoSearchTool to find information about music albumsddg_tool = DuckDuckGoSearchTool()results = ddg_tool.search("How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?")for result in results:print(f"Title: {result.get('title', '')}")print(f"Snippet: {result.get('snippet', '')}")print()# Example 3: Using the WebContentExtractor to extract specific information from web pagesextractor = WebContentExtractor()content = extractor.extract_content("https://universetoday.com/example-article")extracted_info = content.get("extracted_info", {})if "nasa_award_numbers" in extracted_info:print(f"NASA Award Numbers: {extracted_info['nasa_award_numbers']}")# Example 4: Using the SerperSearchTool to find information about competitionsserper_tool = SerperSearchTool()results = serper_tool.search("Who won the Malko Competition for Young Conductors in 2021?")for result in results:print(f"Title: {result.get('title', '')}")print(f"Snippet: {result.get('snippet', '')}")print()```For more detailed information on the enhanced web search tools, see [Enhanced Web Search](enhanced_web_search.md).## Command-Line UsageYou can also run the agent from the command line using the provided scripts:
19 unmodified lines20212223242517 unmodified lines43444546474819 unmodified lines- [Serper Integration](serper_integration.md): Guide for using the Serper API for Google search- [DuckDuckGo Integration](duckduckgo_integration.md): Guide for using DuckDuckGo search- [Multi-Search Integration](multi_search_integration.md): Guide for using multiple search providers- [Perplexity Integration](perplexity_integration.md): Guide for using Perplexity AI### Memory Integrations17 unmodified lines## Recent Updates- **April 30, 2025**: Added troubleshooting guide and langchain_openai integration documentation- **April 25, 2025**: Updated model configurations to use GPT-4o- **April 20, 2025**: Added multi-search integration documentation19 unmodified lines2021222324252617 unmodified lines4445464748495019 unmodified lines- [Serper Integration](serper_integration.md): Guide for using the Serper API for Google search- [DuckDuckGo Integration](duckduckgo_integration.md): Guide for using DuckDuckGo search- [Multi-Search Integration](multi_search_integration.md): Guide for using multiple search providers- [Enhanced Web Search](enhanced_web_search.md): Guide for the enhanced web search tools for GAIA assessment- [Perplexity Integration](perplexity_integration.md): Guide for using Perplexity AI### Memory Integrations17 unmodified lines## Recent Updates- **May 1, 2025**: Added enhanced web search tools for GAIA assessment questions- **April 30, 2025**: Added troubleshooting guide and langchain_openai integration documentation- **April 25, 2025**: Updated model configurations to use GPT-4o- **April 20, 2025**: Added multi-search integration documentation
docs/enhanced_web_search.mdAdded+299
docs/examples.md+62
docs/index.md+2