|
| 1 | +--- |
| 2 | +layout: integration |
| 3 | +name: Serpex |
| 4 | +description: Multi-engine web search for Haystack — access Google, Bing, DuckDuckGo, Brave, Yahoo, and Yandex via Serpex API |
| 5 | +authors: |
| 6 | + - name: Divyesh Radadiya |
| 7 | + socials: |
| 8 | + github: divyeshradadiya |
| 9 | +pypi: https://pypi.org/project/serpex-haystack/ |
| 10 | +repo: https://github.com/divyeshradadiya/serpex-haystack |
| 11 | +report_issue: https://github.com/divyeshradadiya/serpex-haystack/issues |
| 12 | +type: Search & Extraction |
| 13 | +logo: /logos/serpex.png |
| 14 | +version: Haystack 2.0 |
| 15 | +toc: true |
| 16 | +--- |
| 17 | + |
| 18 | +### **Table of Contents** |
| 19 | +- [Overview](#overview) |
| 20 | +- [Installation](#installation) |
| 21 | +- [Usage](#usage) |
| 22 | +- [License](#license) |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +[Serpex](https://serpex.dev) is a unified web search API that provides access to multiple search engines through a single interface. This Haystack integration allows you to seamlessly incorporate web search results into your Haystack RAG (Retrieval-Augmented Generation) pipelines and AI applications. |
| 27 | + |
| 28 | +**Website**: [serpex.dev](https://serpex.dev) |
| 29 | + |
| 30 | +### Key Features |
| 31 | + |
| 32 | +- 🔍 **Multi-Engine Support**: Switch between Google, Bing, DuckDuckGo, Brave, Yahoo, and Yandex |
| 33 | +- ⚡ **High Performance**: Fast and reliable API with automatic retries |
| 34 | +- 🎯 **Rich Results**: Get organic search results with titles, snippets, and URLs |
| 35 | +- 🕒 **Time Filters**: Filter results by day, week, month, or year |
| 36 | +- 🔒 **Type-Safe**: Fully typed with comprehensive type hints |
| 37 | +- 📝 **Haystack Native**: Seamless integration with Haystack 2.0+ components |
| 38 | + |
| 39 | +## Installation |
| 40 | + |
| 41 | +```bash |
| 42 | +pip install serpex-haystack |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Basic Usage |
| 48 | + |
| 49 | +```python |
| 50 | +from haystack.utils import Secret |
| 51 | +from haystack_integrations.components.websearch.serpex import SerpexWebSearch |
| 52 | + |
| 53 | +# Initialize the component |
| 54 | +web_search = SerpexWebSearch( |
| 55 | + api_key=Secret.from_env_var("SERPEX_API_KEY"), |
| 56 | + engine="google", # Options: google, bing, duckduckgo, brave, yahoo, yandex |
| 57 | +) |
| 58 | + |
| 59 | +# Perform a search |
| 60 | +results = web_search.run(query="What is Haystack AI?") |
| 61 | + |
| 62 | +# Access the results |
| 63 | +for doc in results["documents"]: |
| 64 | + print(f"Title: {doc.meta['title']}") |
| 65 | + print(f"URL: {doc.meta['url']}") |
| 66 | + print(f"Snippet: {doc.content}\n") |
| 67 | +``` |
| 68 | + |
| 69 | +### Agent Example |
| 70 | + |
| 71 | +Use Serpex in a Haystack agent for dynamic web search: |
| 72 | + |
| 73 | +```python |
| 74 | +import os |
| 75 | +from haystack.components.agents import Agent |
| 76 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 77 | +from haystack.dataclasses import ChatMessage |
| 78 | +from haystack.tools import ComponentTool |
| 79 | +from haystack.utils import Secret |
| 80 | +from haystack_integrations.components.websearch.serpex import SerpexWebSearch |
| 81 | + |
| 82 | +os.environ["OPENAI_API_KEY"] = "<YOUR OPENAI API KEY>" |
| 83 | + |
| 84 | +# Create a web search tool |
| 85 | +search_tool = ComponentTool(component=SerpexWebSearch(api_key=Secret.from_env_var("SERPEX_API_KEY"))) |
| 86 | + |
| 87 | +# Create an agent with web search capability |
| 88 | +basic_agent = Agent( |
| 89 | + chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"), |
| 90 | + system_prompt="You are a helpful web agent.", |
| 91 | + tools=[search_tool], |
| 92 | +) |
| 93 | + |
| 94 | +# Ask the agent a question that requires web search |
| 95 | +result = basic_agent.run(messages=[ChatMessage.from_user("What are the latest developments in AI agents?")]) |
| 96 | + |
| 97 | +print(result['last_message'].text) |
| 98 | +``` |
| 99 | + |
| 100 | +### Advanced Features |
| 101 | + |
| 102 | +#### Multiple Search Engines |
| 103 | + |
| 104 | +```python |
| 105 | +# Compare results from different engines |
| 106 | +google_search = SerpexWebSearch(engine="google") |
| 107 | +bing_search = SerpexWebSearch(engine="bing") |
| 108 | +duckduckgo_search = SerpexWebSearch(engine="duckduckgo") |
| 109 | +``` |
| 110 | + |
| 111 | +#### Time Range Filtering |
| 112 | + |
| 113 | +```python |
| 114 | +# Get only recent results |
| 115 | +recent_results = web_search.run( |
| 116 | + query="AI news", |
| 117 | + time_range="week" # Options: "day", "week", "month", "year", "all" |
| 118 | +) |
| 119 | +``` |
| 120 | + |
| 121 | +#### Runtime Configuration Override |
| 122 | + |
| 123 | +```python |
| 124 | +# Override default settings per query |
| 125 | +results = web_search.run( |
| 126 | + query="Python tutorials", |
| 127 | + engine="duckduckgo", # Override default engine |
| 128 | +) |
| 129 | +``` |
| 130 | + |
| 131 | +### Component API |
| 132 | + |
| 133 | +#### SerpexWebSearch |
| 134 | + |
| 135 | +**Parameters:** |
| 136 | +- `api_key` (Secret): Serpex API key. Defaults to `SERPEX_API_KEY` environment variable. |
| 137 | +- `engine` (str): Search engine to use. Options: `"auto"`, `"google"`, `"bing"`, `"duckduckgo"`, `"brave"`, `"yahoo"`, `"yandex"`. Default: `"google"`. |
| 138 | +- `timeout` (float): Request timeout in seconds. Default: `10.0`. |
| 139 | +- `retry_attempts` (int): Number of retry attempts. Default: `2`. |
| 140 | + |
| 141 | +**Inputs:** |
| 142 | +- `query` (str): The search query string. |
| 143 | +- `engine` (str, optional): Override the default search engine. |
| 144 | +- `time_range` (str, optional): Filter by time range (`"all"`, `"day"`, `"week"`, `"month"`, `"year"`). |
| 145 | + |
| 146 | +**Outputs:** |
| 147 | +- `documents` (`List[Document]`): List of Haystack Document objects containing search results. |
| 148 | + |
| 149 | +Each document includes: |
| 150 | +- **content**: The search result snippet |
| 151 | +- **meta**: |
| 152 | + - `title`: Result title |
| 153 | + - `url`: Result URL |
| 154 | + - `position`: Position in search results |
| 155 | + - `query`: Original search query |
| 156 | + - `engine`: Search engine used |
| 157 | + |
| 158 | +## License |
| 159 | + |
| 160 | +`serpex-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. |
0 commit comments