/ docs-website / versioned_docs / version-2.25 / pipeline-components / websearch / serperdevwebsearch.mdx
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 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | Before [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx) or [Converters](../converters.mdx) | 17 | **Mandatory init variables** | `api_key`: The SearchAPI API key. Can be set with `SERPERDEV_API_KEY` env var. | 18 | **Mandatory run variables** | `query`: A string with your query | 19 | **Output variables** | `documents`: A list of documents <br /> <br />`links`: A list of strings of resulting links | 20 | **API reference** | [Websearch](/reference/websearch-api) | 21 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/websearch/serper_dev.py | 22 23 </div> 24 25 ## Overview 26 27 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. 28 29 To search the content of the web pages, use the [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx) component. 30 31 `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. 32 33 :::info[Alternative search] 34 35 To use [Search API](https://www.searchapi.io/) as an alternative, see its respective [documentation page](searchapiwebsearch.mdx). 36 ::: 37 38 ## Usage 39 40 ### On its own 41 42 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. 43 44 ```python 45 from haystack.components.websearch import SerperDevWebSearch 46 from haystack.utils import Secret 47 48 web_search = SerperDevWebSearch(api_key=Secret.from_token("<your-api-key>")) 49 query = "What is the capital of Germany?" 50 51 response = web_search.run(query) 52 ``` 53 54 ### In a pipeline 55 56 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. 57 58 ```python 59 from haystack import Pipeline 60 from haystack.utils import Secret 61 from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder 62 from haystack.components.fetchers import LinkContentFetcher 63 from haystack.components.converters import HTMLToDocument 64 from haystack.components.generators.chat import OpenAIChatGenerator 65 from haystack.components.websearch import SerperDevWebSearch 66 from haystack.dataclasses import ChatMessage 67 from haystack.utils import Secret 68 69 web_search = SerperDevWebSearch(api_key=Secret.from_token("<your-api-key>"), top_k=2) 70 link_content = LinkContentFetcher() 71 html_converter = HTMLToDocument() 72 73 prompt_template = [ 74 ChatMessage.from_system("You are a helpful assistant."), 75 ChatMessage.from_user( 76 "Given the information below:\n" 77 "{% for document in documents %}{{ document.content }}{% endfor %}\n" 78 "Answer question: {{ query }}.\nAnswer:", 79 ), 80 ] 81 82 prompt_builder = ChatPromptBuilder( 83 template=prompt_template, 84 required_variables={"query", "documents"}, 85 ) 86 llm = OpenAIChatGenerator( 87 api_key=Secret.from_token("<your-api-key>"), 88 model="gpt-3.5-turbo", 89 ) 90 91 pipe = Pipeline() 92 pipe.add_component("search", web_search) 93 pipe.add_component("fetcher", link_content) 94 pipe.add_component("converter", html_converter) 95 pipe.add_component("prompt_builder", prompt_builder) 96 pipe.add_component("llm", llm) 97 98 pipe.connect("search.links", "fetcher.urls") 99 pipe.connect("fetcher.streams", "converter.sources") 100 pipe.connect("converter.documents", "prompt_builder.documents") 101 pipe.connect("prompt_builder.messages", "llm.messages") 102 103 query = "What is the most famous landmark in Berlin?" 104 105 pipe.run(data={"search": {"query": query}, "prompt_builder": {"query": query}}) 106 ``` 107 108 ## Additional References 109 110 :notebook: Tutorial: [Building Fallbacks to Websearch with Conditional Routing](https://haystack.deepset.ai/tutorials/36_building_fallbacks_with_conditional_routing)