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. PerplexitySearchResults is a LangChain tool wrapper that lets agents query the API and receive a JSON array of search results.

Overview

Integration details

Setup

The integration lives in the @langchain/perplexity package:
npm install @langchain/perplexity @langchain/core

Credentials

Set up a Perplexity API key and set it as an environment variable named PERPLEXITY_API_KEY:
process.env.PERPLEXITY_API_KEY = "your-api-key";
It’s also helpful (but not needed) to set up LangSmith for best-in-class observability:
process.env.LANGSMITH_TRACING = "true";
process.env.LANGSMITH_API_KEY = "your-api-key";

Instantiation

import { PerplexitySearchResults } from "@langchain/perplexity";

const tool = new PerplexitySearchResults({
  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.

Invocation

Direct call

const result = await tool.invoke("Who won the most recent FIFA World Cup?");
console.log(result);
The tool returns a JSON-encoded array of search results, each with title, url, snippet, date, and last_updated.

Use with an agent

import { ChatAnthropic } from "@langchain/anthropic";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

const llm = new ChatAnthropic({ model: "claude-3-5-haiku-latest" });

const agent = createReactAgent({
  llm,
  tools: [tool],
});

const response = await agent.invoke({
  messages: [
    { role: "user", content: "What's the latest news on the Mars Sample Return mission?" },
  ],
});

API reference

For detailed documentation of all PerplexitySearchResults features and configurations head to the API reference.