-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (provider/perplexity): add Perplexity provider (#4501)
Co-authored-by: ybelakov <[email protected]>
- Loading branch information
Showing
26 changed files
with
903 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ai-sdk/perplexity': patch | ||
--- | ||
|
||
feat (provider/perplexity): add Perplexity provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
content/providers/01-ai-sdk-providers/70-perplexity.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
title: Perplexity | ||
description: Learn how to use Perplexity's Sonar API with the AI SDK. | ||
--- | ||
|
||
# Perplexity Provider | ||
|
||
The [Perplexity](https://sonar.perplexity.ai) 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](https://docs.perplexity.ai). | ||
|
||
## Setup | ||
|
||
The Perplexity provider is available via the `@ai-sdk/perplexity` module. You can install it with: | ||
|
||
<Tabs items={['pnpm', 'npm', 'yarn']}> | ||
<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> | ||
</Tabs> | ||
|
||
## Provider Instance | ||
|
||
You can import the default provider instance `perplexity` from `@ai-sdk/perplexity`: | ||
|
||
```ts | ||
import { perplexity } from '@ai-sdk/perplexity'; | ||
``` | ||
|
||
For custom configuration, you can import `createPerplexity` and create a provider instance with your settings: | ||
|
||
```ts | ||
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](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation. | ||
|
||
## Language Models | ||
|
||
You can create Perplexity models using a provider instance: | ||
|
||
```ts | ||
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?', | ||
}); | ||
``` | ||
|
||
Perplexity models can be used in the `streamText` and `streamUI` functions (see | ||
[AI SDK Core](/docs/ai-sdk-core) and [AI SDK RSC](/docs/ai-sdk-rsc)). | ||
|
||
### Provider Metadata | ||
|
||
The Perplexity provider includes additional experimental metadata in the response through `experimental_providerMetadata`: | ||
|
||
```ts | ||
const result = await generateText({ | ||
model: perplexity('sonar-pro'), | ||
prompt: 'What are the latest developments in quantum computing?', | ||
}); | ||
|
||
console.log(result.experimental_providerMetadata); | ||
// Example output: | ||
// { | ||
// perplexity: { | ||
// citations: [ | ||
// 'https://www.sfchronicle.com', | ||
// 'https://www.cbsnews.com/sanfrancisco/', | ||
// ], | ||
// usage: { citationTokens: 5286, numSearchQueries: 1 }, | ||
// }, | ||
// } | ||
``` | ||
|
||
The metadata includes: | ||
|
||
- `citations`: Array of URLs used as sources for the response | ||
- `usage`: Object containing `citationTokens` and `numSearchQueries` metrics | ||
|
||
<Note> | ||
For more details about Perplexity's citations see the [Perplexity chat | ||
completion docs](https://docs.perplexity.ai/api-reference/chat-completions). | ||
</Note> | ||
|
||
## Model Capabilities | ||
|
||
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | | ||
| ----------- | ------------------- | ------------------- | ------------------- | ------------------- | | ||
| `sonar-pro` | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | | ||
| `sonar` | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | | ||
|
||
### Key Features | ||
|
||
- **Real-time Web Search**: Both models provide grounded responses using real-time web search | ||
- **Citations**: Sonar Pro provides 2x more citations than standard Sonar | ||
- **Data Privacy**: No training on customer data | ||
- **Self-serve API**: Immediate access with scalable pricing | ||
- **Advanced Queries**: Support for complex queries and follow-up questions | ||
|
||
<Note> | ||
Please see the [Perplexity docs](https://docs.perplexity.ai) for detailed API | ||
documentation and the latest updates. | ||
</Note> |
71 changes: 0 additions & 71 deletions
71
content/providers/02-openai-compatible-providers/10-perplexity.mdx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'dotenv/config'; | ||
import { expect } from 'vitest'; | ||
import { | ||
perplexity as provider, | ||
PerplexityErrorData, | ||
} from '@ai-sdk/perplexity'; | ||
import { | ||
createFeatureTestSuite, | ||
createLanguageModelWithCapabilities, | ||
} from './feature-test-suite'; | ||
import { APICallError } from '@ai-sdk/provider'; | ||
|
||
const createChatModel = (modelId: string) => | ||
createLanguageModelWithCapabilities(provider.chat(modelId)); | ||
|
||
createFeatureTestSuite({ | ||
name: 'perplexity', | ||
models: { | ||
invalidModel: provider.chat('no-such-model'), | ||
languageModels: [createChatModel('sonar-pro'), createChatModel('sonar')], | ||
}, | ||
timeout: 30000, | ||
customAssertions: { | ||
errorValidator: (error: APICallError) => { | ||
expect((error.data as PerplexityErrorData).code).toBe( | ||
'Some requested entity was not found', | ||
); | ||
}, | ||
}, | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'dotenv/config'; | ||
import { perplexity } from '@ai-sdk/perplexity'; | ||
import { generateText } from 'ai'; | ||
|
||
async function main() { | ||
const result = await generateText({ | ||
model: perplexity('sonar-pro'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}); | ||
|
||
console.log(result.text); | ||
console.log(); | ||
console.log('Token usage:', result.usage); | ||
console.log('Finish reason:', result.finishReason); | ||
console.log('Metadata:', result.experimental_providerMetadata); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @ai-sdk/perplexity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# AI SDK - Perplexity Provider | ||
|
||
The **[Perplexity provider](https://sdk.vercel.ai/providers/ai-sdk-providers/perplexity)** for the [AI SDK](https://sdk.vercel.ai/docs) | ||
contains language model support for Perplexity's Sonar API - a powerful answer engine with real-time web search capabilities. | ||
|
||
## Features | ||
|
||
- Real-time web search grounding for accurate, up-to-date responses | ||
- Support for advanced queries and follow-up questions | ||
- Multiple tiers available: | ||
- **Sonar Pro**: Enhanced capabilities for complex tasks with 2x more citations | ||
- **Sonar**: Lightweight offering optimized for speed and cost | ||
- Industry-leading answer quality | ||
- Data privacy - no training on customer data | ||
- Self-serve API access with scalable pricing | ||
|
||
## Setup | ||
|
||
The Perplexity provider is available in the `@ai-sdk/perplexity` module. You can install it with: | ||
|
||
```bash | ||
npm i @ai-sdk/perplexity | ||
``` | ||
|
||
## Provider Instance | ||
|
||
You can import the default provider instance `perplexity` from `@ai-sdk/perplexity`: | ||
|
||
```ts | ||
import { perplexity } from '@ai-sdk/perplexity'; | ||
``` | ||
|
||
## Example | ||
|
||
```ts | ||
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?', | ||
}); | ||
``` | ||
|
||
## Documentation | ||
|
||
Please check out the **[Perplexity provider documentation](https://sdk.vercel.ai/providers/ai-sdk-providers/perplexity)** for more information. |
Oops, something went wrong.