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

Overview

Integration details

ClassPackageLocalSerializablePY supportDownloadsVersion
ChatPerplexity@langchain/communitybetaNPM - DownloadsNPM - Version

Model features

See the links in the table headers below for guides on how to use specific features.
Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingToken usageLogprobs
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/community integration package.

Credentials

Head to https://perplexity.ai to sign up for Perplexity 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/community package:
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
<IntegrationInstallTooltip></IntegrationInstallTooltip>

<Npm2Yarn>
  @langchain/community @langchain/core
</Npm2Yarn>

Instantiation

Now we can instantiate our model object and generate chat completions:
import { ChatPerplexity } from "@langchain/community/chat_models/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.

API reference

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