本页还没有译文
本页 20 段还没译成简体中文,下面显示的是英文原文。PerplexitySearchRetriever integration
还没翻译译自英文(docs.langchain.com)本页 20 段都还没译
The Perplexity Search API returns real-time, grounded web search results that you can drop directly into a retrieval pipeline.
Overview
This will help you get started with PerplexitySearchRetriever retrieval. For detailed documentation of all PerplexitySearchRetriever features and configurations head to the API reference.
Integration details
| Retriever | Source | Package | PY support |
|---|---|---|---|
PerplexitySearchRetriever | The web, via the Perplexity Search API. | @langchain/perplexity | ✅ |
Setup
You will need to populate a PERPLEXITY_API_KEY environment variable with your Perplexity API key, or pass it into the constructor as apiKey. Get a key from the Perplexity API key dashboard.
process.env.PERPLEXITY_API_KEY = "your-api-key";If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:
// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";
// process.env.LANGSMITH_TRACING = "true";Installation
This retriever lives in the @langchain/perplexity package:
npm install @langchain/perplexity @langchain/coreyarn add @langchain/perplexity @langchain/corepnpm add @langchain/perplexity @langchain/coreInstantiation
import { PerplexitySearchRetriever } from "@langchain/perplexity";
const retriever = new PerplexitySearchRetriever({
maxResults: 5,
// searchDomainFilter: ["wikipedia.org"],
// searchRecencyFilter: "week",
});Constructor options include maxResults, country, searchDomainFilter, searchRecencyFilter, searchAfterDate, and searchBeforeDate. See the Perplexity Search API parameters for details.
Usage
const query = "Who won the most recent FIFA World Cup?";
const docs = await retriever.invoke(query);Each returned Document has the search result snippet as pageContent and { title, url, date, lastUpdated } in metadata.
Use within a chain
Like other retrievers, PerplexitySearchRetriever can be incorporated into LLM applications via chains.
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatAnthropic } from "@langchain/anthropic";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { Document } from "@langchain/core/documents";
const prompt = ChatPromptTemplate.fromTemplate(`
Answer the question based only on the context provided.
Context: {context}
Question: {question}`);
const llm = new ChatAnthropic({ model: "claude-haiku-4-5" });
const formatDocs = (docs: Document[]) =>
docs.map((doc) => doc.pageContent).join("\n\n");
const ragChain = RunnableSequence.from([
{
context: retriever.pipe(formatDocs),
question: new RunnablePassthrough(),
},
prompt,
llm,
new StringOutputParser(),
]);
await ragChain.invoke("Who won the most recent FIFA World Cup?");API reference
For detailed documentation of all PerplexitySearchRetriever features and configurations head to the API reference.