Skip to main content

Documentation Index

Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-naomid-1779801766-572e290.mintlify.app/llms.txt

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

This guide will help you get started with Perplexity chat models. For detailed documentation of all ChatPerplexity features and configurations head to the API reference.

Overview

Integration details

ClassPackageSerializablePY supportDownloadsVersion
ChatPerplexity@langchain/perplexitybetaNPM - DownloadsNPM - Version

Model features

See the links in the table headers below for guides on how to use specific features. Note that at the time of writing, Perplexity only supports structured outputs on certain usage tiers.

Setup

To access Perplexity models you’ll need to create a Perplexity account, get an API key, and install the @langchain/perplexity integration package.

Credentials

Head to the Perplexity API key dashboard to sign up and generate an API key. Once you’ve done this set the PERPLEXITY_API_KEY environment variable:
export PERPLEXITY_API_KEY="your-api-key"
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

Installation

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

Instantiation

Now you can instantiate the model:
import { ChatPerplexity } from "@langchain/perplexity";

const llm = new ChatPerplexity({
  model: "sonar",
  temperature: 0,
  maxTokens: undefined,
  timeout: undefined,
  maxRetries: 2,
  // other params...
});

Invocation

const aiMsg = await llm.invoke([
  {
    role: "system",
    content: "You are a helpful assistant that translates English to French. Translate the user sentence.",
  },
  {
    role: "user",
    content: "I love programming.",
  },
]);
aiMsg;
AIMessage {
  "id": "run-71853938-aa30-4861-9019-f12323c09f9a",
  "content": "J'adore la programmation.",
  "additional_kwargs": {
    "citations": [
      "https://careersatagoda.com/blog/why-we-love-programming/",
      "https://henrikwarne.com/2012/06/02/why-i-love-coding/",
      "https://forum.freecodecamp.org/t/i-love-programming-but/497502",
      "https://ilovecoding.org",
      "https://thecodinglove.com"
    ]
  },
  "response_metadata": {
    "tokenUsage": {
      "promptTokens": 20,
      "completionTokens": 9,
      "totalTokens": 29
    }
  },
  "tool_calls": [],
  "invalid_tool_calls": []
}
console.log(aiMsg.content);
J'adore la programmation.

The @langchain/perplexity package also includes search components that do not use the chat API: See the Perplexity provider overview for setup on all three components.

API reference

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