@ai-sdk/perplexity 3.0.8 → 3.0.9
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 +6 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -2
- package/src/__snapshots__/convert-to-perplexity-messages.test.ts.snap +48 -0
- package/src/convert-perplexity-usage.ts +48 -0
- package/src/convert-to-perplexity-messages.test.ts +85 -0
- package/src/convert-to-perplexity-messages.ts +107 -0
- package/src/index.ts +6 -0
- package/src/map-perplexity-finish-reason.ts +13 -0
- package/src/perplexity-language-model-options.ts +8 -0
- package/src/perplexity-language-model-prompt.ts +25 -0
- package/src/perplexity-language-model.test.ts +1101 -0
- package/src/perplexity-language-model.ts +442 -0
- package/src/perplexity-provider.ts +101 -0
- package/src/version.ts +6 -0
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/perplexity",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.9",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
11
|
+
"src",
|
|
11
12
|
"CHANGELOG.md",
|
|
12
13
|
"README.md"
|
|
13
14
|
],
|
|
@@ -28,7 +29,7 @@
|
|
|
28
29
|
"tsup": "^8",
|
|
29
30
|
"typescript": "5.8.3",
|
|
30
31
|
"zod": "3.25.76",
|
|
31
|
-
"@ai-sdk/test-server": "1.0.
|
|
32
|
+
"@ai-sdk/test-server": "1.0.2",
|
|
32
33
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
33
34
|
},
|
|
34
35
|
"peerDependencies": {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`convertToPerplexityMessages > assistant messages > should convert an assistant message with text content 1`] = `
|
|
4
|
+
[
|
|
5
|
+
{
|
|
6
|
+
"content": "Assistant reply",
|
|
7
|
+
"role": "assistant",
|
|
8
|
+
},
|
|
9
|
+
]
|
|
10
|
+
`;
|
|
11
|
+
|
|
12
|
+
exports[`convertToPerplexityMessages > system messages > should convert a system message with text content 1`] = `
|
|
13
|
+
[
|
|
14
|
+
{
|
|
15
|
+
"content": "System initialization",
|
|
16
|
+
"role": "system",
|
|
17
|
+
},
|
|
18
|
+
]
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
exports[`convertToPerplexityMessages > user messages > should convert a user message with image parts 1`] = `
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
"content": [
|
|
25
|
+
{
|
|
26
|
+
"text": "Hello ",
|
|
27
|
+
"type": "text",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"image_url": {
|
|
31
|
+
"url": "data:image/png;base64,AAECAw==",
|
|
32
|
+
},
|
|
33
|
+
"type": "image_url",
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
"role": "user",
|
|
37
|
+
},
|
|
38
|
+
]
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
exports[`convertToPerplexityMessages > user messages > should convert a user message with text parts 1`] = `
|
|
42
|
+
[
|
|
43
|
+
{
|
|
44
|
+
"content": "Hello World",
|
|
45
|
+
"role": "user",
|
|
46
|
+
},
|
|
47
|
+
]
|
|
48
|
+
`;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { LanguageModelV3Usage } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
export function convertPerplexityUsage(
|
|
4
|
+
usage:
|
|
5
|
+
| {
|
|
6
|
+
prompt_tokens?: number | null | undefined;
|
|
7
|
+
completion_tokens?: number | null | undefined;
|
|
8
|
+
reasoning_tokens?: number | null | undefined;
|
|
9
|
+
}
|
|
10
|
+
| undefined
|
|
11
|
+
| null,
|
|
12
|
+
): LanguageModelV3Usage {
|
|
13
|
+
if (usage == null) {
|
|
14
|
+
return {
|
|
15
|
+
inputTokens: {
|
|
16
|
+
total: undefined,
|
|
17
|
+
noCache: undefined,
|
|
18
|
+
cacheRead: undefined,
|
|
19
|
+
cacheWrite: undefined,
|
|
20
|
+
},
|
|
21
|
+
outputTokens: {
|
|
22
|
+
total: undefined,
|
|
23
|
+
text: undefined,
|
|
24
|
+
reasoning: undefined,
|
|
25
|
+
},
|
|
26
|
+
raw: undefined,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const promptTokens = usage.prompt_tokens ?? 0;
|
|
31
|
+
const completionTokens = usage.completion_tokens ?? 0;
|
|
32
|
+
const reasoningTokens = usage.reasoning_tokens ?? 0;
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
inputTokens: {
|
|
36
|
+
total: promptTokens,
|
|
37
|
+
noCache: promptTokens,
|
|
38
|
+
cacheRead: undefined,
|
|
39
|
+
cacheWrite: undefined,
|
|
40
|
+
},
|
|
41
|
+
outputTokens: {
|
|
42
|
+
total: completionTokens,
|
|
43
|
+
text: completionTokens - reasoningTokens,
|
|
44
|
+
reasoning: reasoningTokens,
|
|
45
|
+
},
|
|
46
|
+
raw: usage,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { convertToPerplexityMessages } from './convert-to-perplexity-messages';
|
|
2
|
+
import { UnsupportedFunctionalityError } from '@ai-sdk/provider';
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
|
|
5
|
+
describe('convertToPerplexityMessages', () => {
|
|
6
|
+
describe('system messages', () => {
|
|
7
|
+
it('should convert a system message with text content', () => {
|
|
8
|
+
expect(
|
|
9
|
+
convertToPerplexityMessages([
|
|
10
|
+
{
|
|
11
|
+
role: 'system',
|
|
12
|
+
content: 'System initialization',
|
|
13
|
+
},
|
|
14
|
+
]),
|
|
15
|
+
).toMatchSnapshot();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('user messages', () => {
|
|
20
|
+
it('should convert a user message with text parts', () => {
|
|
21
|
+
expect(
|
|
22
|
+
convertToPerplexityMessages([
|
|
23
|
+
{
|
|
24
|
+
role: 'user',
|
|
25
|
+
content: [
|
|
26
|
+
{ type: 'text', text: 'Hello ' },
|
|
27
|
+
{ type: 'text', text: 'World' },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
]),
|
|
31
|
+
).toMatchSnapshot();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should convert a user message with image parts', () => {
|
|
35
|
+
expect(
|
|
36
|
+
convertToPerplexityMessages([
|
|
37
|
+
{
|
|
38
|
+
role: 'user',
|
|
39
|
+
content: [
|
|
40
|
+
{ type: 'text', text: 'Hello ' },
|
|
41
|
+
{
|
|
42
|
+
type: 'file',
|
|
43
|
+
data: new Uint8Array([0, 1, 2, 3]),
|
|
44
|
+
mediaType: 'image/png',
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
]),
|
|
49
|
+
).toMatchSnapshot();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('assistant messages', () => {
|
|
54
|
+
it('should convert an assistant message with text content', () => {
|
|
55
|
+
expect(
|
|
56
|
+
convertToPerplexityMessages([
|
|
57
|
+
{
|
|
58
|
+
role: 'assistant',
|
|
59
|
+
content: [{ type: 'text', text: 'Assistant reply' }],
|
|
60
|
+
},
|
|
61
|
+
]),
|
|
62
|
+
).toMatchSnapshot();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('tool messages', () => {
|
|
67
|
+
it('should throw an error for tool messages', () => {
|
|
68
|
+
expect(() => {
|
|
69
|
+
convertToPerplexityMessages([
|
|
70
|
+
{
|
|
71
|
+
role: 'tool',
|
|
72
|
+
content: [
|
|
73
|
+
{
|
|
74
|
+
type: 'tool-result',
|
|
75
|
+
toolCallId: 'dummy-tool-call-id',
|
|
76
|
+
toolName: 'dummy-tool-name',
|
|
77
|
+
output: { type: 'text', value: 'This should fail' },
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
]);
|
|
82
|
+
}).toThrow(UnsupportedFunctionalityError);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LanguageModelV3Prompt,
|
|
3
|
+
UnsupportedFunctionalityError,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import {
|
|
6
|
+
PerplexityMessageContent,
|
|
7
|
+
PerplexityPrompt,
|
|
8
|
+
} from './perplexity-language-model-prompt';
|
|
9
|
+
import { convertUint8ArrayToBase64 } from '@ai-sdk/provider-utils';
|
|
10
|
+
|
|
11
|
+
export function convertToPerplexityMessages(
|
|
12
|
+
prompt: LanguageModelV3Prompt,
|
|
13
|
+
): PerplexityPrompt {
|
|
14
|
+
const messages: PerplexityPrompt = [];
|
|
15
|
+
|
|
16
|
+
for (const { role, content } of prompt) {
|
|
17
|
+
switch (role) {
|
|
18
|
+
case 'system': {
|
|
19
|
+
messages.push({ role: 'system', content });
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
case 'user':
|
|
24
|
+
case 'assistant': {
|
|
25
|
+
const hasMultipartContent = content.some(
|
|
26
|
+
part =>
|
|
27
|
+
(part.type === 'file' && part.mediaType.startsWith('image/')) ||
|
|
28
|
+
(part.type === 'file' && part.mediaType === 'application/pdf'),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const messageContent = content
|
|
32
|
+
.map((part, index) => {
|
|
33
|
+
switch (part.type) {
|
|
34
|
+
case 'text': {
|
|
35
|
+
return {
|
|
36
|
+
type: 'text',
|
|
37
|
+
text: part.text,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
case 'file': {
|
|
41
|
+
if (part.mediaType === 'application/pdf') {
|
|
42
|
+
return part.data instanceof URL
|
|
43
|
+
? {
|
|
44
|
+
type: 'file_url',
|
|
45
|
+
file_url: {
|
|
46
|
+
url: part.data.toString(),
|
|
47
|
+
},
|
|
48
|
+
file_name: part.filename,
|
|
49
|
+
}
|
|
50
|
+
: {
|
|
51
|
+
type: 'file_url',
|
|
52
|
+
file_url: {
|
|
53
|
+
url:
|
|
54
|
+
typeof part.data === 'string'
|
|
55
|
+
? part.data
|
|
56
|
+
: convertUint8ArrayToBase64(part.data),
|
|
57
|
+
},
|
|
58
|
+
file_name: part.filename || `document-${index}.pdf`,
|
|
59
|
+
};
|
|
60
|
+
} else if (part.mediaType.startsWith('image/')) {
|
|
61
|
+
return part.data instanceof URL
|
|
62
|
+
? {
|
|
63
|
+
type: 'image_url',
|
|
64
|
+
image_url: {
|
|
65
|
+
url: part.data.toString(),
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
: {
|
|
69
|
+
type: 'image_url',
|
|
70
|
+
image_url: {
|
|
71
|
+
url: `data:${part.mediaType ?? 'image/jpeg'};base64,${
|
|
72
|
+
typeof part.data === 'string'
|
|
73
|
+
? part.data
|
|
74
|
+
: convertUint8ArrayToBase64(part.data)
|
|
75
|
+
}`,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
.filter(Boolean) as PerplexityMessageContent[];
|
|
83
|
+
messages.push({
|
|
84
|
+
role,
|
|
85
|
+
content: hasMultipartContent
|
|
86
|
+
? messageContent
|
|
87
|
+
: messageContent
|
|
88
|
+
.filter(part => part.type === 'text')
|
|
89
|
+
.map(part => part.text)
|
|
90
|
+
.join(''),
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case 'tool': {
|
|
95
|
+
throw new UnsupportedFunctionalityError({
|
|
96
|
+
functionality: 'Tool messages',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
default: {
|
|
100
|
+
const _exhaustiveCheck: never = role;
|
|
101
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return messages;
|
|
107
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LanguageModelV3FinishReason } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
export function mapPerplexityFinishReason(
|
|
4
|
+
finishReason: string | null | undefined,
|
|
5
|
+
): LanguageModelV3FinishReason['unified'] {
|
|
6
|
+
switch (finishReason) {
|
|
7
|
+
case 'stop':
|
|
8
|
+
case 'length':
|
|
9
|
+
return finishReason;
|
|
10
|
+
default:
|
|
11
|
+
return 'other';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type PerplexityPrompt = Array<PerplexityMessage>;
|
|
2
|
+
|
|
3
|
+
export type PerplexityMessage = {
|
|
4
|
+
role: 'system' | 'user' | 'assistant';
|
|
5
|
+
content: string | PerplexityMessageContent[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type PerplexityMessageContent =
|
|
9
|
+
| {
|
|
10
|
+
type: 'text';
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
type: 'image_url';
|
|
15
|
+
image_url: {
|
|
16
|
+
url: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
type: 'file_url';
|
|
21
|
+
file_url: {
|
|
22
|
+
url: string;
|
|
23
|
+
};
|
|
24
|
+
file_name?: string;
|
|
25
|
+
};
|