Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.langchain.com/llms.txt

Use this file to discover all available pages before exploring further.

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

RetrieverSourcePackagePY support
PerplexitySearchRetrieverThe 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/core

Instantiation

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-3-5-haiku-latest" });

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.