LinkupWebSearch

LinkupWebSearch

Linkup Search API로 웹을 검색해주는 컴포넌트예요. 주로 ChatPromptBuilder 앞이나 인덱싱 파이프라인 시작 부분에 두면 돼요.

출처: LinkupWebSearch

본문

개요

LinkupWebSearch에 쿼리를 주면 Linkup Search API로 웹을 검색해서, 결과를 Haystack Document 객체로 반환하고 함께 소스 URL 목록도 내보내 줘요.

각 결과는 Document가 되는데, 내용(content)은 Linkup이 그 결과에 대해 반환한 텍스트이고, 문서의 meta에는 결과 제목과 URL이 저장돼요.

depth 파라미터로 속도와 정확성을 맞바꿀 수 있어요.

  • "fast": 키워드 기반 쿼리만 사용, 서브초 응답(beta).
  • "standard": 단일 검색 패스. 이것이 기본값이에요.
  • "deep": 에이전틱 워크플로우를 실행하며, 시간이 더 걸려요.

top_k는 결과 수를 제한하고 Linkup API의 max_results 파라미터에 대응돼요. include_images, from_date, to_date, include_domains, exclude_domains 같은 추가 API 옵션을 쓰려면 search_params로 넘겨요. 사용 가능한 모든 옵션은 Linkup API reference에서 확인할 수 있어요. 이미지 결과는 텍스트가 없어서 include_images를 켜면 내용이 빈 Document가 추가돼요.

단일 검색에 대해 top_k, depth, search_params를 run()에 넘겨 오버라이드할 수 있어요. 주의할 점: run()에 넘긴 search_params 사전은 초기화 때 설정한 것과 병합되는 게 아니라 완전히 대체돼요.

LinkupWebSearch는 run_async()를 통한 비동기 실행도 지원해요. 내부 클라이언트는 첫 검색 때 지연 생성(lazily)돼요. 첫 호출의 콜드 스타트 지연을 피하려면 warm_up()을 명시적으로 호출하면 돼요.

LinkupWebSearch는 Linkup API 키가 필요해요. 기본적으로 LINKUP_API_KEY 환경 변수를 찾아요. 아니면 초기화할 때 api_key를 직접 넘길 수 있어요.

사용법

LinkupWebSearch를 쓰려면 linkup-haystack 패키지를 설치해요.

pip install linkup-haystack

단독 사용 예시예요. 쿼리로 웹을 검색해서 Document 목록을 반환받아요.

from haystack_integrations.components.websearch.linkup import LinkupWebSearch
from haystack.utils import Secret

web_search = LinkupWebSearch(
    api_key=Secret.from_env_var("LINKUP_API_KEY"),
    top_k=5,
    depth="standard",
)
query = "What is Haystack by deepset?"

response = web_search.run(query=query)

for doc in response["documents"]:
    print(doc.meta["url"])
    print(doc.content)

파이프라인 안에서:

아래는 LinkupWebSearch로 웹에서 답을 찾는 Retrieval-Augmented Generation(RAG) 파이프라인 예시예요.

from haystack import Pipeline
from haystack.utils import Secret
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack_integrations.components.websearch.linkup import LinkupWebSearch
from haystack.dataclasses import ChatMessage

web_search = LinkupWebSearch(
    api_key=Secret.from_env_var("LINKUP_API_KEY"),
    top_k=3,
)

prompt_template = [
    ChatMessage.from_system("You are a helpful assistant."),
    ChatMessage.from_user(
        "Given the information below:\n"
        "{% for document in documents %}{{ document.content }}\n{% endfor %}\n"
        "Answer the following question: {{ query }}.\nAnswer:",
    ),
]

prompt_builder = ChatPromptBuilder(
    template=prompt_template,
    required_variables={"query", "documents"},
)

llm = OpenAIChatGenerator(
    api_key=Secret.from_env_var("OPENAI_API_KEY"),
)

pipe = Pipeline()
pipe.add_component("search", web_search)
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)

pipe.connect("search.documents", "prompt_builder.documents")
pipe.connect("prompt_builder.prompt", "llm.messages")

query = "What is Haystack by deepset?"

result = pipe.run(data={"search": {"query": query}, "prompt_builder": {"query": query}})

print(result["llm"]["replies"][0].text)

더 알아보기 (Learn more)