@ai-sdk/perplexity 0.0.0-64aae7dd-20260114144918
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 +1195 -0
- package/LICENSE +13 -0
- package/README.md +47 -0
- package/dist/index.d.mts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +527 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +513 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Vercel, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# AI SDK - Perplexity Provider
|
|
2
|
+
|
|
3
|
+
The **[Perplexity provider](https://ai-sdk.dev/providers/ai-sdk-providers/perplexity)** for the [AI SDK](https://ai-sdk.dev/docs)
|
|
4
|
+
contains language model support for Perplexity's Sonar API - a powerful answer engine with real-time web search capabilities.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- Real-time web search grounding for accurate, up-to-date responses
|
|
9
|
+
- Support for advanced queries and follow-up questions
|
|
10
|
+
- Multiple tiers available:
|
|
11
|
+
- **Sonar Pro**: Enhanced capabilities for complex tasks with 2x more citations
|
|
12
|
+
- **Sonar**: Lightweight offering optimized for speed and cost
|
|
13
|
+
- Industry-leading answer quality
|
|
14
|
+
- Data privacy - no training on customer data
|
|
15
|
+
- Self-serve API access with scalable pricing
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
The Perplexity provider is available in the `@ai-sdk/perplexity` module. You can install it with:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i @ai-sdk/perplexity
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Provider Instance
|
|
26
|
+
|
|
27
|
+
You can import the default provider instance `perplexity` from `@ai-sdk/perplexity`:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { perplexity } from '@ai-sdk/perplexity';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Example
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { perplexity } from '@ai-sdk/perplexity';
|
|
37
|
+
import { generateText } from 'ai';
|
|
38
|
+
|
|
39
|
+
const { text } = await generateText({
|
|
40
|
+
model: perplexity('sonar-pro'),
|
|
41
|
+
prompt: 'What are the latest developments in quantum computing?',
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
Please check out the **[Perplexity provider documentation](https://ai-sdk.dev/providers/ai-sdk-providers/perplexity)** for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
type PerplexityLanguageModelId = 'sonar-deep-research' | 'sonar-reasoning-pro' | 'sonar-reasoning' | 'sonar-pro' | 'sonar' | (string & {});
|
|
5
|
+
|
|
6
|
+
interface PerplexityProvider extends ProviderV3 {
|
|
7
|
+
/**
|
|
8
|
+
Creates an Perplexity chat model for text generation.
|
|
9
|
+
*/
|
|
10
|
+
(modelId: PerplexityLanguageModelId): LanguageModelV3;
|
|
11
|
+
/**
|
|
12
|
+
Creates an Perplexity language model for text generation.
|
|
13
|
+
*/
|
|
14
|
+
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV3;
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Use `embeddingModel` instead.
|
|
17
|
+
*/
|
|
18
|
+
textEmbeddingModel(modelId: string): never;
|
|
19
|
+
}
|
|
20
|
+
interface PerplexityProviderSettings {
|
|
21
|
+
/**
|
|
22
|
+
Base URL for the perplexity API calls.
|
|
23
|
+
*/
|
|
24
|
+
baseURL?: string;
|
|
25
|
+
/**
|
|
26
|
+
API key for authenticating requests.
|
|
27
|
+
*/
|
|
28
|
+
apiKey?: string;
|
|
29
|
+
/**
|
|
30
|
+
Custom headers to include in the requests.
|
|
31
|
+
*/
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
35
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
36
|
+
*/
|
|
37
|
+
fetch?: FetchFunction;
|
|
38
|
+
}
|
|
39
|
+
declare function createPerplexity(options?: PerplexityProviderSettings): PerplexityProvider;
|
|
40
|
+
declare const perplexity: PerplexityProvider;
|
|
41
|
+
|
|
42
|
+
declare const VERSION: string;
|
|
43
|
+
|
|
44
|
+
export { type PerplexityProvider, type PerplexityProviderSettings, VERSION, createPerplexity, perplexity };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ProviderV3, LanguageModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
type PerplexityLanguageModelId = 'sonar-deep-research' | 'sonar-reasoning-pro' | 'sonar-reasoning' | 'sonar-pro' | 'sonar' | (string & {});
|
|
5
|
+
|
|
6
|
+
interface PerplexityProvider extends ProviderV3 {
|
|
7
|
+
/**
|
|
8
|
+
Creates an Perplexity chat model for text generation.
|
|
9
|
+
*/
|
|
10
|
+
(modelId: PerplexityLanguageModelId): LanguageModelV3;
|
|
11
|
+
/**
|
|
12
|
+
Creates an Perplexity language model for text generation.
|
|
13
|
+
*/
|
|
14
|
+
languageModel(modelId: PerplexityLanguageModelId): LanguageModelV3;
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Use `embeddingModel` instead.
|
|
17
|
+
*/
|
|
18
|
+
textEmbeddingModel(modelId: string): never;
|
|
19
|
+
}
|
|
20
|
+
interface PerplexityProviderSettings {
|
|
21
|
+
/**
|
|
22
|
+
Base URL for the perplexity API calls.
|
|
23
|
+
*/
|
|
24
|
+
baseURL?: string;
|
|
25
|
+
/**
|
|
26
|
+
API key for authenticating requests.
|
|
27
|
+
*/
|
|
28
|
+
apiKey?: string;
|
|
29
|
+
/**
|
|
30
|
+
Custom headers to include in the requests.
|
|
31
|
+
*/
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
35
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
36
|
+
*/
|
|
37
|
+
fetch?: FetchFunction;
|
|
38
|
+
}
|
|
39
|
+
declare function createPerplexity(options?: PerplexityProviderSettings): PerplexityProvider;
|
|
40
|
+
declare const perplexity: PerplexityProvider;
|
|
41
|
+
|
|
42
|
+
declare const VERSION: string;
|
|
43
|
+
|
|
44
|
+
export { type PerplexityProvider, type PerplexityProviderSettings, VERSION, createPerplexity, perplexity };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
VERSION: () => VERSION,
|
|
24
|
+
createPerplexity: () => createPerplexity,
|
|
25
|
+
perplexity: () => perplexity
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/perplexity-provider.ts
|
|
30
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
31
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
32
|
+
|
|
33
|
+
// src/perplexity-language-model.ts
|
|
34
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
35
|
+
var import_v4 = require("zod/v4");
|
|
36
|
+
|
|
37
|
+
// src/convert-perplexity-usage.ts
|
|
38
|
+
function convertPerplexityUsage(usage) {
|
|
39
|
+
var _a, _b, _c;
|
|
40
|
+
if (usage == null) {
|
|
41
|
+
return {
|
|
42
|
+
inputTokens: {
|
|
43
|
+
total: void 0,
|
|
44
|
+
noCache: void 0,
|
|
45
|
+
cacheRead: void 0,
|
|
46
|
+
cacheWrite: void 0
|
|
47
|
+
},
|
|
48
|
+
outputTokens: {
|
|
49
|
+
total: void 0,
|
|
50
|
+
text: void 0,
|
|
51
|
+
reasoning: void 0
|
|
52
|
+
},
|
|
53
|
+
raw: void 0
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
|
|
57
|
+
const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
|
|
58
|
+
const reasoningTokens = (_c = usage.reasoning_tokens) != null ? _c : 0;
|
|
59
|
+
return {
|
|
60
|
+
inputTokens: {
|
|
61
|
+
total: promptTokens,
|
|
62
|
+
noCache: promptTokens,
|
|
63
|
+
cacheRead: void 0,
|
|
64
|
+
cacheWrite: void 0
|
|
65
|
+
},
|
|
66
|
+
outputTokens: {
|
|
67
|
+
total: completionTokens,
|
|
68
|
+
text: completionTokens - reasoningTokens,
|
|
69
|
+
reasoning: reasoningTokens
|
|
70
|
+
},
|
|
71
|
+
raw: usage
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/convert-to-perplexity-messages.ts
|
|
76
|
+
var import_provider = require("@ai-sdk/provider");
|
|
77
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
78
|
+
function convertToPerplexityMessages(prompt) {
|
|
79
|
+
const messages = [];
|
|
80
|
+
for (const { role, content } of prompt) {
|
|
81
|
+
switch (role) {
|
|
82
|
+
case "system": {
|
|
83
|
+
messages.push({ role: "system", content });
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "user":
|
|
87
|
+
case "assistant": {
|
|
88
|
+
const hasMultipartContent = content.some(
|
|
89
|
+
(part) => part.type === "file" && part.mediaType.startsWith("image/") || part.type === "file" && part.mediaType === "application/pdf"
|
|
90
|
+
);
|
|
91
|
+
const messageContent = content.map((part, index) => {
|
|
92
|
+
var _a;
|
|
93
|
+
switch (part.type) {
|
|
94
|
+
case "text": {
|
|
95
|
+
return {
|
|
96
|
+
type: "text",
|
|
97
|
+
text: part.text
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
case "file": {
|
|
101
|
+
if (part.mediaType === "application/pdf") {
|
|
102
|
+
return part.data instanceof URL ? {
|
|
103
|
+
type: "file_url",
|
|
104
|
+
file_url: {
|
|
105
|
+
url: part.data.toString()
|
|
106
|
+
},
|
|
107
|
+
file_name: part.filename
|
|
108
|
+
} : {
|
|
109
|
+
type: "file_url",
|
|
110
|
+
file_url: {
|
|
111
|
+
url: typeof part.data === "string" ? part.data : (0, import_provider_utils.convertUint8ArrayToBase64)(part.data)
|
|
112
|
+
},
|
|
113
|
+
file_name: part.filename || `document-${index}.pdf`
|
|
114
|
+
};
|
|
115
|
+
} else if (part.mediaType.startsWith("image/")) {
|
|
116
|
+
return part.data instanceof URL ? {
|
|
117
|
+
type: "image_url",
|
|
118
|
+
image_url: {
|
|
119
|
+
url: part.data.toString()
|
|
120
|
+
}
|
|
121
|
+
} : {
|
|
122
|
+
type: "image_url",
|
|
123
|
+
image_url: {
|
|
124
|
+
url: `data:${(_a = part.mediaType) != null ? _a : "image/jpeg"};base64,${typeof part.data === "string" ? part.data : (0, import_provider_utils.convertUint8ArrayToBase64)(part.data)}`
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}).filter(Boolean);
|
|
131
|
+
messages.push({
|
|
132
|
+
role,
|
|
133
|
+
content: hasMultipartContent ? messageContent : messageContent.filter((part) => part.type === "text").map((part) => part.text).join("")
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "tool": {
|
|
138
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
139
|
+
functionality: "Tool messages"
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
default: {
|
|
143
|
+
const _exhaustiveCheck = role;
|
|
144
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return messages;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/map-perplexity-finish-reason.ts
|
|
152
|
+
function mapPerplexityFinishReason(finishReason) {
|
|
153
|
+
switch (finishReason) {
|
|
154
|
+
case "stop":
|
|
155
|
+
case "length":
|
|
156
|
+
return finishReason;
|
|
157
|
+
default:
|
|
158
|
+
return "other";
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/perplexity-language-model.ts
|
|
163
|
+
var PerplexityLanguageModel = class {
|
|
164
|
+
constructor(modelId, config) {
|
|
165
|
+
this.specificationVersion = "v3";
|
|
166
|
+
this.provider = "perplexity";
|
|
167
|
+
this.supportedUrls = {
|
|
168
|
+
// No URLs are supported.
|
|
169
|
+
};
|
|
170
|
+
this.modelId = modelId;
|
|
171
|
+
this.config = config;
|
|
172
|
+
}
|
|
173
|
+
getArgs({
|
|
174
|
+
prompt,
|
|
175
|
+
maxOutputTokens,
|
|
176
|
+
temperature,
|
|
177
|
+
topP,
|
|
178
|
+
topK,
|
|
179
|
+
frequencyPenalty,
|
|
180
|
+
presencePenalty,
|
|
181
|
+
stopSequences,
|
|
182
|
+
responseFormat,
|
|
183
|
+
seed,
|
|
184
|
+
providerOptions
|
|
185
|
+
}) {
|
|
186
|
+
var _a;
|
|
187
|
+
const warnings = [];
|
|
188
|
+
if (topK != null) {
|
|
189
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
190
|
+
}
|
|
191
|
+
if (stopSequences != null) {
|
|
192
|
+
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
193
|
+
}
|
|
194
|
+
if (seed != null) {
|
|
195
|
+
warnings.push({ type: "unsupported", feature: "seed" });
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
args: {
|
|
199
|
+
// model id:
|
|
200
|
+
model: this.modelId,
|
|
201
|
+
// standardized settings:
|
|
202
|
+
frequency_penalty: frequencyPenalty,
|
|
203
|
+
max_tokens: maxOutputTokens,
|
|
204
|
+
presence_penalty: presencePenalty,
|
|
205
|
+
temperature,
|
|
206
|
+
top_k: topK,
|
|
207
|
+
top_p: topP,
|
|
208
|
+
// response format:
|
|
209
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? {
|
|
210
|
+
type: "json_schema",
|
|
211
|
+
json_schema: { schema: responseFormat.schema }
|
|
212
|
+
} : void 0,
|
|
213
|
+
// provider extensions
|
|
214
|
+
...(_a = providerOptions == null ? void 0 : providerOptions.perplexity) != null ? _a : {},
|
|
215
|
+
// messages:
|
|
216
|
+
messages: convertToPerplexityMessages(prompt)
|
|
217
|
+
},
|
|
218
|
+
warnings
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
async doGenerate(options) {
|
|
222
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
223
|
+
const { args: body, warnings } = this.getArgs(options);
|
|
224
|
+
const {
|
|
225
|
+
responseHeaders,
|
|
226
|
+
value: response,
|
|
227
|
+
rawValue: rawResponse
|
|
228
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
229
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
230
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
231
|
+
body,
|
|
232
|
+
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
233
|
+
errorSchema: perplexityErrorSchema,
|
|
234
|
+
errorToMessage
|
|
235
|
+
}),
|
|
236
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
237
|
+
perplexityResponseSchema
|
|
238
|
+
),
|
|
239
|
+
abortSignal: options.abortSignal,
|
|
240
|
+
fetch: this.config.fetch
|
|
241
|
+
});
|
|
242
|
+
const choice = response.choices[0];
|
|
243
|
+
const content = [];
|
|
244
|
+
const text = choice.message.content;
|
|
245
|
+
if (text.length > 0) {
|
|
246
|
+
content.push({ type: "text", text });
|
|
247
|
+
}
|
|
248
|
+
if (response.citations != null) {
|
|
249
|
+
for (const url of response.citations) {
|
|
250
|
+
content.push({
|
|
251
|
+
type: "source",
|
|
252
|
+
sourceType: "url",
|
|
253
|
+
id: this.config.generateId(),
|
|
254
|
+
url
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
content,
|
|
260
|
+
finishReason: {
|
|
261
|
+
unified: mapPerplexityFinishReason(choice.finish_reason),
|
|
262
|
+
raw: (_a = choice.finish_reason) != null ? _a : void 0
|
|
263
|
+
},
|
|
264
|
+
usage: convertPerplexityUsage(response.usage),
|
|
265
|
+
request: { body },
|
|
266
|
+
response: {
|
|
267
|
+
...getResponseMetadata(response),
|
|
268
|
+
headers: responseHeaders,
|
|
269
|
+
body: rawResponse
|
|
270
|
+
},
|
|
271
|
+
warnings,
|
|
272
|
+
providerMetadata: {
|
|
273
|
+
perplexity: {
|
|
274
|
+
images: (_c = (_b = response.images) == null ? void 0 : _b.map((image) => ({
|
|
275
|
+
imageUrl: image.image_url,
|
|
276
|
+
originUrl: image.origin_url,
|
|
277
|
+
height: image.height,
|
|
278
|
+
width: image.width
|
|
279
|
+
}))) != null ? _c : null,
|
|
280
|
+
usage: {
|
|
281
|
+
citationTokens: (_e = (_d = response.usage) == null ? void 0 : _d.citation_tokens) != null ? _e : null,
|
|
282
|
+
numSearchQueries: (_g = (_f = response.usage) == null ? void 0 : _f.num_search_queries) != null ? _g : null
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
async doStream(options) {
|
|
289
|
+
const { args, warnings } = this.getArgs(options);
|
|
290
|
+
const body = { ...args, stream: true };
|
|
291
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
292
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
293
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
294
|
+
body,
|
|
295
|
+
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
296
|
+
errorSchema: perplexityErrorSchema,
|
|
297
|
+
errorToMessage
|
|
298
|
+
}),
|
|
299
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
300
|
+
perplexityChunkSchema
|
|
301
|
+
),
|
|
302
|
+
abortSignal: options.abortSignal,
|
|
303
|
+
fetch: this.config.fetch
|
|
304
|
+
});
|
|
305
|
+
let finishReason = {
|
|
306
|
+
unified: "other",
|
|
307
|
+
raw: void 0
|
|
308
|
+
};
|
|
309
|
+
let usage = void 0;
|
|
310
|
+
const providerMetadata = {
|
|
311
|
+
perplexity: {
|
|
312
|
+
usage: {
|
|
313
|
+
citationTokens: null,
|
|
314
|
+
numSearchQueries: null
|
|
315
|
+
},
|
|
316
|
+
images: null
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
let isFirstChunk = true;
|
|
320
|
+
let isActive = false;
|
|
321
|
+
const self = this;
|
|
322
|
+
return {
|
|
323
|
+
stream: response.pipeThrough(
|
|
324
|
+
new TransformStream({
|
|
325
|
+
start(controller) {
|
|
326
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
327
|
+
},
|
|
328
|
+
transform(chunk, controller) {
|
|
329
|
+
var _a, _b, _c;
|
|
330
|
+
if (options.includeRawChunks) {
|
|
331
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
332
|
+
}
|
|
333
|
+
if (!chunk.success) {
|
|
334
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const value = chunk.value;
|
|
338
|
+
if (isFirstChunk) {
|
|
339
|
+
controller.enqueue({
|
|
340
|
+
type: "response-metadata",
|
|
341
|
+
...getResponseMetadata(value)
|
|
342
|
+
});
|
|
343
|
+
(_a = value.citations) == null ? void 0 : _a.forEach((url) => {
|
|
344
|
+
controller.enqueue({
|
|
345
|
+
type: "source",
|
|
346
|
+
sourceType: "url",
|
|
347
|
+
id: self.config.generateId(),
|
|
348
|
+
url
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
isFirstChunk = false;
|
|
352
|
+
}
|
|
353
|
+
if (value.usage != null) {
|
|
354
|
+
usage = value.usage;
|
|
355
|
+
providerMetadata.perplexity.usage = {
|
|
356
|
+
citationTokens: (_b = value.usage.citation_tokens) != null ? _b : null,
|
|
357
|
+
numSearchQueries: (_c = value.usage.num_search_queries) != null ? _c : null
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (value.images != null) {
|
|
361
|
+
providerMetadata.perplexity.images = value.images.map((image) => ({
|
|
362
|
+
imageUrl: image.image_url,
|
|
363
|
+
originUrl: image.origin_url,
|
|
364
|
+
height: image.height,
|
|
365
|
+
width: image.width
|
|
366
|
+
}));
|
|
367
|
+
}
|
|
368
|
+
const choice = value.choices[0];
|
|
369
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
370
|
+
finishReason = {
|
|
371
|
+
unified: mapPerplexityFinishReason(choice.finish_reason),
|
|
372
|
+
raw: choice.finish_reason
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const delta = choice.delta;
|
|
379
|
+
const textContent = delta.content;
|
|
380
|
+
if (textContent != null) {
|
|
381
|
+
if (!isActive) {
|
|
382
|
+
controller.enqueue({ type: "text-start", id: "0" });
|
|
383
|
+
isActive = true;
|
|
384
|
+
}
|
|
385
|
+
controller.enqueue({
|
|
386
|
+
type: "text-delta",
|
|
387
|
+
id: "0",
|
|
388
|
+
delta: textContent
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
flush(controller) {
|
|
393
|
+
if (isActive) {
|
|
394
|
+
controller.enqueue({ type: "text-end", id: "0" });
|
|
395
|
+
}
|
|
396
|
+
controller.enqueue({
|
|
397
|
+
type: "finish",
|
|
398
|
+
finishReason,
|
|
399
|
+
usage: convertPerplexityUsage(usage),
|
|
400
|
+
providerMetadata
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
})
|
|
404
|
+
),
|
|
405
|
+
request: { body },
|
|
406
|
+
response: { headers: responseHeaders }
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
function getResponseMetadata({
|
|
411
|
+
id,
|
|
412
|
+
model,
|
|
413
|
+
created
|
|
414
|
+
}) {
|
|
415
|
+
return {
|
|
416
|
+
id,
|
|
417
|
+
modelId: model,
|
|
418
|
+
timestamp: new Date(created * 1e3)
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
var perplexityUsageSchema = import_v4.z.object({
|
|
422
|
+
prompt_tokens: import_v4.z.number(),
|
|
423
|
+
completion_tokens: import_v4.z.number(),
|
|
424
|
+
total_tokens: import_v4.z.number().nullish(),
|
|
425
|
+
citation_tokens: import_v4.z.number().nullish(),
|
|
426
|
+
num_search_queries: import_v4.z.number().nullish(),
|
|
427
|
+
reasoning_tokens: import_v4.z.number().nullish()
|
|
428
|
+
});
|
|
429
|
+
var perplexityImageSchema = import_v4.z.object({
|
|
430
|
+
image_url: import_v4.z.string(),
|
|
431
|
+
origin_url: import_v4.z.string(),
|
|
432
|
+
height: import_v4.z.number(),
|
|
433
|
+
width: import_v4.z.number()
|
|
434
|
+
});
|
|
435
|
+
var perplexityResponseSchema = import_v4.z.object({
|
|
436
|
+
id: import_v4.z.string(),
|
|
437
|
+
created: import_v4.z.number(),
|
|
438
|
+
model: import_v4.z.string(),
|
|
439
|
+
choices: import_v4.z.array(
|
|
440
|
+
import_v4.z.object({
|
|
441
|
+
message: import_v4.z.object({
|
|
442
|
+
role: import_v4.z.literal("assistant"),
|
|
443
|
+
content: import_v4.z.string()
|
|
444
|
+
}),
|
|
445
|
+
finish_reason: import_v4.z.string().nullish()
|
|
446
|
+
})
|
|
447
|
+
),
|
|
448
|
+
citations: import_v4.z.array(import_v4.z.string()).nullish(),
|
|
449
|
+
images: import_v4.z.array(perplexityImageSchema).nullish(),
|
|
450
|
+
usage: perplexityUsageSchema.nullish()
|
|
451
|
+
});
|
|
452
|
+
var perplexityChunkSchema = import_v4.z.object({
|
|
453
|
+
id: import_v4.z.string(),
|
|
454
|
+
created: import_v4.z.number(),
|
|
455
|
+
model: import_v4.z.string(),
|
|
456
|
+
choices: import_v4.z.array(
|
|
457
|
+
import_v4.z.object({
|
|
458
|
+
delta: import_v4.z.object({
|
|
459
|
+
role: import_v4.z.literal("assistant"),
|
|
460
|
+
content: import_v4.z.string()
|
|
461
|
+
}),
|
|
462
|
+
finish_reason: import_v4.z.string().nullish()
|
|
463
|
+
})
|
|
464
|
+
),
|
|
465
|
+
citations: import_v4.z.array(import_v4.z.string()).nullish(),
|
|
466
|
+
images: import_v4.z.array(perplexityImageSchema).nullish(),
|
|
467
|
+
usage: perplexityUsageSchema.nullish()
|
|
468
|
+
});
|
|
469
|
+
var perplexityErrorSchema = import_v4.z.object({
|
|
470
|
+
error: import_v4.z.object({
|
|
471
|
+
code: import_v4.z.number(),
|
|
472
|
+
message: import_v4.z.string().nullish(),
|
|
473
|
+
type: import_v4.z.string().nullish()
|
|
474
|
+
})
|
|
475
|
+
});
|
|
476
|
+
var errorToMessage = (data) => {
|
|
477
|
+
var _a, _b;
|
|
478
|
+
return (_b = (_a = data.error.message) != null ? _a : data.error.type) != null ? _b : "unknown error";
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
// src/version.ts
|
|
482
|
+
var VERSION = true ? "0.0.0-64aae7dd-20260114144918" : "0.0.0-test";
|
|
483
|
+
|
|
484
|
+
// src/perplexity-provider.ts
|
|
485
|
+
function createPerplexity(options = {}) {
|
|
486
|
+
const getHeaders = () => (0, import_provider_utils3.withUserAgentSuffix)(
|
|
487
|
+
{
|
|
488
|
+
Authorization: `Bearer ${(0, import_provider_utils3.loadApiKey)({
|
|
489
|
+
apiKey: options.apiKey,
|
|
490
|
+
environmentVariableName: "PERPLEXITY_API_KEY",
|
|
491
|
+
description: "Perplexity"
|
|
492
|
+
})}`,
|
|
493
|
+
...options.headers
|
|
494
|
+
},
|
|
495
|
+
`ai-sdk/perplexity/${VERSION}`
|
|
496
|
+
);
|
|
497
|
+
const createLanguageModel = (modelId) => {
|
|
498
|
+
var _a;
|
|
499
|
+
return new PerplexityLanguageModel(modelId, {
|
|
500
|
+
baseURL: (0, import_provider_utils3.withoutTrailingSlash)(
|
|
501
|
+
(_a = options.baseURL) != null ? _a : "https://api.perplexity.ai"
|
|
502
|
+
),
|
|
503
|
+
headers: getHeaders,
|
|
504
|
+
generateId: import_provider_utils3.generateId,
|
|
505
|
+
fetch: options.fetch
|
|
506
|
+
});
|
|
507
|
+
};
|
|
508
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
509
|
+
provider.specificationVersion = "v3";
|
|
510
|
+
provider.languageModel = createLanguageModel;
|
|
511
|
+
provider.embeddingModel = (modelId) => {
|
|
512
|
+
throw new import_provider2.NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
513
|
+
};
|
|
514
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
515
|
+
provider.imageModel = (modelId) => {
|
|
516
|
+
throw new import_provider2.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
517
|
+
};
|
|
518
|
+
return provider;
|
|
519
|
+
}
|
|
520
|
+
var perplexity = createPerplexity();
|
|
521
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
522
|
+
0 && (module.exports = {
|
|
523
|
+
VERSION,
|
|
524
|
+
createPerplexity,
|
|
525
|
+
perplexity
|
|
526
|
+
});
|
|
527
|
+
//# sourceMappingURL=index.js.map
|