PerplexityWebSearch
PerplexityWebSearch
Perplexity Search API를 사용해 웹을 검색하는 컴포넌트예요.
출처: 문서
본문
| 항목 | 내용 |
|---|---|
| 파이프라인에서 가장 흔한 위치 | ChatPromptBuilder 앞, 또는 인덱싱 파이프라인의 맨 처음 |
| 필수 init 변수 | api_key: Perplexity API 키. PERPLEXITY_API_KEY 환경 변수로 설정 가능 |
| 필수 run 변수 | query: 검색 쿼리를 담은 문자열 |
| 출력 변수 | documents: 검색 결과 콘텐츠와 메타데이터를 담은 Haystack Document 목록. links: 결과 URL 문자열 목록 |
| API reference | Integrations |
| GitHub 링크 | perplexity_websearch.py |
| 패키지 이름 | perplexity-haystack |
개요
PerplexityWebSearch에 쿼리를 주면 Perplexity Search API를 사용해 웹을 검색하고, 관련 콘텐츠를 Haystack Document 객체로 반환해요. 동시에 소스 URL 목록도 돌려주죠.
반환되는 각 Document는 텍스트 스니펫을 content로 담고, meta 딕셔너리에는 title, url, date, last_updated 필드를 담아요.
PerplexityWebSearch는 동작하려면 Perplexity API 키가 필요해요. 기본적으로 PERPLEXITY_API_KEY 환경 변수에서 읽어오고, 초기화 시 api_key를 직접 전달할 수도 있어요.
top_k 파라미터는 반환할 최대 결과 수를 제어해요(1~20 사이, 기본값 10).
search_params를 사용해 검색 결과를 필터링하고 다듬을 수 있는데, country, search_recency_filter, search_domain_filter, 날짜 범위 필터 같은 키를 지원해요. 초기화 시 또는 run() 호출 시마다 설정할 수 있죠. 전체 파라미터 목록은 Perplexity Search API reference를 참고하세요.
PerplexityWebSearch는 동기(run())와 비동기(run_async()) 동작을 모두 지원해요.
사용법
단독으로 사용하기
from haystack.utils import Secret
from haystack_integrations.components.websearch.perplexity import PerplexityWebSearch
web_search = PerplexityWebSearch(
api_key=Secret.from_env_var("PERPLEXITY_API_KEY"),
top_k=5,
)
result = web_search.run(query="What is Haystack by deepset?")
for doc in result["documents"]:
print(doc.content)
print(doc.meta["url"])
검색 필터와 함께 사용하기:
from haystack.utils import Secret
from haystack_integrations.components.websearch.perplexity import PerplexityWebSearch
web_search = PerplexityWebSearch(
api_key=Secret.from_env_var("PERPLEXITY_API_KEY"),
top_k=5,
search_params={"country": "us", "search_recency_filter": "week"},
)
result = web_search.run(query="Latest AI research papers")
for doc in result["documents"]:
print(doc.meta["title"], doc.meta["url"])
파이프라인에서 사용하기
PerplexityWebSearch로 웹에서 답을 찾아보는 RAG 파이프라인 예시예요.
from haystack import Pipeline
from haystack.utils import Secret
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.perplexity import (
PerplexityChatGenerator,
)
from haystack_integrations.components.websearch.perplexity import PerplexityWebSearch
web_search = PerplexityWebSearch(
api_key=Secret.from_env_var("PERPLEXITY_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 = PerplexityChatGenerator(
api_key=Secret.from_env_var("PERPLEXITY_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)