@ai-sdk/perplexity 4.0.52 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +15 -9
- package/dist/index.d.ts +94 -126
- package/dist/index.js +964 -405
- package/dist/index.js.map +1 -1
- package/docs/70-perplexity.mdx +220 -69
- package/package.json +1 -1
- package/src/convert-perplexity-usage.ts +29 -15
- package/src/convert-to-perplexity-input.ts +206 -0
- package/src/index.ts +4 -0
- package/src/map-perplexity-finish-reason.ts +27 -8
- package/src/perplexity-agent-api.ts +206 -0
- package/src/perplexity-embedding-model.ts +5 -2
- package/src/perplexity-language-model-options.ts +91 -115
- package/src/perplexity-language-model-prompt.ts +25 -18
- package/src/perplexity-language-model.ts +641 -291
- package/src/perplexity-options.ts +9 -6
- package/src/perplexity-prepare-tools.ts +48 -0
- package/src/perplexity-provider.ts +3 -3
- package/src/convert-to-perplexity-messages.ts +0 -143
package/docs/70-perplexity.mdx
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Perplexity
|
|
3
|
-
description: Learn how to use Perplexity's
|
|
3
|
+
description: Learn how to use Perplexity's Agent API with the AI SDK.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Perplexity Provider
|
|
7
7
|
|
|
8
|
-
The [Perplexity](https://
|
|
8
|
+
The [Perplexity](https://www.perplexity.ai) provider offers access to the
|
|
9
|
+
[Agent API](https://docs.perplexity.ai/docs/agent-api/quickstart). The Agent API
|
|
10
|
+
can route across models, search the web, call tools, and return cited answers.
|
|
9
11
|
|
|
10
12
|
API keys can be obtained from the [Perplexity Platform](https://docs.perplexity.ai).
|
|
11
13
|
|
|
@@ -62,11 +64,19 @@ import { perplexity } from '@ai-sdk/perplexity';
|
|
|
62
64
|
import { generateText } from 'ai';
|
|
63
65
|
|
|
64
66
|
const { text } = await generateText({
|
|
65
|
-
model: perplexity('
|
|
67
|
+
model: perplexity('low'),
|
|
66
68
|
prompt: 'What are the latest developments in quantum computing?',
|
|
67
69
|
});
|
|
68
70
|
```
|
|
69
71
|
|
|
72
|
+
The Agent API provides the `fast`, `low`, `medium`, `high`, and `xhigh`
|
|
73
|
+
presets. You can also pass an Agent API model ID directly, such as
|
|
74
|
+
`perplexity/sonar`.
|
|
75
|
+
|
|
76
|
+
Version 5 uses the Agent API exclusively. Legacy Sonar model IDs and provider
|
|
77
|
+
options are not mapped; see [Migrating from v4 Sonar to v5 Agent
|
|
78
|
+
API](#migrating-from-v4-sonar-to-v5-agent-api) before upgrading.
|
|
79
|
+
|
|
70
80
|
### Sources
|
|
71
81
|
|
|
72
82
|
Websites that have been used to generate the response are included in the `sources` property of the result:
|
|
@@ -76,14 +86,81 @@ import { perplexity } from '@ai-sdk/perplexity';
|
|
|
76
86
|
import { generateText } from 'ai';
|
|
77
87
|
|
|
78
88
|
const { text, sources } = await generateText({
|
|
79
|
-
model: perplexity('
|
|
89
|
+
model: perplexity('low'),
|
|
80
90
|
prompt: 'What are the latest developments in quantum computing?',
|
|
81
91
|
});
|
|
82
92
|
|
|
83
93
|
console.log(sources);
|
|
84
94
|
```
|
|
85
95
|
|
|
86
|
-
###
|
|
96
|
+
### Agent Tools
|
|
97
|
+
|
|
98
|
+
Pass native Agent API tools through `providerOptions.perplexity.tools`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import {
|
|
102
|
+
perplexity,
|
|
103
|
+
type PerplexityLanguageModelOptions,
|
|
104
|
+
} from '@ai-sdk/perplexity';
|
|
105
|
+
import { generateText } from 'ai';
|
|
106
|
+
|
|
107
|
+
const { text, sources } = await generateText({
|
|
108
|
+
model: perplexity('low'),
|
|
109
|
+
prompt: 'Summarize recent US federal AI policy from official sources.',
|
|
110
|
+
providerOptions: {
|
|
111
|
+
perplexity: {
|
|
112
|
+
tools: [
|
|
113
|
+
{
|
|
114
|
+
type: 'web_search',
|
|
115
|
+
filters: {
|
|
116
|
+
search_domain_filter: [
|
|
117
|
+
'whitehouse.gov',
|
|
118
|
+
'congress.gov',
|
|
119
|
+
'federalregister.gov',
|
|
120
|
+
],
|
|
121
|
+
search_recency_filter: 'month',
|
|
122
|
+
},
|
|
123
|
+
max_results: 10,
|
|
124
|
+
search_context_size: 'medium',
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
} satisfies PerplexityLanguageModelOptions,
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Supported native tools are `web_search`, `fetch_url`, `people_search`,
|
|
133
|
+
`finance_search`, `sandbox`, `mcp`, and `connector`. Agent API presets include
|
|
134
|
+
preconfigured tools that remain enabled; the `tools` option can add explicit
|
|
135
|
+
tools or supply tools when using a direct model ID.
|
|
136
|
+
|
|
137
|
+
Search and fetch results are exposed as AI SDK sources. Other native tool traces,
|
|
138
|
+
including finance, sandbox, and MCP results, are available in `response.body`
|
|
139
|
+
and raw stream chunks (`include: { rawChunks: true }`). They are not exposed as
|
|
140
|
+
AI SDK client tool calls. Accounts with zero data retention may reject
|
|
141
|
+
file-capable tools such as `finance_search` and `sandbox`.
|
|
142
|
+
|
|
143
|
+
AI SDK function tools can be passed through the top-level `tools` option:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { perplexity } from '@ai-sdk/perplexity';
|
|
147
|
+
import { generateText, tool } from 'ai';
|
|
148
|
+
import { z } from 'zod';
|
|
149
|
+
|
|
150
|
+
const result = await generateText({
|
|
151
|
+
model: perplexity('low'),
|
|
152
|
+
prompt: 'What is the weather in San Francisco?',
|
|
153
|
+
tools: {
|
|
154
|
+
weather: tool({
|
|
155
|
+
description: 'Get the weather for a city',
|
|
156
|
+
inputSchema: z.object({ city: z.string() }),
|
|
157
|
+
execute: async ({ city }) => ({ city, temperature: 18 }),
|
|
158
|
+
}),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Provider Options and Metadata
|
|
87
164
|
|
|
88
165
|
The Perplexity provider includes additional metadata in the response through `providerMetadata`.
|
|
89
166
|
Additional configuration options are available through `providerOptions`.
|
|
@@ -96,12 +173,13 @@ import {
|
|
|
96
173
|
import { generateText } from 'ai';
|
|
97
174
|
|
|
98
175
|
const result = await generateText({
|
|
99
|
-
model: perplexity('
|
|
176
|
+
model: perplexity('low'),
|
|
100
177
|
prompt: 'What are the latest developments in quantum computing?',
|
|
101
178
|
providerOptions: {
|
|
102
179
|
perplexity: {
|
|
103
|
-
|
|
104
|
-
|
|
180
|
+
max_steps: 5,
|
|
181
|
+
store: true,
|
|
182
|
+
reasoning: { effort: 'low' },
|
|
105
183
|
} satisfies PerplexityLanguageModelOptions,
|
|
106
184
|
},
|
|
107
185
|
});
|
|
@@ -110,87 +188,160 @@ console.log(result.providerMetadata);
|
|
|
110
188
|
// Example output:
|
|
111
189
|
// {
|
|
112
190
|
// perplexity: {
|
|
113
|
-
// usage: { citationTokens:
|
|
114
|
-
// images:
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
// ]
|
|
191
|
+
// usage: { citationTokens: null, numSearchQueries: 1 },
|
|
192
|
+
// images: null,
|
|
193
|
+
// cost: { totalCost: 0.006, currency: 'USD', ... },
|
|
194
|
+
// toolCalls: { search_web: { invocation: 1 } },
|
|
118
195
|
// },
|
|
119
196
|
// }
|
|
120
197
|
```
|
|
121
198
|
|
|
122
199
|
#### Provider Options
|
|
123
200
|
|
|
124
|
-
The following
|
|
201
|
+
The following Agent API options are available:
|
|
202
|
+
|
|
203
|
+
- **instructions** _string_
|
|
204
|
+
|
|
205
|
+
Top-level instructions for the Agent API run.
|
|
206
|
+
|
|
207
|
+
- **tools** _array_
|
|
125
208
|
|
|
126
|
-
|
|
209
|
+
Native Agent API tools and their configuration.
|
|
127
210
|
|
|
128
|
-
|
|
211
|
+
- **models** _string[]_
|
|
129
212
|
|
|
130
|
-
|
|
213
|
+
A fallback model list for Agent API routing.
|
|
131
214
|
|
|
132
|
-
|
|
215
|
+
- **max_steps** _number_
|
|
216
|
+
|
|
217
|
+
The maximum number of agentic steps.
|
|
218
|
+
|
|
219
|
+
- **max_tool_calls** _number_
|
|
220
|
+
|
|
221
|
+
The maximum number of native tool calls. Setting this to `0` disables all
|
|
222
|
+
preset tools.
|
|
223
|
+
|
|
224
|
+
- **previous_response_id** _string_
|
|
225
|
+
|
|
226
|
+
Continue a conversation from an earlier Agent API response.
|
|
227
|
+
|
|
228
|
+
- **store** _boolean_
|
|
229
|
+
|
|
230
|
+
Whether the response can be retrieved later. Setting `store: false` hides it
|
|
231
|
+
from retrieval; it does not disable persistence, and the response can still
|
|
232
|
+
be used as a `previous_response_id` continuation source. See Perplexity's
|
|
233
|
+
[conversation state documentation](https://docs.perplexity.ai/docs/agent-api/conversation-state)
|
|
234
|
+
for retention behavior and account-specific zero data retention restrictions.
|
|
235
|
+
|
|
236
|
+
- **language_preference** _string_
|
|
237
|
+
|
|
238
|
+
Preferred response language as an ISO 639-1 language code.
|
|
239
|
+
|
|
240
|
+
- **reasoning** _object_
|
|
241
|
+
|
|
242
|
+
Reasoning configuration. `effort` supports `'minimal'`, `'low'`, `'medium'`,
|
|
243
|
+
`'high'`, and `'xhigh'`.
|
|
244
|
+
|
|
245
|
+
- **skills** _array_
|
|
246
|
+
|
|
247
|
+
Agent API skill configuration.
|
|
133
248
|
|
|
134
249
|
<Note>
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
250
|
+
See the [Perplexity Agent API
|
|
251
|
+
documentation](https://docs.perplexity.ai/docs/agent-api/quickstart) for
|
|
252
|
+
details about native request fields.
|
|
138
253
|
</Note>
|
|
139
254
|
|
|
140
255
|
#### Provider Metadata
|
|
141
256
|
|
|
142
257
|
The response metadata includes:
|
|
143
258
|
|
|
144
|
-
- `usage`:
|
|
145
|
-
|
|
259
|
+
- `usage`: `citationTokens` (always `null` for Agent API responses) and the
|
|
260
|
+
number of search queries
|
|
261
|
+
- `cost`: Token, tool, and total costs returned by Perplexity
|
|
262
|
+
- `toolCalls`: Invocation counts grouped by native tool
|
|
263
|
+
- `images`: Always `null`; the Agent API does not return Sonar image results
|
|
146
264
|
|
|
147
|
-
###
|
|
265
|
+
### Structured Output
|
|
148
266
|
|
|
149
|
-
|
|
150
|
-
|
|
267
|
+
`Output.object` and `Output.array` send an Agent API JSON schema response
|
|
268
|
+
format. JSON output without a schema is not supported.
|
|
151
269
|
|
|
152
|
-
|
|
153
|
-
const result = await generateText({
|
|
154
|
-
model: perplexity('sonar-pro'),
|
|
155
|
-
messages: [
|
|
156
|
-
{
|
|
157
|
-
role: 'user',
|
|
158
|
-
content: [
|
|
159
|
-
{
|
|
160
|
-
type: 'text',
|
|
161
|
-
text: 'What is this document about?',
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
type: 'file',
|
|
165
|
-
data: fs.readFileSync('./data/ai.pdf'),
|
|
166
|
-
mediaType: 'application/pdf',
|
|
167
|
-
filename: 'ai.pdf', // optional
|
|
168
|
-
},
|
|
169
|
-
],
|
|
170
|
-
},
|
|
171
|
-
],
|
|
172
|
-
});
|
|
173
|
-
```
|
|
270
|
+
### Input Images
|
|
174
271
|
|
|
175
|
-
|
|
272
|
+
Image URL and data inputs are converted to Agent API `input_image` parts.
|
|
273
|
+
The Agent API does not provide a Sonar-equivalent PDF input field, so PDF file
|
|
274
|
+
parts are not supported.
|
|
176
275
|
|
|
177
|
-
|
|
178
|
-
{
|
|
179
|
-
type: 'file',
|
|
180
|
-
data: new URL('https://example.com/document.pdf'),
|
|
181
|
-
mediaType: 'application/pdf',
|
|
182
|
-
filename: 'document.pdf', // optional
|
|
183
|
-
}
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
The model will have access to the contents of the PDF file and
|
|
187
|
-
respond to questions about it.
|
|
276
|
+
### Migrating from v4 Sonar to v5 Agent API
|
|
188
277
|
|
|
189
278
|
<Note>
|
|
190
|
-
|
|
191
|
-
|
|
279
|
+
Version 5 is a breaking migration from Sonar Chat Completions to the Agent
|
|
280
|
+
API. It does not provide legacy model aliases, translate Sonar provider
|
|
281
|
+
options, or emit migration warnings at runtime.
|
|
192
282
|
</Note>
|
|
193
283
|
|
|
284
|
+
<Warning>
|
|
285
|
+
Perplexity supports Sonar Chat Completions only until September 27, 2026.
|
|
286
|
+
Upgrade language-generation applications to the Agent API before that date;
|
|
287
|
+
staying on version 4 does not preserve service after the Sonar shutdown.
|
|
288
|
+
</Warning>
|
|
289
|
+
|
|
290
|
+
Replace each Sonar model ID with an Agent API preset or direct Agent API model
|
|
291
|
+
ID. These presets are suggested starting points, not equivalent aliases: a
|
|
292
|
+
preset can select different models and tools, so cost, latency, and output can
|
|
293
|
+
change.
|
|
294
|
+
|
|
295
|
+
| v4 Sonar model | Suggested v5 starting point |
|
|
296
|
+
| --------------------- | --------------------------- |
|
|
297
|
+
| `sonar` | `fast` |
|
|
298
|
+
| `sonar-pro` | `low` |
|
|
299
|
+
| `sonar-reasoning` | `medium` |
|
|
300
|
+
| `sonar-reasoning-pro` | `medium` |
|
|
301
|
+
| `sonar-deep-research` | `high` |
|
|
302
|
+
|
|
303
|
+
For example:
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
// v4
|
|
307
|
+
perplexity('sonar-pro');
|
|
308
|
+
|
|
309
|
+
// v5
|
|
310
|
+
perplexity('low');
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Move Sonar search and reasoning options to their Agent API locations:
|
|
314
|
+
|
|
315
|
+
| v4 provider option | v5 Agent API option |
|
|
316
|
+
| ---------------------------------------- | -------------------------------------- |
|
|
317
|
+
| Domain, recency, and date filters | `tools[].filters` on `web_search` |
|
|
318
|
+
| `num_search_results` | `tools[].max_results` |
|
|
319
|
+
| `web_search_options.search_context_size` | `tools[].search_context_size` |
|
|
320
|
+
| `web_search_options.user_location` | `tools[].user_location` |
|
|
321
|
+
| `reasoning_effort` | `reasoning.effort` |
|
|
322
|
+
| `disable_search` | Omit `web_search` for direct model IDs |
|
|
323
|
+
|
|
324
|
+
Preset tools cannot be removed individually. Use a direct model ID without a
|
|
325
|
+
`web_search` tool when search must be disabled, or set `max_tool_calls: 0` to
|
|
326
|
+
disable all preset tool calls.
|
|
327
|
+
|
|
328
|
+
The Agent API has no equivalent for Sonar PDF or video inputs, image or video
|
|
329
|
+
results, `search_language_filter`, or `stream_mode`. Related questions require
|
|
330
|
+
prompting or structured output. Remove these options during migration.
|
|
331
|
+
|
|
332
|
+
Language requests now use `/v1/agent`. Custom `baseURL` proxies must route that
|
|
333
|
+
path. Raw responses and streams use the Agent API's typed output and SSE event
|
|
334
|
+
formats, and Perplexity provider metadata, usage, cost, and source identifiers
|
|
335
|
+
can differ. The embeddings API is unchanged.
|
|
336
|
+
|
|
337
|
+
For PDF inputs, extract the document text before sending it to the Agent API,
|
|
338
|
+
or choose another provider that supports PDF input. Move image/video retrieval
|
|
339
|
+
and language-filtered search to a separate service if your application requires
|
|
340
|
+
those features. Rework consumers of raw Sonar stream events for typed Agent
|
|
341
|
+
events. See Perplexity's [migration
|
|
342
|
+
guide](https://docs.perplexity.ai/docs/agent-api/migrate-from-sonar/how-to)
|
|
343
|
+
for the full API-level comparison.
|
|
344
|
+
|
|
194
345
|
## Embedding Models
|
|
195
346
|
|
|
196
347
|
You can create models that call the [Perplexity embeddings API](https://docs.perplexity.ai/docs/embeddings/quickstart)
|
|
@@ -266,13 +417,13 @@ The following optional provider options are available for embedding models:
|
|
|
266
417
|
|
|
267
418
|
## Model Capabilities
|
|
268
419
|
|
|
269
|
-
| Model
|
|
270
|
-
|
|
|
271
|
-
| `
|
|
272
|
-
| `
|
|
273
|
-
| `
|
|
274
|
-
| `
|
|
275
|
-
| `
|
|
420
|
+
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
|
|
421
|
+
| -------- | ----------- | ----------------- | ---------- | -------------- |
|
|
422
|
+
| `fast` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
423
|
+
| `low` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
424
|
+
| `medium` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
425
|
+
| `high` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
426
|
+
| `xhigh` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
276
427
|
|
|
277
428
|
<Note>
|
|
278
429
|
Please see the [Perplexity docs](https://docs.perplexity.ai) for detailed API
|
package/package.json
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import type { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
1
|
+
import type { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
import { createNullLanguageModelUsage } from '@ai-sdk/provider-utils';
|
|
3
3
|
|
|
4
4
|
export function convertPerplexityUsage(
|
|
5
5
|
usage:
|
|
6
6
|
| {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
input_tokens?: number | null;
|
|
8
|
+
output_tokens?: number | null;
|
|
9
|
+
total_tokens?: number | null;
|
|
10
|
+
input_tokens_details?: {
|
|
11
|
+
cached_tokens?: number | null;
|
|
12
|
+
cache_creation_input_tokens?: number | null;
|
|
13
|
+
cache_read_input_tokens?: number | null;
|
|
14
|
+
} | null;
|
|
15
|
+
output_tokens_details?: {
|
|
16
|
+
reasoning_tokens?: number | null;
|
|
17
|
+
} | null;
|
|
18
|
+
[key: string]: unknown;
|
|
10
19
|
}
|
|
11
20
|
| undefined
|
|
12
21
|
| null,
|
|
@@ -15,23 +24,28 @@ export function convertPerplexityUsage(
|
|
|
15
24
|
return createNullLanguageModelUsage();
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
27
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
28
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
29
|
+
const cacheReadTokens =
|
|
30
|
+
usage.input_tokens_details?.cache_read_input_tokens ??
|
|
31
|
+
usage.input_tokens_details?.cached_tokens ??
|
|
32
|
+
0;
|
|
33
|
+
const cacheWriteTokens =
|
|
34
|
+
usage.input_tokens_details?.cache_creation_input_tokens ?? 0;
|
|
35
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
|
|
21
36
|
|
|
22
37
|
return {
|
|
23
38
|
inputTokens: {
|
|
24
|
-
total:
|
|
25
|
-
noCache:
|
|
26
|
-
cacheRead:
|
|
27
|
-
cacheWrite:
|
|
39
|
+
total: inputTokens,
|
|
40
|
+
noCache: Math.max(0, inputTokens - cacheReadTokens - cacheWriteTokens),
|
|
41
|
+
cacheRead: cacheReadTokens,
|
|
42
|
+
cacheWrite: cacheWriteTokens,
|
|
28
43
|
},
|
|
29
44
|
outputTokens: {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
text: completionTokens,
|
|
45
|
+
total: outputTokens,
|
|
46
|
+
text: Math.max(0, outputTokens - reasoningTokens),
|
|
33
47
|
reasoning: reasoningTokens,
|
|
34
48
|
},
|
|
35
|
-
raw: usage,
|
|
49
|
+
raw: usage as JSONObject,
|
|
36
50
|
};
|
|
37
51
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnsupportedFunctionalityError,
|
|
3
|
+
type LanguageModelV4Prompt,
|
|
4
|
+
type LanguageModelV4ToolResultOutput,
|
|
5
|
+
type SharedV4Warning,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
7
|
+
import {
|
|
8
|
+
convertUint8ArrayToBase64,
|
|
9
|
+
getTopLevelMediaType,
|
|
10
|
+
resolveFullMediaType,
|
|
11
|
+
} from '@ai-sdk/provider-utils';
|
|
12
|
+
import type {
|
|
13
|
+
PerplexityAgentInput,
|
|
14
|
+
PerplexityAgentInputContent,
|
|
15
|
+
} from './perplexity-language-model-prompt';
|
|
16
|
+
|
|
17
|
+
function serializeToolOutput(output: LanguageModelV4ToolResultOutput): string {
|
|
18
|
+
switch (output.type) {
|
|
19
|
+
case 'text':
|
|
20
|
+
case 'error-text':
|
|
21
|
+
return output.value;
|
|
22
|
+
case 'json':
|
|
23
|
+
case 'error-json':
|
|
24
|
+
return JSON.stringify(output.value);
|
|
25
|
+
case 'execution-denied':
|
|
26
|
+
return output.reason ?? 'Tool call execution denied.';
|
|
27
|
+
case 'content': {
|
|
28
|
+
if (output.value.some(part => part.type !== 'text')) {
|
|
29
|
+
throw new UnsupportedFunctionalityError({
|
|
30
|
+
functionality: 'file and custom tool result content',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return output.value
|
|
34
|
+
.map(part => (part.type === 'text' ? part.text : ''))
|
|
35
|
+
.join('');
|
|
36
|
+
}
|
|
37
|
+
default: {
|
|
38
|
+
const _exhaustiveCheck: never = output;
|
|
39
|
+
throw new Error(`Unsupported tool output: ${_exhaustiveCheck}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getThoughtSignature(
|
|
45
|
+
providerOptions:
|
|
46
|
+
| Record<string, Record<string, unknown> | undefined>
|
|
47
|
+
| undefined,
|
|
48
|
+
): string | undefined {
|
|
49
|
+
const thoughtSignature = providerOptions?.perplexity?.thoughtSignature;
|
|
50
|
+
return typeof thoughtSignature === 'string' ? thoughtSignature : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function convertToPerplexityInput(prompt: LanguageModelV4Prompt): {
|
|
54
|
+
input: PerplexityAgentInput;
|
|
55
|
+
warnings: SharedV4Warning[];
|
|
56
|
+
} {
|
|
57
|
+
const input: PerplexityAgentInput = [];
|
|
58
|
+
const warnings: SharedV4Warning[] = [];
|
|
59
|
+
|
|
60
|
+
for (const { role, content } of prompt) {
|
|
61
|
+
switch (role) {
|
|
62
|
+
case 'system': {
|
|
63
|
+
input.push({ type: 'message', role: 'system', content });
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
case 'user': {
|
|
68
|
+
const convertedContent: PerplexityAgentInputContent[] = content.map(
|
|
69
|
+
part => {
|
|
70
|
+
switch (part.type) {
|
|
71
|
+
case 'text':
|
|
72
|
+
return { type: 'input_text', text: part.text };
|
|
73
|
+
case 'file': {
|
|
74
|
+
if (getTopLevelMediaType(part.mediaType) !== 'image') {
|
|
75
|
+
throw new UnsupportedFunctionalityError({
|
|
76
|
+
functionality: `file part media type ${part.mediaType}`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
switch (part.data.type) {
|
|
81
|
+
case 'url':
|
|
82
|
+
return {
|
|
83
|
+
type: 'input_image',
|
|
84
|
+
image_url: part.data.url.toString(),
|
|
85
|
+
};
|
|
86
|
+
case 'data':
|
|
87
|
+
return {
|
|
88
|
+
type: 'input_image',
|
|
89
|
+
image_url: `data:${resolveFullMediaType({ part })};base64,${
|
|
90
|
+
typeof part.data.data === 'string'
|
|
91
|
+
? part.data.data
|
|
92
|
+
: convertUint8ArrayToBase64(part.data.data)
|
|
93
|
+
}`,
|
|
94
|
+
};
|
|
95
|
+
case 'reference':
|
|
96
|
+
throw new UnsupportedFunctionalityError({
|
|
97
|
+
functionality: 'file parts with provider references',
|
|
98
|
+
});
|
|
99
|
+
case 'text':
|
|
100
|
+
throw new UnsupportedFunctionalityError({
|
|
101
|
+
functionality: 'text file parts',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const isTextOnly = convertedContent.every(
|
|
110
|
+
part => part.type === 'input_text',
|
|
111
|
+
);
|
|
112
|
+
input.push({
|
|
113
|
+
type: 'message',
|
|
114
|
+
role: 'user',
|
|
115
|
+
content: isTextOnly
|
|
116
|
+
? convertedContent
|
|
117
|
+
.map(part => (part.type === 'input_text' ? part.text : ''))
|
|
118
|
+
.join('')
|
|
119
|
+
: convertedContent,
|
|
120
|
+
});
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
case 'assistant': {
|
|
125
|
+
const text = content
|
|
126
|
+
.filter(part => part.type === 'text')
|
|
127
|
+
.map(part => part.text)
|
|
128
|
+
.join('');
|
|
129
|
+
|
|
130
|
+
if (text.length > 0) {
|
|
131
|
+
input.push({ type: 'message', role: 'assistant', content: text });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const part of content) {
|
|
135
|
+
switch (part.type) {
|
|
136
|
+
case 'text':
|
|
137
|
+
break;
|
|
138
|
+
case 'tool-call':
|
|
139
|
+
input.push({
|
|
140
|
+
type: 'function_call',
|
|
141
|
+
call_id: part.toolCallId,
|
|
142
|
+
name: part.toolName,
|
|
143
|
+
arguments: JSON.stringify(part.input ?? {}),
|
|
144
|
+
thought_signature: getThoughtSignature(part.providerOptions),
|
|
145
|
+
});
|
|
146
|
+
break;
|
|
147
|
+
case 'tool-result':
|
|
148
|
+
input.push({
|
|
149
|
+
type: 'function_call_output',
|
|
150
|
+
call_id: part.toolCallId,
|
|
151
|
+
name: part.toolName,
|
|
152
|
+
output: serializeToolOutput(part.output),
|
|
153
|
+
thought_signature: getThoughtSignature(part.providerOptions),
|
|
154
|
+
});
|
|
155
|
+
break;
|
|
156
|
+
case 'reasoning':
|
|
157
|
+
warnings.push({
|
|
158
|
+
type: 'unsupported',
|
|
159
|
+
feature: 'reasoning content in prompt',
|
|
160
|
+
});
|
|
161
|
+
break;
|
|
162
|
+
case 'file':
|
|
163
|
+
case 'reasoning-file':
|
|
164
|
+
case 'custom':
|
|
165
|
+
throw new UnsupportedFunctionalityError({
|
|
166
|
+
functionality: `assistant ${part.type} parts`,
|
|
167
|
+
});
|
|
168
|
+
default: {
|
|
169
|
+
const _exhaustiveCheck: never = part;
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Unsupported assistant part: ${_exhaustiveCheck}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
case 'tool': {
|
|
180
|
+
for (const part of content) {
|
|
181
|
+
if (part.type === 'tool-approval-response') {
|
|
182
|
+
throw new UnsupportedFunctionalityError({
|
|
183
|
+
functionality: 'tool approval responses',
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
input.push({
|
|
188
|
+
type: 'function_call_output',
|
|
189
|
+
call_id: part.toolCallId,
|
|
190
|
+
name: part.toolName,
|
|
191
|
+
output: serializeToolOutput(part.output),
|
|
192
|
+
thought_signature: getThoughtSignature(part.providerOptions),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
default: {
|
|
199
|
+
const _exhaustiveCheck: never = role;
|
|
200
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { input, warnings };
|
|
206
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,10 @@ export type {
|
|
|
4
4
|
PerplexityProviderSettings,
|
|
5
5
|
} from './perplexity-provider';
|
|
6
6
|
export type { PerplexityLanguageModelOptions } from './perplexity-language-model-options';
|
|
7
|
+
export type {
|
|
8
|
+
PerplexityAgentPreset,
|
|
9
|
+
PerplexityLanguageModelId,
|
|
10
|
+
} from './perplexity-options';
|
|
7
11
|
export type {
|
|
8
12
|
PerplexityEmbeddingModelId,
|
|
9
13
|
PerplexityEmbeddingModelOptions,
|