> ## 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.

# PerplexitySearchRetriever integration

> Integrate with the PerplexitySearchRetriever retriever using LangChain JavaScript.

The [Perplexity Search API](https://docs.perplexity.ai/docs/search/quickstart) 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](/oss/javascript/langchain/retrieval). For detailed documentation of all `PerplexitySearchRetriever` features and configurations head to the [API reference](https://reference.langchain.com/javascript/langchain-perplexity/PerplexitySearchRetriever).

### Integration details

| Retriever                                                                                                                | Source                                  |                                     Package                                    | [PY support](https://python.langchain.com/docs/integrations/retrievers/perplexity_search/) |
| :----------------------------------------------------------------------------------------------------------------------- | :-------------------------------------- | :----------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------: |
| [`PerplexitySearchRetriever`](https://reference.langchain.com/javascript/langchain-perplexity/PerplexitySearchRetriever) | The web, via the Perplexity Search API. | [`@langchain/perplexity`](https://www.npmjs.com/package/@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](https://www.perplexity.ai/account/api/keys).

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
process.env.PERPLEXITY_API_KEY = "your-api-key";
```

If you want to get automated tracing from individual queries, you can also set your [LangSmith](/langsmith/observability) API key by uncommenting below:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";
// process.env.LANGSMITH_TRACING = "true";
```

### Installation

This retriever lives in the `@langchain/perplexity` package:

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/perplexity @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/perplexity @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/perplexity @langchain/core
  ```
</CodeGroup>

## Instantiation

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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](https://docs.perplexity.ai/docs/search/quickstart) for details.

## Usage

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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](/oss/javascript/langchain/retrieval).

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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](https://reference.langchain.com/javascript/langchain-perplexity/PerplexitySearchRetriever).

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/retrievers/perplexity_search.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
