File size: 6,857 Bytes
93b3423 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import json
from typing import Any, Dict, List, Optional, Union
import requests
from haystack import ComponentError, Document, component, default_from_dict, default_to_dict, logging
from haystack.utils import Secret, deserialize_secrets_inplace
logger = logging.getLogger(__name__)
SERPERDEV_BASE_URL = "https://google.serper.dev/search"
class SerperDevError(ComponentError): ...
@component
class SerperDevWebSearch:
"""
Uses [Serper](https://serper.dev/) to search the web for relevant documents.
See the [Serper Dev website](https://serper.dev/) for more details.
Usage example:
```python
from haystack.components.websearch import SerperDevWebSearch
from haystack.utils import Secret
websearch = SerperDevWebSearch(top_k=10, api_key=Secret.from_token("test-api-key"))
results = websearch.run(query="Who is the boyfriend of Olivia Wilde?")
assert results["documents"]
assert results["links"]
```
"""
def __init__(
self,
api_key: Secret = Secret.from_env_var("SERPERDEV_API_KEY"),
top_k: Optional[int] = 10,
allowed_domains: Optional[List[str]] = None,
search_params: Optional[Dict[str, Any]] = None,
):
"""
Initialize the SerperDevWebSearch component.
:param api_key: API key for the Serper API.
:param top_k: Number of documents to return.
:param allowed_domains: List of domains to limit the search to.
:param search_params: Additional parameters passed to the Serper API.
For example, you can set 'num' to 20 to increase the number of search results.
See the [Serper website](https://serper.dev/) for more details.
"""
self.api_key = api_key
self.top_k = top_k
self.allowed_domains = allowed_domains
self.search_params = search_params or {}
# Ensure that the API key is resolved.
_ = self.api_key.resolve_value()
def to_dict(self) -> Dict[str, Any]:
"""
Serializes the component to a dictionary.
:returns:
Dictionary with serialized data.
"""
return default_to_dict(
self,
top_k=self.top_k,
allowed_domains=self.allowed_domains,
search_params=self.search_params,
api_key=self.api_key.to_dict(),
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "SerperDevWebSearch":
"""
Serializes the component to a dictionary.
:returns:
Dictionary with serialized data.
"""
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
return default_from_dict(cls, data)
@component.output_types(documents=List[Document], links=List[str])
def run(self, query: str) -> Dict[str, Union[List[Document], List[str]]]:
"""
Use [Serper](https://serper.dev/) to search the web.
:param query: Search query.
:returns: A dictionary with the following keys:
- "documents": List of documents returned by the search engine.
- "links": List of links returned by the search engine.
:raises SerperDevError: If an error occurs while querying the SerperDev API.
:raises TimeoutError: If the request to the SerperDev API times out.
"""
query_prepend = "OR ".join(f"site:{domain} " for domain in self.allowed_domains) if self.allowed_domains else ""
payload = json.dumps(
{"q": query_prepend + query, "gl": "us", "hl": "en", "autocorrect": True, **self.search_params}
)
headers = {"X-API-KEY": self.api_key.resolve_value(), "Content-Type": "application/json"}
try:
response = requests.post(SERPERDEV_BASE_URL, headers=headers, data=payload, timeout=30) # type: ignore
response.raise_for_status() # Will raise an HTTPError for bad responses
except requests.Timeout as error:
raise TimeoutError(f"Request to {self.__class__.__name__} timed out.") from error
except requests.RequestException as e:
raise SerperDevError(f"An error occurred while querying {self.__class__.__name__}. Error: {e}") from e
# If we reached this point, it means the request was successful and we can proceed
json_result = response.json()
# we get the snippet from the json result and put it in the content field of the document
organic = [
Document(meta={k: v for k, v in d.items() if k != "snippet"}, content=d.get("snippet"))
for d in json_result["organic"]
]
# answer box is what search engine shows as a direct answer to the query
answer_box = []
if "answerBox" in json_result:
answer_dict = json_result["answerBox"]
highlighted_answers = answer_dict.get("snippetHighlighted")
answer_box_content = None
# Check if highlighted_answers is a list and has at least one element
if isinstance(highlighted_answers, list) and len(highlighted_answers) > 0:
answer_box_content = highlighted_answers[0]
elif isinstance(highlighted_answers, str):
answer_box_content = highlighted_answers
if not answer_box_content:
for key in ["snippet", "answer", "title"]:
if key in answer_dict:
answer_box_content = answer_dict[key]
break
if answer_box_content:
answer_box = [
Document(
content=answer_box_content,
meta={"title": answer_dict.get("title", ""), "link": answer_dict.get("link", "")},
)
]
# these are related questions that search engine shows
people_also_ask = []
if "peopleAlsoAsk" in json_result:
for result in json_result["peopleAlsoAsk"]:
title = result.get("title", "")
people_also_ask.append(
Document(
content=result["snippet"] if result.get("snippet") else title,
meta={"title": title, "link": result.get("link", None)},
)
)
documents = answer_box + organic + people_also_ask
links = [result["link"] for result in json_result["organic"]]
logger.debug(
"Serper Dev returned {number_documents} documents for the query '{query}'",
number_documents=len(documents),
query=query,
)
return {"documents": documents[: self.top_k], "links": links[: self.top_k]}
|