Perplexity Provider

Perplexity is supported via OpenAI API compatibility - the OpenAI provider is used in the examples below.

The Perplexity provider contains language model support for the Perplexity API. It creates language model objects that can be used with the generateText and streamText.

Setup

The Perplexity provider is available via the @ai-sdk/openai module as it is compatible with the OpenAI API. You can install it with

pnpm
npm
yarn
pnpm add @ai-sdk/openai

Provider Instance

To use Perplexity, you can create a custom provider instance with the createOpenAI function from @ai-sdk/openai:

import { createOpenAI } from '@ai-sdk/openai';
const perplexity = createOpenAI({
apiKey: process.env.PERPLEXITY_API_KEY ?? '',
baseURL: 'https://api.perplexity.ai/',
});

Language Models

You can create Perplexity models using a provider instance. The first argument is the model id, e.g. llama-3-sonar-large-32k-online.

const model = perplexity('llama-3-sonar-large-32k-online');

Example

You can use Perplexity language models to generate text with the generateText function:

import { createOpenAI } from '@ai-sdk/openai';
import { generateText } from 'ai';
const perplexity = createOpenAI({
apiKey: process.env.PERPLEXITY_API_KEY ?? '',
baseURL: 'https://api.perplexity.ai/',
});
const { text } = await generateText({
model: perplexity('llama-3-sonar-large-32k-online'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

Perplexity language models can also be used in the streamText and streamUI functions (see AI SDK Core and AI SDK RSC).