本页还没有译文
本页 20 段还没译成한국어,下面显示的是英文原文。PerplexitySearch integration
还没翻译译自英文(docs.langchain.com)本页 20 段都还没译
Perplexity Search is a web search API that returns ranked, source-attributed results designed for use by LLMs and agents. The Search API endpoint returns the underlying web results that power Perplexity's answer engine.
We can use this as a retriever. It will show functionality specific to this integration. After going through, it may be useful to explore relevant use-case pages to learn how to use this retriever as part of a larger chain.
Setup
Installation
The integration lives in the langchain-perplexity package:
pip install -qU langchain-perplexityCredentials
You'll need a Perplexity API key to use this integration. Create one in the Perplexity API key dashboard, then set it as the PPLX_API_KEY environment variable (the integration also accepts PERPLEXITY_API_KEY).
import getpass
import os
if not os.environ.get("PPLX_API_KEY"):
os.environ["PPLX_API_KEY"] = getpass.getpass("Perplexity API key:\n")If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"Instantiation
PerplexitySearchRetriever accepts the following constructor arguments:
k— max number of results (1–20). Defaults to10.max_tokens— max total tokens across all results.max_tokens_per_page— max tokens returned per result page.country— ISO country code to bias results (e.g."US").search_domain_filter— list of domains to include or exclude (max 20). Prefix a domain with-to exclude it. See the domain filter docs.search_recency_filter— one of"day","week","month","year". See the date and time filter docs.search_after_date/search_before_date— date strings inMM/DD/YYYYformat.
from langchain_perplexity import PerplexitySearchRetriever
retriever = PerplexitySearchRetriever(k=3)Usage
query = "what year was breath of the wild released?"
retriever.invoke(query)Each returned Document has the result snippet as its page_content, plus title, url, date, and last_updated in metadata.
Filtered usage
You can constrain the search with the same filters supported by the Perplexity Search API:
from langchain_perplexity import PerplexitySearchRetriever
retriever = PerplexitySearchRetriever(
k=5,
country="US",
search_recency_filter="week",
search_domain_filter=["nytimes.com", "reuters.com", "-pinterest.com"],
)
retriever.invoke("US Federal Reserve interest rate decisions")from langchain_perplexity import PerplexitySearchRetriever
# Restrict results to a specific date range
retriever = PerplexitySearchRetriever(
k=5,
search_after_date="01/01/2025",
search_before_date="06/30/2025",
)
retriever.invoke("US Federal Reserve interest rate decisions")Use within a chain
We can easily combine this retriever into a chain to build a simple RAG pipeline.
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
prompt = ChatPromptTemplate.from_template(
"""Answer the question based only on the context provided.
Context: {context}
Question: {question}"""
)
llm = ChatOpenAI(model="gpt-5.5")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
chain.invoke("What were the major outcomes of the most recent Federal Reserve meeting?")API reference
For detailed documentation of the Perplexity Search API and all of its options, see the Search API reference and the Perplexity API documentation.