|
| 1 | +--- |
| 2 | +title: "LinkupWebSearch" |
| 3 | +id: linkupwebsearch |
| 4 | +slug: "/linkupwebsearch" |
| 5 | +description: "Search engine using the Linkup Search API." |
| 6 | +--- |
| 7 | + |
| 8 | +# LinkupWebSearch |
| 9 | + |
| 10 | +Search the web using the Linkup Search API. |
| 11 | + |
| 12 | +<div className="key-value-table"> |
| 13 | + |
| 14 | +| | | |
| 15 | +| --- | --- | |
| 16 | +| **Most common position in a pipeline** | Before a [`ChatPromptBuilder`](../builders/chatpromptbuilder.mdx) or right at the beginning of an indexing pipeline | |
| 17 | +| **Mandatory init variables** | `api_key`: The Linkup API key. Can be set with the `LINKUP_API_KEY` env var. | |
| 18 | +| **Mandatory run variables** | `query`: A string with your search query. | |
| 19 | +| **Output variables** | `documents`: A list of Haystack Documents containing search result content, with the result title and URL in the metadata. <br /> <br />`links`: A list of strings of resulting URLs. | |
| 20 | +| **API reference** | [Linkup Search API](/reference/integrations-linkup) | |
| 21 | +| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/linkup/src/haystack_integrations/components/websearch/linkup/linkup_websearch.py | |
| 22 | +| **Package name** | `linkup-haystack` | |
| 23 | + |
| 24 | +</div> |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +When you give `LinkupWebSearch` a query, it uses the [Linkup](https://www.linkup.so) Search API to search the web and returns the results as Haystack `Document` objects, together with a list of the source URLs. |
| 29 | + |
| 30 | +Each result becomes a `Document` whose content is the text Linkup returns for that result, with the result title and URL stored in the Document's `meta`. |
| 31 | + |
| 32 | +Use the `depth` parameter to trade latency for thoroughness: |
| 33 | + |
| 34 | +- `"fast"`: keyword-based queries only, sub-second response (beta). |
| 35 | +- `"standard"`: a single search pass. This is the default. |
| 36 | +- `"deep"`: runs an agentic workflow, which takes longer. |
| 37 | + |
| 38 | +`top_k` limits the number of results and maps to the `max_results` parameter of the Linkup API. To use additional API options, such as `include_images`, `from_date`, `to_date`, `include_domains`, or `exclude_domains`, pass them in `search_params`. See the [Linkup API reference](https://docs.linkup.so/pages/documentation/api-reference/endpoint/post-search) for all available options. Image results carry no text, so enabling `include_images` adds Documents with empty content. |
| 39 | + |
| 40 | +You can override `top_k`, `depth`, and `search_params` for a single search by passing them to `run()`. Note that a `search_params` dictionary passed to `run()` fully replaces the one set at initialization instead of being merged with it. |
| 41 | + |
| 42 | +`LinkupWebSearch` also supports asynchronous execution through `run_async()`. The underlying client is created lazily on the first search. To avoid the cold-start latency of the first call, you can call `warm_up()` explicitly. |
| 43 | + |
| 44 | +`LinkupWebSearch` requires a Linkup API key to work. By default, it looks for a `LINKUP_API_KEY` environment variable. Alternatively, you can pass an `api_key` directly during initialization. |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +Install the `linkup-haystack` package to use the `LinkupWebSearch` component: |
| 49 | + |
| 50 | +```shell |
| 51 | +pip install linkup-haystack |
| 52 | +``` |
| 53 | + |
| 54 | +### On its own |
| 55 | + |
| 56 | +Here is a quick example of how `LinkupWebSearch` searches the web based on a query and returns a list of Documents. |
| 57 | + |
| 58 | +```python |
| 59 | +from haystack_integrations.components.websearch.linkup import LinkupWebSearch |
| 60 | +from haystack.utils import Secret |
| 61 | + |
| 62 | +web_search = LinkupWebSearch( |
| 63 | + api_key=Secret.from_env_var("LINKUP_API_KEY"), |
| 64 | + top_k=5, |
| 65 | + depth="standard", |
| 66 | +) |
| 67 | +query = "What is Haystack by deepset?" |
| 68 | + |
| 69 | +response = web_search.run(query=query) |
| 70 | + |
| 71 | +for doc in response["documents"]: |
| 72 | + print(doc.meta["url"]) |
| 73 | + print(doc.content) |
| 74 | +``` |
| 75 | + |
| 76 | +### In a pipeline |
| 77 | + |
| 78 | +Here is an example of a Retrieval-Augmented Generation (RAG) pipeline that uses `LinkupWebSearch` to look up an answer on the web. |
| 79 | + |
| 80 | +```python |
| 81 | +from haystack import Pipeline |
| 82 | +from haystack.utils import Secret |
| 83 | +from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder |
| 84 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 85 | +from haystack_integrations.components.websearch.linkup import LinkupWebSearch |
| 86 | +from haystack.dataclasses import ChatMessage |
| 87 | + |
| 88 | +web_search = LinkupWebSearch( |
| 89 | + api_key=Secret.from_env_var("LINKUP_API_KEY"), |
| 90 | + top_k=3, |
| 91 | +) |
| 92 | + |
| 93 | +prompt_template = [ |
| 94 | + ChatMessage.from_system("You are a helpful assistant."), |
| 95 | + ChatMessage.from_user( |
| 96 | + "Given the information below:\n" |
| 97 | + "{% for document in documents %}{{ document.content }}\n{% endfor %}\n" |
| 98 | + "Answer the following question: {{ query }}.\nAnswer:", |
| 99 | + ), |
| 100 | +] |
| 101 | + |
| 102 | +prompt_builder = ChatPromptBuilder( |
| 103 | + template=prompt_template, |
| 104 | + required_variables={"query", "documents"}, |
| 105 | +) |
| 106 | + |
| 107 | +llm = OpenAIChatGenerator( |
| 108 | + api_key=Secret.from_env_var("OPENAI_API_KEY"), |
| 109 | +) |
| 110 | + |
| 111 | +pipe = Pipeline() |
| 112 | +pipe.add_component("search", web_search) |
| 113 | +pipe.add_component("prompt_builder", prompt_builder) |
| 114 | +pipe.add_component("llm", llm) |
| 115 | + |
| 116 | +pipe.connect("search.documents", "prompt_builder.documents") |
| 117 | +pipe.connect("prompt_builder.prompt", "llm.messages") |
| 118 | + |
| 119 | +query = "What is Haystack by deepset?" |
| 120 | + |
| 121 | +result = pipe.run(data={"search": {"query": query}, "prompt_builder": {"query": query}}) |
| 122 | + |
| 123 | +print(result["llm"]["replies"][0].text) |
| 124 | +``` |
0 commit comments