content/providers/01-ai-sdk-providers/70-perplexity.mdx
The Perplexity provider offers access to Sonar API - a language model that uniquely combines real-time web search with natural language processing. Each response is grounded in current web data and includes detailed citations, making it ideal for research, fact-checking, and obtaining up-to-date information.
API keys can be obtained from the Perplexity Platform.
The Perplexity provider is available via the @ai-sdk/perplexity module. You can install it with:
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}> <Tab> <Snippet text="pnpm add @ai-sdk/perplexity" dark /> </Tab> <Tab> <Snippet text="npm install @ai-sdk/perplexity" dark /> </Tab> <Tab> <Snippet text="yarn add @ai-sdk/perplexity" dark /> </Tab>
<Tab> <Snippet text="bun add @ai-sdk/perplexity" dark /> </Tab> </Tabs>You can import the default provider instance perplexity from @ai-sdk/perplexity:
import { perplexity } from '@ai-sdk/perplexity';
For custom configuration, you can import createPerplexity and create a provider instance with your settings:
import { createPerplexity } from '@ai-sdk/perplexity';
const perplexity = createPerplexity({
apiKey: process.env.PERPLEXITY_API_KEY ?? '',
});
You can use the following optional settings to customize the Perplexity provider instance:
baseURL string
Use a different URL prefix for API calls.
The default prefix is https://api.perplexity.ai.
apiKey string
API key that is being sent using the Authorization header. It defaults to
the PERPLEXITY_API_KEY environment variable.
headers Record<string,string>
Custom headers to include in the requests.
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation.
You can create Perplexity models using a provider instance:
import { perplexity } from '@ai-sdk/perplexity';
import { generateText } from 'ai';
const { text } = await generateText({
model: perplexity('sonar-pro'),
prompt: 'What are the latest developments in quantum computing?',
});
Websites that have been used to generate the response are included in the sources property of the result:
import { perplexity } from '@ai-sdk/perplexity';
import { generateText } from 'ai';
const { text, sources } = await generateText({
model: perplexity('sonar-pro'),
prompt: 'What are the latest developments in quantum computing?',
});
console.log(sources);
The Perplexity provider includes additional metadata in the response through providerMetadata.
Additional configuration options are available through providerOptions.
const result = await generateText({
model: perplexity('sonar-pro'),
prompt: 'What are the latest developments in quantum computing?',
providerOptions: {
perplexity: {
return_images: true, // Enable image responses (Tier-2 Perplexity users only)
search_recency_filter: 'week', // Filter search results by recency
},
},
});
console.log(result.providerMetadata);
// Example output:
// {
// perplexity: {
// usage: { citationTokens: 5286, numSearchQueries: 1 },
// images: [
// { imageUrl: "https://example.com/image1.jpg", originUrl: "https://elsewhere.com/page1", height: 1280, width: 720 },
// { imageUrl: "https://example.com/image2.jpg", originUrl: "https://elsewhere.com/page2", height: 1280, width: 720 }
// ]
// },
// }
The following provider-specific options are available:
return_images boolean
Enable image responses. When set to true, the response may include relevant images. This feature is only available to Perplexity Tier-2 users and above.
search_recency_filter string
Filter search results by recency. Possible values: 'hour', 'day', 'week', 'month'. If not specified, defaults to all time.
The response metadata includes:
usage: Object containing citationTokens and numSearchQueries metricsimages: Array of image objects when return_images is enabled (Tier-2 users only). Each image contains imageUrl, originUrl, height, and width.The Perplexity provider supports reading PDF files.
You can pass PDF files as part of the message content using the file type:
const result = await generateText({
model: perplexity('sonar-pro'),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'What is this document about?',
},
{
type: 'file',
data: fs.readFileSync('./data/ai.pdf'),
mediaType: 'application/pdf',
filename: 'ai.pdf', // optional
},
],
},
],
});
You can also pass the URL of a PDF:
{
type: 'file',
data: new URL('https://example.com/document.pdf'),
mediaType: 'application/pdf',
filename: 'document.pdf', // optional
}
The model will have access to the contents of the PDF file and respond to questions about it.
<Note> For more details about Perplexity's capabilities, see the [Perplexity chat completion docs](https://docs.perplexity.ai/api-reference/chat-completions). </Note>| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
|---|---|---|---|---|
sonar-deep-research | <Cross size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
sonar-reasoning-pro | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
sonar-reasoning | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
sonar-pro | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
sonar | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> |