@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
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
export type PerplexityAgentPreset =
|
|
2
|
+
| 'fast'
|
|
3
|
+
| 'low'
|
|
4
|
+
| 'medium'
|
|
5
|
+
| 'high'
|
|
6
|
+
| 'xhigh';
|
|
7
|
+
|
|
2
8
|
export type PerplexityLanguageModelId =
|
|
3
|
-
|
|
|
4
|
-
| 'sonar
|
|
5
|
-
| 'sonar-reasoning'
|
|
6
|
-
| 'sonar-pro'
|
|
7
|
-
| 'sonar'
|
|
9
|
+
| PerplexityAgentPreset
|
|
10
|
+
| 'perplexity/sonar'
|
|
8
11
|
| (string & {});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LanguageModelV4CallOptions,
|
|
3
|
+
SharedV4Warning,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import type { PerplexityAgentTool } from './perplexity-agent-api';
|
|
6
|
+
|
|
7
|
+
export function preparePerplexityTools({
|
|
8
|
+
tools,
|
|
9
|
+
toolChoice,
|
|
10
|
+
}: {
|
|
11
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
12
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'];
|
|
13
|
+
}): {
|
|
14
|
+
tools: PerplexityAgentTool[];
|
|
15
|
+
warnings: SharedV4Warning[];
|
|
16
|
+
} {
|
|
17
|
+
const preparedTools: PerplexityAgentTool[] = [];
|
|
18
|
+
const warnings: SharedV4Warning[] = [];
|
|
19
|
+
|
|
20
|
+
for (const tool of tools ?? []) {
|
|
21
|
+
if (tool.type === 'provider') {
|
|
22
|
+
warnings.push({
|
|
23
|
+
type: 'unsupported',
|
|
24
|
+
feature: `provider-defined tool ${tool.name}`,
|
|
25
|
+
});
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
preparedTools.push({
|
|
30
|
+
type: 'function',
|
|
31
|
+
name: tool.name,
|
|
32
|
+
description: tool.description,
|
|
33
|
+
parameters: tool.inputSchema,
|
|
34
|
+
strict: tool.strict,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (toolChoice != null && toolChoice.type !== 'auto') {
|
|
39
|
+
warnings.push({
|
|
40
|
+
type: 'unsupported',
|
|
41
|
+
feature: 'toolChoice',
|
|
42
|
+
details:
|
|
43
|
+
'The Perplexity Agent API currently selects tools automatically.',
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { tools: preparedTools, warnings };
|
|
48
|
+
}
|
|
@@ -19,12 +19,12 @@ import { VERSION } from './version';
|
|
|
19
19
|
|
|
20
20
|
export interface PerplexityProvider extends ProviderV4 {
|
|
21
21
|
/**
|
|
22
|
-
* Creates
|
|
22
|
+
* Creates a Perplexity Agent API model or preset for text generation.
|
|
23
23
|
*/
|
|
24
24
|
(modelId: PerplexityLanguageModelId): LanguageModelV4;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Creates
|
|
27
|
+
* Creates a Perplexity Agent API model or preset for text generation.
|
|
28
28
|
*/
|
|
29
29
|
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV4;
|
|
30
30
|
|
|
@@ -46,7 +46,7 @@ export interface PerplexityProvider extends ProviderV4 {
|
|
|
46
46
|
|
|
47
47
|
export interface PerplexityProviderSettings {
|
|
48
48
|
/**
|
|
49
|
-
* Base URL for
|
|
49
|
+
* Base URL for Perplexity API calls.
|
|
50
50
|
*/
|
|
51
51
|
baseURL?: string;
|
|
52
52
|
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
UnsupportedFunctionalityError,
|
|
3
|
-
type LanguageModelV4Prompt,
|
|
4
|
-
} from '@ai-sdk/provider';
|
|
5
|
-
import type {
|
|
6
|
-
PerplexityMessageContent,
|
|
7
|
-
PerplexityPrompt,
|
|
8
|
-
} from './perplexity-language-model-prompt';
|
|
9
|
-
import {
|
|
10
|
-
convertUint8ArrayToBase64,
|
|
11
|
-
getTopLevelMediaType,
|
|
12
|
-
resolveFullMediaType,
|
|
13
|
-
} from '@ai-sdk/provider-utils';
|
|
14
|
-
|
|
15
|
-
export function convertToPerplexityMessages(
|
|
16
|
-
prompt: LanguageModelV4Prompt,
|
|
17
|
-
): PerplexityPrompt {
|
|
18
|
-
const messages: PerplexityPrompt = [];
|
|
19
|
-
|
|
20
|
-
for (const { role, content } of prompt) {
|
|
21
|
-
switch (role) {
|
|
22
|
-
case 'system': {
|
|
23
|
-
messages.push({ role: 'system', content });
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
case 'user':
|
|
28
|
-
case 'assistant': {
|
|
29
|
-
const hasMultipartContent = content.some(
|
|
30
|
-
part =>
|
|
31
|
-
(part.type === 'file' &&
|
|
32
|
-
getTopLevelMediaType(part.mediaType) === 'image') ||
|
|
33
|
-
(part.type === 'file' &&
|
|
34
|
-
getTopLevelMediaType(part.mediaType) === 'application'),
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const messageContent = content
|
|
38
|
-
.map((part, index) => {
|
|
39
|
-
switch (part.type) {
|
|
40
|
-
case 'text': {
|
|
41
|
-
return {
|
|
42
|
-
type: 'text',
|
|
43
|
-
text: part.text,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
case 'file': {
|
|
47
|
-
switch (part.data.type) {
|
|
48
|
-
case 'reference': {
|
|
49
|
-
throw new UnsupportedFunctionalityError({
|
|
50
|
-
functionality: 'file parts with provider references',
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
case 'text': {
|
|
54
|
-
throw new UnsupportedFunctionalityError({
|
|
55
|
-
functionality: 'text file parts',
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
case 'url':
|
|
59
|
-
case 'data': {
|
|
60
|
-
const topLevelMediaType = getTopLevelMediaType(
|
|
61
|
-
part.mediaType,
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (topLevelMediaType === 'application') {
|
|
65
|
-
const fullMediaType = resolveFullMediaType({ part });
|
|
66
|
-
|
|
67
|
-
if (fullMediaType !== 'application/pdf') {
|
|
68
|
-
throw new UnsupportedFunctionalityError({
|
|
69
|
-
functionality: `file part media type ${fullMediaType}`,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return part.data.type === 'url'
|
|
74
|
-
? {
|
|
75
|
-
type: 'file_url',
|
|
76
|
-
file_url: {
|
|
77
|
-
url: part.data.url.toString(),
|
|
78
|
-
},
|
|
79
|
-
file_name: part.filename,
|
|
80
|
-
}
|
|
81
|
-
: {
|
|
82
|
-
type: 'file_url',
|
|
83
|
-
file_url: {
|
|
84
|
-
url:
|
|
85
|
-
typeof part.data.data === 'string'
|
|
86
|
-
? part.data.data
|
|
87
|
-
: convertUint8ArrayToBase64(part.data.data),
|
|
88
|
-
},
|
|
89
|
-
file_name: part.filename || `document-${index}.pdf`,
|
|
90
|
-
};
|
|
91
|
-
} else if (topLevelMediaType === 'image') {
|
|
92
|
-
return part.data.type === 'url'
|
|
93
|
-
? {
|
|
94
|
-
type: 'image_url',
|
|
95
|
-
image_url: {
|
|
96
|
-
url: part.data.url.toString(),
|
|
97
|
-
},
|
|
98
|
-
}
|
|
99
|
-
: {
|
|
100
|
-
type: 'image_url',
|
|
101
|
-
image_url: {
|
|
102
|
-
url: `data:${resolveFullMediaType({ part })};base64,${
|
|
103
|
-
typeof part.data.data === 'string'
|
|
104
|
-
? part.data.data
|
|
105
|
-
: convertUint8ArrayToBase64(part.data.data)
|
|
106
|
-
}`,
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
throw new UnsupportedFunctionalityError({
|
|
111
|
-
functionality: `file part media type ${part.mediaType}`,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
})
|
|
118
|
-
.filter(Boolean) as PerplexityMessageContent[];
|
|
119
|
-
messages.push({
|
|
120
|
-
role,
|
|
121
|
-
content: hasMultipartContent
|
|
122
|
-
? messageContent
|
|
123
|
-
: messageContent
|
|
124
|
-
.filter(part => part.type === 'text')
|
|
125
|
-
.map(part => part.text)
|
|
126
|
-
.join(''),
|
|
127
|
-
});
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
case 'tool': {
|
|
131
|
-
throw new UnsupportedFunctionalityError({
|
|
132
|
-
functionality: 'Tool messages',
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
default: {
|
|
136
|
-
const _exhaustiveCheck: never = role;
|
|
137
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return messages;
|
|
143
|
-
}
|