@ai-sdk/openai 2.0.0-canary.2 → 2.0.0-canary.20
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 +257 -0
- package/README.md +2 -2
- package/dist/index.d.mts +39 -176
- package/dist/index.d.ts +39 -176
- package/dist/index.js +1118 -802
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1144 -815
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +379 -0
- package/dist/internal/index.d.ts +379 -0
- package/{internal/dist → dist/internal}/index.js +1108 -785
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +1125 -796
- package/dist/internal/index.mjs.map +1 -0
- package/internal.d.ts +1 -0
- package/package.json +19 -18
- package/internal/dist/index.d.mts +0 -290
- package/internal/dist/index.d.ts +0 -290
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -6,8 +6,7 @@ import {
|
|
|
6
6
|
|
|
7
7
|
// src/openai-chat-language-model.ts
|
|
8
8
|
import {
|
|
9
|
-
InvalidResponseDataError
|
|
10
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
9
|
+
InvalidResponseDataError
|
|
11
10
|
} from "@ai-sdk/provider";
|
|
12
11
|
import {
|
|
13
12
|
combineHeaders,
|
|
@@ -15,18 +14,18 @@ import {
|
|
|
15
14
|
createJsonResponseHandler,
|
|
16
15
|
generateId,
|
|
17
16
|
isParsableJson,
|
|
17
|
+
parseProviderOptions,
|
|
18
18
|
postJsonToApi
|
|
19
19
|
} from "@ai-sdk/provider-utils";
|
|
20
|
-
import { z as
|
|
20
|
+
import { z as z3 } from "zod";
|
|
21
21
|
|
|
22
22
|
// src/convert-to-openai-chat-messages.ts
|
|
23
23
|
import {
|
|
24
24
|
UnsupportedFunctionalityError
|
|
25
25
|
} from "@ai-sdk/provider";
|
|
26
|
-
import {
|
|
26
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
27
27
|
function convertToOpenAIChatMessages({
|
|
28
28
|
prompt,
|
|
29
|
-
useLegacyFunctionCalling = false,
|
|
30
29
|
systemMessageMode = "system"
|
|
31
30
|
}) {
|
|
32
31
|
const messages = [];
|
|
@@ -67,55 +66,71 @@ function convertToOpenAIChatMessages({
|
|
|
67
66
|
messages.push({
|
|
68
67
|
role: "user",
|
|
69
68
|
content: content.map((part, index) => {
|
|
70
|
-
var _a, _b, _c
|
|
69
|
+
var _a, _b, _c;
|
|
71
70
|
switch (part.type) {
|
|
72
71
|
case "text": {
|
|
73
72
|
return { type: "text", text: part.text };
|
|
74
73
|
}
|
|
75
|
-
case "image": {
|
|
76
|
-
return {
|
|
77
|
-
type: "image_url",
|
|
78
|
-
image_url: {
|
|
79
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
|
|
80
|
-
// OpenAI specific extension: image detail
|
|
81
|
-
detail: (_c = (_b = part.providerOptions) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
74
|
case "file": {
|
|
86
|
-
if (part.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
type: "input_audio",
|
|
102
|
-
input_audio: { data: part.data, format: "mp3" }
|
|
103
|
-
};
|
|
75
|
+
if (part.mediaType.startsWith("image/")) {
|
|
76
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
77
|
+
return {
|
|
78
|
+
type: "image_url",
|
|
79
|
+
image_url: {
|
|
80
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
|
|
81
|
+
// OpenAI specific extension: image detail
|
|
82
|
+
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
} else if (part.mediaType.startsWith("audio/")) {
|
|
86
|
+
if (part.data instanceof URL) {
|
|
87
|
+
throw new UnsupportedFunctionalityError({
|
|
88
|
+
functionality: "audio file parts with URLs"
|
|
89
|
+
});
|
|
104
90
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
switch (part.mediaType) {
|
|
92
|
+
case "audio/wav": {
|
|
93
|
+
return {
|
|
94
|
+
type: "input_audio",
|
|
95
|
+
input_audio: {
|
|
96
|
+
data: convertToBase64(part.data),
|
|
97
|
+
format: "wav"
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
case "audio/mp3":
|
|
102
|
+
case "audio/mpeg": {
|
|
103
|
+
return {
|
|
104
|
+
type: "input_audio",
|
|
105
|
+
input_audio: {
|
|
106
|
+
data: convertToBase64(part.data),
|
|
107
|
+
format: "mp3"
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
default: {
|
|
112
|
+
throw new UnsupportedFunctionalityError({
|
|
113
|
+
functionality: `audio content parts with media type ${part.mediaType}`
|
|
114
|
+
});
|
|
115
|
+
}
|
|
113
116
|
}
|
|
114
|
-
|
|
117
|
+
} else if (part.mediaType === "application/pdf") {
|
|
118
|
+
if (part.data instanceof URL) {
|
|
115
119
|
throw new UnsupportedFunctionalityError({
|
|
116
|
-
functionality:
|
|
120
|
+
functionality: "PDF file parts with URLs"
|
|
117
121
|
});
|
|
118
122
|
}
|
|
123
|
+
return {
|
|
124
|
+
type: "file",
|
|
125
|
+
file: {
|
|
126
|
+
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
|
|
127
|
+
file_data: `data:application/pdf;base64,${part.data}`
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
} else {
|
|
131
|
+
throw new UnsupportedFunctionalityError({
|
|
132
|
+
functionality: `file part media type ${part.mediaType}`
|
|
133
|
+
});
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
136
|
}
|
|
@@ -145,41 +160,20 @@ function convertToOpenAIChatMessages({
|
|
|
145
160
|
}
|
|
146
161
|
}
|
|
147
162
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
messages.push({
|
|
155
|
-
role: "assistant",
|
|
156
|
-
content: text,
|
|
157
|
-
function_call: toolCalls.length > 0 ? toolCalls[0].function : void 0
|
|
158
|
-
});
|
|
159
|
-
} else {
|
|
160
|
-
messages.push({
|
|
161
|
-
role: "assistant",
|
|
162
|
-
content: text,
|
|
163
|
-
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
164
|
-
});
|
|
165
|
-
}
|
|
163
|
+
messages.push({
|
|
164
|
+
role: "assistant",
|
|
165
|
+
content: text,
|
|
166
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
167
|
+
});
|
|
166
168
|
break;
|
|
167
169
|
}
|
|
168
170
|
case "tool": {
|
|
169
171
|
for (const toolResponse of content) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
176
|
-
} else {
|
|
177
|
-
messages.push({
|
|
178
|
-
role: "tool",
|
|
179
|
-
tool_call_id: toolResponse.toolCallId,
|
|
180
|
-
content: JSON.stringify(toolResponse.result)
|
|
181
|
-
});
|
|
182
|
-
}
|
|
172
|
+
messages.push({
|
|
173
|
+
role: "tool",
|
|
174
|
+
tool_call_id: toolResponse.toolCallId,
|
|
175
|
+
content: JSON.stringify(toolResponse.result)
|
|
176
|
+
});
|
|
183
177
|
}
|
|
184
178
|
break;
|
|
185
179
|
}
|
|
@@ -192,17 +186,17 @@ function convertToOpenAIChatMessages({
|
|
|
192
186
|
return { messages, warnings };
|
|
193
187
|
}
|
|
194
188
|
|
|
195
|
-
// src/
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
189
|
+
// src/get-response-metadata.ts
|
|
190
|
+
function getResponseMetadata({
|
|
191
|
+
id,
|
|
192
|
+
model,
|
|
193
|
+
created
|
|
194
|
+
}) {
|
|
195
|
+
return {
|
|
196
|
+
id: id != null ? id : void 0,
|
|
197
|
+
modelId: model != null ? model : void 0,
|
|
198
|
+
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
199
|
+
};
|
|
206
200
|
}
|
|
207
201
|
|
|
208
202
|
// src/map-openai-finish-reason.ts
|
|
@@ -222,18 +216,75 @@ function mapOpenAIFinishReason(finishReason) {
|
|
|
222
216
|
}
|
|
223
217
|
}
|
|
224
218
|
|
|
225
|
-
// src/openai-
|
|
219
|
+
// src/openai-chat-options.ts
|
|
226
220
|
import { z } from "zod";
|
|
221
|
+
var openaiProviderOptions = z.object({
|
|
222
|
+
/**
|
|
223
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
224
|
+
*
|
|
225
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
226
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
227
|
+
*/
|
|
228
|
+
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
229
|
+
/**
|
|
230
|
+
* Return the log probabilities of the tokens.
|
|
231
|
+
*
|
|
232
|
+
* Setting to true will return the log probabilities of the tokens that
|
|
233
|
+
* were generated.
|
|
234
|
+
*
|
|
235
|
+
* Setting to a number will return the log probabilities of the top n
|
|
236
|
+
* tokens that were generated.
|
|
237
|
+
*/
|
|
238
|
+
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
|
239
|
+
/**
|
|
240
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
241
|
+
*/
|
|
242
|
+
parallelToolCalls: z.boolean().optional(),
|
|
243
|
+
/**
|
|
244
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
245
|
+
* monitor and detect abuse.
|
|
246
|
+
*/
|
|
247
|
+
user: z.string().optional(),
|
|
248
|
+
/**
|
|
249
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
250
|
+
*/
|
|
251
|
+
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
|
|
252
|
+
/**
|
|
253
|
+
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
254
|
+
*/
|
|
255
|
+
maxCompletionTokens: z.number().optional(),
|
|
256
|
+
/**
|
|
257
|
+
* Whether to enable persistence in responses API.
|
|
258
|
+
*/
|
|
259
|
+
store: z.boolean().optional(),
|
|
260
|
+
/**
|
|
261
|
+
* Metadata to associate with the request.
|
|
262
|
+
*/
|
|
263
|
+
metadata: z.record(z.string()).optional(),
|
|
264
|
+
/**
|
|
265
|
+
* Parameters for prediction mode.
|
|
266
|
+
*/
|
|
267
|
+
prediction: z.record(z.any()).optional(),
|
|
268
|
+
/**
|
|
269
|
+
* Whether to use structured outputs.
|
|
270
|
+
*
|
|
271
|
+
* @default true
|
|
272
|
+
*/
|
|
273
|
+
structuredOutputs: z.boolean().optional()
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// src/openai-error.ts
|
|
277
|
+
import { z as z2 } from "zod";
|
|
227
278
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
228
|
-
var openaiErrorDataSchema =
|
|
229
|
-
error:
|
|
230
|
-
message:
|
|
279
|
+
var openaiErrorDataSchema = z2.object({
|
|
280
|
+
error: z2.object({
|
|
281
|
+
message: z2.string(),
|
|
231
282
|
// The additional information below is handled loosely to support
|
|
232
283
|
// OpenAI-compatible providers that have slightly different error
|
|
233
284
|
// responses:
|
|
234
|
-
type:
|
|
235
|
-
param:
|
|
236
|
-
code:
|
|
285
|
+
type: z2.string().nullish(),
|
|
286
|
+
param: z2.any().nullish(),
|
|
287
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
237
288
|
})
|
|
238
289
|
});
|
|
239
290
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
@@ -241,19 +292,6 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
241
292
|
errorToMessage: (data) => data.error.message
|
|
242
293
|
});
|
|
243
294
|
|
|
244
|
-
// src/get-response-metadata.ts
|
|
245
|
-
function getResponseMetadata({
|
|
246
|
-
id,
|
|
247
|
-
model,
|
|
248
|
-
created
|
|
249
|
-
}) {
|
|
250
|
-
return {
|
|
251
|
-
id: id != null ? id : void 0,
|
|
252
|
-
modelId: model != null ? model : void 0,
|
|
253
|
-
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
295
|
// src/openai-prepare-tools.ts
|
|
258
296
|
import {
|
|
259
297
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
@@ -261,7 +299,6 @@ import {
|
|
|
261
299
|
function prepareTools({
|
|
262
300
|
tools,
|
|
263
301
|
toolChoice,
|
|
264
|
-
useLegacyFunctionCalling = false,
|
|
265
302
|
structuredOutputs
|
|
266
303
|
}) {
|
|
267
304
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
@@ -269,48 +306,6 @@ function prepareTools({
|
|
|
269
306
|
if (tools == null) {
|
|
270
307
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
271
308
|
}
|
|
272
|
-
if (useLegacyFunctionCalling) {
|
|
273
|
-
const openaiFunctions = [];
|
|
274
|
-
for (const tool of tools) {
|
|
275
|
-
if (tool.type === "provider-defined") {
|
|
276
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
277
|
-
} else {
|
|
278
|
-
openaiFunctions.push({
|
|
279
|
-
name: tool.name,
|
|
280
|
-
description: tool.description,
|
|
281
|
-
parameters: tool.parameters
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (toolChoice == null) {
|
|
286
|
-
return {
|
|
287
|
-
functions: openaiFunctions,
|
|
288
|
-
function_call: void 0,
|
|
289
|
-
toolWarnings
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
const type2 = toolChoice.type;
|
|
293
|
-
switch (type2) {
|
|
294
|
-
case "auto":
|
|
295
|
-
case "none":
|
|
296
|
-
case void 0:
|
|
297
|
-
return {
|
|
298
|
-
functions: openaiFunctions,
|
|
299
|
-
function_call: void 0,
|
|
300
|
-
toolWarnings
|
|
301
|
-
};
|
|
302
|
-
case "required":
|
|
303
|
-
throw new UnsupportedFunctionalityError2({
|
|
304
|
-
functionality: "useLegacyFunctionCalling and toolChoice: required"
|
|
305
|
-
});
|
|
306
|
-
default:
|
|
307
|
-
return {
|
|
308
|
-
functions: openaiFunctions,
|
|
309
|
-
function_call: { name: toolChoice.toolName },
|
|
310
|
-
toolWarnings
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
309
|
const openaiTools2 = [];
|
|
315
310
|
for (const tool of tools) {
|
|
316
311
|
if (tool.type === "provider-defined") {
|
|
@@ -350,7 +345,7 @@ function prepareTools({
|
|
|
350
345
|
default: {
|
|
351
346
|
const _exhaustiveCheck = type;
|
|
352
347
|
throw new UnsupportedFunctionalityError2({
|
|
353
|
-
functionality: `
|
|
348
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
354
349
|
});
|
|
355
350
|
}
|
|
356
351
|
}
|
|
@@ -358,31 +353,20 @@ function prepareTools({
|
|
|
358
353
|
|
|
359
354
|
// src/openai-chat-language-model.ts
|
|
360
355
|
var OpenAIChatLanguageModel = class {
|
|
361
|
-
constructor(modelId,
|
|
356
|
+
constructor(modelId, config) {
|
|
362
357
|
this.specificationVersion = "v2";
|
|
358
|
+
this.supportedUrls = {
|
|
359
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
360
|
+
};
|
|
363
361
|
this.modelId = modelId;
|
|
364
|
-
this.settings = settings;
|
|
365
362
|
this.config = config;
|
|
366
363
|
}
|
|
367
|
-
get supportsStructuredOutputs() {
|
|
368
|
-
var _a;
|
|
369
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : isReasoningModel(this.modelId);
|
|
370
|
-
}
|
|
371
|
-
get defaultObjectGenerationMode() {
|
|
372
|
-
if (isAudioModel(this.modelId)) {
|
|
373
|
-
return "tool";
|
|
374
|
-
}
|
|
375
|
-
return this.supportsStructuredOutputs ? "json" : "tool";
|
|
376
|
-
}
|
|
377
364
|
get provider() {
|
|
378
365
|
return this.config.provider;
|
|
379
366
|
}
|
|
380
|
-
|
|
381
|
-
return !this.settings.downloadImages;
|
|
382
|
-
}
|
|
383
|
-
getArgs({
|
|
367
|
+
async getArgs({
|
|
384
368
|
prompt,
|
|
385
|
-
|
|
369
|
+
maxOutputTokens,
|
|
386
370
|
temperature,
|
|
387
371
|
topP,
|
|
388
372
|
topK,
|
|
@@ -395,36 +379,30 @@ var OpenAIChatLanguageModel = class {
|
|
|
395
379
|
toolChoice,
|
|
396
380
|
providerOptions
|
|
397
381
|
}) {
|
|
398
|
-
var _a, _b, _c
|
|
382
|
+
var _a, _b, _c;
|
|
399
383
|
const warnings = [];
|
|
384
|
+
const openaiOptions = (_a = await parseProviderOptions({
|
|
385
|
+
provider: "openai",
|
|
386
|
+
providerOptions,
|
|
387
|
+
schema: openaiProviderOptions
|
|
388
|
+
})) != null ? _a : {};
|
|
389
|
+
const structuredOutputs = (_b = openaiOptions.structuredOutputs) != null ? _b : true;
|
|
400
390
|
if (topK != null) {
|
|
401
391
|
warnings.push({
|
|
402
392
|
type: "unsupported-setting",
|
|
403
393
|
setting: "topK"
|
|
404
394
|
});
|
|
405
395
|
}
|
|
406
|
-
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !
|
|
396
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
|
|
407
397
|
warnings.push({
|
|
408
398
|
type: "unsupported-setting",
|
|
409
399
|
setting: "responseFormat",
|
|
410
400
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
411
401
|
});
|
|
412
402
|
}
|
|
413
|
-
const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
|
|
414
|
-
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
|
|
415
|
-
throw new UnsupportedFunctionalityError3({
|
|
416
|
-
functionality: "useLegacyFunctionCalling with parallelToolCalls"
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
|
|
420
|
-
throw new UnsupportedFunctionalityError3({
|
|
421
|
-
functionality: "structuredOutputs with useLegacyFunctionCalling"
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
403
|
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
|
|
425
404
|
{
|
|
426
405
|
prompt,
|
|
427
|
-
useLegacyFunctionCalling,
|
|
428
406
|
systemMessageMode: getSystemMessageMode(this.modelId)
|
|
429
407
|
}
|
|
430
408
|
);
|
|
@@ -433,36 +411,38 @@ var OpenAIChatLanguageModel = class {
|
|
|
433
411
|
// model id:
|
|
434
412
|
model: this.modelId,
|
|
435
413
|
// model specific settings:
|
|
436
|
-
logit_bias:
|
|
437
|
-
logprobs:
|
|
438
|
-
top_logprobs: typeof
|
|
439
|
-
user:
|
|
440
|
-
parallel_tool_calls:
|
|
414
|
+
logit_bias: openaiOptions.logitBias,
|
|
415
|
+
logprobs: openaiOptions.logprobs === true || typeof openaiOptions.logprobs === "number" ? true : void 0,
|
|
416
|
+
top_logprobs: typeof openaiOptions.logprobs === "number" ? openaiOptions.logprobs : typeof openaiOptions.logprobs === "boolean" ? openaiOptions.logprobs ? 0 : void 0 : void 0,
|
|
417
|
+
user: openaiOptions.user,
|
|
418
|
+
parallel_tool_calls: openaiOptions.parallelToolCalls,
|
|
441
419
|
// standardized settings:
|
|
442
|
-
max_tokens:
|
|
420
|
+
max_tokens: maxOutputTokens,
|
|
443
421
|
temperature,
|
|
444
422
|
top_p: topP,
|
|
445
423
|
frequency_penalty: frequencyPenalty,
|
|
446
424
|
presence_penalty: presencePenalty,
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
425
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? (
|
|
426
|
+
// TODO convert into provider option
|
|
427
|
+
structuredOutputs && responseFormat.schema != null ? {
|
|
428
|
+
type: "json_schema",
|
|
429
|
+
json_schema: {
|
|
430
|
+
schema: responseFormat.schema,
|
|
431
|
+
strict: true,
|
|
432
|
+
name: (_c = responseFormat.name) != null ? _c : "response",
|
|
433
|
+
description: responseFormat.description
|
|
434
|
+
}
|
|
435
|
+
} : { type: "json_object" }
|
|
436
|
+
) : void 0,
|
|
457
437
|
stop: stopSequences,
|
|
458
438
|
seed,
|
|
459
439
|
// openai specific settings:
|
|
460
|
-
// TODO remove in next major version; we auto-map
|
|
461
|
-
max_completion_tokens:
|
|
462
|
-
store:
|
|
463
|
-
metadata:
|
|
464
|
-
prediction:
|
|
465
|
-
reasoning_effort:
|
|
440
|
+
// TODO remove in next major version; we auto-map maxOutputTokens now
|
|
441
|
+
max_completion_tokens: openaiOptions.maxCompletionTokens,
|
|
442
|
+
store: openaiOptions.store,
|
|
443
|
+
metadata: openaiOptions.metadata,
|
|
444
|
+
prediction: openaiOptions.prediction,
|
|
445
|
+
reasoning_effort: openaiOptions.reasoningEffort,
|
|
466
446
|
// messages:
|
|
467
447
|
messages
|
|
468
448
|
};
|
|
@@ -526,33 +506,37 @@ var OpenAIChatLanguageModel = class {
|
|
|
526
506
|
}
|
|
527
507
|
baseArgs.max_tokens = void 0;
|
|
528
508
|
}
|
|
509
|
+
} else if (this.modelId.startsWith("gpt-4o-search-preview") || this.modelId.startsWith("gpt-4o-mini-search-preview")) {
|
|
510
|
+
if (baseArgs.temperature != null) {
|
|
511
|
+
baseArgs.temperature = void 0;
|
|
512
|
+
warnings.push({
|
|
513
|
+
type: "unsupported-setting",
|
|
514
|
+
setting: "temperature",
|
|
515
|
+
details: "temperature is not supported for the search preview models and has been removed."
|
|
516
|
+
});
|
|
517
|
+
}
|
|
529
518
|
}
|
|
530
519
|
const {
|
|
531
520
|
tools: openaiTools2,
|
|
532
521
|
toolChoice: openaiToolChoice,
|
|
533
|
-
functions,
|
|
534
|
-
function_call,
|
|
535
522
|
toolWarnings
|
|
536
523
|
} = prepareTools({
|
|
537
524
|
tools,
|
|
538
525
|
toolChoice,
|
|
539
|
-
|
|
540
|
-
structuredOutputs: this.supportsStructuredOutputs
|
|
526
|
+
structuredOutputs
|
|
541
527
|
});
|
|
542
528
|
return {
|
|
543
529
|
args: {
|
|
544
530
|
...baseArgs,
|
|
545
531
|
tools: openaiTools2,
|
|
546
|
-
tool_choice: openaiToolChoice
|
|
547
|
-
functions,
|
|
548
|
-
function_call
|
|
532
|
+
tool_choice: openaiToolChoice
|
|
549
533
|
},
|
|
550
534
|
warnings: [...warnings, ...toolWarnings]
|
|
551
535
|
};
|
|
552
536
|
}
|
|
553
537
|
async doGenerate(options) {
|
|
554
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
555
|
-
const { args: body, warnings } = this.getArgs(options);
|
|
538
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
539
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
556
540
|
const {
|
|
557
541
|
responseHeaders,
|
|
558
542
|
value: response,
|
|
@@ -571,105 +555,61 @@ var OpenAIChatLanguageModel = class {
|
|
|
571
555
|
abortSignal: options.abortSignal,
|
|
572
556
|
fetch: this.config.fetch
|
|
573
557
|
});
|
|
574
|
-
const { messages: rawPrompt, ...rawSettings } = body;
|
|
575
558
|
const choice = response.choices[0];
|
|
576
|
-
const
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
providerMetadata.openai.reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
|
|
559
|
+
const content = [];
|
|
560
|
+
const text = choice.message.content;
|
|
561
|
+
if (text != null && text.length > 0) {
|
|
562
|
+
content.push({ type: "text", text });
|
|
581
563
|
}
|
|
564
|
+
for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
|
|
565
|
+
content.push({
|
|
566
|
+
type: "tool-call",
|
|
567
|
+
toolCallType: "function",
|
|
568
|
+
toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
|
|
569
|
+
toolName: toolCall.function.name,
|
|
570
|
+
args: toolCall.function.arguments
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
const completionTokenDetails = (_c = response.usage) == null ? void 0 : _c.completion_tokens_details;
|
|
574
|
+
const promptTokenDetails = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details;
|
|
575
|
+
const providerMetadata = { openai: {} };
|
|
582
576
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
583
577
|
providerMetadata.openai.acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
584
578
|
}
|
|
585
579
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
586
580
|
providerMetadata.openai.rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
587
581
|
}
|
|
588
|
-
if ((
|
|
589
|
-
providerMetadata.openai.
|
|
582
|
+
if (((_e = choice.logprobs) == null ? void 0 : _e.content) != null) {
|
|
583
|
+
providerMetadata.openai.logprobs = choice.logprobs.content;
|
|
590
584
|
}
|
|
591
585
|
return {
|
|
592
|
-
|
|
593
|
-
toolCalls: this.settings.useLegacyFunctionCalling && choice.message.function_call ? [
|
|
594
|
-
{
|
|
595
|
-
toolCallType: "function",
|
|
596
|
-
toolCallId: generateId(),
|
|
597
|
-
toolName: choice.message.function_call.name,
|
|
598
|
-
args: choice.message.function_call.arguments
|
|
599
|
-
}
|
|
600
|
-
] : (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
601
|
-
var _a2;
|
|
602
|
-
return {
|
|
603
|
-
toolCallType: "function",
|
|
604
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
605
|
-
toolName: toolCall.function.name,
|
|
606
|
-
args: toolCall.function.arguments
|
|
607
|
-
};
|
|
608
|
-
}),
|
|
586
|
+
content,
|
|
609
587
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
610
588
|
usage: {
|
|
611
|
-
|
|
612
|
-
|
|
589
|
+
inputTokens: (_g = (_f = response.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
|
|
590
|
+
outputTokens: (_i = (_h = response.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0,
|
|
591
|
+
totalTokens: (_k = (_j = response.usage) == null ? void 0 : _j.total_tokens) != null ? _k : void 0,
|
|
592
|
+
reasoningTokens: (_l = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null ? _l : void 0,
|
|
593
|
+
cachedInputTokens: (_m = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null ? _m : void 0
|
|
594
|
+
},
|
|
595
|
+
request: { body },
|
|
596
|
+
response: {
|
|
597
|
+
...getResponseMetadata(response),
|
|
598
|
+
headers: responseHeaders,
|
|
599
|
+
body: rawResponse
|
|
613
600
|
},
|
|
614
|
-
rawCall: { rawPrompt, rawSettings },
|
|
615
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
|
616
|
-
request: { body: JSON.stringify(body) },
|
|
617
|
-
response: getResponseMetadata(response),
|
|
618
601
|
warnings,
|
|
619
|
-
logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
|
|
620
602
|
providerMetadata
|
|
621
603
|
};
|
|
622
604
|
}
|
|
623
605
|
async doStream(options) {
|
|
624
|
-
|
|
625
|
-
const result = await this.doGenerate(options);
|
|
626
|
-
const simulatedStream = new ReadableStream({
|
|
627
|
-
start(controller) {
|
|
628
|
-
controller.enqueue({ type: "response-metadata", ...result.response });
|
|
629
|
-
if (result.text) {
|
|
630
|
-
controller.enqueue({
|
|
631
|
-
type: "text-delta",
|
|
632
|
-
textDelta: result.text
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
if (result.toolCalls) {
|
|
636
|
-
for (const toolCall of result.toolCalls) {
|
|
637
|
-
controller.enqueue({
|
|
638
|
-
type: "tool-call-delta",
|
|
639
|
-
toolCallType: "function",
|
|
640
|
-
toolCallId: toolCall.toolCallId,
|
|
641
|
-
toolName: toolCall.toolName,
|
|
642
|
-
argsTextDelta: toolCall.args
|
|
643
|
-
});
|
|
644
|
-
controller.enqueue({
|
|
645
|
-
type: "tool-call",
|
|
646
|
-
...toolCall
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
controller.enqueue({
|
|
651
|
-
type: "finish",
|
|
652
|
-
finishReason: result.finishReason,
|
|
653
|
-
usage: result.usage,
|
|
654
|
-
logprobs: result.logprobs,
|
|
655
|
-
providerMetadata: result.providerMetadata
|
|
656
|
-
});
|
|
657
|
-
controller.close();
|
|
658
|
-
}
|
|
659
|
-
});
|
|
660
|
-
return {
|
|
661
|
-
stream: simulatedStream,
|
|
662
|
-
rawCall: result.rawCall,
|
|
663
|
-
rawResponse: result.rawResponse,
|
|
664
|
-
warnings: result.warnings
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
const { args, warnings } = this.getArgs(options);
|
|
606
|
+
const { args, warnings } = await this.getArgs(options);
|
|
668
607
|
const body = {
|
|
669
608
|
...args,
|
|
670
609
|
stream: true,
|
|
671
|
-
|
|
672
|
-
|
|
610
|
+
stream_options: {
|
|
611
|
+
include_usage: true
|
|
612
|
+
}
|
|
673
613
|
};
|
|
674
614
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
675
615
|
url: this.config.url({
|
|
@@ -685,22 +625,23 @@ var OpenAIChatLanguageModel = class {
|
|
|
685
625
|
abortSignal: options.abortSignal,
|
|
686
626
|
fetch: this.config.fetch
|
|
687
627
|
});
|
|
688
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
689
628
|
const toolCalls = [];
|
|
690
629
|
let finishReason = "unknown";
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
630
|
+
const usage = {
|
|
631
|
+
inputTokens: void 0,
|
|
632
|
+
outputTokens: void 0,
|
|
633
|
+
totalTokens: void 0
|
|
694
634
|
};
|
|
695
|
-
let logprobs;
|
|
696
635
|
let isFirstChunk = true;
|
|
697
|
-
const { useLegacyFunctionCalling } = this.settings;
|
|
698
636
|
const providerMetadata = { openai: {} };
|
|
699
637
|
return {
|
|
700
638
|
stream: response.pipeThrough(
|
|
701
639
|
new TransformStream({
|
|
640
|
+
start(controller) {
|
|
641
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
642
|
+
},
|
|
702
643
|
transform(chunk, controller) {
|
|
703
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
644
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
704
645
|
if (!chunk.success) {
|
|
705
646
|
finishReason = "error";
|
|
706
647
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -720,60 +661,37 @@ var OpenAIChatLanguageModel = class {
|
|
|
720
661
|
});
|
|
721
662
|
}
|
|
722
663
|
if (value.usage != null) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
promptTokens: prompt_tokens != null ? prompt_tokens : void 0,
|
|
731
|
-
completionTokens: completion_tokens != null ? completion_tokens : void 0
|
|
732
|
-
};
|
|
733
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
734
|
-
providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
664
|
+
usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
|
|
665
|
+
usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
|
|
666
|
+
usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
|
|
667
|
+
usage.reasoningTokens = (_e = (_d = value.usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
|
|
668
|
+
usage.cachedInputTokens = (_g = (_f = value.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0;
|
|
669
|
+
if (((_h = value.usage.completion_tokens_details) == null ? void 0 : _h.accepted_prediction_tokens) != null) {
|
|
670
|
+
providerMetadata.openai.acceptedPredictionTokens = (_i = value.usage.completion_tokens_details) == null ? void 0 : _i.accepted_prediction_tokens;
|
|
735
671
|
}
|
|
736
|
-
if ((completion_tokens_details == null ? void 0 :
|
|
737
|
-
providerMetadata.openai.
|
|
738
|
-
}
|
|
739
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
|
|
740
|
-
providerMetadata.openai.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
|
|
741
|
-
}
|
|
742
|
-
if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
|
|
743
|
-
providerMetadata.openai.cachedPromptTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
|
|
672
|
+
if (((_j = value.usage.completion_tokens_details) == null ? void 0 : _j.rejected_prediction_tokens) != null) {
|
|
673
|
+
providerMetadata.openai.rejectedPredictionTokens = (_k = value.usage.completion_tokens_details) == null ? void 0 : _k.rejected_prediction_tokens;
|
|
744
674
|
}
|
|
745
675
|
}
|
|
746
676
|
const choice = value.choices[0];
|
|
747
677
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
748
678
|
finishReason = mapOpenAIFinishReason(choice.finish_reason);
|
|
749
679
|
}
|
|
680
|
+
if (((_l = choice == null ? void 0 : choice.logprobs) == null ? void 0 : _l.content) != null) {
|
|
681
|
+
providerMetadata.openai.logprobs = choice.logprobs.content;
|
|
682
|
+
}
|
|
750
683
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
751
684
|
return;
|
|
752
685
|
}
|
|
753
686
|
const delta = choice.delta;
|
|
754
687
|
if (delta.content != null) {
|
|
755
688
|
controller.enqueue({
|
|
756
|
-
type: "text
|
|
757
|
-
|
|
689
|
+
type: "text",
|
|
690
|
+
text: delta.content
|
|
758
691
|
});
|
|
759
692
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
);
|
|
763
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
764
|
-
if (logprobs === void 0) logprobs = [];
|
|
765
|
-
logprobs.push(...mappedLogprobs);
|
|
766
|
-
}
|
|
767
|
-
const mappedToolCalls = useLegacyFunctionCalling && delta.function_call != null ? [
|
|
768
|
-
{
|
|
769
|
-
type: "function",
|
|
770
|
-
id: generateId(),
|
|
771
|
-
function: delta.function_call,
|
|
772
|
-
index: 0
|
|
773
|
-
}
|
|
774
|
-
] : delta.tool_calls;
|
|
775
|
-
if (mappedToolCalls != null) {
|
|
776
|
-
for (const toolCallDelta of mappedToolCalls) {
|
|
693
|
+
if (delta.tool_calls != null) {
|
|
694
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
777
695
|
const index = toolCallDelta.index;
|
|
778
696
|
if (toolCalls[index] == null) {
|
|
779
697
|
if (toolCallDelta.type !== "function") {
|
|
@@ -788,7 +706,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
788
706
|
message: `Expected 'id' to be a string.`
|
|
789
707
|
});
|
|
790
708
|
}
|
|
791
|
-
if (((
|
|
709
|
+
if (((_m = toolCallDelta.function) == null ? void 0 : _m.name) == null) {
|
|
792
710
|
throw new InvalidResponseDataError({
|
|
793
711
|
data: toolCallDelta,
|
|
794
712
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -799,12 +717,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
799
717
|
type: "function",
|
|
800
718
|
function: {
|
|
801
719
|
name: toolCallDelta.function.name,
|
|
802
|
-
arguments: (
|
|
720
|
+
arguments: (_n = toolCallDelta.function.arguments) != null ? _n : ""
|
|
803
721
|
},
|
|
804
722
|
hasFinished: false
|
|
805
723
|
};
|
|
806
724
|
const toolCall2 = toolCalls[index];
|
|
807
|
-
if (((
|
|
725
|
+
if (((_o = toolCall2.function) == null ? void 0 : _o.name) != null && ((_p = toolCall2.function) == null ? void 0 : _p.arguments) != null) {
|
|
808
726
|
if (toolCall2.function.arguments.length > 0) {
|
|
809
727
|
controller.enqueue({
|
|
810
728
|
type: "tool-call-delta",
|
|
@@ -818,7 +736,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
818
736
|
controller.enqueue({
|
|
819
737
|
type: "tool-call",
|
|
820
738
|
toolCallType: "function",
|
|
821
|
-
toolCallId: (
|
|
739
|
+
toolCallId: (_q = toolCall2.id) != null ? _q : generateId(),
|
|
822
740
|
toolName: toolCall2.function.name,
|
|
823
741
|
args: toolCall2.function.arguments
|
|
824
742
|
});
|
|
@@ -831,21 +749,21 @@ var OpenAIChatLanguageModel = class {
|
|
|
831
749
|
if (toolCall.hasFinished) {
|
|
832
750
|
continue;
|
|
833
751
|
}
|
|
834
|
-
if (((
|
|
835
|
-
toolCall.function.arguments += (
|
|
752
|
+
if (((_r = toolCallDelta.function) == null ? void 0 : _r.arguments) != null) {
|
|
753
|
+
toolCall.function.arguments += (_t = (_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null ? _t : "";
|
|
836
754
|
}
|
|
837
755
|
controller.enqueue({
|
|
838
756
|
type: "tool-call-delta",
|
|
839
757
|
toolCallType: "function",
|
|
840
758
|
toolCallId: toolCall.id,
|
|
841
759
|
toolName: toolCall.function.name,
|
|
842
|
-
argsTextDelta: (
|
|
760
|
+
argsTextDelta: (_u = toolCallDelta.function.arguments) != null ? _u : ""
|
|
843
761
|
});
|
|
844
|
-
if (((
|
|
762
|
+
if (((_v = toolCall.function) == null ? void 0 : _v.name) != null && ((_w = toolCall.function) == null ? void 0 : _w.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
845
763
|
controller.enqueue({
|
|
846
764
|
type: "tool-call",
|
|
847
765
|
toolCallType: "function",
|
|
848
|
-
toolCallId: (
|
|
766
|
+
toolCallId: (_x = toolCall.id) != null ? _x : generateId(),
|
|
849
767
|
toolName: toolCall.function.name,
|
|
850
768
|
args: toolCall.function.arguments
|
|
851
769
|
});
|
|
@@ -855,125 +773,111 @@ var OpenAIChatLanguageModel = class {
|
|
|
855
773
|
}
|
|
856
774
|
},
|
|
857
775
|
flush(controller) {
|
|
858
|
-
var _a, _b;
|
|
859
776
|
controller.enqueue({
|
|
860
777
|
type: "finish",
|
|
861
778
|
finishReason,
|
|
862
|
-
|
|
863
|
-
usage: {
|
|
864
|
-
promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
|
|
865
|
-
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
|
866
|
-
},
|
|
779
|
+
usage,
|
|
867
780
|
...providerMetadata != null ? { providerMetadata } : {}
|
|
868
781
|
});
|
|
869
782
|
}
|
|
870
783
|
})
|
|
871
784
|
),
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
request: { body: JSON.stringify(body) },
|
|
875
|
-
warnings
|
|
785
|
+
request: { body },
|
|
786
|
+
response: { headers: responseHeaders }
|
|
876
787
|
};
|
|
877
788
|
}
|
|
878
789
|
};
|
|
879
|
-
var openaiTokenUsageSchema =
|
|
880
|
-
prompt_tokens:
|
|
881
|
-
completion_tokens:
|
|
882
|
-
|
|
883
|
-
|
|
790
|
+
var openaiTokenUsageSchema = z3.object({
|
|
791
|
+
prompt_tokens: z3.number().nullish(),
|
|
792
|
+
completion_tokens: z3.number().nullish(),
|
|
793
|
+
total_tokens: z3.number().nullish(),
|
|
794
|
+
prompt_tokens_details: z3.object({
|
|
795
|
+
cached_tokens: z3.number().nullish()
|
|
884
796
|
}).nullish(),
|
|
885
|
-
completion_tokens_details:
|
|
886
|
-
reasoning_tokens:
|
|
887
|
-
accepted_prediction_tokens:
|
|
888
|
-
rejected_prediction_tokens:
|
|
797
|
+
completion_tokens_details: z3.object({
|
|
798
|
+
reasoning_tokens: z3.number().nullish(),
|
|
799
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
800
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
889
801
|
}).nullish()
|
|
890
802
|
}).nullish();
|
|
891
|
-
var openaiChatResponseSchema =
|
|
892
|
-
id:
|
|
893
|
-
created:
|
|
894
|
-
model:
|
|
895
|
-
choices:
|
|
896
|
-
|
|
897
|
-
message:
|
|
898
|
-
role:
|
|
899
|
-
content:
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
type: z2.literal("function"),
|
|
908
|
-
function: z2.object({
|
|
909
|
-
name: z2.string(),
|
|
910
|
-
arguments: z2.string()
|
|
803
|
+
var openaiChatResponseSchema = z3.object({
|
|
804
|
+
id: z3.string().nullish(),
|
|
805
|
+
created: z3.number().nullish(),
|
|
806
|
+
model: z3.string().nullish(),
|
|
807
|
+
choices: z3.array(
|
|
808
|
+
z3.object({
|
|
809
|
+
message: z3.object({
|
|
810
|
+
role: z3.literal("assistant").nullish(),
|
|
811
|
+
content: z3.string().nullish(),
|
|
812
|
+
tool_calls: z3.array(
|
|
813
|
+
z3.object({
|
|
814
|
+
id: z3.string().nullish(),
|
|
815
|
+
type: z3.literal("function"),
|
|
816
|
+
function: z3.object({
|
|
817
|
+
name: z3.string(),
|
|
818
|
+
arguments: z3.string()
|
|
911
819
|
})
|
|
912
820
|
})
|
|
913
821
|
).nullish()
|
|
914
822
|
}),
|
|
915
|
-
index:
|
|
916
|
-
logprobs:
|
|
917
|
-
content:
|
|
918
|
-
|
|
919
|
-
token:
|
|
920
|
-
logprob:
|
|
921
|
-
top_logprobs:
|
|
922
|
-
|
|
923
|
-
token:
|
|
924
|
-
logprob:
|
|
823
|
+
index: z3.number(),
|
|
824
|
+
logprobs: z3.object({
|
|
825
|
+
content: z3.array(
|
|
826
|
+
z3.object({
|
|
827
|
+
token: z3.string(),
|
|
828
|
+
logprob: z3.number(),
|
|
829
|
+
top_logprobs: z3.array(
|
|
830
|
+
z3.object({
|
|
831
|
+
token: z3.string(),
|
|
832
|
+
logprob: z3.number()
|
|
925
833
|
})
|
|
926
834
|
)
|
|
927
835
|
})
|
|
928
|
-
).
|
|
836
|
+
).nullish()
|
|
929
837
|
}).nullish(),
|
|
930
|
-
finish_reason:
|
|
838
|
+
finish_reason: z3.string().nullish()
|
|
931
839
|
})
|
|
932
840
|
),
|
|
933
841
|
usage: openaiTokenUsageSchema
|
|
934
842
|
});
|
|
935
|
-
var openaiChatChunkSchema =
|
|
936
|
-
|
|
937
|
-
id:
|
|
938
|
-
created:
|
|
939
|
-
model:
|
|
940
|
-
choices:
|
|
941
|
-
|
|
942
|
-
delta:
|
|
943
|
-
role:
|
|
944
|
-
content:
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
type: z2.literal("function").optional(),
|
|
954
|
-
function: z2.object({
|
|
955
|
-
name: z2.string().nullish(),
|
|
956
|
-
arguments: z2.string().nullish()
|
|
843
|
+
var openaiChatChunkSchema = z3.union([
|
|
844
|
+
z3.object({
|
|
845
|
+
id: z3.string().nullish(),
|
|
846
|
+
created: z3.number().nullish(),
|
|
847
|
+
model: z3.string().nullish(),
|
|
848
|
+
choices: z3.array(
|
|
849
|
+
z3.object({
|
|
850
|
+
delta: z3.object({
|
|
851
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
852
|
+
content: z3.string().nullish(),
|
|
853
|
+
tool_calls: z3.array(
|
|
854
|
+
z3.object({
|
|
855
|
+
index: z3.number(),
|
|
856
|
+
id: z3.string().nullish(),
|
|
857
|
+
type: z3.literal("function").nullish(),
|
|
858
|
+
function: z3.object({
|
|
859
|
+
name: z3.string().nullish(),
|
|
860
|
+
arguments: z3.string().nullish()
|
|
957
861
|
})
|
|
958
862
|
})
|
|
959
863
|
).nullish()
|
|
960
864
|
}).nullish(),
|
|
961
|
-
logprobs:
|
|
962
|
-
content:
|
|
963
|
-
|
|
964
|
-
token:
|
|
965
|
-
logprob:
|
|
966
|
-
top_logprobs:
|
|
967
|
-
|
|
968
|
-
token:
|
|
969
|
-
logprob:
|
|
865
|
+
logprobs: z3.object({
|
|
866
|
+
content: z3.array(
|
|
867
|
+
z3.object({
|
|
868
|
+
token: z3.string(),
|
|
869
|
+
logprob: z3.number(),
|
|
870
|
+
top_logprobs: z3.array(
|
|
871
|
+
z3.object({
|
|
872
|
+
token: z3.string(),
|
|
873
|
+
logprob: z3.number()
|
|
970
874
|
})
|
|
971
875
|
)
|
|
972
876
|
})
|
|
973
|
-
).
|
|
877
|
+
).nullish()
|
|
974
878
|
}).nullish(),
|
|
975
|
-
finish_reason:
|
|
976
|
-
index:
|
|
879
|
+
finish_reason: z3.string().nullish(),
|
|
880
|
+
index: z3.number()
|
|
977
881
|
})
|
|
978
882
|
),
|
|
979
883
|
usage: openaiTokenUsageSchema
|
|
@@ -981,10 +885,7 @@ var openaiChatChunkSchema = z2.union([
|
|
|
981
885
|
openaiErrorDataSchema
|
|
982
886
|
]);
|
|
983
887
|
function isReasoningModel(modelId) {
|
|
984
|
-
return modelId
|
|
985
|
-
}
|
|
986
|
-
function isAudioModel(modelId) {
|
|
987
|
-
return modelId.startsWith("gpt-4o-audio-preview");
|
|
888
|
+
return modelId.startsWith("o");
|
|
988
889
|
}
|
|
989
890
|
function getSystemMessageMode(modelId) {
|
|
990
891
|
var _a, _b;
|
|
@@ -1006,11 +907,23 @@ var reasoningModels = {
|
|
|
1006
907
|
"o1-preview-2024-09-12": {
|
|
1007
908
|
systemMessageMode: "remove"
|
|
1008
909
|
},
|
|
910
|
+
o3: {
|
|
911
|
+
systemMessageMode: "developer"
|
|
912
|
+
},
|
|
913
|
+
"o3-2025-04-16": {
|
|
914
|
+
systemMessageMode: "developer"
|
|
915
|
+
},
|
|
1009
916
|
"o3-mini": {
|
|
1010
917
|
systemMessageMode: "developer"
|
|
1011
918
|
},
|
|
1012
919
|
"o3-mini-2025-01-31": {
|
|
1013
920
|
systemMessageMode: "developer"
|
|
921
|
+
},
|
|
922
|
+
"o4-mini": {
|
|
923
|
+
systemMessageMode: "developer"
|
|
924
|
+
},
|
|
925
|
+
"o4-mini-2025-04-16": {
|
|
926
|
+
systemMessageMode: "developer"
|
|
1014
927
|
}
|
|
1015
928
|
};
|
|
1016
929
|
|
|
@@ -1019,24 +932,21 @@ import {
|
|
|
1019
932
|
combineHeaders as combineHeaders2,
|
|
1020
933
|
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1021
934
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
935
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1022
936
|
postJsonToApi as postJsonToApi2
|
|
1023
937
|
} from "@ai-sdk/provider-utils";
|
|
1024
|
-
import { z as
|
|
938
|
+
import { z as z5 } from "zod";
|
|
1025
939
|
|
|
1026
940
|
// src/convert-to-openai-completion-prompt.ts
|
|
1027
941
|
import {
|
|
1028
942
|
InvalidPromptError,
|
|
1029
|
-
UnsupportedFunctionalityError as
|
|
943
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1030
944
|
} from "@ai-sdk/provider";
|
|
1031
945
|
function convertToOpenAICompletionPrompt({
|
|
1032
946
|
prompt,
|
|
1033
|
-
inputFormat,
|
|
1034
947
|
user = "user",
|
|
1035
948
|
assistant = "assistant"
|
|
1036
949
|
}) {
|
|
1037
|
-
if (inputFormat === "prompt" && prompt.length === 1 && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0].type === "text") {
|
|
1038
|
-
return { prompt: prompt[0].content[0].text };
|
|
1039
|
-
}
|
|
1040
950
|
let text = "";
|
|
1041
951
|
if (prompt[0].role === "system") {
|
|
1042
952
|
text += `${prompt[0].content}
|
|
@@ -1058,13 +968,8 @@ function convertToOpenAICompletionPrompt({
|
|
|
1058
968
|
case "text": {
|
|
1059
969
|
return part.text;
|
|
1060
970
|
}
|
|
1061
|
-
case "image": {
|
|
1062
|
-
throw new UnsupportedFunctionalityError4({
|
|
1063
|
-
functionality: "images"
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
971
|
}
|
|
1067
|
-
}).join("");
|
|
972
|
+
}).filter(Boolean).join("");
|
|
1068
973
|
text += `${user}:
|
|
1069
974
|
${userMessage}
|
|
1070
975
|
|
|
@@ -1078,7 +983,7 @@ ${userMessage}
|
|
|
1078
983
|
return part.text;
|
|
1079
984
|
}
|
|
1080
985
|
case "tool-call": {
|
|
1081
|
-
throw new
|
|
986
|
+
throw new UnsupportedFunctionalityError3({
|
|
1082
987
|
functionality: "tool-call messages"
|
|
1083
988
|
});
|
|
1084
989
|
}
|
|
@@ -1091,7 +996,7 @@ ${assistantMessage}
|
|
|
1091
996
|
break;
|
|
1092
997
|
}
|
|
1093
998
|
case "tool": {
|
|
1094
|
-
throw new
|
|
999
|
+
throw new UnsupportedFunctionalityError3({
|
|
1095
1000
|
functionality: "tool messages"
|
|
1096
1001
|
});
|
|
1097
1002
|
}
|
|
@@ -1110,36 +1015,68 @@ ${user}:`]
|
|
|
1110
1015
|
};
|
|
1111
1016
|
}
|
|
1112
1017
|
|
|
1113
|
-
// src/
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1018
|
+
// src/openai-completion-options.ts
|
|
1019
|
+
import { z as z4 } from "zod";
|
|
1020
|
+
var openaiCompletionProviderOptions = z4.object({
|
|
1021
|
+
/**
|
|
1022
|
+
Echo back the prompt in addition to the completion.
|
|
1023
|
+
*/
|
|
1024
|
+
echo: z4.boolean().optional(),
|
|
1025
|
+
/**
|
|
1026
|
+
Modify the likelihood of specified tokens appearing in the completion.
|
|
1027
|
+
|
|
1028
|
+
Accepts a JSON object that maps tokens (specified by their token ID in
|
|
1029
|
+
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
|
1030
|
+
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
|
1031
|
+
the bias is added to the logits generated by the model prior to sampling.
|
|
1032
|
+
The exact effect will vary per model, but values between -1 and 1 should
|
|
1033
|
+
decrease or increase likelihood of selection; values like -100 or 100
|
|
1034
|
+
should result in a ban or exclusive selection of the relevant token.
|
|
1035
|
+
|
|
1036
|
+
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
|
1037
|
+
token from being generated.
|
|
1038
|
+
*/
|
|
1039
|
+
logitBias: z4.record(z4.string(), z4.number()).optional(),
|
|
1040
|
+
/**
|
|
1041
|
+
The suffix that comes after a completion of inserted text.
|
|
1042
|
+
*/
|
|
1043
|
+
suffix: z4.string().optional(),
|
|
1044
|
+
/**
|
|
1045
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
|
1046
|
+
monitor and detect abuse. Learn more.
|
|
1047
|
+
*/
|
|
1048
|
+
user: z4.string().optional(),
|
|
1049
|
+
/**
|
|
1050
|
+
Return the log probabilities of the tokens. Including logprobs will increase
|
|
1051
|
+
the response size and can slow down response times. However, it can
|
|
1052
|
+
be useful to better understand how the model is behaving.
|
|
1053
|
+
Setting to true will return the log probabilities of the tokens that
|
|
1054
|
+
were generated.
|
|
1055
|
+
Setting to a number will return the log probabilities of the top n
|
|
1056
|
+
tokens that were generated.
|
|
1057
|
+
*/
|
|
1058
|
+
logprobs: z4.union([z4.boolean(), z4.number()]).optional()
|
|
1059
|
+
});
|
|
1126
1060
|
|
|
1127
1061
|
// src/openai-completion-language-model.ts
|
|
1128
1062
|
var OpenAICompletionLanguageModel = class {
|
|
1129
|
-
constructor(modelId,
|
|
1063
|
+
constructor(modelId, config) {
|
|
1130
1064
|
this.specificationVersion = "v2";
|
|
1131
|
-
this.
|
|
1065
|
+
this.supportedUrls = {
|
|
1066
|
+
// No URLs are supported for completion models.
|
|
1067
|
+
};
|
|
1132
1068
|
this.modelId = modelId;
|
|
1133
|
-
this.settings = settings;
|
|
1134
1069
|
this.config = config;
|
|
1135
1070
|
}
|
|
1071
|
+
get providerOptionsName() {
|
|
1072
|
+
return this.config.provider.split(".")[0].trim();
|
|
1073
|
+
}
|
|
1136
1074
|
get provider() {
|
|
1137
1075
|
return this.config.provider;
|
|
1138
1076
|
}
|
|
1139
|
-
getArgs({
|
|
1140
|
-
inputFormat,
|
|
1077
|
+
async getArgs({
|
|
1141
1078
|
prompt,
|
|
1142
|
-
|
|
1079
|
+
maxOutputTokens,
|
|
1143
1080
|
temperature,
|
|
1144
1081
|
topP,
|
|
1145
1082
|
topK,
|
|
@@ -1149,9 +1086,22 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1149
1086
|
responseFormat,
|
|
1150
1087
|
tools,
|
|
1151
1088
|
toolChoice,
|
|
1152
|
-
seed
|
|
1089
|
+
seed,
|
|
1090
|
+
providerOptions
|
|
1153
1091
|
}) {
|
|
1154
1092
|
const warnings = [];
|
|
1093
|
+
const openaiOptions = {
|
|
1094
|
+
...await parseProviderOptions2({
|
|
1095
|
+
provider: "openai",
|
|
1096
|
+
providerOptions,
|
|
1097
|
+
schema: openaiCompletionProviderOptions
|
|
1098
|
+
}),
|
|
1099
|
+
...await parseProviderOptions2({
|
|
1100
|
+
provider: this.providerOptionsName,
|
|
1101
|
+
providerOptions,
|
|
1102
|
+
schema: openaiCompletionProviderOptions
|
|
1103
|
+
})
|
|
1104
|
+
};
|
|
1155
1105
|
if (topK != null) {
|
|
1156
1106
|
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
|
1157
1107
|
}
|
|
@@ -1168,20 +1118,20 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1168
1118
|
details: "JSON response format is not supported."
|
|
1169
1119
|
});
|
|
1170
1120
|
}
|
|
1171
|
-
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt
|
|
1121
|
+
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt });
|
|
1172
1122
|
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
|
|
1173
1123
|
return {
|
|
1174
1124
|
args: {
|
|
1175
1125
|
// model id:
|
|
1176
1126
|
model: this.modelId,
|
|
1177
1127
|
// model specific settings:
|
|
1178
|
-
echo:
|
|
1179
|
-
logit_bias:
|
|
1180
|
-
logprobs:
|
|
1181
|
-
suffix:
|
|
1182
|
-
user:
|
|
1128
|
+
echo: openaiOptions.echo,
|
|
1129
|
+
logit_bias: openaiOptions.logitBias,
|
|
1130
|
+
logprobs: (openaiOptions == null ? void 0 : openaiOptions.logprobs) === true ? 0 : (openaiOptions == null ? void 0 : openaiOptions.logprobs) === false ? void 0 : openaiOptions == null ? void 0 : openaiOptions.logprobs,
|
|
1131
|
+
suffix: openaiOptions.suffix,
|
|
1132
|
+
user: openaiOptions.user,
|
|
1183
1133
|
// standardized settings:
|
|
1184
|
-
max_tokens:
|
|
1134
|
+
max_tokens: maxOutputTokens,
|
|
1185
1135
|
temperature,
|
|
1186
1136
|
top_p: topP,
|
|
1187
1137
|
frequency_penalty: frequencyPenalty,
|
|
@@ -1196,7 +1146,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1196
1146
|
};
|
|
1197
1147
|
}
|
|
1198
1148
|
async doGenerate(options) {
|
|
1199
|
-
|
|
1149
|
+
var _a, _b, _c;
|
|
1150
|
+
const { args, warnings } = await this.getArgs(options);
|
|
1200
1151
|
const {
|
|
1201
1152
|
responseHeaders,
|
|
1202
1153
|
value: response,
|
|
@@ -1215,30 +1166,37 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1215
1166
|
abortSignal: options.abortSignal,
|
|
1216
1167
|
fetch: this.config.fetch
|
|
1217
1168
|
});
|
|
1218
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1219
1169
|
const choice = response.choices[0];
|
|
1170
|
+
const providerMetadata = { openai: {} };
|
|
1171
|
+
if (choice.logprobs != null) {
|
|
1172
|
+
providerMetadata.openai.logprobs = choice.logprobs;
|
|
1173
|
+
}
|
|
1220
1174
|
return {
|
|
1221
|
-
text: choice.text,
|
|
1175
|
+
content: [{ type: "text", text: choice.text }],
|
|
1222
1176
|
usage: {
|
|
1223
|
-
|
|
1224
|
-
|
|
1177
|
+
inputTokens: (_a = response.usage) == null ? void 0 : _a.prompt_tokens,
|
|
1178
|
+
outputTokens: (_b = response.usage) == null ? void 0 : _b.completion_tokens,
|
|
1179
|
+
totalTokens: (_c = response.usage) == null ? void 0 : _c.total_tokens
|
|
1225
1180
|
},
|
|
1226
1181
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1182
|
+
request: { body: args },
|
|
1183
|
+
response: {
|
|
1184
|
+
...getResponseMetadata(response),
|
|
1185
|
+
headers: responseHeaders,
|
|
1186
|
+
body: rawResponse
|
|
1187
|
+
},
|
|
1188
|
+
providerMetadata,
|
|
1189
|
+
warnings
|
|
1233
1190
|
};
|
|
1234
1191
|
}
|
|
1235
1192
|
async doStream(options) {
|
|
1236
|
-
const { args, warnings } = this.getArgs(options);
|
|
1193
|
+
const { args, warnings } = await this.getArgs(options);
|
|
1237
1194
|
const body = {
|
|
1238
1195
|
...args,
|
|
1239
1196
|
stream: true,
|
|
1240
|
-
|
|
1241
|
-
|
|
1197
|
+
stream_options: {
|
|
1198
|
+
include_usage: true
|
|
1199
|
+
}
|
|
1242
1200
|
};
|
|
1243
1201
|
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
1244
1202
|
url: this.config.url({
|
|
@@ -1254,17 +1212,20 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1254
1212
|
abortSignal: options.abortSignal,
|
|
1255
1213
|
fetch: this.config.fetch
|
|
1256
1214
|
});
|
|
1257
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1258
1215
|
let finishReason = "unknown";
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1216
|
+
const providerMetadata = { openai: {} };
|
|
1217
|
+
const usage = {
|
|
1218
|
+
inputTokens: void 0,
|
|
1219
|
+
outputTokens: void 0,
|
|
1220
|
+
totalTokens: void 0
|
|
1262
1221
|
};
|
|
1263
|
-
let logprobs;
|
|
1264
1222
|
let isFirstChunk = true;
|
|
1265
1223
|
return {
|
|
1266
1224
|
stream: response.pipeThrough(
|
|
1267
1225
|
new TransformStream({
|
|
1226
|
+
start(controller) {
|
|
1227
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1228
|
+
},
|
|
1268
1229
|
transform(chunk, controller) {
|
|
1269
1230
|
if (!chunk.success) {
|
|
1270
1231
|
finishReason = "error";
|
|
@@ -1285,87 +1246,79 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1285
1246
|
});
|
|
1286
1247
|
}
|
|
1287
1248
|
if (value.usage != null) {
|
|
1288
|
-
usage =
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
};
|
|
1249
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
1250
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
1251
|
+
usage.totalTokens = value.usage.total_tokens;
|
|
1292
1252
|
}
|
|
1293
1253
|
const choice = value.choices[0];
|
|
1294
1254
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
1295
1255
|
finishReason = mapOpenAIFinishReason(choice.finish_reason);
|
|
1296
1256
|
}
|
|
1257
|
+
if ((choice == null ? void 0 : choice.logprobs) != null) {
|
|
1258
|
+
providerMetadata.openai.logprobs = choice.logprobs;
|
|
1259
|
+
}
|
|
1297
1260
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
1298
1261
|
controller.enqueue({
|
|
1299
|
-
type: "text
|
|
1300
|
-
|
|
1262
|
+
type: "text",
|
|
1263
|
+
text: choice.text
|
|
1301
1264
|
});
|
|
1302
1265
|
}
|
|
1303
|
-
const mappedLogprobs = mapOpenAICompletionLogProbs(
|
|
1304
|
-
choice == null ? void 0 : choice.logprobs
|
|
1305
|
-
);
|
|
1306
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
1307
|
-
if (logprobs === void 0) logprobs = [];
|
|
1308
|
-
logprobs.push(...mappedLogprobs);
|
|
1309
|
-
}
|
|
1310
1266
|
},
|
|
1311
1267
|
flush(controller) {
|
|
1312
1268
|
controller.enqueue({
|
|
1313
1269
|
type: "finish",
|
|
1314
1270
|
finishReason,
|
|
1315
|
-
|
|
1271
|
+
providerMetadata,
|
|
1316
1272
|
usage
|
|
1317
1273
|
});
|
|
1318
1274
|
}
|
|
1319
1275
|
})
|
|
1320
1276
|
),
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
warnings,
|
|
1324
|
-
request: { body: JSON.stringify(body) }
|
|
1277
|
+
request: { body },
|
|
1278
|
+
response: { headers: responseHeaders }
|
|
1325
1279
|
};
|
|
1326
1280
|
}
|
|
1327
1281
|
};
|
|
1328
|
-
var
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1282
|
+
var usageSchema = z5.object({
|
|
1283
|
+
prompt_tokens: z5.number(),
|
|
1284
|
+
completion_tokens: z5.number(),
|
|
1285
|
+
total_tokens: z5.number()
|
|
1286
|
+
});
|
|
1287
|
+
var openaiCompletionResponseSchema = z5.object({
|
|
1288
|
+
id: z5.string().nullish(),
|
|
1289
|
+
created: z5.number().nullish(),
|
|
1290
|
+
model: z5.string().nullish(),
|
|
1291
|
+
choices: z5.array(
|
|
1292
|
+
z5.object({
|
|
1293
|
+
text: z5.string(),
|
|
1294
|
+
finish_reason: z5.string(),
|
|
1295
|
+
logprobs: z5.object({
|
|
1296
|
+
tokens: z5.array(z5.string()),
|
|
1297
|
+
token_logprobs: z5.array(z5.number()),
|
|
1298
|
+
top_logprobs: z5.array(z5.record(z5.string(), z5.number())).nullish()
|
|
1340
1299
|
}).nullish()
|
|
1341
1300
|
})
|
|
1342
1301
|
),
|
|
1343
|
-
usage:
|
|
1344
|
-
prompt_tokens: z3.number(),
|
|
1345
|
-
completion_tokens: z3.number()
|
|
1346
|
-
})
|
|
1302
|
+
usage: usageSchema.nullish()
|
|
1347
1303
|
});
|
|
1348
|
-
var openaiCompletionChunkSchema =
|
|
1349
|
-
|
|
1350
|
-
id:
|
|
1351
|
-
created:
|
|
1352
|
-
model:
|
|
1353
|
-
choices:
|
|
1354
|
-
|
|
1355
|
-
text:
|
|
1356
|
-
finish_reason:
|
|
1357
|
-
index:
|
|
1358
|
-
logprobs:
|
|
1359
|
-
tokens:
|
|
1360
|
-
token_logprobs:
|
|
1361
|
-
top_logprobs:
|
|
1304
|
+
var openaiCompletionChunkSchema = z5.union([
|
|
1305
|
+
z5.object({
|
|
1306
|
+
id: z5.string().nullish(),
|
|
1307
|
+
created: z5.number().nullish(),
|
|
1308
|
+
model: z5.string().nullish(),
|
|
1309
|
+
choices: z5.array(
|
|
1310
|
+
z5.object({
|
|
1311
|
+
text: z5.string(),
|
|
1312
|
+
finish_reason: z5.string().nullish(),
|
|
1313
|
+
index: z5.number(),
|
|
1314
|
+
logprobs: z5.object({
|
|
1315
|
+
tokens: z5.array(z5.string()),
|
|
1316
|
+
token_logprobs: z5.array(z5.number()),
|
|
1317
|
+
top_logprobs: z5.array(z5.record(z5.string(), z5.number())).nullish()
|
|
1362
1318
|
}).nullish()
|
|
1363
1319
|
})
|
|
1364
1320
|
),
|
|
1365
|
-
usage:
|
|
1366
|
-
prompt_tokens: z3.number(),
|
|
1367
|
-
completion_tokens: z3.number()
|
|
1368
|
-
}).nullish()
|
|
1321
|
+
usage: usageSchema.nullish()
|
|
1369
1322
|
}),
|
|
1370
1323
|
openaiErrorDataSchema
|
|
1371
1324
|
]);
|
|
@@ -1377,32 +1330,45 @@ import {
|
|
|
1377
1330
|
import {
|
|
1378
1331
|
combineHeaders as combineHeaders3,
|
|
1379
1332
|
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1333
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1380
1334
|
postJsonToApi as postJsonToApi3
|
|
1381
1335
|
} from "@ai-sdk/provider-utils";
|
|
1382
|
-
import { z as
|
|
1336
|
+
import { z as z7 } from "zod";
|
|
1337
|
+
|
|
1338
|
+
// src/openai-embedding-options.ts
|
|
1339
|
+
import { z as z6 } from "zod";
|
|
1340
|
+
var openaiEmbeddingProviderOptions = z6.object({
|
|
1341
|
+
/**
|
|
1342
|
+
The number of dimensions the resulting output embeddings should have.
|
|
1343
|
+
Only supported in text-embedding-3 and later models.
|
|
1344
|
+
*/
|
|
1345
|
+
dimensions: z6.number().optional(),
|
|
1346
|
+
/**
|
|
1347
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
|
1348
|
+
monitor and detect abuse. Learn more.
|
|
1349
|
+
*/
|
|
1350
|
+
user: z6.string().optional()
|
|
1351
|
+
});
|
|
1352
|
+
|
|
1353
|
+
// src/openai-embedding-model.ts
|
|
1383
1354
|
var OpenAIEmbeddingModel = class {
|
|
1384
|
-
constructor(modelId,
|
|
1385
|
-
this.specificationVersion = "
|
|
1355
|
+
constructor(modelId, config) {
|
|
1356
|
+
this.specificationVersion = "v2";
|
|
1357
|
+
this.maxEmbeddingsPerCall = 2048;
|
|
1358
|
+
this.supportsParallelCalls = true;
|
|
1386
1359
|
this.modelId = modelId;
|
|
1387
|
-
this.settings = settings;
|
|
1388
1360
|
this.config = config;
|
|
1389
1361
|
}
|
|
1390
1362
|
get provider() {
|
|
1391
1363
|
return this.config.provider;
|
|
1392
1364
|
}
|
|
1393
|
-
get maxEmbeddingsPerCall() {
|
|
1394
|
-
var _a;
|
|
1395
|
-
return (_a = this.settings.maxEmbeddingsPerCall) != null ? _a : 2048;
|
|
1396
|
-
}
|
|
1397
|
-
get supportsParallelCalls() {
|
|
1398
|
-
var _a;
|
|
1399
|
-
return (_a = this.settings.supportsParallelCalls) != null ? _a : true;
|
|
1400
|
-
}
|
|
1401
1365
|
async doEmbed({
|
|
1402
1366
|
values,
|
|
1403
1367
|
headers,
|
|
1404
|
-
abortSignal
|
|
1368
|
+
abortSignal,
|
|
1369
|
+
providerOptions
|
|
1405
1370
|
}) {
|
|
1371
|
+
var _a;
|
|
1406
1372
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1407
1373
|
throw new TooManyEmbeddingValuesForCallError({
|
|
1408
1374
|
provider: this.provider,
|
|
@@ -1411,7 +1377,16 @@ var OpenAIEmbeddingModel = class {
|
|
|
1411
1377
|
values
|
|
1412
1378
|
});
|
|
1413
1379
|
}
|
|
1414
|
-
const
|
|
1380
|
+
const openaiOptions = (_a = await parseProviderOptions3({
|
|
1381
|
+
provider: "openai",
|
|
1382
|
+
providerOptions,
|
|
1383
|
+
schema: openaiEmbeddingProviderOptions
|
|
1384
|
+
})) != null ? _a : {};
|
|
1385
|
+
const {
|
|
1386
|
+
responseHeaders,
|
|
1387
|
+
value: response,
|
|
1388
|
+
rawValue
|
|
1389
|
+
} = await postJsonToApi3({
|
|
1415
1390
|
url: this.config.url({
|
|
1416
1391
|
path: "/embeddings",
|
|
1417
1392
|
modelId: this.modelId
|
|
@@ -1421,8 +1396,8 @@ var OpenAIEmbeddingModel = class {
|
|
|
1421
1396
|
model: this.modelId,
|
|
1422
1397
|
input: values,
|
|
1423
1398
|
encoding_format: "float",
|
|
1424
|
-
dimensions:
|
|
1425
|
-
user:
|
|
1399
|
+
dimensions: openaiOptions.dimensions,
|
|
1400
|
+
user: openaiOptions.user
|
|
1426
1401
|
},
|
|
1427
1402
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
1428
1403
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
@@ -1434,13 +1409,13 @@ var OpenAIEmbeddingModel = class {
|
|
|
1434
1409
|
return {
|
|
1435
1410
|
embeddings: response.data.map((item) => item.embedding),
|
|
1436
1411
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1437
|
-
|
|
1412
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
1438
1413
|
};
|
|
1439
1414
|
}
|
|
1440
1415
|
};
|
|
1441
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1442
|
-
data:
|
|
1443
|
-
usage:
|
|
1416
|
+
var openaiTextEmbeddingResponseSchema = z7.object({
|
|
1417
|
+
data: z7.array(z7.object({ embedding: z7.array(z7.number()) })),
|
|
1418
|
+
usage: z7.object({ prompt_tokens: z7.number() }).nullish()
|
|
1444
1419
|
});
|
|
1445
1420
|
|
|
1446
1421
|
// src/openai-image-model.ts
|
|
@@ -1449,25 +1424,26 @@ import {
|
|
|
1449
1424
|
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1450
1425
|
postJsonToApi as postJsonToApi4
|
|
1451
1426
|
} from "@ai-sdk/provider-utils";
|
|
1452
|
-
import { z as
|
|
1427
|
+
import { z as z8 } from "zod";
|
|
1453
1428
|
|
|
1454
1429
|
// src/openai-image-settings.ts
|
|
1455
1430
|
var modelMaxImagesPerCall = {
|
|
1456
1431
|
"dall-e-3": 1,
|
|
1457
|
-
"dall-e-2": 10
|
|
1432
|
+
"dall-e-2": 10,
|
|
1433
|
+
"gpt-image-1": 10
|
|
1458
1434
|
};
|
|
1435
|
+
var hasDefaultResponseFormat = /* @__PURE__ */ new Set(["gpt-image-1"]);
|
|
1459
1436
|
|
|
1460
1437
|
// src/openai-image-model.ts
|
|
1461
1438
|
var OpenAIImageModel = class {
|
|
1462
|
-
constructor(modelId,
|
|
1439
|
+
constructor(modelId, config) {
|
|
1463
1440
|
this.modelId = modelId;
|
|
1464
|
-
this.settings = settings;
|
|
1465
1441
|
this.config = config;
|
|
1466
|
-
this.specificationVersion = "
|
|
1442
|
+
this.specificationVersion = "v2";
|
|
1467
1443
|
}
|
|
1468
1444
|
get maxImagesPerCall() {
|
|
1469
|
-
var _a
|
|
1470
|
-
return (
|
|
1445
|
+
var _a;
|
|
1446
|
+
return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
|
|
1471
1447
|
}
|
|
1472
1448
|
get provider() {
|
|
1473
1449
|
return this.config.provider;
|
|
@@ -1507,7 +1483,7 @@ var OpenAIImageModel = class {
|
|
|
1507
1483
|
n,
|
|
1508
1484
|
size,
|
|
1509
1485
|
...(_d = providerOptions.openai) != null ? _d : {},
|
|
1510
|
-
response_format: "b64_json"
|
|
1486
|
+
...!hasDefaultResponseFormat.has(this.modelId) ? { response_format: "b64_json" } : {}
|
|
1511
1487
|
},
|
|
1512
1488
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
1513
1489
|
successfulResponseHandler: createJsonResponseHandler4(
|
|
@@ -1523,17 +1499,28 @@ var OpenAIImageModel = class {
|
|
|
1523
1499
|
timestamp: currentDate,
|
|
1524
1500
|
modelId: this.modelId,
|
|
1525
1501
|
headers: responseHeaders
|
|
1502
|
+
},
|
|
1503
|
+
providerMetadata: {
|
|
1504
|
+
openai: {
|
|
1505
|
+
images: response.data.map(
|
|
1506
|
+
(item) => item.revised_prompt ? {
|
|
1507
|
+
revisedPrompt: item.revised_prompt
|
|
1508
|
+
} : null
|
|
1509
|
+
)
|
|
1510
|
+
}
|
|
1526
1511
|
}
|
|
1527
1512
|
};
|
|
1528
1513
|
}
|
|
1529
1514
|
};
|
|
1530
|
-
var openaiImageResponseSchema =
|
|
1531
|
-
data:
|
|
1515
|
+
var openaiImageResponseSchema = z8.object({
|
|
1516
|
+
data: z8.array(
|
|
1517
|
+
z8.object({ b64_json: z8.string(), revised_prompt: z8.string().optional() })
|
|
1518
|
+
)
|
|
1532
1519
|
});
|
|
1533
1520
|
|
|
1534
1521
|
// src/openai-tools.ts
|
|
1535
|
-
import { z as
|
|
1536
|
-
var WebSearchPreviewParameters =
|
|
1522
|
+
import { z as z9 } from "zod";
|
|
1523
|
+
var WebSearchPreviewParameters = z9.object({});
|
|
1537
1524
|
function webSearchPreviewTool({
|
|
1538
1525
|
searchContextSize,
|
|
1539
1526
|
userLocation
|
|
@@ -1552,22 +1539,216 @@ var openaiTools = {
|
|
|
1552
1539
|
webSearchPreview: webSearchPreviewTool
|
|
1553
1540
|
};
|
|
1554
1541
|
|
|
1555
|
-
// src/
|
|
1542
|
+
// src/openai-transcription-model.ts
|
|
1556
1543
|
import {
|
|
1557
1544
|
combineHeaders as combineHeaders5,
|
|
1558
|
-
|
|
1545
|
+
convertBase64ToUint8Array,
|
|
1559
1546
|
createJsonResponseHandler as createJsonResponseHandler5,
|
|
1547
|
+
parseProviderOptions as parseProviderOptions4,
|
|
1548
|
+
postFormDataToApi
|
|
1549
|
+
} from "@ai-sdk/provider-utils";
|
|
1550
|
+
import { z as z11 } from "zod";
|
|
1551
|
+
|
|
1552
|
+
// src/openai-transcription-options.ts
|
|
1553
|
+
import { z as z10 } from "zod";
|
|
1554
|
+
var openAITranscriptionProviderOptions = z10.object({
|
|
1555
|
+
/**
|
|
1556
|
+
* Additional information to include in the transcription response.
|
|
1557
|
+
*/
|
|
1558
|
+
include: z10.array(z10.string()).optional(),
|
|
1559
|
+
/**
|
|
1560
|
+
* The language of the input audio in ISO-639-1 format.
|
|
1561
|
+
*/
|
|
1562
|
+
language: z10.string().optional(),
|
|
1563
|
+
/**
|
|
1564
|
+
* An optional text to guide the model's style or continue a previous audio segment.
|
|
1565
|
+
*/
|
|
1566
|
+
prompt: z10.string().optional(),
|
|
1567
|
+
/**
|
|
1568
|
+
* The sampling temperature, between 0 and 1.
|
|
1569
|
+
* @default 0
|
|
1570
|
+
*/
|
|
1571
|
+
temperature: z10.number().min(0).max(1).default(0).optional(),
|
|
1572
|
+
/**
|
|
1573
|
+
* The timestamp granularities to populate for this transcription.
|
|
1574
|
+
* @default ['segment']
|
|
1575
|
+
*/
|
|
1576
|
+
timestampGranularities: z10.array(z10.enum(["word", "segment"])).default(["segment"]).optional()
|
|
1577
|
+
});
|
|
1578
|
+
|
|
1579
|
+
// src/openai-transcription-model.ts
|
|
1580
|
+
var languageMap = {
|
|
1581
|
+
afrikaans: "af",
|
|
1582
|
+
arabic: "ar",
|
|
1583
|
+
armenian: "hy",
|
|
1584
|
+
azerbaijani: "az",
|
|
1585
|
+
belarusian: "be",
|
|
1586
|
+
bosnian: "bs",
|
|
1587
|
+
bulgarian: "bg",
|
|
1588
|
+
catalan: "ca",
|
|
1589
|
+
chinese: "zh",
|
|
1590
|
+
croatian: "hr",
|
|
1591
|
+
czech: "cs",
|
|
1592
|
+
danish: "da",
|
|
1593
|
+
dutch: "nl",
|
|
1594
|
+
english: "en",
|
|
1595
|
+
estonian: "et",
|
|
1596
|
+
finnish: "fi",
|
|
1597
|
+
french: "fr",
|
|
1598
|
+
galician: "gl",
|
|
1599
|
+
german: "de",
|
|
1600
|
+
greek: "el",
|
|
1601
|
+
hebrew: "he",
|
|
1602
|
+
hindi: "hi",
|
|
1603
|
+
hungarian: "hu",
|
|
1604
|
+
icelandic: "is",
|
|
1605
|
+
indonesian: "id",
|
|
1606
|
+
italian: "it",
|
|
1607
|
+
japanese: "ja",
|
|
1608
|
+
kannada: "kn",
|
|
1609
|
+
kazakh: "kk",
|
|
1610
|
+
korean: "ko",
|
|
1611
|
+
latvian: "lv",
|
|
1612
|
+
lithuanian: "lt",
|
|
1613
|
+
macedonian: "mk",
|
|
1614
|
+
malay: "ms",
|
|
1615
|
+
marathi: "mr",
|
|
1616
|
+
maori: "mi",
|
|
1617
|
+
nepali: "ne",
|
|
1618
|
+
norwegian: "no",
|
|
1619
|
+
persian: "fa",
|
|
1620
|
+
polish: "pl",
|
|
1621
|
+
portuguese: "pt",
|
|
1622
|
+
romanian: "ro",
|
|
1623
|
+
russian: "ru",
|
|
1624
|
+
serbian: "sr",
|
|
1625
|
+
slovak: "sk",
|
|
1626
|
+
slovenian: "sl",
|
|
1627
|
+
spanish: "es",
|
|
1628
|
+
swahili: "sw",
|
|
1629
|
+
swedish: "sv",
|
|
1630
|
+
tagalog: "tl",
|
|
1631
|
+
tamil: "ta",
|
|
1632
|
+
thai: "th",
|
|
1633
|
+
turkish: "tr",
|
|
1634
|
+
ukrainian: "uk",
|
|
1635
|
+
urdu: "ur",
|
|
1636
|
+
vietnamese: "vi",
|
|
1637
|
+
welsh: "cy"
|
|
1638
|
+
};
|
|
1639
|
+
var OpenAITranscriptionModel = class {
|
|
1640
|
+
constructor(modelId, config) {
|
|
1641
|
+
this.modelId = modelId;
|
|
1642
|
+
this.config = config;
|
|
1643
|
+
this.specificationVersion = "v1";
|
|
1644
|
+
}
|
|
1645
|
+
get provider() {
|
|
1646
|
+
return this.config.provider;
|
|
1647
|
+
}
|
|
1648
|
+
async getArgs({
|
|
1649
|
+
audio,
|
|
1650
|
+
mediaType,
|
|
1651
|
+
providerOptions
|
|
1652
|
+
}) {
|
|
1653
|
+
const warnings = [];
|
|
1654
|
+
const openAIOptions = await parseProviderOptions4({
|
|
1655
|
+
provider: "openai",
|
|
1656
|
+
providerOptions,
|
|
1657
|
+
schema: openAITranscriptionProviderOptions
|
|
1658
|
+
});
|
|
1659
|
+
const formData = new FormData();
|
|
1660
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
1661
|
+
formData.append("model", this.modelId);
|
|
1662
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
1663
|
+
if (openAIOptions) {
|
|
1664
|
+
const transcriptionModelOptions = {
|
|
1665
|
+
include: openAIOptions.include,
|
|
1666
|
+
language: openAIOptions.language,
|
|
1667
|
+
prompt: openAIOptions.prompt,
|
|
1668
|
+
temperature: openAIOptions.temperature,
|
|
1669
|
+
timestamp_granularities: openAIOptions.timestampGranularities
|
|
1670
|
+
};
|
|
1671
|
+
for (const [key, value] of Object.entries(transcriptionModelOptions)) {
|
|
1672
|
+
if (value != null) {
|
|
1673
|
+
formData.append(key, String(value));
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
return {
|
|
1678
|
+
formData,
|
|
1679
|
+
warnings
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
async doGenerate(options) {
|
|
1683
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1684
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1685
|
+
const { formData, warnings } = await this.getArgs(options);
|
|
1686
|
+
const {
|
|
1687
|
+
value: response,
|
|
1688
|
+
responseHeaders,
|
|
1689
|
+
rawValue: rawResponse
|
|
1690
|
+
} = await postFormDataToApi({
|
|
1691
|
+
url: this.config.url({
|
|
1692
|
+
path: "/audio/transcriptions",
|
|
1693
|
+
modelId: this.modelId
|
|
1694
|
+
}),
|
|
1695
|
+
headers: combineHeaders5(this.config.headers(), options.headers),
|
|
1696
|
+
formData,
|
|
1697
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
1698
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
1699
|
+
openaiTranscriptionResponseSchema
|
|
1700
|
+
),
|
|
1701
|
+
abortSignal: options.abortSignal,
|
|
1702
|
+
fetch: this.config.fetch
|
|
1703
|
+
});
|
|
1704
|
+
const language = response.language != null && response.language in languageMap ? languageMap[response.language] : void 0;
|
|
1705
|
+
return {
|
|
1706
|
+
text: response.text,
|
|
1707
|
+
segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => ({
|
|
1708
|
+
text: word.word,
|
|
1709
|
+
startSecond: word.start,
|
|
1710
|
+
endSecond: word.end
|
|
1711
|
+
}))) != null ? _e : [],
|
|
1712
|
+
language,
|
|
1713
|
+
durationInSeconds: (_f = response.duration) != null ? _f : void 0,
|
|
1714
|
+
warnings,
|
|
1715
|
+
response: {
|
|
1716
|
+
timestamp: currentDate,
|
|
1717
|
+
modelId: this.modelId,
|
|
1718
|
+
headers: responseHeaders,
|
|
1719
|
+
body: rawResponse
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
}
|
|
1723
|
+
};
|
|
1724
|
+
var openaiTranscriptionResponseSchema = z11.object({
|
|
1725
|
+
text: z11.string(),
|
|
1726
|
+
language: z11.string().nullish(),
|
|
1727
|
+
duration: z11.number().nullish(),
|
|
1728
|
+
words: z11.array(
|
|
1729
|
+
z11.object({
|
|
1730
|
+
word: z11.string(),
|
|
1731
|
+
start: z11.number(),
|
|
1732
|
+
end: z11.number()
|
|
1733
|
+
})
|
|
1734
|
+
).nullish()
|
|
1735
|
+
});
|
|
1736
|
+
|
|
1737
|
+
// src/responses/openai-responses-language-model.ts
|
|
1738
|
+
import {
|
|
1739
|
+
combineHeaders as combineHeaders6,
|
|
1740
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler3,
|
|
1741
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
1560
1742
|
generateId as generateId2,
|
|
1561
|
-
parseProviderOptions,
|
|
1743
|
+
parseProviderOptions as parseProviderOptions5,
|
|
1562
1744
|
postJsonToApi as postJsonToApi5
|
|
1563
1745
|
} from "@ai-sdk/provider-utils";
|
|
1564
|
-
import { z as
|
|
1746
|
+
import { z as z12 } from "zod";
|
|
1565
1747
|
|
|
1566
1748
|
// src/responses/convert-to-openai-responses-messages.ts
|
|
1567
1749
|
import {
|
|
1568
|
-
UnsupportedFunctionalityError as
|
|
1750
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1569
1751
|
} from "@ai-sdk/provider";
|
|
1570
|
-
import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
|
|
1571
1752
|
function convertToOpenAIResponsesMessages({
|
|
1572
1753
|
prompt,
|
|
1573
1754
|
systemMessageMode
|
|
@@ -1606,38 +1787,35 @@ function convertToOpenAIResponsesMessages({
|
|
|
1606
1787
|
messages.push({
|
|
1607
1788
|
role: "user",
|
|
1608
1789
|
content: content.map((part, index) => {
|
|
1609
|
-
var _a, _b, _c
|
|
1790
|
+
var _a, _b, _c;
|
|
1610
1791
|
switch (part.type) {
|
|
1611
1792
|
case "text": {
|
|
1612
1793
|
return { type: "input_text", text: part.text };
|
|
1613
1794
|
}
|
|
1614
|
-
case "image": {
|
|
1615
|
-
return {
|
|
1616
|
-
type: "input_image",
|
|
1617
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase642(part.image)}`,
|
|
1618
|
-
// OpenAI specific extension: image detail
|
|
1619
|
-
detail: (_c = (_b = part.providerOptions) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
1620
|
-
};
|
|
1621
|
-
}
|
|
1622
1795
|
case "file": {
|
|
1623
|
-
if (part.
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
}
|
|
1636
|
-
default: {
|
|
1637
|
-
throw new UnsupportedFunctionalityError5({
|
|
1638
|
-
functionality: "Only PDF files are supported in user messages"
|
|
1796
|
+
if (part.mediaType.startsWith("image/")) {
|
|
1797
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
1798
|
+
return {
|
|
1799
|
+
type: "input_image",
|
|
1800
|
+
image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`,
|
|
1801
|
+
// OpenAI specific extension: image detail
|
|
1802
|
+
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
1803
|
+
};
|
|
1804
|
+
} else if (part.mediaType === "application/pdf") {
|
|
1805
|
+
if (part.data instanceof URL) {
|
|
1806
|
+
throw new UnsupportedFunctionalityError4({
|
|
1807
|
+
functionality: "PDF file parts with URLs"
|
|
1639
1808
|
});
|
|
1640
1809
|
}
|
|
1810
|
+
return {
|
|
1811
|
+
type: "input_file",
|
|
1812
|
+
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
|
|
1813
|
+
file_data: `data:application/pdf;base64,${part.data}`
|
|
1814
|
+
};
|
|
1815
|
+
} else {
|
|
1816
|
+
throw new UnsupportedFunctionalityError4({
|
|
1817
|
+
functionality: `file part media type ${part.mediaType}`
|
|
1818
|
+
});
|
|
1641
1819
|
}
|
|
1642
1820
|
}
|
|
1643
1821
|
}
|
|
@@ -1707,7 +1885,7 @@ function mapOpenAIResponseFinishReason({
|
|
|
1707
1885
|
|
|
1708
1886
|
// src/responses/openai-responses-prepare-tools.ts
|
|
1709
1887
|
import {
|
|
1710
|
-
UnsupportedFunctionalityError as
|
|
1888
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1711
1889
|
} from "@ai-sdk/provider";
|
|
1712
1890
|
function prepareResponsesTools({
|
|
1713
1891
|
tools,
|
|
@@ -1767,8 +1945,8 @@ function prepareResponsesTools({
|
|
|
1767
1945
|
};
|
|
1768
1946
|
default: {
|
|
1769
1947
|
const _exhaustiveCheck = type;
|
|
1770
|
-
throw new
|
|
1771
|
-
functionality: `
|
|
1948
|
+
throw new UnsupportedFunctionalityError5({
|
|
1949
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1772
1950
|
});
|
|
1773
1951
|
}
|
|
1774
1952
|
}
|
|
@@ -1778,15 +1956,17 @@ function prepareResponsesTools({
|
|
|
1778
1956
|
var OpenAIResponsesLanguageModel = class {
|
|
1779
1957
|
constructor(modelId, config) {
|
|
1780
1958
|
this.specificationVersion = "v2";
|
|
1781
|
-
this.
|
|
1959
|
+
this.supportedUrls = {
|
|
1960
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1961
|
+
};
|
|
1782
1962
|
this.modelId = modelId;
|
|
1783
1963
|
this.config = config;
|
|
1784
1964
|
}
|
|
1785
1965
|
get provider() {
|
|
1786
1966
|
return this.config.provider;
|
|
1787
1967
|
}
|
|
1788
|
-
getArgs({
|
|
1789
|
-
|
|
1968
|
+
async getArgs({
|
|
1969
|
+
maxOutputTokens,
|
|
1790
1970
|
temperature,
|
|
1791
1971
|
stopSequences,
|
|
1792
1972
|
topP,
|
|
@@ -1829,7 +2009,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1829
2009
|
systemMessageMode: modelConfig.systemMessageMode
|
|
1830
2010
|
});
|
|
1831
2011
|
warnings.push(...messageWarnings);
|
|
1832
|
-
const openaiOptions =
|
|
2012
|
+
const openaiOptions = await parseProviderOptions5({
|
|
1833
2013
|
provider: "openai",
|
|
1834
2014
|
providerOptions,
|
|
1835
2015
|
schema: openaiResponsesProviderOptionsSchema
|
|
@@ -1840,7 +2020,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1840
2020
|
input: messages,
|
|
1841
2021
|
temperature,
|
|
1842
2022
|
top_p: topP,
|
|
1843
|
-
max_output_tokens:
|
|
2023
|
+
max_output_tokens: maxOutputTokens,
|
|
1844
2024
|
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
1845
2025
|
text: {
|
|
1846
2026
|
format: responseFormat.schema != null ? {
|
|
@@ -1860,8 +2040,15 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1860
2040
|
user: openaiOptions == null ? void 0 : openaiOptions.user,
|
|
1861
2041
|
instructions: openaiOptions == null ? void 0 : openaiOptions.instructions,
|
|
1862
2042
|
// model-specific settings:
|
|
1863
|
-
...modelConfig.isReasoningModel && (openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
|
|
1864
|
-
reasoning: {
|
|
2043
|
+
...modelConfig.isReasoningModel && ((openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null || (openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null) && {
|
|
2044
|
+
reasoning: {
|
|
2045
|
+
...(openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
|
|
2046
|
+
effort: openaiOptions.reasoningEffort
|
|
2047
|
+
},
|
|
2048
|
+
...(openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null && {
|
|
2049
|
+
summary: openaiOptions.reasoningSummary
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
1865
2052
|
},
|
|
1866
2053
|
...modelConfig.requiredAutoTruncation && {
|
|
1867
2054
|
truncation: "auto"
|
|
@@ -1904,8 +2091,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1904
2091
|
};
|
|
1905
2092
|
}
|
|
1906
2093
|
async doGenerate(options) {
|
|
1907
|
-
var _a, _b, _c, _d, _e;
|
|
1908
|
-
const { args: body, warnings } = this.getArgs(options);
|
|
2094
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2095
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
1909
2096
|
const {
|
|
1910
2097
|
responseHeaders,
|
|
1911
2098
|
value: response,
|
|
@@ -1915,122 +2102,142 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1915
2102
|
path: "/responses",
|
|
1916
2103
|
modelId: this.modelId
|
|
1917
2104
|
}),
|
|
1918
|
-
headers:
|
|
2105
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
1919
2106
|
body,
|
|
1920
2107
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
1921
|
-
successfulResponseHandler:
|
|
1922
|
-
|
|
1923
|
-
id:
|
|
1924
|
-
created_at:
|
|
1925
|
-
model:
|
|
1926
|
-
output:
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
type:
|
|
1930
|
-
role:
|
|
1931
|
-
content:
|
|
1932
|
-
|
|
1933
|
-
type:
|
|
1934
|
-
text:
|
|
1935
|
-
annotations:
|
|
1936
|
-
|
|
1937
|
-
type:
|
|
1938
|
-
start_index:
|
|
1939
|
-
end_index:
|
|
1940
|
-
url:
|
|
1941
|
-
title:
|
|
2108
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
2109
|
+
z12.object({
|
|
2110
|
+
id: z12.string(),
|
|
2111
|
+
created_at: z12.number(),
|
|
2112
|
+
model: z12.string(),
|
|
2113
|
+
output: z12.array(
|
|
2114
|
+
z12.discriminatedUnion("type", [
|
|
2115
|
+
z12.object({
|
|
2116
|
+
type: z12.literal("message"),
|
|
2117
|
+
role: z12.literal("assistant"),
|
|
2118
|
+
content: z12.array(
|
|
2119
|
+
z12.object({
|
|
2120
|
+
type: z12.literal("output_text"),
|
|
2121
|
+
text: z12.string(),
|
|
2122
|
+
annotations: z12.array(
|
|
2123
|
+
z12.object({
|
|
2124
|
+
type: z12.literal("url_citation"),
|
|
2125
|
+
start_index: z12.number(),
|
|
2126
|
+
end_index: z12.number(),
|
|
2127
|
+
url: z12.string(),
|
|
2128
|
+
title: z12.string()
|
|
1942
2129
|
})
|
|
1943
2130
|
)
|
|
1944
2131
|
})
|
|
1945
2132
|
)
|
|
1946
2133
|
}),
|
|
1947
|
-
|
|
1948
|
-
type:
|
|
1949
|
-
call_id:
|
|
1950
|
-
name:
|
|
1951
|
-
arguments:
|
|
2134
|
+
z12.object({
|
|
2135
|
+
type: z12.literal("function_call"),
|
|
2136
|
+
call_id: z12.string(),
|
|
2137
|
+
name: z12.string(),
|
|
2138
|
+
arguments: z12.string()
|
|
1952
2139
|
}),
|
|
1953
|
-
|
|
1954
|
-
type:
|
|
2140
|
+
z12.object({
|
|
2141
|
+
type: z12.literal("web_search_call")
|
|
1955
2142
|
}),
|
|
1956
|
-
|
|
1957
|
-
type:
|
|
2143
|
+
z12.object({
|
|
2144
|
+
type: z12.literal("computer_call")
|
|
1958
2145
|
}),
|
|
1959
|
-
|
|
1960
|
-
type:
|
|
2146
|
+
z12.object({
|
|
2147
|
+
type: z12.literal("reasoning"),
|
|
2148
|
+
summary: z12.array(
|
|
2149
|
+
z12.object({
|
|
2150
|
+
type: z12.literal("summary_text"),
|
|
2151
|
+
text: z12.string()
|
|
2152
|
+
})
|
|
2153
|
+
)
|
|
1961
2154
|
})
|
|
1962
2155
|
])
|
|
1963
2156
|
),
|
|
1964
|
-
incomplete_details:
|
|
1965
|
-
usage:
|
|
2157
|
+
incomplete_details: z12.object({ reason: z12.string() }).nullable(),
|
|
2158
|
+
usage: usageSchema2
|
|
1966
2159
|
})
|
|
1967
2160
|
),
|
|
1968
2161
|
abortSignal: options.abortSignal,
|
|
1969
2162
|
fetch: this.config.fetch
|
|
1970
2163
|
});
|
|
1971
|
-
const
|
|
1972
|
-
const
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
2164
|
+
const content = [];
|
|
2165
|
+
for (const part of response.output) {
|
|
2166
|
+
switch (part.type) {
|
|
2167
|
+
case "reasoning": {
|
|
2168
|
+
content.push({
|
|
2169
|
+
type: "reasoning",
|
|
2170
|
+
text: part.summary.map((summary) => summary.text).join()
|
|
2171
|
+
});
|
|
2172
|
+
break;
|
|
2173
|
+
}
|
|
2174
|
+
case "message": {
|
|
2175
|
+
for (const contentPart of part.content) {
|
|
2176
|
+
content.push({
|
|
2177
|
+
type: "text",
|
|
2178
|
+
text: contentPart.text
|
|
2179
|
+
});
|
|
2180
|
+
for (const annotation of contentPart.annotations) {
|
|
2181
|
+
content.push({
|
|
2182
|
+
type: "source",
|
|
2183
|
+
sourceType: "url",
|
|
2184
|
+
id: (_c = (_b = (_a = this.config).generateId) == null ? void 0 : _b.call(_a)) != null ? _c : generateId2(),
|
|
2185
|
+
url: annotation.url,
|
|
2186
|
+
title: annotation.title
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
break;
|
|
2191
|
+
}
|
|
2192
|
+
case "function_call": {
|
|
2193
|
+
content.push({
|
|
2194
|
+
type: "tool-call",
|
|
2195
|
+
toolCallType: "function",
|
|
2196
|
+
toolCallId: part.call_id,
|
|
2197
|
+
toolName: part.name,
|
|
2198
|
+
args: part.arguments
|
|
2199
|
+
});
|
|
2200
|
+
break;
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
1978
2204
|
return {
|
|
1979
|
-
|
|
1980
|
-
sources: outputTextElements.flatMap(
|
|
1981
|
-
(content) => content.annotations.map((annotation) => {
|
|
1982
|
-
var _a2, _b2, _c2;
|
|
1983
|
-
return {
|
|
1984
|
-
sourceType: "url",
|
|
1985
|
-
id: (_c2 = (_b2 = (_a2 = this.config).generateId) == null ? void 0 : _b2.call(_a2)) != null ? _c2 : generateId2(),
|
|
1986
|
-
url: annotation.url,
|
|
1987
|
-
title: annotation.title
|
|
1988
|
-
};
|
|
1989
|
-
})
|
|
1990
|
-
),
|
|
2205
|
+
content,
|
|
1991
2206
|
finishReason: mapOpenAIResponseFinishReason({
|
|
1992
|
-
finishReason: (
|
|
1993
|
-
hasToolCalls:
|
|
2207
|
+
finishReason: (_d = response.incomplete_details) == null ? void 0 : _d.reason,
|
|
2208
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
|
1994
2209
|
}),
|
|
1995
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1996
2210
|
usage: {
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
rawSettings: {}
|
|
2003
|
-
},
|
|
2004
|
-
rawResponse: {
|
|
2005
|
-
headers: responseHeaders,
|
|
2006
|
-
body: rawResponse
|
|
2007
|
-
},
|
|
2008
|
-
request: {
|
|
2009
|
-
body: JSON.stringify(body)
|
|
2211
|
+
inputTokens: response.usage.input_tokens,
|
|
2212
|
+
outputTokens: response.usage.output_tokens,
|
|
2213
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
2214
|
+
reasoningTokens: (_f = (_e = response.usage.output_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0,
|
|
2215
|
+
cachedInputTokens: (_h = (_g = response.usage.input_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : void 0
|
|
2010
2216
|
},
|
|
2217
|
+
request: { body },
|
|
2011
2218
|
response: {
|
|
2012
2219
|
id: response.id,
|
|
2013
2220
|
timestamp: new Date(response.created_at * 1e3),
|
|
2014
|
-
modelId: response.model
|
|
2221
|
+
modelId: response.model,
|
|
2222
|
+
headers: responseHeaders,
|
|
2223
|
+
body: rawResponse
|
|
2015
2224
|
},
|
|
2016
2225
|
providerMetadata: {
|
|
2017
2226
|
openai: {
|
|
2018
|
-
responseId: response.id
|
|
2019
|
-
cachedPromptTokens: (_c = (_b = response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : null,
|
|
2020
|
-
reasoningTokens: (_e = (_d = response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : null
|
|
2227
|
+
responseId: response.id
|
|
2021
2228
|
}
|
|
2022
2229
|
},
|
|
2023
2230
|
warnings
|
|
2024
2231
|
};
|
|
2025
2232
|
}
|
|
2026
2233
|
async doStream(options) {
|
|
2027
|
-
const { args: body, warnings } = this.getArgs(options);
|
|
2234
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
2028
2235
|
const { responseHeaders, value: response } = await postJsonToApi5({
|
|
2029
2236
|
url: this.config.url({
|
|
2030
2237
|
path: "/responses",
|
|
2031
2238
|
modelId: this.modelId
|
|
2032
2239
|
}),
|
|
2033
|
-
headers:
|
|
2240
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
2034
2241
|
body: {
|
|
2035
2242
|
...body,
|
|
2036
2243
|
stream: true
|
|
@@ -2044,16 +2251,20 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2044
2251
|
});
|
|
2045
2252
|
const self = this;
|
|
2046
2253
|
let finishReason = "unknown";
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2254
|
+
const usage = {
|
|
2255
|
+
inputTokens: void 0,
|
|
2256
|
+
outputTokens: void 0,
|
|
2257
|
+
totalTokens: void 0
|
|
2258
|
+
};
|
|
2051
2259
|
let responseId = null;
|
|
2052
2260
|
const ongoingToolCalls = {};
|
|
2053
2261
|
let hasToolCalls = false;
|
|
2054
2262
|
return {
|
|
2055
2263
|
stream: response.pipeThrough(
|
|
2056
2264
|
new TransformStream({
|
|
2265
|
+
start(controller) {
|
|
2266
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
2267
|
+
},
|
|
2057
2268
|
transform(chunk, controller) {
|
|
2058
2269
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2059
2270
|
if (!chunk.success) {
|
|
@@ -2097,8 +2308,13 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2097
2308
|
});
|
|
2098
2309
|
} else if (isTextDeltaChunk(value)) {
|
|
2099
2310
|
controller.enqueue({
|
|
2100
|
-
type: "text
|
|
2101
|
-
|
|
2311
|
+
type: "text",
|
|
2312
|
+
text: value.delta
|
|
2313
|
+
});
|
|
2314
|
+
} else if (isResponseReasoningSummaryTextDeltaChunk(value)) {
|
|
2315
|
+
controller.enqueue({
|
|
2316
|
+
type: "reasoning",
|
|
2317
|
+
text: value.delta
|
|
2102
2318
|
});
|
|
2103
2319
|
} else if (isResponseOutputItemDoneChunk(value) && value.item.type === "function_call") {
|
|
2104
2320
|
ongoingToolCalls[value.output_index] = void 0;
|
|
@@ -2115,19 +2331,18 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2115
2331
|
finishReason: (_a = value.response.incomplete_details) == null ? void 0 : _a.reason,
|
|
2116
2332
|
hasToolCalls
|
|
2117
2333
|
});
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
reasoningTokens = (
|
|
2334
|
+
usage.inputTokens = value.response.usage.input_tokens;
|
|
2335
|
+
usage.outputTokens = value.response.usage.output_tokens;
|
|
2336
|
+
usage.totalTokens = value.response.usage.input_tokens + value.response.usage.output_tokens;
|
|
2337
|
+
usage.reasoningTokens = (_c = (_b = value.response.usage.output_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0;
|
|
2338
|
+
usage.cachedInputTokens = (_e = (_d = value.response.usage.input_tokens_details) == null ? void 0 : _d.cached_tokens) != null ? _e : void 0;
|
|
2122
2339
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
2123
2340
|
controller.enqueue({
|
|
2124
2341
|
type: "source",
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
title: value.annotation.title
|
|
2130
|
-
}
|
|
2342
|
+
sourceType: "url",
|
|
2343
|
+
id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId2(),
|
|
2344
|
+
url: value.annotation.url,
|
|
2345
|
+
title: value.annotation.title
|
|
2131
2346
|
});
|
|
2132
2347
|
}
|
|
2133
2348
|
},
|
|
@@ -2135,103 +2350,101 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2135
2350
|
controller.enqueue({
|
|
2136
2351
|
type: "finish",
|
|
2137
2352
|
finishReason,
|
|
2138
|
-
usage
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
responseId,
|
|
2143
|
-
cachedPromptTokens,
|
|
2144
|
-
reasoningTokens
|
|
2145
|
-
}
|
|
2353
|
+
usage,
|
|
2354
|
+
providerMetadata: {
|
|
2355
|
+
openai: {
|
|
2356
|
+
responseId
|
|
2146
2357
|
}
|
|
2147
2358
|
}
|
|
2148
2359
|
});
|
|
2149
2360
|
}
|
|
2150
2361
|
})
|
|
2151
2362
|
),
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
rawSettings: {}
|
|
2155
|
-
},
|
|
2156
|
-
rawResponse: { headers: responseHeaders },
|
|
2157
|
-
request: { body: JSON.stringify(body) },
|
|
2158
|
-
warnings
|
|
2363
|
+
request: { body },
|
|
2364
|
+
response: { headers: responseHeaders }
|
|
2159
2365
|
};
|
|
2160
2366
|
}
|
|
2161
2367
|
};
|
|
2162
|
-
var
|
|
2163
|
-
input_tokens:
|
|
2164
|
-
input_tokens_details:
|
|
2165
|
-
output_tokens:
|
|
2166
|
-
output_tokens_details:
|
|
2368
|
+
var usageSchema2 = z12.object({
|
|
2369
|
+
input_tokens: z12.number(),
|
|
2370
|
+
input_tokens_details: z12.object({ cached_tokens: z12.number().nullish() }).nullish(),
|
|
2371
|
+
output_tokens: z12.number(),
|
|
2372
|
+
output_tokens_details: z12.object({ reasoning_tokens: z12.number().nullish() }).nullish()
|
|
2167
2373
|
});
|
|
2168
|
-
var textDeltaChunkSchema =
|
|
2169
|
-
type:
|
|
2170
|
-
delta:
|
|
2374
|
+
var textDeltaChunkSchema = z12.object({
|
|
2375
|
+
type: z12.literal("response.output_text.delta"),
|
|
2376
|
+
delta: z12.string()
|
|
2171
2377
|
});
|
|
2172
|
-
var responseFinishedChunkSchema =
|
|
2173
|
-
type:
|
|
2174
|
-
response:
|
|
2175
|
-
incomplete_details:
|
|
2176
|
-
usage:
|
|
2378
|
+
var responseFinishedChunkSchema = z12.object({
|
|
2379
|
+
type: z12.enum(["response.completed", "response.incomplete"]),
|
|
2380
|
+
response: z12.object({
|
|
2381
|
+
incomplete_details: z12.object({ reason: z12.string() }).nullish(),
|
|
2382
|
+
usage: usageSchema2
|
|
2177
2383
|
})
|
|
2178
2384
|
});
|
|
2179
|
-
var responseCreatedChunkSchema =
|
|
2180
|
-
type:
|
|
2181
|
-
response:
|
|
2182
|
-
id:
|
|
2183
|
-
created_at:
|
|
2184
|
-
model:
|
|
2385
|
+
var responseCreatedChunkSchema = z12.object({
|
|
2386
|
+
type: z12.literal("response.created"),
|
|
2387
|
+
response: z12.object({
|
|
2388
|
+
id: z12.string(),
|
|
2389
|
+
created_at: z12.number(),
|
|
2390
|
+
model: z12.string()
|
|
2185
2391
|
})
|
|
2186
2392
|
});
|
|
2187
|
-
var responseOutputItemDoneSchema =
|
|
2188
|
-
type:
|
|
2189
|
-
output_index:
|
|
2190
|
-
item:
|
|
2191
|
-
|
|
2192
|
-
type:
|
|
2393
|
+
var responseOutputItemDoneSchema = z12.object({
|
|
2394
|
+
type: z12.literal("response.output_item.done"),
|
|
2395
|
+
output_index: z12.number(),
|
|
2396
|
+
item: z12.discriminatedUnion("type", [
|
|
2397
|
+
z12.object({
|
|
2398
|
+
type: z12.literal("message")
|
|
2193
2399
|
}),
|
|
2194
|
-
|
|
2195
|
-
type:
|
|
2196
|
-
id:
|
|
2197
|
-
call_id:
|
|
2198
|
-
name:
|
|
2199
|
-
arguments:
|
|
2200
|
-
status:
|
|
2400
|
+
z12.object({
|
|
2401
|
+
type: z12.literal("function_call"),
|
|
2402
|
+
id: z12.string(),
|
|
2403
|
+
call_id: z12.string(),
|
|
2404
|
+
name: z12.string(),
|
|
2405
|
+
arguments: z12.string(),
|
|
2406
|
+
status: z12.literal("completed")
|
|
2201
2407
|
})
|
|
2202
2408
|
])
|
|
2203
2409
|
});
|
|
2204
|
-
var responseFunctionCallArgumentsDeltaSchema =
|
|
2205
|
-
type:
|
|
2206
|
-
item_id:
|
|
2207
|
-
output_index:
|
|
2208
|
-
delta:
|
|
2410
|
+
var responseFunctionCallArgumentsDeltaSchema = z12.object({
|
|
2411
|
+
type: z12.literal("response.function_call_arguments.delta"),
|
|
2412
|
+
item_id: z12.string(),
|
|
2413
|
+
output_index: z12.number(),
|
|
2414
|
+
delta: z12.string()
|
|
2209
2415
|
});
|
|
2210
|
-
var responseOutputItemAddedSchema =
|
|
2211
|
-
type:
|
|
2212
|
-
output_index:
|
|
2213
|
-
item:
|
|
2214
|
-
|
|
2215
|
-
type:
|
|
2416
|
+
var responseOutputItemAddedSchema = z12.object({
|
|
2417
|
+
type: z12.literal("response.output_item.added"),
|
|
2418
|
+
output_index: z12.number(),
|
|
2419
|
+
item: z12.discriminatedUnion("type", [
|
|
2420
|
+
z12.object({
|
|
2421
|
+
type: z12.literal("message")
|
|
2216
2422
|
}),
|
|
2217
|
-
|
|
2218
|
-
type:
|
|
2219
|
-
id:
|
|
2220
|
-
call_id:
|
|
2221
|
-
name:
|
|
2222
|
-
arguments:
|
|
2423
|
+
z12.object({
|
|
2424
|
+
type: z12.literal("function_call"),
|
|
2425
|
+
id: z12.string(),
|
|
2426
|
+
call_id: z12.string(),
|
|
2427
|
+
name: z12.string(),
|
|
2428
|
+
arguments: z12.string()
|
|
2223
2429
|
})
|
|
2224
2430
|
])
|
|
2225
2431
|
});
|
|
2226
|
-
var responseAnnotationAddedSchema =
|
|
2227
|
-
type:
|
|
2228
|
-
annotation:
|
|
2229
|
-
type:
|
|
2230
|
-
url:
|
|
2231
|
-
title:
|
|
2432
|
+
var responseAnnotationAddedSchema = z12.object({
|
|
2433
|
+
type: z12.literal("response.output_text.annotation.added"),
|
|
2434
|
+
annotation: z12.object({
|
|
2435
|
+
type: z12.literal("url_citation"),
|
|
2436
|
+
url: z12.string(),
|
|
2437
|
+
title: z12.string()
|
|
2232
2438
|
})
|
|
2233
2439
|
});
|
|
2234
|
-
var
|
|
2440
|
+
var responseReasoningSummaryTextDeltaSchema = z12.object({
|
|
2441
|
+
type: z12.literal("response.reasoning_summary_text.delta"),
|
|
2442
|
+
item_id: z12.string(),
|
|
2443
|
+
output_index: z12.number(),
|
|
2444
|
+
summary_index: z12.number(),
|
|
2445
|
+
delta: z12.string()
|
|
2446
|
+
});
|
|
2447
|
+
var openaiResponsesChunkSchema = z12.union([
|
|
2235
2448
|
textDeltaChunkSchema,
|
|
2236
2449
|
responseFinishedChunkSchema,
|
|
2237
2450
|
responseCreatedChunkSchema,
|
|
@@ -2239,7 +2452,8 @@ var openaiResponsesChunkSchema = z7.union([
|
|
|
2239
2452
|
responseFunctionCallArgumentsDeltaSchema,
|
|
2240
2453
|
responseOutputItemAddedSchema,
|
|
2241
2454
|
responseAnnotationAddedSchema,
|
|
2242
|
-
|
|
2455
|
+
responseReasoningSummaryTextDeltaSchema,
|
|
2456
|
+
z12.object({ type: z12.string() }).passthrough()
|
|
2243
2457
|
// fallback for unknown chunks
|
|
2244
2458
|
]);
|
|
2245
2459
|
function isTextDeltaChunk(chunk) {
|
|
@@ -2263,6 +2477,9 @@ function isResponseOutputItemAddedChunk(chunk) {
|
|
|
2263
2477
|
function isResponseAnnotationAddedChunk(chunk) {
|
|
2264
2478
|
return chunk.type === "response.output_text.annotation.added";
|
|
2265
2479
|
}
|
|
2480
|
+
function isResponseReasoningSummaryTextDeltaChunk(chunk) {
|
|
2481
|
+
return chunk.type === "response.reasoning_summary_text.delta";
|
|
2482
|
+
}
|
|
2266
2483
|
function getResponsesModelConfig(modelId) {
|
|
2267
2484
|
if (modelId.startsWith("o")) {
|
|
2268
2485
|
if (modelId.startsWith("o1-mini") || modelId.startsWith("o1-preview")) {
|
|
@@ -2284,23 +2501,127 @@ function getResponsesModelConfig(modelId) {
|
|
|
2284
2501
|
requiredAutoTruncation: false
|
|
2285
2502
|
};
|
|
2286
2503
|
}
|
|
2287
|
-
var openaiResponsesProviderOptionsSchema =
|
|
2288
|
-
metadata:
|
|
2289
|
-
parallelToolCalls:
|
|
2290
|
-
previousResponseId:
|
|
2291
|
-
store:
|
|
2292
|
-
user:
|
|
2293
|
-
reasoningEffort:
|
|
2294
|
-
strictSchemas:
|
|
2295
|
-
instructions:
|
|
2504
|
+
var openaiResponsesProviderOptionsSchema = z12.object({
|
|
2505
|
+
metadata: z12.any().nullish(),
|
|
2506
|
+
parallelToolCalls: z12.boolean().nullish(),
|
|
2507
|
+
previousResponseId: z12.string().nullish(),
|
|
2508
|
+
store: z12.boolean().nullish(),
|
|
2509
|
+
user: z12.string().nullish(),
|
|
2510
|
+
reasoningEffort: z12.string().nullish(),
|
|
2511
|
+
strictSchemas: z12.boolean().nullish(),
|
|
2512
|
+
instructions: z12.string().nullish(),
|
|
2513
|
+
reasoningSummary: z12.string().nullish()
|
|
2296
2514
|
});
|
|
2297
2515
|
|
|
2516
|
+
// src/openai-speech-model.ts
|
|
2517
|
+
import {
|
|
2518
|
+
combineHeaders as combineHeaders7,
|
|
2519
|
+
createBinaryResponseHandler,
|
|
2520
|
+
parseProviderOptions as parseProviderOptions6,
|
|
2521
|
+
postJsonToApi as postJsonToApi6
|
|
2522
|
+
} from "@ai-sdk/provider-utils";
|
|
2523
|
+
import { z as z13 } from "zod";
|
|
2524
|
+
var OpenAIProviderOptionsSchema = z13.object({
|
|
2525
|
+
instructions: z13.string().nullish(),
|
|
2526
|
+
speed: z13.number().min(0.25).max(4).default(1).nullish()
|
|
2527
|
+
});
|
|
2528
|
+
var OpenAISpeechModel = class {
|
|
2529
|
+
constructor(modelId, config) {
|
|
2530
|
+
this.modelId = modelId;
|
|
2531
|
+
this.config = config;
|
|
2532
|
+
this.specificationVersion = "v1";
|
|
2533
|
+
}
|
|
2534
|
+
get provider() {
|
|
2535
|
+
return this.config.provider;
|
|
2536
|
+
}
|
|
2537
|
+
async getArgs({
|
|
2538
|
+
text,
|
|
2539
|
+
voice = "alloy",
|
|
2540
|
+
outputFormat = "mp3",
|
|
2541
|
+
speed,
|
|
2542
|
+
instructions,
|
|
2543
|
+
providerOptions
|
|
2544
|
+
}) {
|
|
2545
|
+
const warnings = [];
|
|
2546
|
+
const openAIOptions = await parseProviderOptions6({
|
|
2547
|
+
provider: "openai",
|
|
2548
|
+
providerOptions,
|
|
2549
|
+
schema: OpenAIProviderOptionsSchema
|
|
2550
|
+
});
|
|
2551
|
+
const requestBody = {
|
|
2552
|
+
model: this.modelId,
|
|
2553
|
+
input: text,
|
|
2554
|
+
voice,
|
|
2555
|
+
response_format: "mp3",
|
|
2556
|
+
speed,
|
|
2557
|
+
instructions
|
|
2558
|
+
};
|
|
2559
|
+
if (outputFormat) {
|
|
2560
|
+
if (["mp3", "opus", "aac", "flac", "wav", "pcm"].includes(outputFormat)) {
|
|
2561
|
+
requestBody.response_format = outputFormat;
|
|
2562
|
+
} else {
|
|
2563
|
+
warnings.push({
|
|
2564
|
+
type: "unsupported-setting",
|
|
2565
|
+
setting: "outputFormat",
|
|
2566
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
|
|
2567
|
+
});
|
|
2568
|
+
}
|
|
2569
|
+
}
|
|
2570
|
+
if (openAIOptions) {
|
|
2571
|
+
const speechModelOptions = {};
|
|
2572
|
+
for (const key in speechModelOptions) {
|
|
2573
|
+
const value = speechModelOptions[key];
|
|
2574
|
+
if (value !== void 0) {
|
|
2575
|
+
requestBody[key] = value;
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
return {
|
|
2580
|
+
requestBody,
|
|
2581
|
+
warnings
|
|
2582
|
+
};
|
|
2583
|
+
}
|
|
2584
|
+
async doGenerate(options) {
|
|
2585
|
+
var _a, _b, _c;
|
|
2586
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2587
|
+
const { requestBody, warnings } = await this.getArgs(options);
|
|
2588
|
+
const {
|
|
2589
|
+
value: audio,
|
|
2590
|
+
responseHeaders,
|
|
2591
|
+
rawValue: rawResponse
|
|
2592
|
+
} = await postJsonToApi6({
|
|
2593
|
+
url: this.config.url({
|
|
2594
|
+
path: "/audio/speech",
|
|
2595
|
+
modelId: this.modelId
|
|
2596
|
+
}),
|
|
2597
|
+
headers: combineHeaders7(this.config.headers(), options.headers),
|
|
2598
|
+
body: requestBody,
|
|
2599
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
2600
|
+
successfulResponseHandler: createBinaryResponseHandler(),
|
|
2601
|
+
abortSignal: options.abortSignal,
|
|
2602
|
+
fetch: this.config.fetch
|
|
2603
|
+
});
|
|
2604
|
+
return {
|
|
2605
|
+
audio,
|
|
2606
|
+
warnings,
|
|
2607
|
+
request: {
|
|
2608
|
+
body: JSON.stringify(requestBody)
|
|
2609
|
+
},
|
|
2610
|
+
response: {
|
|
2611
|
+
timestamp: currentDate,
|
|
2612
|
+
modelId: this.modelId,
|
|
2613
|
+
headers: responseHeaders,
|
|
2614
|
+
body: rawResponse
|
|
2615
|
+
}
|
|
2616
|
+
};
|
|
2617
|
+
}
|
|
2618
|
+
};
|
|
2619
|
+
|
|
2298
2620
|
// src/openai-provider.ts
|
|
2299
2621
|
function createOpenAI(options = {}) {
|
|
2300
|
-
var _a, _b
|
|
2622
|
+
var _a, _b;
|
|
2301
2623
|
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.openai.com/v1";
|
|
2302
|
-
const
|
|
2303
|
-
const providerName = (_c = options.name) != null ? _c : "openai";
|
|
2624
|
+
const providerName = (_b = options.name) != null ? _b : "openai";
|
|
2304
2625
|
const getHeaders = () => ({
|
|
2305
2626
|
Authorization: `Bearer ${loadApiKey({
|
|
2306
2627
|
apiKey: options.apiKey,
|
|
@@ -2311,45 +2632,52 @@ function createOpenAI(options = {}) {
|
|
|
2311
2632
|
"OpenAI-Project": options.project,
|
|
2312
2633
|
...options.headers
|
|
2313
2634
|
});
|
|
2314
|
-
const createChatModel = (modelId
|
|
2635
|
+
const createChatModel = (modelId) => new OpenAIChatLanguageModel(modelId, {
|
|
2315
2636
|
provider: `${providerName}.chat`,
|
|
2316
2637
|
url: ({ path }) => `${baseURL}${path}`,
|
|
2317
2638
|
headers: getHeaders,
|
|
2318
|
-
compatibility,
|
|
2319
2639
|
fetch: options.fetch
|
|
2320
2640
|
});
|
|
2321
|
-
const createCompletionModel = (modelId
|
|
2641
|
+
const createCompletionModel = (modelId) => new OpenAICompletionLanguageModel(modelId, {
|
|
2322
2642
|
provider: `${providerName}.completion`,
|
|
2323
2643
|
url: ({ path }) => `${baseURL}${path}`,
|
|
2324
2644
|
headers: getHeaders,
|
|
2325
|
-
compatibility,
|
|
2326
2645
|
fetch: options.fetch
|
|
2327
2646
|
});
|
|
2328
|
-
const createEmbeddingModel = (modelId
|
|
2647
|
+
const createEmbeddingModel = (modelId) => new OpenAIEmbeddingModel(modelId, {
|
|
2329
2648
|
provider: `${providerName}.embedding`,
|
|
2330
2649
|
url: ({ path }) => `${baseURL}${path}`,
|
|
2331
2650
|
headers: getHeaders,
|
|
2332
2651
|
fetch: options.fetch
|
|
2333
2652
|
});
|
|
2334
|
-
const createImageModel = (modelId
|
|
2653
|
+
const createImageModel = (modelId) => new OpenAIImageModel(modelId, {
|
|
2335
2654
|
provider: `${providerName}.image`,
|
|
2336
2655
|
url: ({ path }) => `${baseURL}${path}`,
|
|
2337
2656
|
headers: getHeaders,
|
|
2338
2657
|
fetch: options.fetch
|
|
2339
2658
|
});
|
|
2340
|
-
const
|
|
2659
|
+
const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
|
|
2660
|
+
provider: `${providerName}.transcription`,
|
|
2661
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2662
|
+
headers: getHeaders,
|
|
2663
|
+
fetch: options.fetch
|
|
2664
|
+
});
|
|
2665
|
+
const createSpeechModel = (modelId) => new OpenAISpeechModel(modelId, {
|
|
2666
|
+
provider: `${providerName}.speech`,
|
|
2667
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2668
|
+
headers: getHeaders,
|
|
2669
|
+
fetch: options.fetch
|
|
2670
|
+
});
|
|
2671
|
+
const createLanguageModel = (modelId) => {
|
|
2341
2672
|
if (new.target) {
|
|
2342
2673
|
throw new Error(
|
|
2343
2674
|
"The OpenAI model function cannot be called with the new keyword."
|
|
2344
2675
|
);
|
|
2345
2676
|
}
|
|
2346
2677
|
if (modelId === "gpt-3.5-turbo-instruct") {
|
|
2347
|
-
return createCompletionModel(
|
|
2348
|
-
modelId,
|
|
2349
|
-
settings
|
|
2350
|
-
);
|
|
2678
|
+
return createCompletionModel(modelId);
|
|
2351
2679
|
}
|
|
2352
|
-
return createChatModel(modelId
|
|
2680
|
+
return createChatModel(modelId);
|
|
2353
2681
|
};
|
|
2354
2682
|
const createResponsesModel = (modelId) => {
|
|
2355
2683
|
return new OpenAIResponsesLanguageModel(modelId, {
|
|
@@ -2359,8 +2687,8 @@ function createOpenAI(options = {}) {
|
|
|
2359
2687
|
fetch: options.fetch
|
|
2360
2688
|
});
|
|
2361
2689
|
};
|
|
2362
|
-
const provider = function(modelId
|
|
2363
|
-
return createLanguageModel(modelId
|
|
2690
|
+
const provider = function(modelId) {
|
|
2691
|
+
return createLanguageModel(modelId);
|
|
2364
2692
|
};
|
|
2365
2693
|
provider.languageModel = createLanguageModel;
|
|
2366
2694
|
provider.chat = createChatModel;
|
|
@@ -2371,13 +2699,14 @@ function createOpenAI(options = {}) {
|
|
|
2371
2699
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2372
2700
|
provider.image = createImageModel;
|
|
2373
2701
|
provider.imageModel = createImageModel;
|
|
2702
|
+
provider.transcription = createTranscriptionModel;
|
|
2703
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
2704
|
+
provider.speech = createSpeechModel;
|
|
2705
|
+
provider.speechModel = createSpeechModel;
|
|
2374
2706
|
provider.tools = openaiTools;
|
|
2375
2707
|
return provider;
|
|
2376
2708
|
}
|
|
2377
|
-
var openai = createOpenAI(
|
|
2378
|
-
compatibility: "strict"
|
|
2379
|
-
// strict for OpenAI API
|
|
2380
|
-
});
|
|
2709
|
+
var openai = createOpenAI();
|
|
2381
2710
|
export {
|
|
2382
2711
|
createOpenAI,
|
|
2383
2712
|
openai
|