@ai-sdk/perplexity 0.0.7 → 1.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 +19 -0
- package/dist/index.d.mts +4 -23
- package/dist/index.d.ts +4 -23
- package/dist/index.js +377 -89
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +387 -88
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @ai-sdk/perplexity
|
|
2
2
|
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 2e898b4: feat (provider/perplexity): rewrite provider and support sources
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [2e898b4]
|
|
12
|
+
- @ai-sdk/provider-utils@2.1.8
|
|
13
|
+
|
|
14
|
+
## 0.0.8
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [3ff4ef8]
|
|
19
|
+
- @ai-sdk/provider-utils@2.1.7
|
|
20
|
+
- @ai-sdk/openai-compatible@0.1.9
|
|
21
|
+
|
|
3
22
|
## 0.0.7
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,36 +1,17 @@
|
|
|
1
1
|
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { OpenAICompatibleChatSettings } from '@ai-sdk/openai-compatible';
|
|
4
|
-
import { z } from 'zod';
|
|
5
3
|
|
|
6
|
-
type
|
|
7
|
-
interface PerplexityChatSettings extends OpenAICompatibleChatSettings {
|
|
8
|
-
}
|
|
4
|
+
type PerplexityLanguageModelId = 'sonar-reasoning-pro' | 'sonar-reasoning' | 'sonar-pro' | 'sonar' | (string & {});
|
|
9
5
|
|
|
10
|
-
declare const perplexityErrorSchema: z.ZodObject<{
|
|
11
|
-
code: z.ZodString;
|
|
12
|
-
error: z.ZodString;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
code: string;
|
|
15
|
-
error: string;
|
|
16
|
-
}, {
|
|
17
|
-
code: string;
|
|
18
|
-
error: string;
|
|
19
|
-
}>;
|
|
20
|
-
type PerplexityErrorData = z.infer<typeof perplexityErrorSchema>;
|
|
21
6
|
interface PerplexityProvider extends ProviderV1 {
|
|
22
7
|
/**
|
|
23
8
|
Creates an Perplexity chat model for text generation.
|
|
24
9
|
*/
|
|
25
|
-
(modelId:
|
|
10
|
+
(modelId: PerplexityLanguageModelId): LanguageModelV1;
|
|
26
11
|
/**
|
|
27
12
|
Creates an Perplexity language model for text generation.
|
|
28
13
|
*/
|
|
29
|
-
languageModel(modelId:
|
|
30
|
-
/**
|
|
31
|
-
Creates an Perplexity chat model for text generation.
|
|
32
|
-
*/
|
|
33
|
-
chat: (modelId: PerplexityChatModelId, settings?: PerplexityChatSettings) => LanguageModelV1;
|
|
14
|
+
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV1;
|
|
34
15
|
}
|
|
35
16
|
interface PerplexityProviderSettings {
|
|
36
17
|
/**
|
|
@@ -54,4 +35,4 @@ interface PerplexityProviderSettings {
|
|
|
54
35
|
declare function createPerplexity(options?: PerplexityProviderSettings): PerplexityProvider;
|
|
55
36
|
declare const perplexity: PerplexityProvider;
|
|
56
37
|
|
|
57
|
-
export { type
|
|
38
|
+
export { type PerplexityProvider, type PerplexityProviderSettings, createPerplexity, perplexity };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,17 @@
|
|
|
1
1
|
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { OpenAICompatibleChatSettings } from '@ai-sdk/openai-compatible';
|
|
4
|
-
import { z } from 'zod';
|
|
5
3
|
|
|
6
|
-
type
|
|
7
|
-
interface PerplexityChatSettings extends OpenAICompatibleChatSettings {
|
|
8
|
-
}
|
|
4
|
+
type PerplexityLanguageModelId = 'sonar-reasoning-pro' | 'sonar-reasoning' | 'sonar-pro' | 'sonar' | (string & {});
|
|
9
5
|
|
|
10
|
-
declare const perplexityErrorSchema: z.ZodObject<{
|
|
11
|
-
code: z.ZodString;
|
|
12
|
-
error: z.ZodString;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
code: string;
|
|
15
|
-
error: string;
|
|
16
|
-
}, {
|
|
17
|
-
code: string;
|
|
18
|
-
error: string;
|
|
19
|
-
}>;
|
|
20
|
-
type PerplexityErrorData = z.infer<typeof perplexityErrorSchema>;
|
|
21
6
|
interface PerplexityProvider extends ProviderV1 {
|
|
22
7
|
/**
|
|
23
8
|
Creates an Perplexity chat model for text generation.
|
|
24
9
|
*/
|
|
25
|
-
(modelId:
|
|
10
|
+
(modelId: PerplexityLanguageModelId): LanguageModelV1;
|
|
26
11
|
/**
|
|
27
12
|
Creates an Perplexity language model for text generation.
|
|
28
13
|
*/
|
|
29
|
-
languageModel(modelId:
|
|
30
|
-
/**
|
|
31
|
-
Creates an Perplexity chat model for text generation.
|
|
32
|
-
*/
|
|
33
|
-
chat: (modelId: PerplexityChatModelId, settings?: PerplexityChatSettings) => LanguageModelV1;
|
|
14
|
+
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV1;
|
|
34
15
|
}
|
|
35
16
|
interface PerplexityProviderSettings {
|
|
36
17
|
/**
|
|
@@ -54,4 +35,4 @@ interface PerplexityProviderSettings {
|
|
|
54
35
|
declare function createPerplexity(options?: PerplexityProviderSettings): PerplexityProvider;
|
|
55
36
|
declare const perplexity: PerplexityProvider;
|
|
56
37
|
|
|
57
|
-
export { type
|
|
38
|
+
export { type PerplexityProvider, type PerplexityProviderSettings, createPerplexity, perplexity };
|
package/dist/index.js
CHANGED
|
@@ -26,115 +26,405 @@ __export(src_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/perplexity-provider.ts
|
|
29
|
-
var
|
|
30
|
-
var import_openai_compatible = require("@ai-sdk/openai-compatible");
|
|
29
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
31
30
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
32
|
-
var import_zod2 = require("zod");
|
|
33
31
|
|
|
34
|
-
// src/perplexity-
|
|
32
|
+
// src/perplexity-language-model.ts
|
|
33
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
35
34
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
36
35
|
var import_zod = require("zod");
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
|
|
37
|
+
// src/convert-to-perplexity-messages.ts
|
|
38
|
+
var import_provider = require("@ai-sdk/provider");
|
|
39
|
+
function convertToPerplexityMessages(prompt) {
|
|
40
|
+
const messages = [];
|
|
41
|
+
for (const { role, content } of prompt) {
|
|
42
|
+
switch (role) {
|
|
43
|
+
case "system": {
|
|
44
|
+
messages.push({ role: "system", content });
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case "user":
|
|
48
|
+
case "assistant": {
|
|
49
|
+
messages.push({
|
|
50
|
+
role,
|
|
51
|
+
content: content.map((part) => {
|
|
52
|
+
switch (part.type) {
|
|
53
|
+
case "text": {
|
|
54
|
+
return part.text;
|
|
55
|
+
}
|
|
56
|
+
case "image": {
|
|
57
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
58
|
+
functionality: "Image content parts in user messages"
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
case "file": {
|
|
62
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
63
|
+
functionality: "File content parts in user messages"
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
case "tool-call": {
|
|
67
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
68
|
+
functionality: "Tool calls in assistant messages"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
default: {
|
|
72
|
+
const _exhaustiveCheck = part;
|
|
73
|
+
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}).join("")
|
|
77
|
+
});
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "tool": {
|
|
81
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
82
|
+
functionality: "Tool messages"
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
default: {
|
|
86
|
+
const _exhaustiveCheck = role;
|
|
87
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return messages;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/map-perplexity-finish-reason.ts
|
|
95
|
+
function mapPerplexityFinishReason(finishReason) {
|
|
96
|
+
switch (finishReason) {
|
|
97
|
+
case "stop":
|
|
98
|
+
case "length":
|
|
99
|
+
return finishReason;
|
|
100
|
+
default:
|
|
101
|
+
return "unknown";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/perplexity-language-model.ts
|
|
106
|
+
var PerplexityLanguageModel = class {
|
|
107
|
+
constructor(modelId, config) {
|
|
108
|
+
this.specificationVersion = "v1";
|
|
109
|
+
this.defaultObjectGenerationMode = "json";
|
|
110
|
+
this.supportsStructuredOutputs = true;
|
|
111
|
+
this.supportsImageUrls = false;
|
|
112
|
+
this.provider = "perplexity";
|
|
113
|
+
this.modelId = modelId;
|
|
114
|
+
this.config = config;
|
|
115
|
+
}
|
|
116
|
+
getArgs({
|
|
117
|
+
mode,
|
|
118
|
+
prompt,
|
|
119
|
+
maxTokens,
|
|
120
|
+
temperature,
|
|
121
|
+
topP,
|
|
122
|
+
topK,
|
|
123
|
+
frequencyPenalty,
|
|
124
|
+
presencePenalty,
|
|
125
|
+
stopSequences,
|
|
126
|
+
responseFormat,
|
|
127
|
+
seed,
|
|
128
|
+
providerMetadata
|
|
129
|
+
}) {
|
|
130
|
+
var _a;
|
|
131
|
+
const type = mode.type;
|
|
132
|
+
const warnings = [];
|
|
133
|
+
if (topK != null) {
|
|
134
|
+
warnings.push({
|
|
135
|
+
type: "unsupported-setting",
|
|
136
|
+
setting: "topK"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (stopSequences != null) {
|
|
140
|
+
warnings.push({
|
|
141
|
+
type: "unsupported-setting",
|
|
142
|
+
setting: "stopSequences"
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (seed != null) {
|
|
146
|
+
warnings.push({
|
|
147
|
+
type: "unsupported-setting",
|
|
148
|
+
setting: "seed"
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const baseArgs = {
|
|
152
|
+
// model id:
|
|
153
|
+
model: this.modelId,
|
|
154
|
+
// standardized settings:
|
|
155
|
+
frequency_penalty: frequencyPenalty,
|
|
156
|
+
max_tokens: maxTokens,
|
|
157
|
+
presence_penalty: presencePenalty,
|
|
158
|
+
temperature,
|
|
159
|
+
top_k: topK,
|
|
160
|
+
top_p: topP,
|
|
161
|
+
// response format:
|
|
162
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? {
|
|
163
|
+
type: "json_schema",
|
|
164
|
+
json_schema: { schema: responseFormat.schema }
|
|
165
|
+
} : void 0,
|
|
166
|
+
// provider extensions
|
|
167
|
+
...(_a = providerMetadata == null ? void 0 : providerMetadata.perplexity) != null ? _a : {},
|
|
168
|
+
// messages:
|
|
169
|
+
messages: convertToPerplexityMessages(prompt)
|
|
170
|
+
};
|
|
171
|
+
switch (type) {
|
|
172
|
+
case "regular": {
|
|
173
|
+
return { args: baseArgs, warnings };
|
|
174
|
+
}
|
|
175
|
+
case "object-json": {
|
|
176
|
+
return {
|
|
177
|
+
args: {
|
|
178
|
+
...baseArgs,
|
|
179
|
+
response_format: {
|
|
180
|
+
type: "json_schema",
|
|
181
|
+
json_schema: { schema: mode.schema }
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
warnings
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
case "object-tool": {
|
|
188
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
189
|
+
functionality: "tool-mode object generation"
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
default: {
|
|
193
|
+
const _exhaustiveCheck = type;
|
|
194
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async doGenerate(options) {
|
|
199
|
+
var _a, _b, _c, _d;
|
|
200
|
+
const { args, warnings } = this.getArgs(options);
|
|
201
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils.postJsonToApi)({
|
|
202
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
203
|
+
headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
|
|
204
|
+
body: args,
|
|
205
|
+
failedResponseHandler: (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
206
|
+
errorSchema: perplexityErrorSchema,
|
|
207
|
+
errorToMessage: (data) => data.error
|
|
208
|
+
}),
|
|
209
|
+
successfulResponseHandler: (0, import_provider_utils.createJsonResponseHandler)(
|
|
210
|
+
perplexityResponseSchema
|
|
211
|
+
),
|
|
212
|
+
abortSignal: options.abortSignal,
|
|
213
|
+
fetch: this.config.fetch
|
|
43
214
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(_c = parsed.value.usage) != null ? _c : void 0
|
|
48
|
-
);
|
|
49
|
-
},
|
|
50
|
-
createStreamExtractor: () => {
|
|
51
|
-
let citations;
|
|
52
|
-
let images;
|
|
53
|
-
let usage;
|
|
215
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
216
|
+
const choice = response.choices[0];
|
|
217
|
+
const text = choice.message.content;
|
|
54
218
|
return {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (parsed.success) {
|
|
62
|
-
citations = (_a = parsed.value.citations) != null ? _a : citations;
|
|
63
|
-
images = (_b = parsed.value.images) != null ? _b : images;
|
|
64
|
-
usage = (_c = parsed.value.usage) != null ? _c : usage;
|
|
65
|
-
}
|
|
219
|
+
text,
|
|
220
|
+
toolCalls: [],
|
|
221
|
+
finishReason: mapPerplexityFinishReason(choice.finish_reason),
|
|
222
|
+
usage: {
|
|
223
|
+
promptTokens: response.usage.prompt_tokens,
|
|
224
|
+
completionTokens: response.usage.completion_tokens
|
|
66
225
|
},
|
|
67
|
-
|
|
226
|
+
rawCall: { rawPrompt, rawSettings },
|
|
227
|
+
rawResponse: { headers: responseHeaders },
|
|
228
|
+
request: { body: JSON.stringify(args) },
|
|
229
|
+
response: getResponseMetadata(response),
|
|
230
|
+
warnings,
|
|
231
|
+
sources: response.citations.map((url) => ({
|
|
232
|
+
sourceType: "url",
|
|
233
|
+
id: this.config.generateId(),
|
|
234
|
+
url
|
|
235
|
+
})),
|
|
236
|
+
providerMetadata: {
|
|
237
|
+
perplexity: {
|
|
238
|
+
images: (_b = (_a = response.images) == null ? void 0 : _a.map((image) => ({
|
|
239
|
+
imageUrl: image.image_url,
|
|
240
|
+
originUrl: image.origin_url,
|
|
241
|
+
height: image.height,
|
|
242
|
+
width: image.width
|
|
243
|
+
}))) != null ? _b : null,
|
|
244
|
+
usage: {
|
|
245
|
+
citationTokens: (_c = response.usage.citation_tokens) != null ? _c : null,
|
|
246
|
+
numSearchQueries: (_d = response.usage.num_search_queries) != null ? _d : null
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
68
250
|
};
|
|
69
251
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
252
|
+
async doStream(options) {
|
|
253
|
+
const { args, warnings } = this.getArgs(options);
|
|
254
|
+
const body = { ...args, stream: true };
|
|
255
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils.postJsonToApi)({
|
|
256
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
257
|
+
headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
|
|
258
|
+
body,
|
|
259
|
+
failedResponseHandler: (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
260
|
+
errorSchema: perplexityErrorSchema,
|
|
261
|
+
errorToMessage: (data) => data.error
|
|
262
|
+
}),
|
|
263
|
+
successfulResponseHandler: (0, import_provider_utils.createEventSourceResponseHandler)(
|
|
264
|
+
perplexityChunkSchema
|
|
265
|
+
),
|
|
266
|
+
abortSignal: options.abortSignal,
|
|
267
|
+
fetch: this.config.fetch
|
|
268
|
+
});
|
|
269
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
270
|
+
let finishReason = "unknown";
|
|
271
|
+
let usage = {
|
|
272
|
+
promptTokens: Number.NaN,
|
|
273
|
+
completionTokens: Number.NaN
|
|
274
|
+
};
|
|
275
|
+
const providerMetadata = {
|
|
276
|
+
perplexity: {
|
|
85
277
|
usage: {
|
|
86
|
-
citationTokens:
|
|
87
|
-
numSearchQueries:
|
|
88
|
-
}
|
|
278
|
+
citationTokens: null,
|
|
279
|
+
numSearchQueries: null
|
|
280
|
+
},
|
|
281
|
+
images: null
|
|
89
282
|
}
|
|
90
|
-
}
|
|
91
|
-
|
|
283
|
+
};
|
|
284
|
+
let isFirstChunk = true;
|
|
285
|
+
const self = this;
|
|
286
|
+
return {
|
|
287
|
+
stream: response.pipeThrough(
|
|
288
|
+
new TransformStream({
|
|
289
|
+
transform(chunk, controller) {
|
|
290
|
+
var _a, _b, _c;
|
|
291
|
+
if (!chunk.success) {
|
|
292
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const value = chunk.value;
|
|
296
|
+
if (isFirstChunk) {
|
|
297
|
+
controller.enqueue({
|
|
298
|
+
type: "response-metadata",
|
|
299
|
+
...getResponseMetadata(value)
|
|
300
|
+
});
|
|
301
|
+
(_a = value.citations) == null ? void 0 : _a.forEach((url) => {
|
|
302
|
+
controller.enqueue({
|
|
303
|
+
type: "source",
|
|
304
|
+
source: {
|
|
305
|
+
sourceType: "url",
|
|
306
|
+
id: self.config.generateId(),
|
|
307
|
+
url
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
isFirstChunk = false;
|
|
312
|
+
}
|
|
313
|
+
if (value.usage != null) {
|
|
314
|
+
usage = {
|
|
315
|
+
promptTokens: value.usage.prompt_tokens,
|
|
316
|
+
completionTokens: value.usage.completion_tokens
|
|
317
|
+
};
|
|
318
|
+
providerMetadata.perplexity.usage = {
|
|
319
|
+
citationTokens: (_b = value.usage.citation_tokens) != null ? _b : null,
|
|
320
|
+
numSearchQueries: (_c = value.usage.num_search_queries) != null ? _c : null
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
if (value.images != null) {
|
|
324
|
+
providerMetadata.perplexity.images = value.images.map((image) => ({
|
|
325
|
+
imageUrl: image.image_url,
|
|
326
|
+
originUrl: image.origin_url,
|
|
327
|
+
height: image.height,
|
|
328
|
+
width: image.width
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
const choice = value.choices[0];
|
|
332
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
333
|
+
finishReason = mapPerplexityFinishReason(choice.finish_reason);
|
|
334
|
+
}
|
|
335
|
+
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const delta = choice.delta;
|
|
339
|
+
const textContent = delta.content;
|
|
340
|
+
if (textContent != null) {
|
|
341
|
+
controller.enqueue({
|
|
342
|
+
type: "text-delta",
|
|
343
|
+
textDelta: textContent
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
flush(controller) {
|
|
348
|
+
controller.enqueue({
|
|
349
|
+
type: "finish",
|
|
350
|
+
finishReason,
|
|
351
|
+
usage,
|
|
352
|
+
providerMetadata
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
),
|
|
357
|
+
rawCall: { rawPrompt, rawSettings },
|
|
358
|
+
rawResponse: { headers: responseHeaders },
|
|
359
|
+
request: { body: JSON.stringify(body) },
|
|
360
|
+
warnings
|
|
361
|
+
};
|
|
362
|
+
}
|
|
92
363
|
};
|
|
93
|
-
|
|
364
|
+
function getResponseMetadata({
|
|
365
|
+
id,
|
|
366
|
+
model,
|
|
367
|
+
created
|
|
368
|
+
}) {
|
|
369
|
+
return {
|
|
370
|
+
id,
|
|
371
|
+
modelId: model,
|
|
372
|
+
timestamp: new Date(created * 1e3)
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
var perplexityUsageSchema = import_zod.z.object({
|
|
376
|
+
prompt_tokens: import_zod.z.number(),
|
|
377
|
+
completion_tokens: import_zod.z.number(),
|
|
378
|
+
citation_tokens: import_zod.z.number().nullish(),
|
|
379
|
+
num_search_queries: import_zod.z.number().nullish()
|
|
380
|
+
});
|
|
94
381
|
var perplexityImageSchema = import_zod.z.object({
|
|
95
382
|
image_url: import_zod.z.string(),
|
|
96
383
|
origin_url: import_zod.z.string(),
|
|
97
384
|
height: import_zod.z.number(),
|
|
98
385
|
width: import_zod.z.number()
|
|
99
386
|
});
|
|
100
|
-
var perplexityUsageSchema = import_zod.z.object({
|
|
101
|
-
citation_tokens: import_zod.z.number().nullish(),
|
|
102
|
-
num_search_queries: import_zod.z.number().nullish()
|
|
103
|
-
});
|
|
104
387
|
var perplexityResponseSchema = import_zod.z.object({
|
|
105
|
-
|
|
388
|
+
id: import_zod.z.string(),
|
|
389
|
+
created: import_zod.z.number(),
|
|
390
|
+
model: import_zod.z.string(),
|
|
391
|
+
choices: import_zod.z.array(
|
|
392
|
+
import_zod.z.object({
|
|
393
|
+
message: import_zod.z.object({
|
|
394
|
+
role: import_zod.z.literal("assistant"),
|
|
395
|
+
content: import_zod.z.string()
|
|
396
|
+
}),
|
|
397
|
+
finish_reason: import_zod.z.string()
|
|
398
|
+
})
|
|
399
|
+
),
|
|
400
|
+
citations: import_zod.z.array(import_zod.z.string()),
|
|
106
401
|
images: import_zod.z.array(perplexityImageSchema).nullish(),
|
|
107
|
-
usage: perplexityUsageSchema
|
|
402
|
+
usage: perplexityUsageSchema
|
|
108
403
|
});
|
|
109
|
-
var
|
|
404
|
+
var perplexityChunkSchema = import_zod.z.object({
|
|
405
|
+
id: import_zod.z.string(),
|
|
406
|
+
created: import_zod.z.number(),
|
|
407
|
+
model: import_zod.z.string(),
|
|
110
408
|
choices: import_zod.z.array(
|
|
111
409
|
import_zod.z.object({
|
|
112
|
-
finish_reason: import_zod.z.string().nullish(),
|
|
113
410
|
delta: import_zod.z.object({
|
|
114
|
-
role: import_zod.z.
|
|
411
|
+
role: import_zod.z.literal("assistant"),
|
|
115
412
|
content: import_zod.z.string()
|
|
116
|
-
})
|
|
413
|
+
}),
|
|
414
|
+
finish_reason: import_zod.z.string().nullish()
|
|
117
415
|
})
|
|
118
|
-
)
|
|
119
|
-
citations:
|
|
416
|
+
),
|
|
417
|
+
citations: import_zod.z.array(import_zod.z.string()).nullish(),
|
|
120
418
|
images: import_zod.z.array(perplexityImageSchema).nullish(),
|
|
121
419
|
usage: perplexityUsageSchema.nullish()
|
|
122
420
|
});
|
|
421
|
+
var perplexityErrorSchema = import_zod.z.object({
|
|
422
|
+
code: import_zod.z.string(),
|
|
423
|
+
error: import_zod.z.string()
|
|
424
|
+
});
|
|
123
425
|
|
|
124
426
|
// src/perplexity-provider.ts
|
|
125
|
-
var perplexityErrorSchema = import_zod2.z.object({
|
|
126
|
-
code: import_zod2.z.string(),
|
|
127
|
-
error: import_zod2.z.string()
|
|
128
|
-
});
|
|
129
|
-
var perplexityErrorStructure = {
|
|
130
|
-
errorSchema: perplexityErrorSchema,
|
|
131
|
-
errorToMessage: (data) => data.error
|
|
132
|
-
};
|
|
133
427
|
function createPerplexity(options = {}) {
|
|
134
|
-
var _a;
|
|
135
|
-
const baseURL = (0, import_provider_utils2.withoutTrailingSlash)(
|
|
136
|
-
(_a = options.baseURL) != null ? _a : "https://api.perplexity.ai"
|
|
137
|
-
);
|
|
138
428
|
const getHeaders = () => ({
|
|
139
429
|
Authorization: `Bearer ${(0, import_provider_utils2.loadApiKey)({
|
|
140
430
|
apiKey: options.apiKey,
|
|
@@ -143,23 +433,21 @@ function createPerplexity(options = {}) {
|
|
|
143
433
|
})}`,
|
|
144
434
|
...options.headers
|
|
145
435
|
});
|
|
146
|
-
const createLanguageModel = (modelId
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
436
|
+
const createLanguageModel = (modelId) => {
|
|
437
|
+
var _a;
|
|
438
|
+
return new PerplexityLanguageModel(modelId, {
|
|
439
|
+
baseURL: (0, import_provider_utils2.withoutTrailingSlash)(
|
|
440
|
+
(_a = options.baseURL) != null ? _a : "https://api.perplexity.ai"
|
|
441
|
+
),
|
|
150
442
|
headers: getHeaders,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
errorStructure: perplexityErrorStructure,
|
|
154
|
-
metadataExtractor: perplexityMetadataExtractor,
|
|
155
|
-
supportsStructuredOutputs: true
|
|
443
|
+
generateId: import_provider_utils2.generateId,
|
|
444
|
+
fetch: options.fetch
|
|
156
445
|
});
|
|
157
446
|
};
|
|
158
|
-
const provider = (modelId
|
|
447
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
159
448
|
provider.languageModel = createLanguageModel;
|
|
160
|
-
provider.chat = createLanguageModel;
|
|
161
449
|
provider.textEmbeddingModel = (modelId) => {
|
|
162
|
-
throw new
|
|
450
|
+
throw new import_provider3.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
163
451
|
};
|
|
164
452
|
return provider;
|
|
165
453
|
}
|