|
| 1 | +--- |
| 2 | +title: "SerperDev" |
| 3 | +id: integrations-serperdev |
| 4 | +description: "SerperDev integration for Haystack" |
| 5 | +slug: "/integrations-serperdev" |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +## haystack_integrations.components.websearch.serperdev.websearch |
| 10 | + |
| 11 | +### SerperDevWebSearch |
| 12 | + |
| 13 | +Uses [Serper](https://serper.dev/) to search the web for relevant documents. |
| 14 | + |
| 15 | +See the [Serper Dev website](https://serper.dev/) for more details. |
| 16 | + |
| 17 | +Usage example: |
| 18 | + |
| 19 | +```python |
| 20 | +from haystack.utils import Secret |
| 21 | + |
| 22 | +from haystack_integrations.components.websearch.serperdev import SerperDevWebSearch |
| 23 | + |
| 24 | +serper_dev_api = Secret.from_env_var("SERPERDEV_API_KEY") |
| 25 | + |
| 26 | +websearch = SerperDevWebSearch(top_k=10, api_key=serper_dev_api) |
| 27 | +results = websearch.run(query="Who is the boyfriend of Olivia Wilde?") |
| 28 | + |
| 29 | +assert results["documents"] |
| 30 | +assert results["links"] |
| 31 | + |
| 32 | +# Example with domain filtering - exclude subdomains |
| 33 | +websearch_filtered = SerperDevWebSearch( |
| 34 | + top_k=10, |
| 35 | + allowed_domains=["example.com"], |
| 36 | + exclude_subdomains=True, # Only results from example.com, not blog.example.com |
| 37 | + api_key=serper_dev_api, |
| 38 | +) |
| 39 | +results_filtered = websearch_filtered.run(query="search query") |
| 40 | +``` |
| 41 | + |
| 42 | +#### __init__ |
| 43 | + |
| 44 | +```python |
| 45 | +__init__( |
| 46 | + api_key: Secret = Secret.from_env_var("SERPERDEV_API_KEY"), |
| 47 | + top_k: int | None = 10, |
| 48 | + allowed_domains: list[str] | None = None, |
| 49 | + search_params: dict[str, Any] | None = None, |
| 50 | + *, |
| 51 | + exclude_subdomains: bool = False |
| 52 | +) -> None |
| 53 | +``` |
| 54 | + |
| 55 | +Initialize the SerperDevWebSearch component. |
| 56 | + |
| 57 | +**Parameters:** |
| 58 | + |
| 59 | +- **api_key** (<code>Secret</code>) – API key for the Serper API. |
| 60 | +- **top_k** (<code>int | None</code>) – Number of documents to return. |
| 61 | +- **allowed_domains** (<code>list\[str\] | None</code>) – List of domains to limit the search to. |
| 62 | +- **exclude_subdomains** (<code>bool</code>) – Whether to exclude subdomains when filtering by allowed_domains. |
| 63 | + If True, only results from the exact domains in allowed_domains will be returned. |
| 64 | + If False, results from subdomains will also be included. Defaults to False. |
| 65 | +- **search_params** (<code>dict\[str, Any\] | None</code>) – Additional parameters passed to the Serper API. |
| 66 | + For example, you can set 'num' to 20 to increase the number of search results. |
| 67 | + See the [Serper website](https://serper.dev/) for more details. |
| 68 | + |
| 69 | +#### to_dict |
| 70 | + |
| 71 | +```python |
| 72 | +to_dict() -> dict[str, Any] |
| 73 | +``` |
| 74 | + |
| 75 | +Serializes the component to a dictionary. |
| 76 | + |
| 77 | +**Returns:** |
| 78 | + |
| 79 | +- <code>dict\[str, Any\]</code> – Dictionary with serialized data. |
| 80 | + |
| 81 | +#### from_dict |
| 82 | + |
| 83 | +```python |
| 84 | +from_dict(data: dict[str, Any]) -> SerperDevWebSearch |
| 85 | +``` |
| 86 | + |
| 87 | +Deserializes the component from a dictionary. |
| 88 | + |
| 89 | +**Parameters:** |
| 90 | + |
| 91 | +- **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. |
| 92 | + |
| 93 | +**Returns:** |
| 94 | + |
| 95 | +- <code>SerperDevWebSearch</code> – The deserialized component. |
| 96 | + |
| 97 | +#### run |
| 98 | + |
| 99 | +```python |
| 100 | +run(query: str) -> dict[str, list[Document] | list[str]] |
| 101 | +``` |
| 102 | + |
| 103 | +Use [Serper](https://serper.dev/) to search the web. |
| 104 | + |
| 105 | +**Parameters:** |
| 106 | + |
| 107 | +- **query** (<code>str</code>) – Search query. |
| 108 | + |
| 109 | +**Returns:** |
| 110 | + |
| 111 | +- <code>dict\[str, list\[Document\] | list\[str\]\]</code> – A dictionary with the following keys: |
| 112 | +- "documents": List of documents returned by the search engine. |
| 113 | +- "links": List of links returned by the search engine. |
| 114 | + |
| 115 | +**Raises:** |
| 116 | + |
| 117 | +- <code>SerperDevError</code> – If an error occurs while querying the SerperDev API. |
| 118 | +- <code>TimeoutError</code> – If the request to the SerperDev API times out. |
| 119 | + |
| 120 | +#### run_async |
| 121 | + |
| 122 | +```python |
| 123 | +run_async(query: str) -> dict[str, list[Document] | list[str]] |
| 124 | +``` |
| 125 | + |
| 126 | +Asynchronously uses [Serper](https://serper.dev/) to search the web. |
| 127 | + |
| 128 | +This is the asynchronous version of the `run` method with the same parameters and return values. |
| 129 | + |
| 130 | +**Parameters:** |
| 131 | + |
| 132 | +- **query** (<code>str</code>) – Search query. |
| 133 | + |
| 134 | +**Returns:** |
| 135 | + |
| 136 | +- <code>dict\[str, list\[Document\] | list\[str\]\]</code> – A dictionary with the following keys: |
| 137 | +- "documents": List of documents returned by the search engine. |
| 138 | +- "links": List of links returned by the search engine. |
| 139 | + |
| 140 | +**Raises:** |
| 141 | + |
| 142 | +- <code>SerperDevError</code> – If an error occurs while querying the SerperDev API. |
| 143 | +- <code>TimeoutError</code> – If the request to the SerperDev API times out. |
0 commit comments