serperdevwebsearch.mdx
  1  ---
  2  title: "SerperDevWebSearch"
  3  id: serperdevwebsearch
  4  slug: "/serperdevwebsearch"
  5  description: "Search engine using SerperDev API."
  6  ---
  7  
  8  # SerperDevWebSearch
  9  
 10  Search engine using SerperDev API.
 11  
 12  |  |  |
 13  | --- | --- |
 14  | **Most common position in a pipeline** | Before [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx)  or [Converters](../converters.mdx)  |
 15  | **Mandatory init variables** | "api_key": The SearchAPI API key. Can be set with `SERPERDEV_API_KEY` env var. |
 16  | **Mandatory run variables** | “query”: A string with your query |
 17  | **Output variables** | “documents”: A list of documents  <br /> <br />”links”: A list of strings of resulting links |
 18  | **API reference** | [Websearch](/reference/websearch-api) |
 19  | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/websearch/serper_dev.py |
 20  
 21  ## Overview
 22  
 23  When you give `SerperDevWebSearch` a query, it returns a list of the URLs most relevant to your search. It uses page snippets (pieces of text displayed under the page title in search results) to find the answers, not the whole pages.
 24  
 25  To search the content of the web pages, use the [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx) component.
 26  
 27  `SerperDevWebSearch` requires a [SerperDev](https://serper.dev/) key to work. It uses a `SERPERDEV_API_KEY` environment variable by default. Otherwise, you can pass an `api_key` at initialization – see code examples below.
 28  
 29  :::note
 30  Alternative search
 31  
 32  To use [Search API](https://www.searchapi.io/) as an alternative, see its respective [documentation page](searchapiwebsearch.mdx).
 33  :::
 34  
 35  ## Usage
 36  
 37  ### On its own
 38  
 39  This is an example of how `SerperDevWebSearch` looks up answers to our query on the web and converts the results into a list of documents with content snippets of the results, as well as URLs as strings.
 40  
 41  ```python
 42  from haystack.components.websearch import SerperDevWebSearch
 43  from haystack.utils import Secret
 44  
 45  web_search = SerperDevWebSearch(api_key=Secret.from_token("<your-api-key>"))
 46  query = "What is the capital of Germany?"
 47  
 48  response = web_search.run(query)
 49  ```
 50  
 51  ### In a pipeline
 52  
 53  Here’s an example of a RAG pipeline where we use a `SerperDevWebSearch` to look up the answer to the query. The resulting documents are then passed to `LinkContentFetcher` to get the full text from the URLs. Finally, `PromptBuilder` and `OpenAIGenerator` work together to form the final answer.
 54  
 55  ```python
 56  from haystack import Pipeline
 57  from haystack.utils import Secret
 58  from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
 59  from haystack.components.fetchers import LinkContentFetcher
 60  from haystack.components.converters import HTMLToDocument
 61  from haystack.components.generators.chat import OpenAIChatGenerator
 62  from haystack.components.websearch import SerperDevWebSearch
 63  from haystack.dataclasses import ChatMessage
 64  from haystack.utils import Secret
 65  
 66  web_search = SerperDevWebSearch(api_key=Secret.from_token("<your-api-key>"), top_k=2)
 67  link_content = LinkContentFetcher()
 68  html_converter = HTMLToDocument()
 69  
 70  prompt_template = [
 71      ChatMessage.from_system("You are a helpful assistant."),
 72      ChatMessage.from_user(
 73          "Given the information below:\n"
 74          "{% for document in documents %}{{ document.content }}{% endfor %}\n"
 75          "Answer question: {{ query }}.\nAnswer:",
 76      ),
 77  ]
 78  
 79  prompt_builder = ChatPromptBuilder(
 80      template=prompt_template,
 81      required_variables={"query", "documents"},
 82  )
 83  llm = OpenAIChatGenerator(
 84      api_key=Secret.from_token("<your-api-key>"),
 85      model="gpt-3.5-turbo",
 86  )
 87  
 88  pipe = Pipeline()
 89  pipe.add_component("search", web_search)
 90  pipe.add_component("fetcher", link_content)
 91  pipe.add_component("converter", html_converter)
 92  pipe.add_component("prompt_builder", prompt_builder)
 93  pipe.add_component("llm", llm)
 94  
 95  pipe.connect("search.links", "fetcher.urls")
 96  pipe.connect("fetcher.streams", "converter.sources")
 97  pipe.connect("converter.documents", "prompt_builder.documents")
 98  pipe.connect("prompt_builder.messages", "llm.messages")
 99  
100  query = "What is the most famous landmark in Berlin?"
101  
102  pipe.run(data={"search": {"query": query}, "prompt_builder": {"query": query}})
103  ```
104  
105  ## Additional References
106  
107  :notebook: Tutorial: [Building Fallbacks to Websearch with Conditional Routing](https://haystack.deepset.ai/tutorials/36_building_fallbacks_with_conditional_routing)