/ docs-website / versioned_docs / version-2.27 / pipeline-components / websearch / searchapiwebsearch.mdx
searchapiwebsearch.mdx
1 --- 2 title: "SearchApiWebSearch" 3 id: searchapiwebsearch 4 slug: "/searchapiwebsearch" 5 description: "Search engine using Search API." 6 --- 7 8 # SearchApiWebSearch 9 10 Search engine using Search 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 `SEARCHAPI_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/searchapi.py | 22 23 </div> 24 25 ## Overview 26 27 When you give `SearchApiWebSearch` 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 `SearchApiWebSearch` requires a [SearchApi](https://www.searchapi.io) key to work. It uses a `SEARCHAPI_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 [Serper Dev](https://serper.dev/?gclid=Cj0KCQiAgqGrBhDtARIsAM5s0_kPElllv3M59UPok1Ad-ZNudLaY21zDvbt5qw-b78OcUoqqvplVHRwaAgRgEALw_wcB) as an alternative, see its respective [documentation page](serperdevwebsearch.mdx). 36 ::: 37 38 ## Usage 39 40 ### On its own 41 42 This is an example of how `SearchApiWebSearch` 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 SearchApiWebSearch 46 47 web_search = SearchApiWebSearch(api_key=Secret.from_token("<your-api-key>")) 48 query = "What is the capital of Germany?" 49 50 response = web_search.run(query) 51 ``` 52 53 ### In a pipeline 54 55 Here’s an example of a RAG pipeline where we use a `SearchApiWebSearch` 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. 56 57 ```python 58 from haystack import Pipeline 59 from haystack.utils import Secret 60 from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder 61 from haystack.components.fetchers import LinkContentFetcher 62 from haystack.components.converters import HTMLToDocument 63 from haystack.components.generators.chat import OpenAIChatGenerator 64 from haystack.components.websearch import SearchApiWebSearch 65 from haystack.dataclasses import ChatMessage 66 67 web_search = SearchApiWebSearch(api_key=Secret.from_token("<your-api-key>"), top_k=2) 68 link_content = LinkContentFetcher() 69 html_converter = HTMLToDocument() 70 71 prompt_template = [ 72 ChatMessage.from_system("You are a helpful assistant."), 73 ChatMessage.from_user( 74 "Given the information below:\n" 75 "{% for document in documents %}{{ document.content }}{% endfor %}\n" 76 "Answer question: {{ query }}.\nAnswer:", 77 ), 78 ] 79 80 prompt_builder = ChatPromptBuilder( 81 template=prompt_template, 82 required_variables={"query", "documents"}, 83 ) 84 llm = OpenAIChatGenerator( 85 api_key=Secret.from_token("<your-api-key>"), 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 ```