npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@langchain/perplexity

v0.2.0

Published

Perplexity integration for LangChain.js (chat models, Search retriever, and Search tool).

Readme

@langchain/perplexity

This package provides a LangChain.js integration for Perplexity AI, including chat models, the Perplexity Search retriever, and the Perplexity Search tool.

Installation

npm install @langchain/perplexity @langchain/core

Setup

You need a Perplexity API key. Set the PERPLEXITY_API_KEY environment variable or pass it directly to the constructor.

export PERPLEXITY_API_KEY="your-api-key"

Usage

Basic Chat

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

const model = new ChatPerplexity({
  model: "sonar",
});

const response = await model.invoke([
  ["human", "What is the capital of France?"],
]);

console.log(response.content);
// Citations are available in additional_kwargs
console.log(response.additional_kwargs.citations);

Streaming

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

const model = new ChatPerplexity({
  model: "sonar",
  streaming: true,
});

const stream = await model.stream([["human", "Explain quantum computing"]]);

for await (const chunk of stream) {
  process.stdout.write(chunk.content as string);
}

Structured Output

Perplexity supports structured output via JSON Schema:

import { ChatPerplexity } from "@langchain/perplexity";
import { z } from "zod";

const model = new ChatPerplexity({
  model: "sonar",
});

const structured = model.withStructuredOutput(
  z.object({
    capital: z.string(),
    country: z.string(),
    population: z.number().optional(),
  })
);

const result = await structured.invoke("What is the capital of India?");

console.log(result);
// { capital: "New Delhi", country: "India", population: ... }

Reasoning Models

Perplexity offers reasoning models (e.g. sonar-reasoning) that provide step-by-step thinking. The package includes specialised output parsers that automatically strip <think> tags from reasoning model responses:

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

const model = new ChatPerplexity({
  model: "sonar-reasoning",
});

const result = await model.invoke([
  ["human", "What are the most popular LLM frameworks?"],
]);

console.log(result.content);

Search Configuration

Perplexity models can search the web. You can configure search behaviour:

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

const model = new ChatPerplexity({
  model: "sonar-pro",
  searchDomainFilter: ["wikipedia.org", "arxiv.org"],
  searchRecencyFilter: "week",
  searchMode: "academic",
  webSearchOptions: {
    search_context_size: "high",
    user_location: {
      latitude: 37.7749,
      longitude: -122.4194,
      country: "US",
    },
  },
});

You can also disable web search entirely:

const model = new ChatPerplexity({
  model: "sonar",
  disableSearch: true,
});

Configuration Reference

| Parameter | Type | Description | | ------------------------- | ----------- | ------------------------------------------------------------------------------ | | model | string | Required. Model name (e.g. "sonar", "sonar-pro", "sonar-reasoning"). | | apiKey | string | API key. Defaults to PERPLEXITY_API_KEY env var. | | temperature | number | Sampling temperature (0–2). | | maxTokens | number | Maximum tokens to generate. | | topP | number | Nucleus sampling parameter (0–1). | | topK | number | Top-k sampling parameter (1–2048). | | presencePenalty | number | Presence penalty (-2 to 2). | | frequencyPenalty | number | Frequency penalty (> 0). | | streaming | boolean | Enable streaming responses. | | timeout | number | Request timeout in milliseconds. | | searchDomainFilter | unknown[] | Limit citations to specific domains. | | searchRecencyFilter | string | Time filter: "month", "week", "day", "hour". | | searchMode | string | "academic" or "web". | | returnImages | boolean | Include images in response. | | returnRelatedQuestions | boolean | Return related questions. | | reasoningEffort | string | "low", "medium", or "high" (for deep-research models). | | disableSearch | boolean | Disable web search entirely. | | enableSearchClassifier | boolean | Auto-detect if search is needed. | | webSearchOptions | object | Search context size and user location. | | searchAfterDateFilter | string | Only include content after this date. | | searchBeforeDateFilter | string | Only include content before this date. | | lastUpdatedAfterFilter | string | Only include content updated after this date. | | lastUpdatedBeforeFilter | string | Only include content updated before this date. |

Perplexity Search retriever

PerplexitySearchRetriever calls the Perplexity Search API and returns each result as a LangChain Document whose metadata contains title, url, date, and last_updated.

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

const retriever = new PerplexitySearchRetriever({
  k: 5,
  searchRecencyFilter: "week",
  searchDomainFilter: ["wikipedia.org"],
});

const docs = await retriever.invoke("Latest LLM benchmarks");
for (const doc of docs) {
  console.log(doc.metadata.title, doc.metadata.url);
  console.log(doc.pageContent);
}

Perplexity Search tool

PerplexitySearchResults is a LangChain Tool wrapper around the same /search endpoint. The tool name is perplexity_search_results_json and _call returns a JSON-encoded array of { title, url, snippet, date, last_updated }.

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

const tool = new PerplexitySearchResults({
  maxResults: 5,
  searchRecencyFilter: "week",
});

const json = await tool.invoke("Latest LLM benchmarks");
console.log(JSON.parse(json));

Search constructor parameters

Both classes accept the same Perplexity Search filters:

| Parameter | Type | Description | | --------------------- | -------------------------------------- | ----------------------------------------------------------------------- | | apiKey | string | API key. Defaults to PERPLEXITY_API_KEY or PPLX_API_KEY env var. | | k / maxResults | number | Maximum results (1-20). Defaults to 10. | | maxTokens | number | Retriever only. Maximum tokens across all results. Defaults to 25000. | | maxTokensPerPage | number | Retriever only. Maximum tokens per page. Defaults to 1024. | | country | string | ISO country code (e.g. "US"). | | searchDomainFilter | string[] | Restrict results to up to 20 domains. | | searchRecencyFilter | "day" \| "week" \| "month" \| "year" | Time-based filter. | | searchAfterDate | string | Only include content after this date (%m/%d/%Y). | | searchBeforeDate | string | Only include content before this date (%m/%d/%Y). |

License

MIT