@ai-sdk/perplexity 4.0.0-beta.3 → 4.0.0-beta.31
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 +230 -4
- package/README.md +2 -0
- package/dist/index.js +143 -107
- package/dist/index.js.map +1 -1
- package/package.json +10 -11
- package/src/convert-to-perplexity-messages.ts +10 -1
- package/src/perplexity-language-model.ts +64 -3
- package/dist/index.d.mts +0 -44
- package/dist/index.mjs +0 -513
- package/dist/index.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/perplexity",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.31",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"license": "Apache-2.0",
|
|
5
6
|
"sideEffects": false,
|
|
6
7
|
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
@@ -24,21 +24,21 @@
|
|
|
24
24
|
"./package.json": "./package.json",
|
|
25
25
|
".": {
|
|
26
26
|
"types": "./dist/index.d.ts",
|
|
27
|
-
"import": "./dist/index.
|
|
28
|
-
"
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ai-sdk/provider": "4.0.0-beta.
|
|
33
|
-
"@ai-sdk/provider-utils": "5.0.0-beta.
|
|
32
|
+
"@ai-sdk/provider": "4.0.0-beta.12",
|
|
33
|
+
"@ai-sdk/provider-utils": "5.0.0-beta.27"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "20.17.24",
|
|
37
37
|
"tsup": "^8",
|
|
38
38
|
"typescript": "5.8.3",
|
|
39
39
|
"zod": "3.25.76",
|
|
40
|
-
"@ai-
|
|
41
|
-
"@
|
|
40
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
41
|
+
"@ai-sdk/test-server": "2.0.0-beta.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -47,7 +47,8 @@
|
|
|
47
47
|
"node": ">=18"
|
|
48
48
|
},
|
|
49
49
|
"publishConfig": {
|
|
50
|
-
"access": "public"
|
|
50
|
+
"access": "public",
|
|
51
|
+
"provenance": true
|
|
51
52
|
},
|
|
52
53
|
"homepage": "https://ai-sdk.dev/docs",
|
|
53
54
|
"repository": {
|
|
@@ -64,9 +65,7 @@
|
|
|
64
65
|
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
65
66
|
"build:watch": "pnpm clean && tsup --watch",
|
|
66
67
|
"clean": "del-cli dist docs *.tsbuildinfo",
|
|
67
|
-
"lint": "eslint \"./**/*.ts*\"",
|
|
68
68
|
"type-check": "tsc --build",
|
|
69
|
-
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
|
70
69
|
"test": "pnpm test:node && pnpm test:edge",
|
|
71
70
|
"test:update": "pnpm test:node -u",
|
|
72
71
|
"test:watch": "vitest --config vitest.node.config.js",
|
|
@@ -6,7 +6,10 @@ import {
|
|
|
6
6
|
PerplexityMessageContent,
|
|
7
7
|
PerplexityPrompt,
|
|
8
8
|
} from './perplexity-language-model-prompt';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
convertUint8ArrayToBase64,
|
|
11
|
+
isProviderReference,
|
|
12
|
+
} from '@ai-sdk/provider-utils';
|
|
10
13
|
|
|
11
14
|
export function convertToPerplexityMessages(
|
|
12
15
|
prompt: LanguageModelV4Prompt,
|
|
@@ -38,6 +41,12 @@ export function convertToPerplexityMessages(
|
|
|
38
41
|
};
|
|
39
42
|
}
|
|
40
43
|
case 'file': {
|
|
44
|
+
if (isProviderReference(part.data)) {
|
|
45
|
+
throw new UnsupportedFunctionalityError({
|
|
46
|
+
functionality: 'file parts with provider references',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
41
50
|
if (part.mediaType === 'application/pdf') {
|
|
42
51
|
return part.data instanceof URL
|
|
43
52
|
? {
|
|
@@ -15,7 +15,11 @@ import {
|
|
|
15
15
|
createEventSourceResponseHandler,
|
|
16
16
|
createJsonErrorResponseHandler,
|
|
17
17
|
createJsonResponseHandler,
|
|
18
|
+
isCustomReasoning,
|
|
18
19
|
postJsonToApi,
|
|
20
|
+
serializeModelOptions,
|
|
21
|
+
WORKFLOW_SERIALIZE,
|
|
22
|
+
WORKFLOW_DESERIALIZE,
|
|
19
23
|
} from '@ai-sdk/provider-utils';
|
|
20
24
|
import { z } from 'zod/v4';
|
|
21
25
|
import { convertPerplexityUsage } from './convert-perplexity-usage';
|
|
@@ -25,7 +29,7 @@ import { PerplexityLanguageModelId } from './perplexity-language-model-options';
|
|
|
25
29
|
|
|
26
30
|
type PerplexityChatConfig = {
|
|
27
31
|
baseURL: string;
|
|
28
|
-
headers
|
|
32
|
+
headers?: () => Record<string, string | undefined>;
|
|
29
33
|
generateId: () => string;
|
|
30
34
|
fetch?: FetchFunction;
|
|
31
35
|
};
|
|
@@ -38,6 +42,20 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
38
42
|
|
|
39
43
|
private readonly config: PerplexityChatConfig;
|
|
40
44
|
|
|
45
|
+
static [WORKFLOW_SERIALIZE](model: PerplexityLanguageModel) {
|
|
46
|
+
return serializeModelOptions({
|
|
47
|
+
modelId: model.modelId,
|
|
48
|
+
config: model.config,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
53
|
+
modelId: PerplexityLanguageModelId;
|
|
54
|
+
config: PerplexityChatConfig;
|
|
55
|
+
}) {
|
|
56
|
+
return new PerplexityLanguageModel(options.modelId, options.config);
|
|
57
|
+
}
|
|
58
|
+
|
|
41
59
|
constructor(
|
|
42
60
|
modelId: PerplexityLanguageModelId,
|
|
43
61
|
config: PerplexityChatConfig,
|
|
@@ -59,6 +77,7 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
59
77
|
frequencyPenalty,
|
|
60
78
|
presencePenalty,
|
|
61
79
|
stopSequences,
|
|
80
|
+
reasoning,
|
|
62
81
|
responseFormat,
|
|
63
82
|
seed,
|
|
64
83
|
providerOptions,
|
|
@@ -77,6 +96,14 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
77
96
|
warnings.push({ type: 'unsupported', feature: 'seed' });
|
|
78
97
|
}
|
|
79
98
|
|
|
99
|
+
if (isCustomReasoning(reasoning)) {
|
|
100
|
+
warnings.push({
|
|
101
|
+
type: 'unsupported',
|
|
102
|
+
feature: 'reasoning',
|
|
103
|
+
details: 'This provider does not support reasoning configuration.',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
80
107
|
return {
|
|
81
108
|
args: {
|
|
82
109
|
// model id:
|
|
@@ -120,7 +147,7 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
120
147
|
rawValue: rawResponse,
|
|
121
148
|
} = await postJsonToApi({
|
|
122
149
|
url: `${this.config.baseURL}/chat/completions`,
|
|
123
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
150
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
124
151
|
body,
|
|
125
152
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
126
153
|
errorSchema: perplexityErrorSchema,
|
|
@@ -181,6 +208,15 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
181
208
|
citationTokens: response.usage?.citation_tokens ?? null,
|
|
182
209
|
numSearchQueries: response.usage?.num_search_queries ?? null,
|
|
183
210
|
},
|
|
211
|
+
cost: response.usage?.cost
|
|
212
|
+
? {
|
|
213
|
+
inputTokensCost: response.usage.cost.input_tokens_cost ?? null,
|
|
214
|
+
outputTokensCost:
|
|
215
|
+
response.usage.cost.output_tokens_cost ?? null,
|
|
216
|
+
requestCost: response.usage.cost.request_cost ?? null,
|
|
217
|
+
totalCost: response.usage.cost.total_cost ?? null,
|
|
218
|
+
}
|
|
219
|
+
: null,
|
|
184
220
|
},
|
|
185
221
|
},
|
|
186
222
|
};
|
|
@@ -195,7 +231,7 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
195
231
|
|
|
196
232
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
197
233
|
url: `${this.config.baseURL}/chat/completions`,
|
|
198
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
234
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
199
235
|
body,
|
|
200
236
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
201
237
|
errorSchema: perplexityErrorSchema,
|
|
@@ -226,6 +262,12 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
226
262
|
citationTokens: number | null;
|
|
227
263
|
numSearchQueries: number | null;
|
|
228
264
|
};
|
|
265
|
+
cost: {
|
|
266
|
+
inputTokensCost: number | null;
|
|
267
|
+
outputTokensCost: number | null;
|
|
268
|
+
requestCost: number | null;
|
|
269
|
+
totalCost: number | null;
|
|
270
|
+
} | null;
|
|
229
271
|
images: Array<{
|
|
230
272
|
imageUrl: string;
|
|
231
273
|
originUrl: string;
|
|
@@ -239,6 +281,7 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
239
281
|
citationTokens: null,
|
|
240
282
|
numSearchQueries: null,
|
|
241
283
|
},
|
|
284
|
+
cost: null,
|
|
242
285
|
images: null,
|
|
243
286
|
},
|
|
244
287
|
};
|
|
@@ -295,6 +338,16 @@ export class PerplexityLanguageModel implements LanguageModelV4 {
|
|
|
295
338
|
citationTokens: value.usage.citation_tokens ?? null,
|
|
296
339
|
numSearchQueries: value.usage.num_search_queries ?? null,
|
|
297
340
|
};
|
|
341
|
+
|
|
342
|
+
providerMetadata.perplexity.cost = value.usage.cost
|
|
343
|
+
? {
|
|
344
|
+
inputTokensCost: value.usage.cost.input_tokens_cost ?? null,
|
|
345
|
+
outputTokensCost:
|
|
346
|
+
value.usage.cost.output_tokens_cost ?? null,
|
|
347
|
+
requestCost: value.usage.cost.request_cost ?? null,
|
|
348
|
+
totalCost: value.usage.cost.total_cost ?? null,
|
|
349
|
+
}
|
|
350
|
+
: null;
|
|
298
351
|
}
|
|
299
352
|
|
|
300
353
|
if (value.images != null) {
|
|
@@ -371,6 +424,13 @@ function getResponseMetadata({
|
|
|
371
424
|
};
|
|
372
425
|
}
|
|
373
426
|
|
|
427
|
+
const perplexityCostSchema = z.object({
|
|
428
|
+
input_tokens_cost: z.number().nullish(),
|
|
429
|
+
output_tokens_cost: z.number().nullish(),
|
|
430
|
+
request_cost: z.number().nullish(),
|
|
431
|
+
total_cost: z.number().nullish(),
|
|
432
|
+
});
|
|
433
|
+
|
|
374
434
|
const perplexityUsageSchema = z.object({
|
|
375
435
|
prompt_tokens: z.number(),
|
|
376
436
|
completion_tokens: z.number(),
|
|
@@ -378,6 +438,7 @@ const perplexityUsageSchema = z.object({
|
|
|
378
438
|
citation_tokens: z.number().nullish(),
|
|
379
439
|
num_search_queries: z.number().nullish(),
|
|
380
440
|
reasoning_tokens: z.number().nullish(),
|
|
441
|
+
cost: perplexityCostSchema.nullish(),
|
|
381
442
|
});
|
|
382
443
|
|
|
383
444
|
export const perplexityImageSchema = z.object({
|
package/dist/index.d.mts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { ProviderV4, LanguageModelV4 } from '@ai-sdk/provider';
|
|
2
|
-
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
|
|
4
|
-
type PerplexityLanguageModelId = 'sonar-deep-research' | 'sonar-reasoning-pro' | 'sonar-reasoning' | 'sonar-pro' | 'sonar' | (string & {});
|
|
5
|
-
|
|
6
|
-
interface PerplexityProvider extends ProviderV4 {
|
|
7
|
-
/**
|
|
8
|
-
* Creates an Perplexity chat model for text generation.
|
|
9
|
-
*/
|
|
10
|
-
(modelId: PerplexityLanguageModelId): LanguageModelV4;
|
|
11
|
-
/**
|
|
12
|
-
* Creates an Perplexity language model for text generation.
|
|
13
|
-
*/
|
|
14
|
-
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV4;
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Use `embeddingModel` instead.
|
|
17
|
-
*/
|
|
18
|
-
textEmbeddingModel(modelId: string): never;
|
|
19
|
-
}
|
|
20
|
-
interface PerplexityProviderSettings {
|
|
21
|
-
/**
|
|
22
|
-
* Base URL for the perplexity API calls.
|
|
23
|
-
*/
|
|
24
|
-
baseURL?: string;
|
|
25
|
-
/**
|
|
26
|
-
* API key for authenticating requests.
|
|
27
|
-
*/
|
|
28
|
-
apiKey?: string;
|
|
29
|
-
/**
|
|
30
|
-
* Custom headers to include in the requests.
|
|
31
|
-
*/
|
|
32
|
-
headers?: Record<string, string>;
|
|
33
|
-
/**
|
|
34
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
35
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
36
|
-
*/
|
|
37
|
-
fetch?: FetchFunction;
|
|
38
|
-
}
|
|
39
|
-
declare function createPerplexity(options?: PerplexityProviderSettings): PerplexityProvider;
|
|
40
|
-
declare const perplexity: PerplexityProvider;
|
|
41
|
-
|
|
42
|
-
declare const VERSION: string;
|
|
43
|
-
|
|
44
|
-
export { type PerplexityProvider, type PerplexityProviderSettings, VERSION, createPerplexity, perplexity };
|