@ai-sdk/perplexity 5.0.2 → 5.0.4
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 +18 -0
- package/dist/index.d.ts +161 -156
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1075 -1339
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
- package/src/perplexity-provider.ts +2 -1
package/dist/index.js
CHANGED
|
@@ -1,1390 +1,1126 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
generateId,
|
|
7
|
-
loadApiKey,
|
|
8
|
-
withoutTrailingSlash,
|
|
9
|
-
withUserAgentSuffix
|
|
10
|
-
} from "@ai-sdk/provider-utils";
|
|
11
|
-
|
|
12
|
-
// src/perplexity-embedding-model.ts
|
|
13
|
-
import {
|
|
14
|
-
TooManyEmbeddingValuesForCallError
|
|
15
|
-
} from "@ai-sdk/provider";
|
|
16
|
-
import {
|
|
17
|
-
combineHeaders as combineHeaders2,
|
|
18
|
-
convertBase64ToUint8Array,
|
|
19
|
-
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
20
|
-
createJsonResponseHandler as createJsonResponseHandler2,
|
|
21
|
-
parseProviderOptions as parseProviderOptions2,
|
|
22
|
-
postJsonToApi as postJsonToApi2,
|
|
23
|
-
serializeModelOptions as serializeModelOptions2,
|
|
24
|
-
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2,
|
|
25
|
-
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2
|
|
26
|
-
} from "@ai-sdk/provider-utils";
|
|
27
|
-
import { z as z4 } from "zod/v4";
|
|
28
|
-
|
|
29
|
-
// src/perplexity-embedding-model-options.ts
|
|
1
|
+
import { APICallError, NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertUint8ArrayToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata, createNullLanguageModelUsage, generateId, getTopLevelMediaType, isCustomReasoning, loadApiKey, parseProviderOptions, postJsonToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
30
3
|
import { z } from "zod/v4";
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
4
|
+
//#region src/perplexity-embedding-model-options.ts
|
|
5
|
+
const perplexityEmbeddingModelOptions = z.object({
|
|
6
|
+
/**
|
|
7
|
+
* The number of dimensions the resulting output embeddings should have
|
|
8
|
+
* (Matryoshka truncation). Ranges from 128 up to the model's full size
|
|
9
|
+
* (1024 for 0.6b models, 2560 for 4b models).
|
|
10
|
+
*/
|
|
11
|
+
dimensions: z.number().int().positive().optional(),
|
|
12
|
+
/**
|
|
13
|
+
* The quantized encoding format returned by the API. Perplexity does not
|
|
14
|
+
* return floating point embeddings; values are decoded to numbers:
|
|
15
|
+
* - `base64_int8` (default): signed int8 values, compare via cosine similarity.
|
|
16
|
+
* - `base64_binary`: packed bits per byte (0-255), compare via Hamming distance.
|
|
17
|
+
*/
|
|
18
|
+
encodingFormat: z.enum(["base64_int8", "base64_binary"]).optional()
|
|
45
19
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
import {
|
|
49
|
-
APICallError
|
|
50
|
-
} from "@ai-sdk/provider";
|
|
51
|
-
import {
|
|
52
|
-
combineHeaders,
|
|
53
|
-
createEventSourceResponseHandler,
|
|
54
|
-
createJsonErrorResponseHandler,
|
|
55
|
-
createJsonResponseHandler,
|
|
56
|
-
createLanguageModelResponseMetadata,
|
|
57
|
-
isCustomReasoning,
|
|
58
|
-
parseProviderOptions,
|
|
59
|
-
postJsonToApi,
|
|
60
|
-
serializeModelOptions,
|
|
61
|
-
WORKFLOW_DESERIALIZE,
|
|
62
|
-
WORKFLOW_SERIALIZE
|
|
63
|
-
} from "@ai-sdk/provider-utils";
|
|
64
|
-
|
|
65
|
-
// src/convert-perplexity-usage.ts
|
|
66
|
-
import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/convert-perplexity-usage.ts
|
|
67
22
|
function convertPerplexityUsage(usage) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
raw: usage
|
|
89
|
-
};
|
|
23
|
+
if (usage == null) return createNullLanguageModelUsage();
|
|
24
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
25
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
26
|
+
const cacheReadTokens = usage.input_tokens_details?.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens ?? 0;
|
|
27
|
+
const cacheWriteTokens = usage.input_tokens_details?.cache_creation_input_tokens ?? 0;
|
|
28
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
|
|
29
|
+
return {
|
|
30
|
+
inputTokens: {
|
|
31
|
+
total: inputTokens,
|
|
32
|
+
noCache: Math.max(0, inputTokens - cacheReadTokens - cacheWriteTokens),
|
|
33
|
+
cacheRead: cacheReadTokens,
|
|
34
|
+
cacheWrite: cacheWriteTokens
|
|
35
|
+
},
|
|
36
|
+
outputTokens: {
|
|
37
|
+
total: outputTokens,
|
|
38
|
+
text: Math.max(0, outputTokens - reasoningTokens),
|
|
39
|
+
reasoning: reasoningTokens
|
|
40
|
+
},
|
|
41
|
+
raw: usage
|
|
42
|
+
};
|
|
90
43
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
import {
|
|
94
|
-
UnsupportedFunctionalityError
|
|
95
|
-
} from "@ai-sdk/provider";
|
|
96
|
-
import {
|
|
97
|
-
convertUint8ArrayToBase64,
|
|
98
|
-
getTopLevelMediaType,
|
|
99
|
-
resolveFullMediaType
|
|
100
|
-
} from "@ai-sdk/provider-utils";
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/convert-to-perplexity-input.ts
|
|
101
46
|
function serializeToolOutput(output) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
throw new UnsupportedFunctionalityError({
|
|
114
|
-
functionality: "file and custom tool result content"
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
return output.value.map((part) => part.type === "text" ? part.text : "").join("");
|
|
118
|
-
}
|
|
119
|
-
default: {
|
|
120
|
-
const _exhaustiveCheck = output;
|
|
121
|
-
throw new Error(`Unsupported tool output: ${_exhaustiveCheck}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
47
|
+
switch (output.type) {
|
|
48
|
+
case "text":
|
|
49
|
+
case "error-text": return output.value;
|
|
50
|
+
case "json":
|
|
51
|
+
case "error-json": return JSON.stringify(output.value);
|
|
52
|
+
case "execution-denied": return output.reason ?? "Tool call execution denied.";
|
|
53
|
+
case "content":
|
|
54
|
+
if (output.value.some((part) => part.type !== "text")) throw new UnsupportedFunctionalityError({ functionality: "file and custom tool result content" });
|
|
55
|
+
return output.value.map((part) => part.type === "text" ? part.text : "").join("");
|
|
56
|
+
default: throw new Error(`Unsupported tool output: ${output}`);
|
|
57
|
+
}
|
|
124
58
|
}
|
|
125
59
|
function getThoughtSignature(providerOptions) {
|
|
126
|
-
|
|
127
|
-
|
|
60
|
+
const thoughtSignature = providerOptions?.perplexity?.thoughtSignature;
|
|
61
|
+
return typeof thoughtSignature === "string" ? thoughtSignature : void 0;
|
|
128
62
|
}
|
|
129
63
|
function convertToPerplexityInput(prompt) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
case "tool": {
|
|
234
|
-
for (const part of content) {
|
|
235
|
-
if (part.type === "tool-approval-response") {
|
|
236
|
-
throw new UnsupportedFunctionalityError({
|
|
237
|
-
functionality: "tool approval responses"
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
input.push({
|
|
241
|
-
type: "function_call_output",
|
|
242
|
-
call_id: part.toolCallId,
|
|
243
|
-
name: part.toolName,
|
|
244
|
-
output: serializeToolOutput(part.output),
|
|
245
|
-
thought_signature: getThoughtSignature(part.providerOptions)
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
default: {
|
|
251
|
-
const _exhaustiveCheck = role;
|
|
252
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return { input, warnings };
|
|
64
|
+
const input = [];
|
|
65
|
+
const warnings = [];
|
|
66
|
+
for (const { role, content } of prompt) switch (role) {
|
|
67
|
+
case "system":
|
|
68
|
+
input.push({
|
|
69
|
+
type: "message",
|
|
70
|
+
role: "system",
|
|
71
|
+
content
|
|
72
|
+
});
|
|
73
|
+
break;
|
|
74
|
+
case "user": {
|
|
75
|
+
const convertedContent = content.map((part) => {
|
|
76
|
+
switch (part.type) {
|
|
77
|
+
case "text": return {
|
|
78
|
+
type: "input_text",
|
|
79
|
+
text: part.text
|
|
80
|
+
};
|
|
81
|
+
case "file":
|
|
82
|
+
if (getTopLevelMediaType(part.mediaType) !== "image") throw new UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
|
|
83
|
+
switch (part.data.type) {
|
|
84
|
+
case "url": return {
|
|
85
|
+
type: "input_image",
|
|
86
|
+
image_url: part.data.url.toString()
|
|
87
|
+
};
|
|
88
|
+
case "data": return {
|
|
89
|
+
type: "input_image",
|
|
90
|
+
image_url: `data:${resolveFullMediaType({ part })};base64,${typeof part.data.data === "string" ? part.data.data : convertUint8ArrayToBase64(part.data.data)}`
|
|
91
|
+
};
|
|
92
|
+
case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
|
|
93
|
+
case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
const isTextOnly = convertedContent.every((part) => part.type === "input_text");
|
|
98
|
+
input.push({
|
|
99
|
+
type: "message",
|
|
100
|
+
role: "user",
|
|
101
|
+
content: isTextOnly ? convertedContent.map((part) => part.type === "input_text" ? part.text : "").join("") : convertedContent
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "assistant": {
|
|
106
|
+
const text = content.filter((part) => part.type === "text").map((part) => part.text).join("");
|
|
107
|
+
if (text.length > 0) input.push({
|
|
108
|
+
type: "message",
|
|
109
|
+
role: "assistant",
|
|
110
|
+
content: text
|
|
111
|
+
});
|
|
112
|
+
for (const part of content) switch (part.type) {
|
|
113
|
+
case "text": break;
|
|
114
|
+
case "tool-call":
|
|
115
|
+
input.push({
|
|
116
|
+
type: "function_call",
|
|
117
|
+
call_id: part.toolCallId,
|
|
118
|
+
name: part.toolName,
|
|
119
|
+
arguments: JSON.stringify(part.input ?? {}),
|
|
120
|
+
thought_signature: getThoughtSignature(part.providerOptions)
|
|
121
|
+
});
|
|
122
|
+
break;
|
|
123
|
+
case "tool-result":
|
|
124
|
+
input.push({
|
|
125
|
+
type: "function_call_output",
|
|
126
|
+
call_id: part.toolCallId,
|
|
127
|
+
name: part.toolName,
|
|
128
|
+
output: serializeToolOutput(part.output),
|
|
129
|
+
thought_signature: getThoughtSignature(part.providerOptions)
|
|
130
|
+
});
|
|
131
|
+
break;
|
|
132
|
+
case "reasoning":
|
|
133
|
+
warnings.push({
|
|
134
|
+
type: "unsupported",
|
|
135
|
+
feature: "reasoning content in prompt"
|
|
136
|
+
});
|
|
137
|
+
break;
|
|
138
|
+
case "file":
|
|
139
|
+
case "reasoning-file":
|
|
140
|
+
case "custom": throw new UnsupportedFunctionalityError({ functionality: `assistant ${part.type} parts` });
|
|
141
|
+
default: throw new Error(`Unsupported assistant part: ${part}`);
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case "tool":
|
|
146
|
+
for (const part of content) {
|
|
147
|
+
if (part.type === "tool-approval-response") throw new UnsupportedFunctionalityError({ functionality: "tool approval responses" });
|
|
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
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
input,
|
|
161
|
+
warnings
|
|
162
|
+
};
|
|
257
163
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
function mapPerplexityFinishReason({
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
switch (status) {
|
|
272
|
-
case "completed":
|
|
273
|
-
return hasFunctionCall ? "tool-calls" : "stop";
|
|
274
|
-
case "requires_action":
|
|
275
|
-
return "tool-calls";
|
|
276
|
-
case "failed":
|
|
277
|
-
return "error";
|
|
278
|
-
case "cancelled":
|
|
279
|
-
case "queued":
|
|
280
|
-
case "in_progress":
|
|
281
|
-
default:
|
|
282
|
-
return hasFunctionCall ? "tool-calls" : "other";
|
|
283
|
-
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/map-perplexity-finish-reason.ts
|
|
166
|
+
function mapPerplexityFinishReason({ status, incompleteReason, hasFunctionCall }) {
|
|
167
|
+
switch (incompleteReason) {
|
|
168
|
+
case "max_output_tokens": return "length";
|
|
169
|
+
case "content_filter": return "content-filter";
|
|
170
|
+
}
|
|
171
|
+
switch (status) {
|
|
172
|
+
case "completed": return hasFunctionCall ? "tool-calls" : "stop";
|
|
173
|
+
case "requires_action": return "tool-calls";
|
|
174
|
+
case "failed": return "error";
|
|
175
|
+
default: return hasFunctionCall ? "tool-calls" : "other";
|
|
176
|
+
}
|
|
284
177
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
source: z2.string().optional()
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/perplexity-agent-api.ts
|
|
180
|
+
const perplexitySearchResultSchema = z.looseObject({
|
|
181
|
+
id: z.number().optional(),
|
|
182
|
+
title: z.string(),
|
|
183
|
+
url: z.string(),
|
|
184
|
+
snippet: z.string().optional(),
|
|
185
|
+
date: z.string().nullish(),
|
|
186
|
+
last_updated: z.string().nullish(),
|
|
187
|
+
source: z.string().optional()
|
|
296
188
|
});
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
189
|
+
const perplexityFetchedContentSchema = z.looseObject({
|
|
190
|
+
title: z.string(),
|
|
191
|
+
url: z.string(),
|
|
192
|
+
snippet: z.string().optional()
|
|
301
193
|
});
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
})
|
|
311
|
-
).nullish()
|
|
194
|
+
const perplexityContentPartSchema = z.looseObject({
|
|
195
|
+
type: z.string(),
|
|
196
|
+
text: z.string().optional(),
|
|
197
|
+
annotations: z.array(z.looseObject({
|
|
198
|
+
type: z.string().optional(),
|
|
199
|
+
url: z.string().optional(),
|
|
200
|
+
title: z.string().optional()
|
|
201
|
+
})).nullish()
|
|
312
202
|
});
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
203
|
+
const handledOutputTypes = [
|
|
204
|
+
"message",
|
|
205
|
+
"search_results",
|
|
206
|
+
"fetch_url_results",
|
|
207
|
+
"function_call"
|
|
318
208
|
];
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
// Their fields (e.g. finance_results.results) have different shapes. Do not
|
|
343
|
-
// validate them as search results, or let malformed handled items fall back.
|
|
344
|
-
z2.object({
|
|
345
|
-
type: z2.string().refine((type) => !handledOutputTypes.includes(type))
|
|
346
|
-
}).transform(() => ({ type: "unhandled" }))
|
|
209
|
+
const perplexityOutputItemSchema = z.union([
|
|
210
|
+
z.looseObject({
|
|
211
|
+
type: z.literal("message"),
|
|
212
|
+
id: z.string().nullish(),
|
|
213
|
+
content: z.array(perplexityContentPartSchema).nullish()
|
|
214
|
+
}),
|
|
215
|
+
z.looseObject({
|
|
216
|
+
type: z.literal("search_results"),
|
|
217
|
+
results: z.array(perplexitySearchResultSchema).nullish()
|
|
218
|
+
}),
|
|
219
|
+
z.looseObject({
|
|
220
|
+
type: z.literal("fetch_url_results"),
|
|
221
|
+
contents: z.array(perplexityFetchedContentSchema).nullish()
|
|
222
|
+
}),
|
|
223
|
+
z.looseObject({
|
|
224
|
+
type: z.literal("function_call"),
|
|
225
|
+
id: z.string().nullish(),
|
|
226
|
+
call_id: z.string().nullish(),
|
|
227
|
+
name: z.string().nullish(),
|
|
228
|
+
arguments: z.string().nullish(),
|
|
229
|
+
thought_signature: z.string().nullish()
|
|
230
|
+
}),
|
|
231
|
+
z.object({ type: z.string().refine((type) => !handledOutputTypes.includes(type)) }).transform(() => ({ type: "unhandled" }))
|
|
347
232
|
]);
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
233
|
+
const perplexityCostSchema = z.looseObject({
|
|
234
|
+
currency: z.string().optional(),
|
|
235
|
+
input_cost: z.number().optional(),
|
|
236
|
+
output_cost: z.number().optional(),
|
|
237
|
+
total_cost: z.number().optional(),
|
|
238
|
+
cache_creation_cost: z.number().nullish(),
|
|
239
|
+
cache_read_cost: z.number().nullish(),
|
|
240
|
+
tool_calls_cost: z.number().nullish()
|
|
356
241
|
});
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
tool_calls_details: z2.record(
|
|
370
|
-
z2.string(),
|
|
371
|
-
z2.looseObject({
|
|
372
|
-
invocation: z2.number().optional()
|
|
373
|
-
})
|
|
374
|
-
).nullish(),
|
|
375
|
-
cost: perplexityCostSchema.nullish()
|
|
242
|
+
const perplexityUsageSchema = z.looseObject({
|
|
243
|
+
input_tokens: z.number(),
|
|
244
|
+
output_tokens: z.number(),
|
|
245
|
+
total_tokens: z.number(),
|
|
246
|
+
input_tokens_details: z.looseObject({
|
|
247
|
+
cached_tokens: z.number().optional(),
|
|
248
|
+
cache_creation_input_tokens: z.number().optional(),
|
|
249
|
+
cache_read_input_tokens: z.number().optional()
|
|
250
|
+
}).nullish(),
|
|
251
|
+
output_tokens_details: z.looseObject({ reasoning_tokens: z.number().optional() }).nullish(),
|
|
252
|
+
tool_calls_details: z.record(z.string(), z.looseObject({ invocation: z.number().optional() })).nullish(),
|
|
253
|
+
cost: perplexityCostSchema.nullish()
|
|
376
254
|
});
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}).nullish(),
|
|
392
|
-
usage: perplexityUsageSchema.nullish()
|
|
255
|
+
const perplexityAgentResponseSchema = z.looseObject({
|
|
256
|
+
id: z.string(),
|
|
257
|
+
created_at: z.number(),
|
|
258
|
+
model: z.string(),
|
|
259
|
+
object: z.literal("response"),
|
|
260
|
+
output: z.array(perplexityOutputItemSchema),
|
|
261
|
+
status: z.string(),
|
|
262
|
+
incomplete_details: z.looseObject({ reason: z.string() }).nullish(),
|
|
263
|
+
error: z.looseObject({
|
|
264
|
+
code: z.string().optional(),
|
|
265
|
+
message: z.string(),
|
|
266
|
+
type: z.string().optional()
|
|
267
|
+
}).nullish(),
|
|
268
|
+
usage: perplexityUsageSchema.nullish()
|
|
393
269
|
});
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
270
|
+
const perplexityAgentChunkSchema = z.looseObject({
|
|
271
|
+
type: z.string(),
|
|
272
|
+
sequence_number: z.number().nullish(),
|
|
273
|
+
response: perplexityAgentResponseSchema.nullish(),
|
|
274
|
+
item: perplexityOutputItemSchema.nullish(),
|
|
275
|
+
output_index: z.number().nullish(),
|
|
276
|
+
item_id: z.string().nullish(),
|
|
277
|
+
content_index: z.number().nullish(),
|
|
278
|
+
delta: z.string().nullish(),
|
|
279
|
+
text: z.string().nullish(),
|
|
280
|
+
thought: z.string().nullish(),
|
|
281
|
+
queries: z.array(z.string()).nullish(),
|
|
282
|
+
urls: z.array(z.string()).nullish(),
|
|
283
|
+
results: z.array(perplexitySearchResultSchema).nullish(),
|
|
284
|
+
contents: z.array(perplexityFetchedContentSchema).nullish(),
|
|
285
|
+
error: z.looseObject({
|
|
286
|
+
code: z.string().nullish(),
|
|
287
|
+
message: z.string(),
|
|
288
|
+
type: z.string().nullish()
|
|
289
|
+
}).nullish()
|
|
414
290
|
});
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
]).optional(),
|
|
424
|
-
detail: z2.union([
|
|
425
|
-
z2.string(),
|
|
426
|
-
z2.array(
|
|
427
|
-
z2.looseObject({
|
|
428
|
-
msg: z2.string()
|
|
429
|
-
})
|
|
430
|
-
)
|
|
431
|
-
]).optional(),
|
|
432
|
-
message: z2.string().optional()
|
|
291
|
+
const perplexityErrorSchema = z.looseObject({
|
|
292
|
+
error: z.union([z.string(), z.looseObject({
|
|
293
|
+
code: z.union([z.string(), z.number()]).optional(),
|
|
294
|
+
message: z.string().nullish(),
|
|
295
|
+
type: z.string().nullish()
|
|
296
|
+
})]).optional(),
|
|
297
|
+
detail: z.union([z.string(), z.array(z.looseObject({ msg: z.string() }))]).optional(),
|
|
298
|
+
message: z.string().optional()
|
|
433
299
|
});
|
|
434
300
|
function perplexityErrorToMessage(data) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
if (typeof data.detail === "string") {
|
|
442
|
-
return data.detail;
|
|
443
|
-
}
|
|
444
|
-
if (Array.isArray(data.detail)) {
|
|
445
|
-
return data.detail.map((detail) => detail.msg).join(", ");
|
|
446
|
-
}
|
|
447
|
-
return data.message ?? "unknown error";
|
|
301
|
+
if (typeof data.error === "string") return data.error;
|
|
302
|
+
if (data.error != null) return data.error.message ?? data.error.type ?? "unknown error";
|
|
303
|
+
if (typeof data.detail === "string") return data.detail;
|
|
304
|
+
if (Array.isArray(data.detail)) return data.detail.map((detail) => detail.msg).join(", ");
|
|
305
|
+
return data.message ?? "unknown error";
|
|
448
306
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
region: z3.string().optional()
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region src/perplexity-language-model-options.ts
|
|
309
|
+
const userLocationSchema = z.looseObject({
|
|
310
|
+
latitude: z.number().optional(),
|
|
311
|
+
longitude: z.number().optional(),
|
|
312
|
+
country: z.string().optional(),
|
|
313
|
+
city: z.string().optional(),
|
|
314
|
+
region: z.string().optional()
|
|
458
315
|
});
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
316
|
+
const webSearchFiltersSchema = z.looseObject({
|
|
317
|
+
search_domain_filter: z.array(z.string()).optional(),
|
|
318
|
+
search_recency_filter: z.enum([
|
|
319
|
+
"hour",
|
|
320
|
+
"day",
|
|
321
|
+
"week",
|
|
322
|
+
"month",
|
|
323
|
+
"year"
|
|
324
|
+
]).optional(),
|
|
325
|
+
search_after_date_filter: z.string().optional(),
|
|
326
|
+
search_before_date_filter: z.string().optional(),
|
|
327
|
+
last_updated_after_filter: z.string().optional(),
|
|
328
|
+
last_updated_before_filter: z.string().optional()
|
|
466
329
|
});
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
330
|
+
const webSearchToolSchema = z.looseObject({
|
|
331
|
+
type: z.literal("web_search"),
|
|
332
|
+
filters: webSearchFiltersSchema.optional(),
|
|
333
|
+
max_results: z.number().int().positive().optional(),
|
|
334
|
+
max_tokens: z.number().optional(),
|
|
335
|
+
max_tokens_per_page: z.number().optional(),
|
|
336
|
+
search_context_size: z.enum([
|
|
337
|
+
"low",
|
|
338
|
+
"medium",
|
|
339
|
+
"high"
|
|
340
|
+
]).optional(),
|
|
341
|
+
user_location: userLocationSchema.optional()
|
|
475
342
|
});
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
343
|
+
const nativeToolSchema = z.union([
|
|
344
|
+
webSearchToolSchema,
|
|
345
|
+
z.looseObject({
|
|
346
|
+
type: z.literal("fetch_url"),
|
|
347
|
+
max_urls: z.number().optional()
|
|
348
|
+
}),
|
|
349
|
+
z.looseObject({ type: z.literal("people_search") }),
|
|
350
|
+
z.looseObject({ type: z.literal("finance_search") }),
|
|
351
|
+
z.looseObject({ type: z.literal("sandbox") }),
|
|
352
|
+
z.looseObject({
|
|
353
|
+
type: z.literal("mcp"),
|
|
354
|
+
server_label: z.string(),
|
|
355
|
+
server_url: z.string(),
|
|
356
|
+
allowed_tools: z.array(z.string()).optional(),
|
|
357
|
+
authorization: z.string().optional(),
|
|
358
|
+
defer_loading: z.boolean().optional(),
|
|
359
|
+
headers: z.record(z.string(), z.string()).optional()
|
|
360
|
+
}),
|
|
361
|
+
z.looseObject({
|
|
362
|
+
type: z.literal("connector"),
|
|
363
|
+
id: z.string(),
|
|
364
|
+
server_label: z.string(),
|
|
365
|
+
server_description: z.string().optional(),
|
|
366
|
+
allowed_tools: z.array(z.string()).optional()
|
|
367
|
+
})
|
|
501
368
|
]);
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
).optional()
|
|
369
|
+
const perplexityLanguageModelOptions = z.looseObject({
|
|
370
|
+
/** Top-level Agent API instructions. */
|
|
371
|
+
instructions: z.string().optional(),
|
|
372
|
+
/**
|
|
373
|
+
* Native Agent API tools. AI SDK function tools can also be supplied through
|
|
374
|
+
* the top-level `tools` option on `generateText` and `streamText`.
|
|
375
|
+
*/
|
|
376
|
+
tools: z.array(nativeToolSchema).optional(),
|
|
377
|
+
/** A fallback model list for Agent API routing. */
|
|
378
|
+
models: z.array(z.string()).optional(),
|
|
379
|
+
/** Maximum number of agentic steps. */
|
|
380
|
+
max_steps: z.number().int().positive().optional(),
|
|
381
|
+
/** Maximum number of native tool calls. */
|
|
382
|
+
max_tool_calls: z.number().int().nonnegative().optional(),
|
|
383
|
+
/** Continue a conversation from an earlier Agent API response. */
|
|
384
|
+
previous_response_id: z.string().optional(),
|
|
385
|
+
/**
|
|
386
|
+
* Whether the response can be retrieved later. Setting this to false does
|
|
387
|
+
* not disable persistence or prevent previous_response_id continuations.
|
|
388
|
+
*/
|
|
389
|
+
store: z.boolean().optional(),
|
|
390
|
+
/** Preferred response language as an ISO 639-1 language code. */
|
|
391
|
+
language_preference: z.string().optional(),
|
|
392
|
+
/** Agent API reasoning configuration. */
|
|
393
|
+
reasoning: z.looseObject({ effort: z.enum([
|
|
394
|
+
"minimal",
|
|
395
|
+
"low",
|
|
396
|
+
"medium",
|
|
397
|
+
"high",
|
|
398
|
+
"xhigh"
|
|
399
|
+
]).optional() }).optional(),
|
|
400
|
+
/** Agent API skill configuration. */
|
|
401
|
+
skills: z.array(z.union([z.looseObject({
|
|
402
|
+
type: z.literal("builtin"),
|
|
403
|
+
name: z.enum([
|
|
404
|
+
"office",
|
|
405
|
+
"office/docx",
|
|
406
|
+
"office/pdf",
|
|
407
|
+
"office/pptx",
|
|
408
|
+
"office/xlsx"
|
|
409
|
+
])
|
|
410
|
+
}), z.looseObject({
|
|
411
|
+
type: z.literal("inline"),
|
|
412
|
+
name: z.string(),
|
|
413
|
+
description: z.string(),
|
|
414
|
+
instructions: z.string()
|
|
415
|
+
})])).optional()
|
|
550
416
|
});
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
function preparePerplexityTools({
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
582
|
-
return { tools: preparedTools, warnings };
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/perplexity-prepare-tools.ts
|
|
419
|
+
function preparePerplexityTools({ tools, toolChoice }) {
|
|
420
|
+
const preparedTools = [];
|
|
421
|
+
const warnings = [];
|
|
422
|
+
for (const tool of tools ?? []) {
|
|
423
|
+
if (tool.type === "provider") {
|
|
424
|
+
warnings.push({
|
|
425
|
+
type: "unsupported",
|
|
426
|
+
feature: `provider-defined tool ${tool.name}`
|
|
427
|
+
});
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
preparedTools.push({
|
|
431
|
+
type: "function",
|
|
432
|
+
name: tool.name,
|
|
433
|
+
description: tool.description,
|
|
434
|
+
parameters: tool.inputSchema,
|
|
435
|
+
strict: tool.strict
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
if (toolChoice != null && toolChoice.type !== "auto") warnings.push({
|
|
439
|
+
type: "unsupported",
|
|
440
|
+
feature: "toolChoice",
|
|
441
|
+
details: "The Perplexity Agent API currently selects tools automatically."
|
|
442
|
+
});
|
|
443
|
+
return {
|
|
444
|
+
tools: preparedTools,
|
|
445
|
+
warnings
|
|
446
|
+
};
|
|
583
447
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
448
|
+
//#endregion
|
|
449
|
+
//#region src/perplexity-language-model.ts
|
|
450
|
+
const presetIds = /* @__PURE__ */ new Set([
|
|
451
|
+
"fast",
|
|
452
|
+
"low",
|
|
453
|
+
"medium",
|
|
454
|
+
"high",
|
|
455
|
+
"xhigh"
|
|
592
456
|
]);
|
|
593
457
|
function getModelSelection(modelId) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
return { model: modelId };
|
|
458
|
+
if (presetIds.has(modelId)) return { preset: modelId };
|
|
459
|
+
return { model: modelId };
|
|
598
460
|
}
|
|
599
461
|
function getResponseMetadata(response) {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
462
|
+
return createLanguageModelResponseMetadata({
|
|
463
|
+
id: response.id,
|
|
464
|
+
model: response.model,
|
|
465
|
+
created: response.created_at
|
|
466
|
+
});
|
|
605
467
|
}
|
|
606
468
|
function getProviderMetadata(usage) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
toolCalls: usage?.tool_calls_details == null ? null : Object.fromEntries(
|
|
627
|
-
Object.entries(usage.tool_calls_details).map(
|
|
628
|
-
([name, details]) => [
|
|
629
|
-
name,
|
|
630
|
-
{ invocation: details.invocation ?? null }
|
|
631
|
-
]
|
|
632
|
-
)
|
|
633
|
-
)
|
|
634
|
-
}
|
|
635
|
-
};
|
|
469
|
+
const cost = usage?.cost;
|
|
470
|
+
return { perplexity: {
|
|
471
|
+
usage: {
|
|
472
|
+
citationTokens: null,
|
|
473
|
+
numSearchQueries: usage?.tool_calls_details ? Object.entries(usage.tool_calls_details).filter(([name]) => name.includes("search")).reduce((total, [, details]) => total + (details.invocation ?? 0), 0) : null
|
|
474
|
+
},
|
|
475
|
+
images: null,
|
|
476
|
+
cost: cost == null ? null : {
|
|
477
|
+
inputTokensCost: cost.input_cost ?? null,
|
|
478
|
+
outputTokensCost: cost.output_cost ?? null,
|
|
479
|
+
requestCost: null,
|
|
480
|
+
totalCost: cost.total_cost ?? null,
|
|
481
|
+
currency: cost.currency ?? null,
|
|
482
|
+
cacheCreationCost: cost.cache_creation_cost ?? null,
|
|
483
|
+
cacheReadCost: cost.cache_read_cost ?? null,
|
|
484
|
+
toolCallsCost: cost.tool_calls_cost ?? null
|
|
485
|
+
},
|
|
486
|
+
toolCalls: usage?.tool_calls_details == null ? null : Object.fromEntries(Object.entries(usage.tool_calls_details).map(([name, details]) => [name, { invocation: details.invocation ?? null }]))
|
|
487
|
+
} };
|
|
636
488
|
}
|
|
637
|
-
function createSource(result,
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
}
|
|
653
|
-
};
|
|
489
|
+
function createSource(result, generateId) {
|
|
490
|
+
return {
|
|
491
|
+
type: "source",
|
|
492
|
+
sourceType: "url",
|
|
493
|
+
id: result.id == null ? generateId() : String(result.id),
|
|
494
|
+
url: result.url,
|
|
495
|
+
title: result.title,
|
|
496
|
+
providerMetadata: { perplexity: {
|
|
497
|
+
resultId: result.id ?? null,
|
|
498
|
+
snippet: result.snippet ?? null,
|
|
499
|
+
date: result.date ?? null,
|
|
500
|
+
lastUpdated: result.last_updated ?? null,
|
|
501
|
+
source: result.source ?? null
|
|
502
|
+
} }
|
|
503
|
+
};
|
|
654
504
|
}
|
|
655
505
|
function hasSearchResultId(source) {
|
|
656
|
-
|
|
506
|
+
return typeof source.providerMetadata?.perplexity?.resultId === "number";
|
|
657
507
|
}
|
|
658
508
|
function getSearchResults(item) {
|
|
659
|
-
|
|
509
|
+
return item.type === "search_results" ? item.results ?? [] : [];
|
|
660
510
|
}
|
|
661
511
|
function getFetchedSources(item) {
|
|
662
|
-
|
|
512
|
+
return item.type === "fetch_url_results" ? item.contents ?? [] : [];
|
|
663
513
|
}
|
|
664
|
-
var PerplexityLanguageModel = class
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
providerMetadata: {
|
|
1135
|
-
perplexity: { snippet: result.snippet ?? null }
|
|
1136
|
-
}
|
|
1137
|
-
});
|
|
1138
|
-
}
|
|
1139
|
-
break;
|
|
1140
|
-
}
|
|
1141
|
-
case "response.reasoning.stopped": {
|
|
1142
|
-
emitReasoningThought(value.thought);
|
|
1143
|
-
if (activeReasoningId != null) {
|
|
1144
|
-
controller.enqueue({
|
|
1145
|
-
type: "reasoning-end",
|
|
1146
|
-
id: activeReasoningId
|
|
1147
|
-
});
|
|
1148
|
-
activeReasoningId = void 0;
|
|
1149
|
-
}
|
|
1150
|
-
break;
|
|
1151
|
-
}
|
|
1152
|
-
case "response.output_item.done": {
|
|
1153
|
-
if (value.item != null) {
|
|
1154
|
-
finishOutputText(value.item, value.output_index);
|
|
1155
|
-
emitOutputSources(value.item);
|
|
1156
|
-
emitFunctionCall(value.item);
|
|
1157
|
-
}
|
|
1158
|
-
break;
|
|
1159
|
-
}
|
|
1160
|
-
case "response.completed":
|
|
1161
|
-
case "response.incomplete": {
|
|
1162
|
-
if (value.response != null) {
|
|
1163
|
-
if (!hasResponseMetadata) {
|
|
1164
|
-
controller.enqueue({
|
|
1165
|
-
type: "response-metadata",
|
|
1166
|
-
...getResponseMetadata(value.response)
|
|
1167
|
-
});
|
|
1168
|
-
hasResponseMetadata = true;
|
|
1169
|
-
}
|
|
1170
|
-
for (const [
|
|
1171
|
-
outputIndex,
|
|
1172
|
-
item
|
|
1173
|
-
] of value.response.output.entries()) {
|
|
1174
|
-
finishOutputText(item, outputIndex);
|
|
1175
|
-
emitOutputSources(item);
|
|
1176
|
-
emitFunctionCall(item);
|
|
1177
|
-
}
|
|
1178
|
-
usage = value.response.usage ?? void 0;
|
|
1179
|
-
const rawFinishReason = value.response.incomplete_details?.reason ?? value.response.status;
|
|
1180
|
-
finishReason = {
|
|
1181
|
-
unified: mapPerplexityFinishReason({
|
|
1182
|
-
status: value.response.status,
|
|
1183
|
-
incompleteReason: value.response.incomplete_details?.reason,
|
|
1184
|
-
hasFunctionCall
|
|
1185
|
-
}),
|
|
1186
|
-
raw: rawFinishReason
|
|
1187
|
-
};
|
|
1188
|
-
}
|
|
1189
|
-
break;
|
|
1190
|
-
}
|
|
1191
|
-
case "response.failed": {
|
|
1192
|
-
finishReason = { unified: "error", raw: "failed" };
|
|
1193
|
-
controller.enqueue({
|
|
1194
|
-
type: "error",
|
|
1195
|
-
error: value.error ?? new Error("Perplexity response failed")
|
|
1196
|
-
});
|
|
1197
|
-
break;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
},
|
|
1201
|
-
flush(controller) {
|
|
1202
|
-
for (const source of pendingSourcesByUrl.values()) {
|
|
1203
|
-
controller.enqueue(source);
|
|
1204
|
-
}
|
|
1205
|
-
if (activeReasoningId != null) {
|
|
1206
|
-
controller.enqueue({
|
|
1207
|
-
type: "reasoning-end",
|
|
1208
|
-
id: activeReasoningId
|
|
1209
|
-
});
|
|
1210
|
-
}
|
|
1211
|
-
for (const [id, state] of textStates) {
|
|
1212
|
-
if (!state.ended) {
|
|
1213
|
-
controller.enqueue({ type: "text-end", id });
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
controller.enqueue({
|
|
1217
|
-
type: "finish",
|
|
1218
|
-
finishReason,
|
|
1219
|
-
usage: convertPerplexityUsage(usage),
|
|
1220
|
-
providerMetadata: getProviderMetadata(usage)
|
|
1221
|
-
});
|
|
1222
|
-
}
|
|
1223
|
-
})
|
|
1224
|
-
),
|
|
1225
|
-
request: { body },
|
|
1226
|
-
response: { headers: responseHeaders }
|
|
1227
|
-
};
|
|
1228
|
-
}
|
|
514
|
+
var PerplexityLanguageModel = class PerplexityLanguageModel {
|
|
515
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
516
|
+
return serializeModelOptions({
|
|
517
|
+
modelId: model.modelId,
|
|
518
|
+
config: model.config
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
522
|
+
return new PerplexityLanguageModel(options.modelId, options.config);
|
|
523
|
+
}
|
|
524
|
+
constructor(modelId, config) {
|
|
525
|
+
this.specificationVersion = "v4";
|
|
526
|
+
this.provider = "perplexity";
|
|
527
|
+
this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
|
|
528
|
+
this.modelId = modelId;
|
|
529
|
+
this.config = config;
|
|
530
|
+
}
|
|
531
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, reasoning, responseFormat, seed, providerOptions, tools, toolChoice }) {
|
|
532
|
+
const warnings = [];
|
|
533
|
+
const perplexityOptions = await parseProviderOptions({
|
|
534
|
+
provider: "perplexity",
|
|
535
|
+
providerOptions,
|
|
536
|
+
schema: perplexityLanguageModelOptions
|
|
537
|
+
}) ?? {};
|
|
538
|
+
if (topK != null) warnings.push({
|
|
539
|
+
type: "unsupported",
|
|
540
|
+
feature: "topK"
|
|
541
|
+
});
|
|
542
|
+
if (frequencyPenalty != null) warnings.push({
|
|
543
|
+
type: "unsupported",
|
|
544
|
+
feature: "frequencyPenalty"
|
|
545
|
+
});
|
|
546
|
+
if (presencePenalty != null) warnings.push({
|
|
547
|
+
type: "unsupported",
|
|
548
|
+
feature: "presencePenalty"
|
|
549
|
+
});
|
|
550
|
+
if (stopSequences != null) warnings.push({
|
|
551
|
+
type: "unsupported",
|
|
552
|
+
feature: "stopSequences"
|
|
553
|
+
});
|
|
554
|
+
if (seed != null) warnings.push({
|
|
555
|
+
type: "unsupported",
|
|
556
|
+
feature: "seed"
|
|
557
|
+
});
|
|
558
|
+
const { tools: nativeTools, ...agentOptions } = perplexityOptions;
|
|
559
|
+
const modelSelection = getModelSelection(this.modelId);
|
|
560
|
+
const { input, warnings: inputWarnings } = convertToPerplexityInput(prompt);
|
|
561
|
+
warnings.push(...inputWarnings);
|
|
562
|
+
const { tools: functionTools, warnings: toolWarnings } = preparePerplexityTools({
|
|
563
|
+
tools,
|
|
564
|
+
toolChoice
|
|
565
|
+
});
|
|
566
|
+
warnings.push(...toolWarnings);
|
|
567
|
+
const agentTools = [...nativeTools ?? [], ...functionTools];
|
|
568
|
+
let reasoningConfig = agentOptions.reasoning;
|
|
569
|
+
if (reasoningConfig == null && isCustomReasoning(reasoning)) {
|
|
570
|
+
if (reasoning === "none") warnings.push({
|
|
571
|
+
type: "unsupported",
|
|
572
|
+
feature: "reasoning \"none\""
|
|
573
|
+
});
|
|
574
|
+
else reasoningConfig = { effort: reasoning };
|
|
575
|
+
}
|
|
576
|
+
const body = {
|
|
577
|
+
...agentOptions,
|
|
578
|
+
...modelSelection,
|
|
579
|
+
input,
|
|
580
|
+
max_output_tokens: maxOutputTokens,
|
|
581
|
+
temperature,
|
|
582
|
+
top_p: topP,
|
|
583
|
+
reasoning: reasoningConfig,
|
|
584
|
+
response_format: responseFormat?.type === "json" && responseFormat.schema != null ? {
|
|
585
|
+
type: "json_schema",
|
|
586
|
+
json_schema: {
|
|
587
|
+
name: responseFormat.name ?? "response",
|
|
588
|
+
description: responseFormat.description,
|
|
589
|
+
schema: responseFormat.schema,
|
|
590
|
+
strict: true
|
|
591
|
+
}
|
|
592
|
+
} : void 0,
|
|
593
|
+
tools: agentTools.length > 0 ? agentTools : void 0
|
|
594
|
+
};
|
|
595
|
+
if (responseFormat?.type === "json" && responseFormat.schema == null) warnings.push({
|
|
596
|
+
type: "unsupported",
|
|
597
|
+
feature: "JSON response format without a schema"
|
|
598
|
+
});
|
|
599
|
+
return {
|
|
600
|
+
args: body,
|
|
601
|
+
warnings
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
async doGenerate(options) {
|
|
605
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
606
|
+
const url = `${this.config.baseURL}/v1/agent`;
|
|
607
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
608
|
+
url,
|
|
609
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
610
|
+
body,
|
|
611
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
612
|
+
errorSchema: perplexityErrorSchema,
|
|
613
|
+
errorToMessage: perplexityErrorToMessage
|
|
614
|
+
}),
|
|
615
|
+
successfulResponseHandler: createJsonResponseHandler(perplexityAgentResponseSchema),
|
|
616
|
+
abortSignal: options.abortSignal,
|
|
617
|
+
fetch: this.config.fetch
|
|
618
|
+
});
|
|
619
|
+
if (response.error != null || response.status === "failed") throw new APICallError({
|
|
620
|
+
message: response.error?.message ?? "Perplexity response failed",
|
|
621
|
+
url,
|
|
622
|
+
requestBodyValues: body,
|
|
623
|
+
statusCode: 400,
|
|
624
|
+
responseHeaders,
|
|
625
|
+
responseBody: rawResponse,
|
|
626
|
+
isRetryable: false
|
|
627
|
+
});
|
|
628
|
+
const content = [];
|
|
629
|
+
const sourceIndexesByUrl = /* @__PURE__ */ new Map();
|
|
630
|
+
let hasFunctionCall = false;
|
|
631
|
+
const addSource = (source) => {
|
|
632
|
+
const existingIndex = sourceIndexesByUrl.get(source.url);
|
|
633
|
+
if (existingIndex == null) {
|
|
634
|
+
sourceIndexesByUrl.set(source.url, content.length);
|
|
635
|
+
content.push(source);
|
|
636
|
+
} else if (hasSearchResultId(source) && !hasSearchResultId(content[existingIndex])) content[existingIndex] = source;
|
|
637
|
+
};
|
|
638
|
+
for (const item of response.output) if (item.type === "message") for (const part of item.content ?? []) {
|
|
639
|
+
if (part.type === "output_text" && part.text != null) content.push({
|
|
640
|
+
type: "text",
|
|
641
|
+
text: part.text
|
|
642
|
+
});
|
|
643
|
+
for (const annotation of part.annotations ?? []) if (annotation.url != null) addSource({
|
|
644
|
+
type: "source",
|
|
645
|
+
sourceType: "url",
|
|
646
|
+
id: this.config.generateId(),
|
|
647
|
+
url: annotation.url,
|
|
648
|
+
title: annotation.title
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
else if (item.type === "search_results") for (const result of getSearchResults(item)) addSource(createSource(result, this.config.generateId));
|
|
652
|
+
else if (item.type === "fetch_url_results") for (const result of getFetchedSources(item)) addSource({
|
|
653
|
+
type: "source",
|
|
654
|
+
sourceType: "url",
|
|
655
|
+
id: this.config.generateId(),
|
|
656
|
+
url: result.url,
|
|
657
|
+
title: result.title,
|
|
658
|
+
providerMetadata: { perplexity: { snippet: result.snippet ?? null } }
|
|
659
|
+
});
|
|
660
|
+
else if (item.type === "function_call" && item.call_id != null && item.name != null && item.arguments != null) {
|
|
661
|
+
hasFunctionCall = true;
|
|
662
|
+
content.push({
|
|
663
|
+
type: "tool-call",
|
|
664
|
+
toolCallId: item.call_id,
|
|
665
|
+
toolName: item.name,
|
|
666
|
+
input: item.arguments,
|
|
667
|
+
providerMetadata: { perplexity: {
|
|
668
|
+
itemId: item.id ?? null,
|
|
669
|
+
...item.thought_signature != null && { thoughtSignature: item.thought_signature }
|
|
670
|
+
} }
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
const finishReason = response.incomplete_details?.reason ?? response.status;
|
|
674
|
+
return {
|
|
675
|
+
content,
|
|
676
|
+
finishReason: {
|
|
677
|
+
unified: mapPerplexityFinishReason({
|
|
678
|
+
status: response.status,
|
|
679
|
+
incompleteReason: response.incomplete_details?.reason,
|
|
680
|
+
hasFunctionCall
|
|
681
|
+
}),
|
|
682
|
+
raw: finishReason
|
|
683
|
+
},
|
|
684
|
+
usage: convertPerplexityUsage(response.usage),
|
|
685
|
+
request: { body },
|
|
686
|
+
response: {
|
|
687
|
+
...getResponseMetadata(response),
|
|
688
|
+
headers: responseHeaders,
|
|
689
|
+
body: rawResponse
|
|
690
|
+
},
|
|
691
|
+
warnings,
|
|
692
|
+
providerMetadata: getProviderMetadata(response.usage)
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
async doStream(options) {
|
|
696
|
+
const { args, warnings } = await this.getArgs(options);
|
|
697
|
+
const body = {
|
|
698
|
+
...args,
|
|
699
|
+
stream: true
|
|
700
|
+
};
|
|
701
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
702
|
+
url: `${this.config.baseURL}/v1/agent`,
|
|
703
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
704
|
+
body,
|
|
705
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
706
|
+
errorSchema: perplexityErrorSchema,
|
|
707
|
+
errorToMessage: perplexityErrorToMessage
|
|
708
|
+
}),
|
|
709
|
+
successfulResponseHandler: createEventSourceResponseHandler(perplexityAgentChunkSchema),
|
|
710
|
+
abortSignal: options.abortSignal,
|
|
711
|
+
fetch: this.config.fetch
|
|
712
|
+
});
|
|
713
|
+
let finishReason = {
|
|
714
|
+
unified: "other",
|
|
715
|
+
raw: void 0
|
|
716
|
+
};
|
|
717
|
+
let usage;
|
|
718
|
+
let hasFunctionCall = false;
|
|
719
|
+
let hasResponseMetadata = false;
|
|
720
|
+
let activeReasoningId;
|
|
721
|
+
const textStates = /* @__PURE__ */ new Map();
|
|
722
|
+
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
|
723
|
+
const pendingSourcesByUrl = /* @__PURE__ */ new Map();
|
|
724
|
+
const seenFunctionCalls = /* @__PURE__ */ new Set();
|
|
725
|
+
const generateId = this.config.generateId;
|
|
726
|
+
return {
|
|
727
|
+
stream: response.pipeThrough(new TransformStream({
|
|
728
|
+
start(controller) {
|
|
729
|
+
controller.enqueue({
|
|
730
|
+
type: "stream-start",
|
|
731
|
+
warnings
|
|
732
|
+
});
|
|
733
|
+
},
|
|
734
|
+
transform(chunk, controller) {
|
|
735
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
736
|
+
type: "raw",
|
|
737
|
+
rawValue: chunk.rawValue
|
|
738
|
+
});
|
|
739
|
+
if (!chunk.success) {
|
|
740
|
+
finishReason = {
|
|
741
|
+
unified: "error",
|
|
742
|
+
raw: void 0
|
|
743
|
+
};
|
|
744
|
+
controller.enqueue({
|
|
745
|
+
type: "error",
|
|
746
|
+
error: chunk.error
|
|
747
|
+
});
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const value = chunk.value;
|
|
751
|
+
const getTextId = (itemId, outputIndex, contentIndex = 0) => {
|
|
752
|
+
const id = itemId ?? String(outputIndex ?? "text");
|
|
753
|
+
return contentIndex == null || contentIndex === 0 ? id : `${id}:${contentIndex}`;
|
|
754
|
+
};
|
|
755
|
+
const emitTextDelta = (id, delta) => {
|
|
756
|
+
let state = textStates.get(id);
|
|
757
|
+
if (state?.ended) return;
|
|
758
|
+
if (state == null) {
|
|
759
|
+
state = {
|
|
760
|
+
text: "",
|
|
761
|
+
ended: false
|
|
762
|
+
};
|
|
763
|
+
textStates.set(id, state);
|
|
764
|
+
controller.enqueue({
|
|
765
|
+
type: "text-start",
|
|
766
|
+
id
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
state.text += delta;
|
|
770
|
+
controller.enqueue({
|
|
771
|
+
type: "text-delta",
|
|
772
|
+
id,
|
|
773
|
+
delta
|
|
774
|
+
});
|
|
775
|
+
};
|
|
776
|
+
const finishText = (id, text) => {
|
|
777
|
+
const state = textStates.get(id);
|
|
778
|
+
if (state?.ended) return;
|
|
779
|
+
const emittedText = state?.text ?? "";
|
|
780
|
+
if (text != null && text.startsWith(emittedText) && text.length > emittedText.length) emitTextDelta(id, text.slice(emittedText.length));
|
|
781
|
+
const finalState = textStates.get(id);
|
|
782
|
+
if (finalState != null) {
|
|
783
|
+
finalState.ended = true;
|
|
784
|
+
controller.enqueue({
|
|
785
|
+
type: "text-end",
|
|
786
|
+
id
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
const finishOutputText = (item, outputIndex) => {
|
|
791
|
+
if (item.type === "message") {
|
|
792
|
+
for (const [contentIndex, part] of (item.content ?? []).entries()) if (part.type === "output_text") finishText(getTextId(item.id, outputIndex, contentIndex), part.text);
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
const emitSource = (source) => {
|
|
796
|
+
if (emittedSourceUrls.has(source.url)) return;
|
|
797
|
+
if (hasSearchResultId(source)) {
|
|
798
|
+
pendingSourcesByUrl.delete(source.url);
|
|
799
|
+
emittedSourceUrls.add(source.url);
|
|
800
|
+
controller.enqueue(source);
|
|
801
|
+
} else if (!pendingSourcesByUrl.has(source.url)) pendingSourcesByUrl.set(source.url, source);
|
|
802
|
+
};
|
|
803
|
+
const emitReasoningThought = (thought) => {
|
|
804
|
+
if (activeReasoningId != null && thought != null) controller.enqueue({
|
|
805
|
+
type: "reasoning-delta",
|
|
806
|
+
id: activeReasoningId,
|
|
807
|
+
delta: thought
|
|
808
|
+
});
|
|
809
|
+
};
|
|
810
|
+
const emitFunctionCall = (item) => {
|
|
811
|
+
if (item.type !== "function_call" || item.call_id == null || item.name == null || item.arguments == null || seenFunctionCalls.has(item.call_id)) return;
|
|
812
|
+
seenFunctionCalls.add(item.call_id);
|
|
813
|
+
hasFunctionCall = true;
|
|
814
|
+
controller.enqueue({
|
|
815
|
+
type: "tool-input-start",
|
|
816
|
+
id: item.call_id,
|
|
817
|
+
toolName: item.name
|
|
818
|
+
});
|
|
819
|
+
controller.enqueue({
|
|
820
|
+
type: "tool-input-delta",
|
|
821
|
+
id: item.call_id,
|
|
822
|
+
delta: item.arguments
|
|
823
|
+
});
|
|
824
|
+
controller.enqueue({
|
|
825
|
+
type: "tool-input-end",
|
|
826
|
+
id: item.call_id
|
|
827
|
+
});
|
|
828
|
+
controller.enqueue({
|
|
829
|
+
type: "tool-call",
|
|
830
|
+
toolCallId: item.call_id,
|
|
831
|
+
toolName: item.name,
|
|
832
|
+
input: item.arguments,
|
|
833
|
+
providerMetadata: { perplexity: {
|
|
834
|
+
itemId: item.id ?? null,
|
|
835
|
+
...item.thought_signature != null && { thoughtSignature: item.thought_signature }
|
|
836
|
+
} }
|
|
837
|
+
});
|
|
838
|
+
};
|
|
839
|
+
const emitOutputSources = (item) => {
|
|
840
|
+
if (item.type === "message") {
|
|
841
|
+
for (const part of item.content ?? []) for (const annotation of part.annotations ?? []) if (annotation.url != null) emitSource({
|
|
842
|
+
type: "source",
|
|
843
|
+
sourceType: "url",
|
|
844
|
+
id: generateId(),
|
|
845
|
+
url: annotation.url,
|
|
846
|
+
title: annotation.title
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
for (const result of getSearchResults(item)) emitSource(createSource(result, generateId));
|
|
850
|
+
for (const result of getFetchedSources(item)) emitSource({
|
|
851
|
+
type: "source",
|
|
852
|
+
sourceType: "url",
|
|
853
|
+
id: generateId(),
|
|
854
|
+
url: result.url,
|
|
855
|
+
title: result.title,
|
|
856
|
+
providerMetadata: { perplexity: { snippet: result.snippet ?? null } }
|
|
857
|
+
});
|
|
858
|
+
};
|
|
859
|
+
switch (value.type) {
|
|
860
|
+
case "response.created":
|
|
861
|
+
case "response.in_progress":
|
|
862
|
+
if (!hasResponseMetadata && value.response != null) {
|
|
863
|
+
controller.enqueue({
|
|
864
|
+
type: "response-metadata",
|
|
865
|
+
...getResponseMetadata(value.response)
|
|
866
|
+
});
|
|
867
|
+
hasResponseMetadata = true;
|
|
868
|
+
}
|
|
869
|
+
break;
|
|
870
|
+
case "response.output_text.delta":
|
|
871
|
+
if (value.delta != null) emitTextDelta(getTextId(value.item_id, value.output_index, value.content_index), value.delta);
|
|
872
|
+
break;
|
|
873
|
+
case "response.output_text.done":
|
|
874
|
+
finishText(getTextId(value.item_id, value.output_index, value.content_index), value.text);
|
|
875
|
+
break;
|
|
876
|
+
case "response.reasoning.started":
|
|
877
|
+
if (activeReasoningId != null) controller.enqueue({
|
|
878
|
+
type: "reasoning-end",
|
|
879
|
+
id: activeReasoningId
|
|
880
|
+
});
|
|
881
|
+
activeReasoningId = `reasoning-${value.sequence_number ?? generateId()}`;
|
|
882
|
+
controller.enqueue({
|
|
883
|
+
type: "reasoning-start",
|
|
884
|
+
id: activeReasoningId
|
|
885
|
+
});
|
|
886
|
+
emitReasoningThought(value.thought);
|
|
887
|
+
break;
|
|
888
|
+
case "response.reasoning.search_queries":
|
|
889
|
+
case "response.reasoning.fetch_url_queries":
|
|
890
|
+
emitReasoningThought(value.thought);
|
|
891
|
+
break;
|
|
892
|
+
case "response.reasoning.search_results":
|
|
893
|
+
emitReasoningThought(value.thought);
|
|
894
|
+
for (const result of value.results ?? []) emitSource(createSource(result, generateId));
|
|
895
|
+
break;
|
|
896
|
+
case "response.reasoning.fetch_url_results":
|
|
897
|
+
emitReasoningThought(value.thought);
|
|
898
|
+
for (const result of value.contents ?? []) emitSource({
|
|
899
|
+
type: "source",
|
|
900
|
+
sourceType: "url",
|
|
901
|
+
id: generateId(),
|
|
902
|
+
url: result.url,
|
|
903
|
+
title: result.title,
|
|
904
|
+
providerMetadata: { perplexity: { snippet: result.snippet ?? null } }
|
|
905
|
+
});
|
|
906
|
+
break;
|
|
907
|
+
case "response.reasoning.stopped":
|
|
908
|
+
emitReasoningThought(value.thought);
|
|
909
|
+
if (activeReasoningId != null) {
|
|
910
|
+
controller.enqueue({
|
|
911
|
+
type: "reasoning-end",
|
|
912
|
+
id: activeReasoningId
|
|
913
|
+
});
|
|
914
|
+
activeReasoningId = void 0;
|
|
915
|
+
}
|
|
916
|
+
break;
|
|
917
|
+
case "response.output_item.done":
|
|
918
|
+
if (value.item != null) {
|
|
919
|
+
finishOutputText(value.item, value.output_index);
|
|
920
|
+
emitOutputSources(value.item);
|
|
921
|
+
emitFunctionCall(value.item);
|
|
922
|
+
}
|
|
923
|
+
break;
|
|
924
|
+
case "response.completed":
|
|
925
|
+
case "response.incomplete":
|
|
926
|
+
if (value.response != null) {
|
|
927
|
+
if (!hasResponseMetadata) {
|
|
928
|
+
controller.enqueue({
|
|
929
|
+
type: "response-metadata",
|
|
930
|
+
...getResponseMetadata(value.response)
|
|
931
|
+
});
|
|
932
|
+
hasResponseMetadata = true;
|
|
933
|
+
}
|
|
934
|
+
for (const [outputIndex, item] of value.response.output.entries()) {
|
|
935
|
+
finishOutputText(item, outputIndex);
|
|
936
|
+
emitOutputSources(item);
|
|
937
|
+
emitFunctionCall(item);
|
|
938
|
+
}
|
|
939
|
+
usage = value.response.usage ?? void 0;
|
|
940
|
+
const rawFinishReason = value.response.incomplete_details?.reason ?? value.response.status;
|
|
941
|
+
finishReason = {
|
|
942
|
+
unified: mapPerplexityFinishReason({
|
|
943
|
+
status: value.response.status,
|
|
944
|
+
incompleteReason: value.response.incomplete_details?.reason,
|
|
945
|
+
hasFunctionCall
|
|
946
|
+
}),
|
|
947
|
+
raw: rawFinishReason
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
break;
|
|
951
|
+
case "response.failed":
|
|
952
|
+
finishReason = {
|
|
953
|
+
unified: "error",
|
|
954
|
+
raw: "failed"
|
|
955
|
+
};
|
|
956
|
+
controller.enqueue({
|
|
957
|
+
type: "error",
|
|
958
|
+
error: value.error ?? /* @__PURE__ */ new Error("Perplexity response failed")
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
},
|
|
962
|
+
flush(controller) {
|
|
963
|
+
for (const source of pendingSourcesByUrl.values()) controller.enqueue(source);
|
|
964
|
+
if (activeReasoningId != null) controller.enqueue({
|
|
965
|
+
type: "reasoning-end",
|
|
966
|
+
id: activeReasoningId
|
|
967
|
+
});
|
|
968
|
+
for (const [id, state] of textStates) if (!state.ended) controller.enqueue({
|
|
969
|
+
type: "text-end",
|
|
970
|
+
id
|
|
971
|
+
});
|
|
972
|
+
controller.enqueue({
|
|
973
|
+
type: "finish",
|
|
974
|
+
finishReason,
|
|
975
|
+
usage: convertPerplexityUsage(usage),
|
|
976
|
+
providerMetadata: getProviderMetadata(usage)
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
})),
|
|
980
|
+
request: { body },
|
|
981
|
+
response: { headers: responseHeaders }
|
|
982
|
+
};
|
|
983
|
+
}
|
|
1229
984
|
};
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
var PerplexityEmbeddingModel = class
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
embeddings: response.data.map(
|
|
1299
|
-
(item) => decodeEmbedding(item.embedding, encodingFormat)
|
|
1300
|
-
),
|
|
1301
|
-
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1302
|
-
providerMetadata: cost ? {
|
|
1303
|
-
perplexity: {
|
|
1304
|
-
cost: {
|
|
1305
|
-
inputCost: cost.input_cost ?? null,
|
|
1306
|
-
totalCost: cost.total_cost ?? null,
|
|
1307
|
-
currency: cost.currency ?? null
|
|
1308
|
-
}
|
|
1309
|
-
}
|
|
1310
|
-
} : void 0,
|
|
1311
|
-
response: { headers: responseHeaders, body: rawValue },
|
|
1312
|
-
warnings: []
|
|
1313
|
-
};
|
|
1314
|
-
}
|
|
985
|
+
//#endregion
|
|
986
|
+
//#region src/perplexity-embedding-model.ts
|
|
987
|
+
var PerplexityEmbeddingModel = class PerplexityEmbeddingModel {
|
|
988
|
+
get provider() {
|
|
989
|
+
return this.config.provider;
|
|
990
|
+
}
|
|
991
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
992
|
+
return serializeModelOptions({
|
|
993
|
+
modelId: model.modelId,
|
|
994
|
+
config: model.config
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
998
|
+
return new PerplexityEmbeddingModel(options.modelId, options.config);
|
|
999
|
+
}
|
|
1000
|
+
constructor(modelId, config) {
|
|
1001
|
+
this.specificationVersion = "v4";
|
|
1002
|
+
this.maxEmbeddingsPerCall = 512;
|
|
1003
|
+
this.supportsParallelCalls = true;
|
|
1004
|
+
this.modelId = modelId;
|
|
1005
|
+
this.config = config;
|
|
1006
|
+
}
|
|
1007
|
+
async doEmbed({ values, abortSignal, headers, providerOptions }) {
|
|
1008
|
+
if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
|
|
1009
|
+
provider: this.provider,
|
|
1010
|
+
modelId: this.modelId,
|
|
1011
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1012
|
+
values
|
|
1013
|
+
});
|
|
1014
|
+
const perplexityOptions = await parseProviderOptions({
|
|
1015
|
+
provider: "perplexity",
|
|
1016
|
+
providerOptions,
|
|
1017
|
+
schema: perplexityEmbeddingModelOptions
|
|
1018
|
+
}) ?? {};
|
|
1019
|
+
const encodingFormat = perplexityOptions.encodingFormat ?? "base64_int8";
|
|
1020
|
+
const { responseHeaders, value: response, rawValue } = await postJsonToApi({
|
|
1021
|
+
url: `${this.config.baseURL}/v1/embeddings`,
|
|
1022
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
1023
|
+
body: {
|
|
1024
|
+
model: this.modelId,
|
|
1025
|
+
input: values,
|
|
1026
|
+
dimensions: perplexityOptions.dimensions,
|
|
1027
|
+
encoding_format: encodingFormat
|
|
1028
|
+
},
|
|
1029
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
1030
|
+
errorSchema: perplexityErrorSchema,
|
|
1031
|
+
errorToMessage: perplexityErrorToMessage
|
|
1032
|
+
}),
|
|
1033
|
+
successfulResponseHandler: createJsonResponseHandler(perplexityEmbeddingResponseSchema),
|
|
1034
|
+
abortSignal,
|
|
1035
|
+
fetch: this.config.fetch
|
|
1036
|
+
});
|
|
1037
|
+
const cost = response.usage?.cost;
|
|
1038
|
+
return {
|
|
1039
|
+
embeddings: response.data.map((item) => decodeEmbedding(item.embedding, encodingFormat)),
|
|
1040
|
+
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1041
|
+
providerMetadata: cost ? { perplexity: { cost: {
|
|
1042
|
+
inputCost: cost.input_cost ?? null,
|
|
1043
|
+
totalCost: cost.total_cost ?? null,
|
|
1044
|
+
currency: cost.currency ?? null
|
|
1045
|
+
} } } : void 0,
|
|
1046
|
+
response: {
|
|
1047
|
+
headers: responseHeaders,
|
|
1048
|
+
body: rawValue
|
|
1049
|
+
},
|
|
1050
|
+
warnings: []
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1315
1053
|
};
|
|
1054
|
+
/**
|
|
1055
|
+
* Decodes a base64-encoded Perplexity embedding into a numeric vector.
|
|
1056
|
+
* For `base64_int8` the bytes are reinterpreted as signed int8 values;
|
|
1057
|
+
* for `base64_binary` the raw unsigned bytes (packed bits) are returned.
|
|
1058
|
+
*/
|
|
1316
1059
|
function decodeEmbedding(base64, encodingFormat) {
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
);
|
|
1324
|
-
return Array.from(int8);
|
|
1325
|
-
}
|
|
1326
|
-
return Array.from(bytes);
|
|
1060
|
+
const bytes = convertBase64ToUint8Array(base64);
|
|
1061
|
+
if (encodingFormat === "base64_int8") {
|
|
1062
|
+
const int8 = new Int8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1063
|
+
return Array.from(int8);
|
|
1064
|
+
}
|
|
1065
|
+
return Array.from(bytes);
|
|
1327
1066
|
}
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1067
|
+
const perplexityEmbeddingResponseSchema = z.object({
|
|
1068
|
+
data: z.array(z.object({ embedding: z.string() })),
|
|
1069
|
+
usage: z.object({
|
|
1070
|
+
prompt_tokens: z.number(),
|
|
1071
|
+
cost: z.object({
|
|
1072
|
+
input_cost: z.number().nullish(),
|
|
1073
|
+
total_cost: z.number().nullish(),
|
|
1074
|
+
currency: z.string().nullish()
|
|
1075
|
+
}).nullish()
|
|
1076
|
+
}).nullish()
|
|
1338
1077
|
});
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1078
|
+
//#endregion
|
|
1079
|
+
//#region src/version.ts
|
|
1080
|
+
const VERSION = "5.0.4";
|
|
1081
|
+
//#endregion
|
|
1082
|
+
//#region src/perplexity-provider.ts
|
|
1344
1083
|
function createPerplexity(options = {}) {
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
return provider;
|
|
1084
|
+
const getHeaders = () => withUserAgentSuffix({
|
|
1085
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1086
|
+
apiKey: options.apiKey,
|
|
1087
|
+
environmentVariableName: "PERPLEXITY_API_KEY",
|
|
1088
|
+
description: "Perplexity"
|
|
1089
|
+
})}`,
|
|
1090
|
+
"X-Pplx-Integration": "vercel-ai-sdk",
|
|
1091
|
+
...options.headers
|
|
1092
|
+
}, `ai-sdk-perplexity/${VERSION}`);
|
|
1093
|
+
const baseURL = withoutTrailingSlash(options.baseURL ?? "https://api.perplexity.ai");
|
|
1094
|
+
const createLanguageModel = (modelId) => {
|
|
1095
|
+
return new PerplexityLanguageModel(modelId, {
|
|
1096
|
+
baseURL,
|
|
1097
|
+
headers: getHeaders,
|
|
1098
|
+
generateId,
|
|
1099
|
+
fetch: options.fetch
|
|
1100
|
+
});
|
|
1101
|
+
};
|
|
1102
|
+
const createEmbeddingModel = (modelId) => new PerplexityEmbeddingModel(modelId, {
|
|
1103
|
+
provider: "perplexity.embedding",
|
|
1104
|
+
baseURL,
|
|
1105
|
+
headers: getHeaders,
|
|
1106
|
+
fetch: options.fetch
|
|
1107
|
+
});
|
|
1108
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
1109
|
+
provider.specificationVersion = "v4";
|
|
1110
|
+
provider.languageModel = createLanguageModel;
|
|
1111
|
+
provider.embedding = createEmbeddingModel;
|
|
1112
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
1113
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1114
|
+
provider.imageModel = (modelId) => {
|
|
1115
|
+
throw new NoSuchModelError({
|
|
1116
|
+
modelId,
|
|
1117
|
+
modelType: "imageModel"
|
|
1118
|
+
});
|
|
1119
|
+
};
|
|
1120
|
+
return provider;
|
|
1383
1121
|
}
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
perplexity
|
|
1389
|
-
};
|
|
1122
|
+
const perplexity = createPerplexity();
|
|
1123
|
+
//#endregion
|
|
1124
|
+
export { VERSION, createPerplexity, perplexity };
|
|
1125
|
+
|
|
1390
1126
|
//# sourceMappingURL=index.js.map
|