@ai-sdk/openai 2.0.0-canary.0 → 2.0.0-canary.10
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 +120 -0
- package/dist/index.d.mts +35 -102
- package/dist/index.d.ts +35 -102
- package/dist/index.js +987 -889
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1007 -899
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +390 -0
- package/dist/internal/index.d.ts +390 -0
- package/{internal/dist → dist/internal}/index.js +967 -877
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +980 -884
- package/dist/internal/index.mjs.map +1 -0
- package/package.json +15 -15
- 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.providerMetadata) == 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,6 +186,19 @@ function convertToOpenAIChatMessages({
|
|
|
192
186
|
return { messages, warnings };
|
|
193
187
|
}
|
|
194
188
|
|
|
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
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
195
202
|
// src/map-openai-chat-logprobs.ts
|
|
196
203
|
function mapOpenAIChatLogProbsOutput(logprobs) {
|
|
197
204
|
var _a, _b;
|
|
@@ -222,18 +229,69 @@ function mapOpenAIFinishReason(finishReason) {
|
|
|
222
229
|
}
|
|
223
230
|
}
|
|
224
231
|
|
|
225
|
-
// src/openai-
|
|
232
|
+
// src/openai-chat-options.ts
|
|
226
233
|
import { z } from "zod";
|
|
234
|
+
var openaiProviderOptions = z.object({
|
|
235
|
+
/**
|
|
236
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
237
|
+
*
|
|
238
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
239
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
240
|
+
*/
|
|
241
|
+
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
242
|
+
/**
|
|
243
|
+
* Return the log probabilities of the tokens.
|
|
244
|
+
*
|
|
245
|
+
* Setting to true will return the log probabilities of the tokens that
|
|
246
|
+
* were generated.
|
|
247
|
+
*
|
|
248
|
+
* Setting to a number will return the log probabilities of the top n
|
|
249
|
+
* tokens that were generated.
|
|
250
|
+
*/
|
|
251
|
+
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
|
252
|
+
/**
|
|
253
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
254
|
+
*/
|
|
255
|
+
parallelToolCalls: z.boolean().optional(),
|
|
256
|
+
/**
|
|
257
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
258
|
+
* monitor and detect abuse.
|
|
259
|
+
*/
|
|
260
|
+
user: z.string().optional(),
|
|
261
|
+
/**
|
|
262
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
263
|
+
*/
|
|
264
|
+
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
|
|
265
|
+
/**
|
|
266
|
+
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
267
|
+
*/
|
|
268
|
+
maxCompletionTokens: z.number().optional(),
|
|
269
|
+
/**
|
|
270
|
+
* Whether to enable persistence in responses API.
|
|
271
|
+
*/
|
|
272
|
+
store: z.boolean().optional(),
|
|
273
|
+
/**
|
|
274
|
+
* Metadata to associate with the request.
|
|
275
|
+
*/
|
|
276
|
+
metadata: z.record(z.string()).optional(),
|
|
277
|
+
/**
|
|
278
|
+
* Parameters for prediction mode.
|
|
279
|
+
*/
|
|
280
|
+
prediction: z.record(z.any()).optional()
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// src/openai-error.ts
|
|
284
|
+
import { z as z2 } from "zod";
|
|
227
285
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
228
|
-
var openaiErrorDataSchema =
|
|
229
|
-
error:
|
|
230
|
-
message:
|
|
286
|
+
var openaiErrorDataSchema = z2.object({
|
|
287
|
+
error: z2.object({
|
|
288
|
+
message: z2.string(),
|
|
231
289
|
// The additional information below is handled loosely to support
|
|
232
290
|
// OpenAI-compatible providers that have slightly different error
|
|
233
291
|
// responses:
|
|
234
|
-
type:
|
|
235
|
-
param:
|
|
236
|
-
code:
|
|
292
|
+
type: z2.string().nullish(),
|
|
293
|
+
param: z2.any().nullish(),
|
|
294
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
237
295
|
})
|
|
238
296
|
});
|
|
239
297
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
@@ -241,76 +299,19 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
241
299
|
errorToMessage: (data) => data.error.message
|
|
242
300
|
});
|
|
243
301
|
|
|
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
302
|
// src/openai-prepare-tools.ts
|
|
258
303
|
import {
|
|
259
304
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
260
305
|
} from "@ai-sdk/provider";
|
|
261
306
|
function prepareTools({
|
|
262
|
-
|
|
263
|
-
|
|
307
|
+
tools,
|
|
308
|
+
toolChoice,
|
|
264
309
|
structuredOutputs
|
|
265
310
|
}) {
|
|
266
|
-
|
|
267
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
311
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
268
312
|
const toolWarnings = [];
|
|
269
313
|
if (tools == null) {
|
|
270
|
-
return { tools: void 0,
|
|
271
|
-
}
|
|
272
|
-
const toolChoice = mode.toolChoice;
|
|
273
|
-
if (useLegacyFunctionCalling) {
|
|
274
|
-
const openaiFunctions = [];
|
|
275
|
-
for (const tool of tools) {
|
|
276
|
-
if (tool.type === "provider-defined") {
|
|
277
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
278
|
-
} else {
|
|
279
|
-
openaiFunctions.push({
|
|
280
|
-
name: tool.name,
|
|
281
|
-
description: tool.description,
|
|
282
|
-
parameters: tool.parameters
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
if (toolChoice == null) {
|
|
287
|
-
return {
|
|
288
|
-
functions: openaiFunctions,
|
|
289
|
-
function_call: void 0,
|
|
290
|
-
toolWarnings
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
const type2 = toolChoice.type;
|
|
294
|
-
switch (type2) {
|
|
295
|
-
case "auto":
|
|
296
|
-
case "none":
|
|
297
|
-
case void 0:
|
|
298
|
-
return {
|
|
299
|
-
functions: openaiFunctions,
|
|
300
|
-
function_call: void 0,
|
|
301
|
-
toolWarnings
|
|
302
|
-
};
|
|
303
|
-
case "required":
|
|
304
|
-
throw new UnsupportedFunctionalityError2({
|
|
305
|
-
functionality: "useLegacyFunctionCalling and toolChoice: required"
|
|
306
|
-
});
|
|
307
|
-
default:
|
|
308
|
-
return {
|
|
309
|
-
functions: openaiFunctions,
|
|
310
|
-
function_call: { name: toolChoice.toolName },
|
|
311
|
-
toolWarnings
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
314
315
|
}
|
|
315
316
|
const openaiTools2 = [];
|
|
316
317
|
for (const tool of tools) {
|
|
@@ -329,18 +330,18 @@ function prepareTools({
|
|
|
329
330
|
}
|
|
330
331
|
}
|
|
331
332
|
if (toolChoice == null) {
|
|
332
|
-
return { tools: openaiTools2,
|
|
333
|
+
return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
|
|
333
334
|
}
|
|
334
335
|
const type = toolChoice.type;
|
|
335
336
|
switch (type) {
|
|
336
337
|
case "auto":
|
|
337
338
|
case "none":
|
|
338
339
|
case "required":
|
|
339
|
-
return { tools: openaiTools2,
|
|
340
|
+
return { tools: openaiTools2, toolChoice: type, toolWarnings };
|
|
340
341
|
case "tool":
|
|
341
342
|
return {
|
|
342
343
|
tools: openaiTools2,
|
|
343
|
-
|
|
344
|
+
toolChoice: {
|
|
344
345
|
type: "function",
|
|
345
346
|
function: {
|
|
346
347
|
name: toolChoice.toolName
|
|
@@ -351,7 +352,7 @@ function prepareTools({
|
|
|
351
352
|
default: {
|
|
352
353
|
const _exhaustiveCheck = type;
|
|
353
354
|
throw new UnsupportedFunctionalityError2({
|
|
354
|
-
functionality: `
|
|
355
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
355
356
|
});
|
|
356
357
|
}
|
|
357
358
|
}
|
|
@@ -360,31 +361,22 @@ function prepareTools({
|
|
|
360
361
|
// src/openai-chat-language-model.ts
|
|
361
362
|
var OpenAIChatLanguageModel = class {
|
|
362
363
|
constructor(modelId, settings, config) {
|
|
363
|
-
this.specificationVersion = "
|
|
364
|
+
this.specificationVersion = "v2";
|
|
364
365
|
this.modelId = modelId;
|
|
365
366
|
this.settings = settings;
|
|
366
367
|
this.config = config;
|
|
367
368
|
}
|
|
368
|
-
get supportsStructuredOutputs() {
|
|
369
|
-
var _a;
|
|
370
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : isReasoningModel(this.modelId);
|
|
371
|
-
}
|
|
372
|
-
get defaultObjectGenerationMode() {
|
|
373
|
-
if (isAudioModel(this.modelId)) {
|
|
374
|
-
return "tool";
|
|
375
|
-
}
|
|
376
|
-
return this.supportsStructuredOutputs ? "json" : "tool";
|
|
377
|
-
}
|
|
378
369
|
get provider() {
|
|
379
370
|
return this.config.provider;
|
|
380
371
|
}
|
|
381
|
-
|
|
382
|
-
return
|
|
372
|
+
async getSupportedUrls() {
|
|
373
|
+
return {
|
|
374
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
375
|
+
};
|
|
383
376
|
}
|
|
384
377
|
getArgs({
|
|
385
|
-
mode,
|
|
386
378
|
prompt,
|
|
387
|
-
|
|
379
|
+
maxOutputTokens,
|
|
388
380
|
temperature,
|
|
389
381
|
topP,
|
|
390
382
|
topK,
|
|
@@ -393,39 +385,33 @@ var OpenAIChatLanguageModel = class {
|
|
|
393
385
|
stopSequences,
|
|
394
386
|
responseFormat,
|
|
395
387
|
seed,
|
|
396
|
-
|
|
388
|
+
tools,
|
|
389
|
+
toolChoice,
|
|
390
|
+
providerOptions
|
|
397
391
|
}) {
|
|
398
|
-
var _a, _b, _c
|
|
399
|
-
const type = mode.type;
|
|
392
|
+
var _a, _b, _c;
|
|
400
393
|
const warnings = [];
|
|
394
|
+
const openaiOptions = (_a = parseProviderOptions({
|
|
395
|
+
provider: "openai",
|
|
396
|
+
providerOptions,
|
|
397
|
+
schema: openaiProviderOptions
|
|
398
|
+
})) != null ? _a : {};
|
|
401
399
|
if (topK != null) {
|
|
402
400
|
warnings.push({
|
|
403
401
|
type: "unsupported-setting",
|
|
404
402
|
setting: "topK"
|
|
405
403
|
});
|
|
406
404
|
}
|
|
407
|
-
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.
|
|
405
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.settings.structuredOutputs) {
|
|
408
406
|
warnings.push({
|
|
409
407
|
type: "unsupported-setting",
|
|
410
408
|
setting: "responseFormat",
|
|
411
409
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
412
410
|
});
|
|
413
411
|
}
|
|
414
|
-
const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
|
|
415
|
-
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
|
|
416
|
-
throw new UnsupportedFunctionalityError3({
|
|
417
|
-
functionality: "useLegacyFunctionCalling with parallelToolCalls"
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
|
|
421
|
-
throw new UnsupportedFunctionalityError3({
|
|
422
|
-
functionality: "structuredOutputs with useLegacyFunctionCalling"
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
412
|
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
|
|
426
413
|
{
|
|
427
414
|
prompt,
|
|
428
|
-
useLegacyFunctionCalling,
|
|
429
415
|
systemMessageMode: getSystemMessageMode(this.modelId)
|
|
430
416
|
}
|
|
431
417
|
);
|
|
@@ -434,35 +420,38 @@ var OpenAIChatLanguageModel = class {
|
|
|
434
420
|
// model id:
|
|
435
421
|
model: this.modelId,
|
|
436
422
|
// model specific settings:
|
|
437
|
-
logit_bias:
|
|
438
|
-
logprobs:
|
|
439
|
-
top_logprobs: typeof
|
|
440
|
-
user:
|
|
441
|
-
parallel_tool_calls:
|
|
423
|
+
logit_bias: openaiOptions.logitBias,
|
|
424
|
+
logprobs: openaiOptions.logprobs === true || typeof openaiOptions.logprobs === "number" ? true : void 0,
|
|
425
|
+
top_logprobs: typeof openaiOptions.logprobs === "number" ? openaiOptions.logprobs : typeof openaiOptions.logprobs === "boolean" ? openaiOptions.logprobs ? 0 : void 0 : void 0,
|
|
426
|
+
user: openaiOptions.user,
|
|
427
|
+
parallel_tool_calls: openaiOptions.parallelToolCalls,
|
|
442
428
|
// standardized settings:
|
|
443
|
-
max_tokens:
|
|
429
|
+
max_tokens: maxOutputTokens,
|
|
444
430
|
temperature,
|
|
445
431
|
top_p: topP,
|
|
446
432
|
frequency_penalty: frequencyPenalty,
|
|
447
433
|
presence_penalty: presencePenalty,
|
|
448
|
-
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ?
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
434
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? (
|
|
435
|
+
// TODO convert into provider option
|
|
436
|
+
this.settings.structuredOutputs && responseFormat.schema != null ? {
|
|
437
|
+
type: "json_schema",
|
|
438
|
+
json_schema: {
|
|
439
|
+
schema: responseFormat.schema,
|
|
440
|
+
strict: true,
|
|
441
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
442
|
+
description: responseFormat.description
|
|
443
|
+
}
|
|
444
|
+
} : { type: "json_object" }
|
|
445
|
+
) : void 0,
|
|
457
446
|
stop: stopSequences,
|
|
458
447
|
seed,
|
|
459
448
|
// 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:
|
|
449
|
+
// TODO remove in next major version; we auto-map maxOutputTokens now
|
|
450
|
+
max_completion_tokens: openaiOptions.maxCompletionTokens,
|
|
451
|
+
store: openaiOptions.store,
|
|
452
|
+
metadata: openaiOptions.metadata,
|
|
453
|
+
prediction: openaiOptions.prediction,
|
|
454
|
+
reasoning_effort: openaiOptions.reasoningEffort,
|
|
466
455
|
// messages:
|
|
467
456
|
messages
|
|
468
457
|
};
|
|
@@ -526,82 +515,33 @@ var OpenAIChatLanguageModel = class {
|
|
|
526
515
|
}
|
|
527
516
|
baseArgs.max_tokens = void 0;
|
|
528
517
|
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
518
|
+
} else if (this.modelId.startsWith("gpt-4o-search-preview") || this.modelId.startsWith("gpt-4o-mini-search-preview")) {
|
|
519
|
+
if (baseArgs.temperature != null) {
|
|
520
|
+
baseArgs.temperature = void 0;
|
|
521
|
+
warnings.push({
|
|
522
|
+
type: "unsupported-setting",
|
|
523
|
+
setting: "temperature",
|
|
524
|
+
details: "temperature is not supported for the search preview models and has been removed."
|
|
536
525
|
});
|
|
537
|
-
return {
|
|
538
|
-
args: {
|
|
539
|
-
...baseArgs,
|
|
540
|
-
tools,
|
|
541
|
-
tool_choice,
|
|
542
|
-
functions,
|
|
543
|
-
function_call
|
|
544
|
-
},
|
|
545
|
-
warnings: [...warnings, ...toolWarnings]
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
case "object-json": {
|
|
549
|
-
return {
|
|
550
|
-
args: {
|
|
551
|
-
...baseArgs,
|
|
552
|
-
response_format: this.supportsStructuredOutputs && mode.schema != null ? {
|
|
553
|
-
type: "json_schema",
|
|
554
|
-
json_schema: {
|
|
555
|
-
schema: mode.schema,
|
|
556
|
-
strict: true,
|
|
557
|
-
name: (_h = mode.name) != null ? _h : "response",
|
|
558
|
-
description: mode.description
|
|
559
|
-
}
|
|
560
|
-
} : { type: "json_object" }
|
|
561
|
-
},
|
|
562
|
-
warnings
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
case "object-tool": {
|
|
566
|
-
return {
|
|
567
|
-
args: useLegacyFunctionCalling ? {
|
|
568
|
-
...baseArgs,
|
|
569
|
-
function_call: {
|
|
570
|
-
name: mode.tool.name
|
|
571
|
-
},
|
|
572
|
-
functions: [
|
|
573
|
-
{
|
|
574
|
-
name: mode.tool.name,
|
|
575
|
-
description: mode.tool.description,
|
|
576
|
-
parameters: mode.tool.parameters
|
|
577
|
-
}
|
|
578
|
-
]
|
|
579
|
-
} : {
|
|
580
|
-
...baseArgs,
|
|
581
|
-
tool_choice: {
|
|
582
|
-
type: "function",
|
|
583
|
-
function: { name: mode.tool.name }
|
|
584
|
-
},
|
|
585
|
-
tools: [
|
|
586
|
-
{
|
|
587
|
-
type: "function",
|
|
588
|
-
function: {
|
|
589
|
-
name: mode.tool.name,
|
|
590
|
-
description: mode.tool.description,
|
|
591
|
-
parameters: mode.tool.parameters,
|
|
592
|
-
strict: this.supportsStructuredOutputs ? true : void 0
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
]
|
|
596
|
-
},
|
|
597
|
-
warnings
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
default: {
|
|
601
|
-
const _exhaustiveCheck = type;
|
|
602
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
603
526
|
}
|
|
604
527
|
}
|
|
528
|
+
const {
|
|
529
|
+
tools: openaiTools2,
|
|
530
|
+
toolChoice: openaiToolChoice,
|
|
531
|
+
toolWarnings
|
|
532
|
+
} = prepareTools({
|
|
533
|
+
tools,
|
|
534
|
+
toolChoice,
|
|
535
|
+
structuredOutputs: (_c = this.settings.structuredOutputs) != null ? _c : false
|
|
536
|
+
});
|
|
537
|
+
return {
|
|
538
|
+
args: {
|
|
539
|
+
...baseArgs,
|
|
540
|
+
tools: openaiTools2,
|
|
541
|
+
tool_choice: openaiToolChoice
|
|
542
|
+
},
|
|
543
|
+
warnings: [...warnings, ...toolWarnings]
|
|
544
|
+
};
|
|
605
545
|
}
|
|
606
546
|
async doGenerate(options) {
|
|
607
547
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -624,10 +564,23 @@ var OpenAIChatLanguageModel = class {
|
|
|
624
564
|
abortSignal: options.abortSignal,
|
|
625
565
|
fetch: this.config.fetch
|
|
626
566
|
});
|
|
627
|
-
const { messages: rawPrompt, ...rawSettings } = body;
|
|
628
567
|
const choice = response.choices[0];
|
|
629
|
-
const
|
|
630
|
-
const
|
|
568
|
+
const content = [];
|
|
569
|
+
const text = choice.message.content;
|
|
570
|
+
if (text != null && text.length > 0) {
|
|
571
|
+
content.push({ type: "text", text });
|
|
572
|
+
}
|
|
573
|
+
for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
|
|
574
|
+
content.push({
|
|
575
|
+
type: "tool-call",
|
|
576
|
+
toolCallType: "function",
|
|
577
|
+
toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
|
|
578
|
+
toolName: toolCall.function.name,
|
|
579
|
+
args: toolCall.function.arguments
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
const completionTokenDetails = (_c = response.usage) == null ? void 0 : _c.completion_tokens_details;
|
|
583
|
+
const promptTokenDetails = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details;
|
|
631
584
|
const providerMetadata = { openai: {} };
|
|
632
585
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
|
|
633
586
|
providerMetadata.openai.reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
|
|
@@ -642,81 +595,24 @@ var OpenAIChatLanguageModel = class {
|
|
|
642
595
|
providerMetadata.openai.cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
|
|
643
596
|
}
|
|
644
597
|
return {
|
|
645
|
-
|
|
646
|
-
toolCalls: this.settings.useLegacyFunctionCalling && choice.message.function_call ? [
|
|
647
|
-
{
|
|
648
|
-
toolCallType: "function",
|
|
649
|
-
toolCallId: generateId(),
|
|
650
|
-
toolName: choice.message.function_call.name,
|
|
651
|
-
args: choice.message.function_call.arguments
|
|
652
|
-
}
|
|
653
|
-
] : (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
654
|
-
var _a2;
|
|
655
|
-
return {
|
|
656
|
-
toolCallType: "function",
|
|
657
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
658
|
-
toolName: toolCall.function.name,
|
|
659
|
-
args: toolCall.function.arguments
|
|
660
|
-
};
|
|
661
|
-
}),
|
|
598
|
+
content,
|
|
662
599
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
663
600
|
usage: {
|
|
664
|
-
|
|
665
|
-
|
|
601
|
+
inputTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
|
|
602
|
+
outputTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0
|
|
603
|
+
},
|
|
604
|
+
request: { body },
|
|
605
|
+
response: {
|
|
606
|
+
...getResponseMetadata(response),
|
|
607
|
+
headers: responseHeaders,
|
|
608
|
+
body: rawResponse
|
|
666
609
|
},
|
|
667
|
-
rawCall: { rawPrompt, rawSettings },
|
|
668
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
|
669
|
-
request: { body: JSON.stringify(body) },
|
|
670
|
-
response: getResponseMetadata(response),
|
|
671
610
|
warnings,
|
|
672
611
|
logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
|
|
673
612
|
providerMetadata
|
|
674
613
|
};
|
|
675
614
|
}
|
|
676
615
|
async doStream(options) {
|
|
677
|
-
if (this.settings.simulateStreaming) {
|
|
678
|
-
const result = await this.doGenerate(options);
|
|
679
|
-
const simulatedStream = new ReadableStream({
|
|
680
|
-
start(controller) {
|
|
681
|
-
controller.enqueue({ type: "response-metadata", ...result.response });
|
|
682
|
-
if (result.text) {
|
|
683
|
-
controller.enqueue({
|
|
684
|
-
type: "text-delta",
|
|
685
|
-
textDelta: result.text
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
if (result.toolCalls) {
|
|
689
|
-
for (const toolCall of result.toolCalls) {
|
|
690
|
-
controller.enqueue({
|
|
691
|
-
type: "tool-call-delta",
|
|
692
|
-
toolCallType: "function",
|
|
693
|
-
toolCallId: toolCall.toolCallId,
|
|
694
|
-
toolName: toolCall.toolName,
|
|
695
|
-
argsTextDelta: toolCall.args
|
|
696
|
-
});
|
|
697
|
-
controller.enqueue({
|
|
698
|
-
type: "tool-call",
|
|
699
|
-
...toolCall
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
controller.enqueue({
|
|
704
|
-
type: "finish",
|
|
705
|
-
finishReason: result.finishReason,
|
|
706
|
-
usage: result.usage,
|
|
707
|
-
logprobs: result.logprobs,
|
|
708
|
-
providerMetadata: result.providerMetadata
|
|
709
|
-
});
|
|
710
|
-
controller.close();
|
|
711
|
-
}
|
|
712
|
-
});
|
|
713
|
-
return {
|
|
714
|
-
stream: simulatedStream,
|
|
715
|
-
rawCall: result.rawCall,
|
|
716
|
-
rawResponse: result.rawResponse,
|
|
717
|
-
warnings: result.warnings
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
616
|
const { args, warnings } = this.getArgs(options);
|
|
721
617
|
const body = {
|
|
722
618
|
...args,
|
|
@@ -741,17 +637,19 @@ var OpenAIChatLanguageModel = class {
|
|
|
741
637
|
const { messages: rawPrompt, ...rawSettings } = args;
|
|
742
638
|
const toolCalls = [];
|
|
743
639
|
let finishReason = "unknown";
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
640
|
+
const usage = {
|
|
641
|
+
inputTokens: void 0,
|
|
642
|
+
outputTokens: void 0
|
|
747
643
|
};
|
|
748
644
|
let logprobs;
|
|
749
645
|
let isFirstChunk = true;
|
|
750
|
-
const { useLegacyFunctionCalling } = this.settings;
|
|
751
646
|
const providerMetadata = { openai: {} };
|
|
752
647
|
return {
|
|
753
648
|
stream: response.pipeThrough(
|
|
754
649
|
new TransformStream({
|
|
650
|
+
start(controller) {
|
|
651
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
652
|
+
},
|
|
755
653
|
transform(chunk, controller) {
|
|
756
654
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
757
655
|
if (!chunk.success) {
|
|
@@ -779,10 +677,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
779
677
|
prompt_tokens_details,
|
|
780
678
|
completion_tokens_details
|
|
781
679
|
} = value.usage;
|
|
782
|
-
usage =
|
|
783
|
-
|
|
784
|
-
completionTokens: completion_tokens != null ? completion_tokens : void 0
|
|
785
|
-
};
|
|
680
|
+
usage.inputTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
|
681
|
+
usage.outputTokens = completion_tokens != null ? completion_tokens : void 0;
|
|
786
682
|
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
787
683
|
providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
788
684
|
}
|
|
@@ -806,8 +702,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
806
702
|
const delta = choice.delta;
|
|
807
703
|
if (delta.content != null) {
|
|
808
704
|
controller.enqueue({
|
|
809
|
-
type: "text
|
|
810
|
-
|
|
705
|
+
type: "text",
|
|
706
|
+
text: delta.content
|
|
811
707
|
});
|
|
812
708
|
}
|
|
813
709
|
const mappedLogprobs = mapOpenAIChatLogProbsOutput(
|
|
@@ -817,16 +713,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
817
713
|
if (logprobs === void 0) logprobs = [];
|
|
818
714
|
logprobs.push(...mappedLogprobs);
|
|
819
715
|
}
|
|
820
|
-
|
|
821
|
-
{
|
|
822
|
-
type: "function",
|
|
823
|
-
id: generateId(),
|
|
824
|
-
function: delta.function_call,
|
|
825
|
-
index: 0
|
|
826
|
-
}
|
|
827
|
-
] : delta.tool_calls;
|
|
828
|
-
if (mappedToolCalls != null) {
|
|
829
|
-
for (const toolCallDelta of mappedToolCalls) {
|
|
716
|
+
if (delta.tool_calls != null) {
|
|
717
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
830
718
|
const index = toolCallDelta.index;
|
|
831
719
|
if (toolCalls[index] == null) {
|
|
832
720
|
if (toolCallDelta.type !== "function") {
|
|
@@ -908,125 +796,111 @@ var OpenAIChatLanguageModel = class {
|
|
|
908
796
|
}
|
|
909
797
|
},
|
|
910
798
|
flush(controller) {
|
|
911
|
-
var _a, _b;
|
|
912
799
|
controller.enqueue({
|
|
913
800
|
type: "finish",
|
|
914
801
|
finishReason,
|
|
915
802
|
logprobs,
|
|
916
|
-
usage
|
|
917
|
-
promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
|
|
918
|
-
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
|
919
|
-
},
|
|
803
|
+
usage,
|
|
920
804
|
...providerMetadata != null ? { providerMetadata } : {}
|
|
921
805
|
});
|
|
922
806
|
}
|
|
923
807
|
})
|
|
924
808
|
),
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
request: { body: JSON.stringify(body) },
|
|
928
|
-
warnings
|
|
809
|
+
request: { body },
|
|
810
|
+
response: { headers: responseHeaders }
|
|
929
811
|
};
|
|
930
812
|
}
|
|
931
813
|
};
|
|
932
|
-
var openaiTokenUsageSchema =
|
|
933
|
-
prompt_tokens:
|
|
934
|
-
completion_tokens:
|
|
935
|
-
prompt_tokens_details:
|
|
936
|
-
cached_tokens:
|
|
814
|
+
var openaiTokenUsageSchema = z3.object({
|
|
815
|
+
prompt_tokens: z3.number().nullish(),
|
|
816
|
+
completion_tokens: z3.number().nullish(),
|
|
817
|
+
prompt_tokens_details: z3.object({
|
|
818
|
+
cached_tokens: z3.number().nullish()
|
|
937
819
|
}).nullish(),
|
|
938
|
-
completion_tokens_details:
|
|
939
|
-
reasoning_tokens:
|
|
940
|
-
accepted_prediction_tokens:
|
|
941
|
-
rejected_prediction_tokens:
|
|
820
|
+
completion_tokens_details: z3.object({
|
|
821
|
+
reasoning_tokens: z3.number().nullish(),
|
|
822
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
823
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
942
824
|
}).nullish()
|
|
943
825
|
}).nullish();
|
|
944
|
-
var openaiChatResponseSchema =
|
|
945
|
-
id:
|
|
946
|
-
created:
|
|
947
|
-
model:
|
|
948
|
-
choices:
|
|
949
|
-
|
|
950
|
-
message:
|
|
951
|
-
role:
|
|
952
|
-
content:
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
type: z2.literal("function"),
|
|
961
|
-
function: z2.object({
|
|
962
|
-
name: z2.string(),
|
|
963
|
-
arguments: z2.string()
|
|
826
|
+
var openaiChatResponseSchema = z3.object({
|
|
827
|
+
id: z3.string().nullish(),
|
|
828
|
+
created: z3.number().nullish(),
|
|
829
|
+
model: z3.string().nullish(),
|
|
830
|
+
choices: z3.array(
|
|
831
|
+
z3.object({
|
|
832
|
+
message: z3.object({
|
|
833
|
+
role: z3.literal("assistant").nullish(),
|
|
834
|
+
content: z3.string().nullish(),
|
|
835
|
+
tool_calls: z3.array(
|
|
836
|
+
z3.object({
|
|
837
|
+
id: z3.string().nullish(),
|
|
838
|
+
type: z3.literal("function"),
|
|
839
|
+
function: z3.object({
|
|
840
|
+
name: z3.string(),
|
|
841
|
+
arguments: z3.string()
|
|
964
842
|
})
|
|
965
843
|
})
|
|
966
844
|
).nullish()
|
|
967
845
|
}),
|
|
968
|
-
index:
|
|
969
|
-
logprobs:
|
|
970
|
-
content:
|
|
971
|
-
|
|
972
|
-
token:
|
|
973
|
-
logprob:
|
|
974
|
-
top_logprobs:
|
|
975
|
-
|
|
976
|
-
token:
|
|
977
|
-
logprob:
|
|
846
|
+
index: z3.number(),
|
|
847
|
+
logprobs: z3.object({
|
|
848
|
+
content: z3.array(
|
|
849
|
+
z3.object({
|
|
850
|
+
token: z3.string(),
|
|
851
|
+
logprob: z3.number(),
|
|
852
|
+
top_logprobs: z3.array(
|
|
853
|
+
z3.object({
|
|
854
|
+
token: z3.string(),
|
|
855
|
+
logprob: z3.number()
|
|
978
856
|
})
|
|
979
857
|
)
|
|
980
858
|
})
|
|
981
859
|
).nullable()
|
|
982
860
|
}).nullish(),
|
|
983
|
-
finish_reason:
|
|
861
|
+
finish_reason: z3.string().nullish()
|
|
984
862
|
})
|
|
985
863
|
),
|
|
986
864
|
usage: openaiTokenUsageSchema
|
|
987
865
|
});
|
|
988
|
-
var openaiChatChunkSchema =
|
|
989
|
-
|
|
990
|
-
id:
|
|
991
|
-
created:
|
|
992
|
-
model:
|
|
993
|
-
choices:
|
|
994
|
-
|
|
995
|
-
delta:
|
|
996
|
-
role:
|
|
997
|
-
content:
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
type: z2.literal("function").optional(),
|
|
1007
|
-
function: z2.object({
|
|
1008
|
-
name: z2.string().nullish(),
|
|
1009
|
-
arguments: z2.string().nullish()
|
|
866
|
+
var openaiChatChunkSchema = z3.union([
|
|
867
|
+
z3.object({
|
|
868
|
+
id: z3.string().nullish(),
|
|
869
|
+
created: z3.number().nullish(),
|
|
870
|
+
model: z3.string().nullish(),
|
|
871
|
+
choices: z3.array(
|
|
872
|
+
z3.object({
|
|
873
|
+
delta: z3.object({
|
|
874
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
875
|
+
content: z3.string().nullish(),
|
|
876
|
+
tool_calls: z3.array(
|
|
877
|
+
z3.object({
|
|
878
|
+
index: z3.number(),
|
|
879
|
+
id: z3.string().nullish(),
|
|
880
|
+
type: z3.literal("function").optional(),
|
|
881
|
+
function: z3.object({
|
|
882
|
+
name: z3.string().nullish(),
|
|
883
|
+
arguments: z3.string().nullish()
|
|
1010
884
|
})
|
|
1011
885
|
})
|
|
1012
886
|
).nullish()
|
|
1013
887
|
}).nullish(),
|
|
1014
|
-
logprobs:
|
|
1015
|
-
content:
|
|
1016
|
-
|
|
1017
|
-
token:
|
|
1018
|
-
logprob:
|
|
1019
|
-
top_logprobs:
|
|
1020
|
-
|
|
1021
|
-
token:
|
|
1022
|
-
logprob:
|
|
888
|
+
logprobs: z3.object({
|
|
889
|
+
content: z3.array(
|
|
890
|
+
z3.object({
|
|
891
|
+
token: z3.string(),
|
|
892
|
+
logprob: z3.number(),
|
|
893
|
+
top_logprobs: z3.array(
|
|
894
|
+
z3.object({
|
|
895
|
+
token: z3.string(),
|
|
896
|
+
logprob: z3.number()
|
|
1023
897
|
})
|
|
1024
898
|
)
|
|
1025
899
|
})
|
|
1026
900
|
).nullable()
|
|
1027
901
|
}).nullish(),
|
|
1028
|
-
finish_reason:
|
|
1029
|
-
index:
|
|
902
|
+
finish_reason: z3.string().nullable().optional(),
|
|
903
|
+
index: z3.number()
|
|
1030
904
|
})
|
|
1031
905
|
),
|
|
1032
906
|
usage: openaiTokenUsageSchema
|
|
@@ -1034,10 +908,7 @@ var openaiChatChunkSchema = z2.union([
|
|
|
1034
908
|
openaiErrorDataSchema
|
|
1035
909
|
]);
|
|
1036
910
|
function isReasoningModel(modelId) {
|
|
1037
|
-
return modelId
|
|
1038
|
-
}
|
|
1039
|
-
function isAudioModel(modelId) {
|
|
1040
|
-
return modelId.startsWith("gpt-4o-audio-preview");
|
|
911
|
+
return modelId.startsWith("o");
|
|
1041
912
|
}
|
|
1042
913
|
function getSystemMessageMode(modelId) {
|
|
1043
914
|
var _a, _b;
|
|
@@ -1068,21 +939,18 @@ var reasoningModels = {
|
|
|
1068
939
|
};
|
|
1069
940
|
|
|
1070
941
|
// src/openai-completion-language-model.ts
|
|
1071
|
-
import {
|
|
1072
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1073
|
-
} from "@ai-sdk/provider";
|
|
1074
942
|
import {
|
|
1075
943
|
combineHeaders as combineHeaders2,
|
|
1076
944
|
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1077
945
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1078
946
|
postJsonToApi as postJsonToApi2
|
|
1079
947
|
} from "@ai-sdk/provider-utils";
|
|
1080
|
-
import { z as
|
|
948
|
+
import { z as z4 } from "zod";
|
|
1081
949
|
|
|
1082
950
|
// src/convert-to-openai-completion-prompt.ts
|
|
1083
951
|
import {
|
|
1084
952
|
InvalidPromptError,
|
|
1085
|
-
UnsupportedFunctionalityError as
|
|
953
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1086
954
|
} from "@ai-sdk/provider";
|
|
1087
955
|
function convertToOpenAICompletionPrompt({
|
|
1088
956
|
prompt,
|
|
@@ -1114,13 +982,8 @@ function convertToOpenAICompletionPrompt({
|
|
|
1114
982
|
case "text": {
|
|
1115
983
|
return part.text;
|
|
1116
984
|
}
|
|
1117
|
-
case "image": {
|
|
1118
|
-
throw new UnsupportedFunctionalityError4({
|
|
1119
|
-
functionality: "images"
|
|
1120
|
-
});
|
|
1121
|
-
}
|
|
1122
985
|
}
|
|
1123
|
-
}).join("");
|
|
986
|
+
}).filter(Boolean).join("");
|
|
1124
987
|
text += `${user}:
|
|
1125
988
|
${userMessage}
|
|
1126
989
|
|
|
@@ -1134,7 +997,7 @@ ${userMessage}
|
|
|
1134
997
|
return part.text;
|
|
1135
998
|
}
|
|
1136
999
|
case "tool-call": {
|
|
1137
|
-
throw new
|
|
1000
|
+
throw new UnsupportedFunctionalityError3({
|
|
1138
1001
|
functionality: "tool-call messages"
|
|
1139
1002
|
});
|
|
1140
1003
|
}
|
|
@@ -1147,7 +1010,7 @@ ${assistantMessage}
|
|
|
1147
1010
|
break;
|
|
1148
1011
|
}
|
|
1149
1012
|
case "tool": {
|
|
1150
|
-
throw new
|
|
1013
|
+
throw new UnsupportedFunctionalityError3({
|
|
1151
1014
|
functionality: "tool messages"
|
|
1152
1015
|
});
|
|
1153
1016
|
}
|
|
@@ -1183,8 +1046,7 @@ function mapOpenAICompletionLogProbs(logprobs) {
|
|
|
1183
1046
|
// src/openai-completion-language-model.ts
|
|
1184
1047
|
var OpenAICompletionLanguageModel = class {
|
|
1185
1048
|
constructor(modelId, settings, config) {
|
|
1186
|
-
this.specificationVersion = "
|
|
1187
|
-
this.defaultObjectGenerationMode = void 0;
|
|
1049
|
+
this.specificationVersion = "v2";
|
|
1188
1050
|
this.modelId = modelId;
|
|
1189
1051
|
this.settings = settings;
|
|
1190
1052
|
this.config = config;
|
|
@@ -1192,11 +1054,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1192
1054
|
get provider() {
|
|
1193
1055
|
return this.config.provider;
|
|
1194
1056
|
}
|
|
1057
|
+
async getSupportedUrls() {
|
|
1058
|
+
return {
|
|
1059
|
+
// no supported urls for completion models
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1195
1062
|
getArgs({
|
|
1196
|
-
mode,
|
|
1197
1063
|
inputFormat,
|
|
1198
1064
|
prompt,
|
|
1199
|
-
|
|
1065
|
+
maxOutputTokens,
|
|
1200
1066
|
temperature,
|
|
1201
1067
|
topP,
|
|
1202
1068
|
topK,
|
|
@@ -1204,16 +1070,19 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1204
1070
|
presencePenalty,
|
|
1205
1071
|
stopSequences: userStopSequences,
|
|
1206
1072
|
responseFormat,
|
|
1073
|
+
tools,
|
|
1074
|
+
toolChoice,
|
|
1207
1075
|
seed
|
|
1208
1076
|
}) {
|
|
1209
|
-
var _a;
|
|
1210
|
-
const type = mode.type;
|
|
1211
1077
|
const warnings = [];
|
|
1212
1078
|
if (topK != null) {
|
|
1213
|
-
warnings.push({
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
});
|
|
1079
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
|
1080
|
+
}
|
|
1081
|
+
if (tools == null ? void 0 : tools.length) {
|
|
1082
|
+
warnings.push({ type: "unsupported-setting", setting: "tools" });
|
|
1083
|
+
}
|
|
1084
|
+
if (toolChoice != null) {
|
|
1085
|
+
warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
|
|
1217
1086
|
}
|
|
1218
1087
|
if (responseFormat != null && responseFormat.type !== "text") {
|
|
1219
1088
|
warnings.push({
|
|
@@ -1224,56 +1093,30 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1224
1093
|
}
|
|
1225
1094
|
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt, inputFormat });
|
|
1226
1095
|
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1096
|
+
return {
|
|
1097
|
+
args: {
|
|
1098
|
+
// model id:
|
|
1099
|
+
model: this.modelId,
|
|
1100
|
+
// model specific settings:
|
|
1101
|
+
echo: this.settings.echo,
|
|
1102
|
+
logit_bias: this.settings.logitBias,
|
|
1103
|
+
logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
|
|
1104
|
+
suffix: this.settings.suffix,
|
|
1105
|
+
user: this.settings.user,
|
|
1106
|
+
// standardized settings:
|
|
1107
|
+
max_tokens: maxOutputTokens,
|
|
1108
|
+
temperature,
|
|
1109
|
+
top_p: topP,
|
|
1110
|
+
frequency_penalty: frequencyPenalty,
|
|
1111
|
+
presence_penalty: presencePenalty,
|
|
1112
|
+
seed,
|
|
1113
|
+
// prompt:
|
|
1114
|
+
prompt: completionPrompt,
|
|
1115
|
+
// stop sequences:
|
|
1116
|
+
stop: stop.length > 0 ? stop : void 0
|
|
1117
|
+
},
|
|
1118
|
+
warnings
|
|
1247
1119
|
};
|
|
1248
|
-
switch (type) {
|
|
1249
|
-
case "regular": {
|
|
1250
|
-
if ((_a = mode.tools) == null ? void 0 : _a.length) {
|
|
1251
|
-
throw new UnsupportedFunctionalityError5({
|
|
1252
|
-
functionality: "tools"
|
|
1253
|
-
});
|
|
1254
|
-
}
|
|
1255
|
-
if (mode.toolChoice) {
|
|
1256
|
-
throw new UnsupportedFunctionalityError5({
|
|
1257
|
-
functionality: "toolChoice"
|
|
1258
|
-
});
|
|
1259
|
-
}
|
|
1260
|
-
return { args: baseArgs, warnings };
|
|
1261
|
-
}
|
|
1262
|
-
case "object-json": {
|
|
1263
|
-
throw new UnsupportedFunctionalityError5({
|
|
1264
|
-
functionality: "object-json mode"
|
|
1265
|
-
});
|
|
1266
|
-
}
|
|
1267
|
-
case "object-tool": {
|
|
1268
|
-
throw new UnsupportedFunctionalityError5({
|
|
1269
|
-
functionality: "object-tool mode"
|
|
1270
|
-
});
|
|
1271
|
-
}
|
|
1272
|
-
default: {
|
|
1273
|
-
const _exhaustiveCheck = type;
|
|
1274
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1277
1120
|
}
|
|
1278
1121
|
async doGenerate(options) {
|
|
1279
1122
|
const { args, warnings } = this.getArgs(options);
|
|
@@ -1295,21 +1138,22 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1295
1138
|
abortSignal: options.abortSignal,
|
|
1296
1139
|
fetch: this.config.fetch
|
|
1297
1140
|
});
|
|
1298
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1299
1141
|
const choice = response.choices[0];
|
|
1300
1142
|
return {
|
|
1301
|
-
text: choice.text,
|
|
1143
|
+
content: [{ type: "text", text: choice.text }],
|
|
1302
1144
|
usage: {
|
|
1303
|
-
|
|
1304
|
-
|
|
1145
|
+
inputTokens: response.usage.prompt_tokens,
|
|
1146
|
+
outputTokens: response.usage.completion_tokens
|
|
1305
1147
|
},
|
|
1306
1148
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
1307
1149
|
logprobs: mapOpenAICompletionLogProbs(choice.logprobs),
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1150
|
+
request: { body: args },
|
|
1151
|
+
response: {
|
|
1152
|
+
...getResponseMetadata(response),
|
|
1153
|
+
headers: responseHeaders,
|
|
1154
|
+
body: rawResponse
|
|
1155
|
+
},
|
|
1156
|
+
warnings
|
|
1313
1157
|
};
|
|
1314
1158
|
}
|
|
1315
1159
|
async doStream(options) {
|
|
@@ -1334,17 +1178,19 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1334
1178
|
abortSignal: options.abortSignal,
|
|
1335
1179
|
fetch: this.config.fetch
|
|
1336
1180
|
});
|
|
1337
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1338
1181
|
let finishReason = "unknown";
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1182
|
+
const usage = {
|
|
1183
|
+
inputTokens: void 0,
|
|
1184
|
+
outputTokens: void 0
|
|
1342
1185
|
};
|
|
1343
1186
|
let logprobs;
|
|
1344
1187
|
let isFirstChunk = true;
|
|
1345
1188
|
return {
|
|
1346
1189
|
stream: response.pipeThrough(
|
|
1347
1190
|
new TransformStream({
|
|
1191
|
+
start(controller) {
|
|
1192
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1193
|
+
},
|
|
1348
1194
|
transform(chunk, controller) {
|
|
1349
1195
|
if (!chunk.success) {
|
|
1350
1196
|
finishReason = "error";
|
|
@@ -1365,10 +1211,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1365
1211
|
});
|
|
1366
1212
|
}
|
|
1367
1213
|
if (value.usage != null) {
|
|
1368
|
-
usage =
|
|
1369
|
-
|
|
1370
|
-
completionTokens: value.usage.completion_tokens
|
|
1371
|
-
};
|
|
1214
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
1215
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
1372
1216
|
}
|
|
1373
1217
|
const choice = value.choices[0];
|
|
1374
1218
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -1376,8 +1220,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1376
1220
|
}
|
|
1377
1221
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
1378
1222
|
controller.enqueue({
|
|
1379
|
-
type: "text
|
|
1380
|
-
|
|
1223
|
+
type: "text",
|
|
1224
|
+
text: choice.text
|
|
1381
1225
|
});
|
|
1382
1226
|
}
|
|
1383
1227
|
const mappedLogprobs = mapOpenAICompletionLogProbs(
|
|
@@ -1398,53 +1242,51 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1398
1242
|
}
|
|
1399
1243
|
})
|
|
1400
1244
|
),
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
warnings,
|
|
1404
|
-
request: { body: JSON.stringify(body) }
|
|
1245
|
+
request: { body },
|
|
1246
|
+
response: { headers: responseHeaders }
|
|
1405
1247
|
};
|
|
1406
1248
|
}
|
|
1407
1249
|
};
|
|
1408
|
-
var openaiCompletionResponseSchema =
|
|
1409
|
-
id:
|
|
1410
|
-
created:
|
|
1411
|
-
model:
|
|
1412
|
-
choices:
|
|
1413
|
-
|
|
1414
|
-
text:
|
|
1415
|
-
finish_reason:
|
|
1416
|
-
logprobs:
|
|
1417
|
-
tokens:
|
|
1418
|
-
token_logprobs:
|
|
1419
|
-
top_logprobs:
|
|
1250
|
+
var openaiCompletionResponseSchema = z4.object({
|
|
1251
|
+
id: z4.string().nullish(),
|
|
1252
|
+
created: z4.number().nullish(),
|
|
1253
|
+
model: z4.string().nullish(),
|
|
1254
|
+
choices: z4.array(
|
|
1255
|
+
z4.object({
|
|
1256
|
+
text: z4.string(),
|
|
1257
|
+
finish_reason: z4.string(),
|
|
1258
|
+
logprobs: z4.object({
|
|
1259
|
+
tokens: z4.array(z4.string()),
|
|
1260
|
+
token_logprobs: z4.array(z4.number()),
|
|
1261
|
+
top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
|
|
1420
1262
|
}).nullish()
|
|
1421
1263
|
})
|
|
1422
1264
|
),
|
|
1423
|
-
usage:
|
|
1424
|
-
prompt_tokens:
|
|
1425
|
-
completion_tokens:
|
|
1265
|
+
usage: z4.object({
|
|
1266
|
+
prompt_tokens: z4.number(),
|
|
1267
|
+
completion_tokens: z4.number()
|
|
1426
1268
|
})
|
|
1427
1269
|
});
|
|
1428
|
-
var openaiCompletionChunkSchema =
|
|
1429
|
-
|
|
1430
|
-
id:
|
|
1431
|
-
created:
|
|
1432
|
-
model:
|
|
1433
|
-
choices:
|
|
1434
|
-
|
|
1435
|
-
text:
|
|
1436
|
-
finish_reason:
|
|
1437
|
-
index:
|
|
1438
|
-
logprobs:
|
|
1439
|
-
tokens:
|
|
1440
|
-
token_logprobs:
|
|
1441
|
-
top_logprobs:
|
|
1270
|
+
var openaiCompletionChunkSchema = z4.union([
|
|
1271
|
+
z4.object({
|
|
1272
|
+
id: z4.string().nullish(),
|
|
1273
|
+
created: z4.number().nullish(),
|
|
1274
|
+
model: z4.string().nullish(),
|
|
1275
|
+
choices: z4.array(
|
|
1276
|
+
z4.object({
|
|
1277
|
+
text: z4.string(),
|
|
1278
|
+
finish_reason: z4.string().nullish(),
|
|
1279
|
+
index: z4.number(),
|
|
1280
|
+
logprobs: z4.object({
|
|
1281
|
+
tokens: z4.array(z4.string()),
|
|
1282
|
+
token_logprobs: z4.array(z4.number()),
|
|
1283
|
+
top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
|
|
1442
1284
|
}).nullish()
|
|
1443
1285
|
})
|
|
1444
1286
|
),
|
|
1445
|
-
usage:
|
|
1446
|
-
prompt_tokens:
|
|
1447
|
-
completion_tokens:
|
|
1287
|
+
usage: z4.object({
|
|
1288
|
+
prompt_tokens: z4.number(),
|
|
1289
|
+
completion_tokens: z4.number()
|
|
1448
1290
|
}).nullish()
|
|
1449
1291
|
}),
|
|
1450
1292
|
openaiErrorDataSchema
|
|
@@ -1457,12 +1299,30 @@ import {
|
|
|
1457
1299
|
import {
|
|
1458
1300
|
combineHeaders as combineHeaders3,
|
|
1459
1301
|
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1302
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1460
1303
|
postJsonToApi as postJsonToApi3
|
|
1461
1304
|
} from "@ai-sdk/provider-utils";
|
|
1462
|
-
import { z as
|
|
1305
|
+
import { z as z6 } from "zod";
|
|
1306
|
+
|
|
1307
|
+
// src/openai-embedding-options.ts
|
|
1308
|
+
import { z as z5 } from "zod";
|
|
1309
|
+
var openaiEmbeddingProviderOptions = z5.object({
|
|
1310
|
+
/**
|
|
1311
|
+
The number of dimensions the resulting output embeddings should have.
|
|
1312
|
+
Only supported in text-embedding-3 and later models.
|
|
1313
|
+
*/
|
|
1314
|
+
dimensions: z5.number().optional(),
|
|
1315
|
+
/**
|
|
1316
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
|
1317
|
+
monitor and detect abuse. Learn more.
|
|
1318
|
+
*/
|
|
1319
|
+
user: z5.string().optional()
|
|
1320
|
+
});
|
|
1321
|
+
|
|
1322
|
+
// src/openai-embedding-model.ts
|
|
1463
1323
|
var OpenAIEmbeddingModel = class {
|
|
1464
1324
|
constructor(modelId, settings, config) {
|
|
1465
|
-
this.specificationVersion = "
|
|
1325
|
+
this.specificationVersion = "v2";
|
|
1466
1326
|
this.modelId = modelId;
|
|
1467
1327
|
this.settings = settings;
|
|
1468
1328
|
this.config = config;
|
|
@@ -1481,8 +1341,10 @@ var OpenAIEmbeddingModel = class {
|
|
|
1481
1341
|
async doEmbed({
|
|
1482
1342
|
values,
|
|
1483
1343
|
headers,
|
|
1484
|
-
abortSignal
|
|
1344
|
+
abortSignal,
|
|
1345
|
+
providerOptions
|
|
1485
1346
|
}) {
|
|
1347
|
+
var _a;
|
|
1486
1348
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1487
1349
|
throw new TooManyEmbeddingValuesForCallError({
|
|
1488
1350
|
provider: this.provider,
|
|
@@ -1491,7 +1353,16 @@ var OpenAIEmbeddingModel = class {
|
|
|
1491
1353
|
values
|
|
1492
1354
|
});
|
|
1493
1355
|
}
|
|
1494
|
-
const
|
|
1356
|
+
const openaiOptions = (_a = parseProviderOptions2({
|
|
1357
|
+
provider: "openai",
|
|
1358
|
+
providerOptions,
|
|
1359
|
+
schema: openaiEmbeddingProviderOptions
|
|
1360
|
+
})) != null ? _a : {};
|
|
1361
|
+
const {
|
|
1362
|
+
responseHeaders,
|
|
1363
|
+
value: response,
|
|
1364
|
+
rawValue
|
|
1365
|
+
} = await postJsonToApi3({
|
|
1495
1366
|
url: this.config.url({
|
|
1496
1367
|
path: "/embeddings",
|
|
1497
1368
|
modelId: this.modelId
|
|
@@ -1501,8 +1372,8 @@ var OpenAIEmbeddingModel = class {
|
|
|
1501
1372
|
model: this.modelId,
|
|
1502
1373
|
input: values,
|
|
1503
1374
|
encoding_format: "float",
|
|
1504
|
-
dimensions:
|
|
1505
|
-
user:
|
|
1375
|
+
dimensions: openaiOptions.dimensions,
|
|
1376
|
+
user: openaiOptions.user
|
|
1506
1377
|
},
|
|
1507
1378
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
1508
1379
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
@@ -1514,13 +1385,13 @@ var OpenAIEmbeddingModel = class {
|
|
|
1514
1385
|
return {
|
|
1515
1386
|
embeddings: response.data.map((item) => item.embedding),
|
|
1516
1387
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1517
|
-
|
|
1388
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
1518
1389
|
};
|
|
1519
1390
|
}
|
|
1520
1391
|
};
|
|
1521
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1522
|
-
data:
|
|
1523
|
-
usage:
|
|
1392
|
+
var openaiTextEmbeddingResponseSchema = z6.object({
|
|
1393
|
+
data: z6.array(z6.object({ embedding: z6.array(z6.number()) })),
|
|
1394
|
+
usage: z6.object({ prompt_tokens: z6.number() }).nullish()
|
|
1524
1395
|
});
|
|
1525
1396
|
|
|
1526
1397
|
// src/openai-image-model.ts
|
|
@@ -1529,7 +1400,7 @@ import {
|
|
|
1529
1400
|
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1530
1401
|
postJsonToApi as postJsonToApi4
|
|
1531
1402
|
} from "@ai-sdk/provider-utils";
|
|
1532
|
-
import { z as
|
|
1403
|
+
import { z as z7 } from "zod";
|
|
1533
1404
|
|
|
1534
1405
|
// src/openai-image-settings.ts
|
|
1535
1406
|
var modelMaxImagesPerCall = {
|
|
@@ -1607,26 +1478,221 @@ var OpenAIImageModel = class {
|
|
|
1607
1478
|
};
|
|
1608
1479
|
}
|
|
1609
1480
|
};
|
|
1610
|
-
var openaiImageResponseSchema =
|
|
1611
|
-
data:
|
|
1481
|
+
var openaiImageResponseSchema = z7.object({
|
|
1482
|
+
data: z7.array(z7.object({ b64_json: z7.string() }))
|
|
1612
1483
|
});
|
|
1613
1484
|
|
|
1614
|
-
// src/
|
|
1485
|
+
// src/openai-tools.ts
|
|
1486
|
+
import { z as z8 } from "zod";
|
|
1487
|
+
var WebSearchPreviewParameters = z8.object({});
|
|
1488
|
+
function webSearchPreviewTool({
|
|
1489
|
+
searchContextSize,
|
|
1490
|
+
userLocation
|
|
1491
|
+
} = {}) {
|
|
1492
|
+
return {
|
|
1493
|
+
type: "provider-defined",
|
|
1494
|
+
id: "openai.web_search_preview",
|
|
1495
|
+
args: {
|
|
1496
|
+
searchContextSize,
|
|
1497
|
+
userLocation
|
|
1498
|
+
},
|
|
1499
|
+
parameters: WebSearchPreviewParameters
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
var openaiTools = {
|
|
1503
|
+
webSearchPreview: webSearchPreviewTool
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
// src/openai-transcription-model.ts
|
|
1615
1507
|
import {
|
|
1616
1508
|
combineHeaders as combineHeaders5,
|
|
1617
|
-
|
|
1509
|
+
convertBase64ToUint8Array,
|
|
1618
1510
|
createJsonResponseHandler as createJsonResponseHandler5,
|
|
1511
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1512
|
+
postFormDataToApi
|
|
1513
|
+
} from "@ai-sdk/provider-utils";
|
|
1514
|
+
import { z as z9 } from "zod";
|
|
1515
|
+
var openAIProviderOptionsSchema = z9.object({
|
|
1516
|
+
include: z9.array(z9.string()).nullish(),
|
|
1517
|
+
language: z9.string().nullish(),
|
|
1518
|
+
prompt: z9.string().nullish(),
|
|
1519
|
+
temperature: z9.number().min(0).max(1).nullish().default(0),
|
|
1520
|
+
timestampGranularities: z9.array(z9.enum(["word", "segment"])).nullish().default(["segment"])
|
|
1521
|
+
});
|
|
1522
|
+
var languageMap = {
|
|
1523
|
+
afrikaans: "af",
|
|
1524
|
+
arabic: "ar",
|
|
1525
|
+
armenian: "hy",
|
|
1526
|
+
azerbaijani: "az",
|
|
1527
|
+
belarusian: "be",
|
|
1528
|
+
bosnian: "bs",
|
|
1529
|
+
bulgarian: "bg",
|
|
1530
|
+
catalan: "ca",
|
|
1531
|
+
chinese: "zh",
|
|
1532
|
+
croatian: "hr",
|
|
1533
|
+
czech: "cs",
|
|
1534
|
+
danish: "da",
|
|
1535
|
+
dutch: "nl",
|
|
1536
|
+
english: "en",
|
|
1537
|
+
estonian: "et",
|
|
1538
|
+
finnish: "fi",
|
|
1539
|
+
french: "fr",
|
|
1540
|
+
galician: "gl",
|
|
1541
|
+
german: "de",
|
|
1542
|
+
greek: "el",
|
|
1543
|
+
hebrew: "he",
|
|
1544
|
+
hindi: "hi",
|
|
1545
|
+
hungarian: "hu",
|
|
1546
|
+
icelandic: "is",
|
|
1547
|
+
indonesian: "id",
|
|
1548
|
+
italian: "it",
|
|
1549
|
+
japanese: "ja",
|
|
1550
|
+
kannada: "kn",
|
|
1551
|
+
kazakh: "kk",
|
|
1552
|
+
korean: "ko",
|
|
1553
|
+
latvian: "lv",
|
|
1554
|
+
lithuanian: "lt",
|
|
1555
|
+
macedonian: "mk",
|
|
1556
|
+
malay: "ms",
|
|
1557
|
+
marathi: "mr",
|
|
1558
|
+
maori: "mi",
|
|
1559
|
+
nepali: "ne",
|
|
1560
|
+
norwegian: "no",
|
|
1561
|
+
persian: "fa",
|
|
1562
|
+
polish: "pl",
|
|
1563
|
+
portuguese: "pt",
|
|
1564
|
+
romanian: "ro",
|
|
1565
|
+
russian: "ru",
|
|
1566
|
+
serbian: "sr",
|
|
1567
|
+
slovak: "sk",
|
|
1568
|
+
slovenian: "sl",
|
|
1569
|
+
spanish: "es",
|
|
1570
|
+
swahili: "sw",
|
|
1571
|
+
swedish: "sv",
|
|
1572
|
+
tagalog: "tl",
|
|
1573
|
+
tamil: "ta",
|
|
1574
|
+
thai: "th",
|
|
1575
|
+
turkish: "tr",
|
|
1576
|
+
ukrainian: "uk",
|
|
1577
|
+
urdu: "ur",
|
|
1578
|
+
vietnamese: "vi",
|
|
1579
|
+
welsh: "cy"
|
|
1580
|
+
};
|
|
1581
|
+
var OpenAITranscriptionModel = class {
|
|
1582
|
+
constructor(modelId, config) {
|
|
1583
|
+
this.modelId = modelId;
|
|
1584
|
+
this.config = config;
|
|
1585
|
+
this.specificationVersion = "v1";
|
|
1586
|
+
}
|
|
1587
|
+
get provider() {
|
|
1588
|
+
return this.config.provider;
|
|
1589
|
+
}
|
|
1590
|
+
getArgs({
|
|
1591
|
+
audio,
|
|
1592
|
+
mediaType,
|
|
1593
|
+
providerOptions
|
|
1594
|
+
}) {
|
|
1595
|
+
var _a, _b, _c, _d, _e;
|
|
1596
|
+
const warnings = [];
|
|
1597
|
+
const openAIOptions = parseProviderOptions3({
|
|
1598
|
+
provider: "openai",
|
|
1599
|
+
providerOptions,
|
|
1600
|
+
schema: openAIProviderOptionsSchema
|
|
1601
|
+
});
|
|
1602
|
+
const formData = new FormData();
|
|
1603
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
1604
|
+
formData.append("model", this.modelId);
|
|
1605
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
1606
|
+
if (openAIOptions) {
|
|
1607
|
+
const transcriptionModelOptions = {
|
|
1608
|
+
include: (_a = openAIOptions.include) != null ? _a : void 0,
|
|
1609
|
+
language: (_b = openAIOptions.language) != null ? _b : void 0,
|
|
1610
|
+
prompt: (_c = openAIOptions.prompt) != null ? _c : void 0,
|
|
1611
|
+
temperature: (_d = openAIOptions.temperature) != null ? _d : void 0,
|
|
1612
|
+
timestamp_granularities: (_e = openAIOptions.timestampGranularities) != null ? _e : void 0
|
|
1613
|
+
};
|
|
1614
|
+
for (const key in transcriptionModelOptions) {
|
|
1615
|
+
const value = transcriptionModelOptions[key];
|
|
1616
|
+
if (value !== void 0) {
|
|
1617
|
+
formData.append(key, String(value));
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
return {
|
|
1622
|
+
formData,
|
|
1623
|
+
warnings
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1626
|
+
async doGenerate(options) {
|
|
1627
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1628
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1629
|
+
const { formData, warnings } = this.getArgs(options);
|
|
1630
|
+
const {
|
|
1631
|
+
value: response,
|
|
1632
|
+
responseHeaders,
|
|
1633
|
+
rawValue: rawResponse
|
|
1634
|
+
} = await postFormDataToApi({
|
|
1635
|
+
url: this.config.url({
|
|
1636
|
+
path: "/audio/transcriptions",
|
|
1637
|
+
modelId: this.modelId
|
|
1638
|
+
}),
|
|
1639
|
+
headers: combineHeaders5(this.config.headers(), options.headers),
|
|
1640
|
+
formData,
|
|
1641
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
1642
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
1643
|
+
openaiTranscriptionResponseSchema
|
|
1644
|
+
),
|
|
1645
|
+
abortSignal: options.abortSignal,
|
|
1646
|
+
fetch: this.config.fetch
|
|
1647
|
+
});
|
|
1648
|
+
const language = response.language != null && response.language in languageMap ? languageMap[response.language] : void 0;
|
|
1649
|
+
return {
|
|
1650
|
+
text: response.text,
|
|
1651
|
+
segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => ({
|
|
1652
|
+
text: word.word,
|
|
1653
|
+
startSecond: word.start,
|
|
1654
|
+
endSecond: word.end
|
|
1655
|
+
}))) != null ? _e : [],
|
|
1656
|
+
language,
|
|
1657
|
+
durationInSeconds: (_f = response.duration) != null ? _f : void 0,
|
|
1658
|
+
warnings,
|
|
1659
|
+
response: {
|
|
1660
|
+
timestamp: currentDate,
|
|
1661
|
+
modelId: this.modelId,
|
|
1662
|
+
headers: responseHeaders,
|
|
1663
|
+
body: rawResponse
|
|
1664
|
+
}
|
|
1665
|
+
};
|
|
1666
|
+
}
|
|
1667
|
+
};
|
|
1668
|
+
var openaiTranscriptionResponseSchema = z9.object({
|
|
1669
|
+
text: z9.string(),
|
|
1670
|
+
language: z9.string().nullish(),
|
|
1671
|
+
duration: z9.number().nullish(),
|
|
1672
|
+
words: z9.array(
|
|
1673
|
+
z9.object({
|
|
1674
|
+
word: z9.string(),
|
|
1675
|
+
start: z9.number(),
|
|
1676
|
+
end: z9.number()
|
|
1677
|
+
})
|
|
1678
|
+
).nullish()
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1681
|
+
// src/responses/openai-responses-language-model.ts
|
|
1682
|
+
import {
|
|
1683
|
+
combineHeaders as combineHeaders6,
|
|
1684
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler3,
|
|
1685
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
1619
1686
|
generateId as generateId2,
|
|
1620
|
-
parseProviderOptions,
|
|
1687
|
+
parseProviderOptions as parseProviderOptions4,
|
|
1621
1688
|
postJsonToApi as postJsonToApi5
|
|
1622
1689
|
} from "@ai-sdk/provider-utils";
|
|
1623
|
-
import { z as
|
|
1690
|
+
import { z as z10 } from "zod";
|
|
1624
1691
|
|
|
1625
1692
|
// src/responses/convert-to-openai-responses-messages.ts
|
|
1626
1693
|
import {
|
|
1627
|
-
UnsupportedFunctionalityError as
|
|
1694
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1628
1695
|
} from "@ai-sdk/provider";
|
|
1629
|
-
import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
|
|
1630
1696
|
function convertToOpenAIResponsesMessages({
|
|
1631
1697
|
prompt,
|
|
1632
1698
|
systemMessageMode
|
|
@@ -1665,38 +1731,35 @@ function convertToOpenAIResponsesMessages({
|
|
|
1665
1731
|
messages.push({
|
|
1666
1732
|
role: "user",
|
|
1667
1733
|
content: content.map((part, index) => {
|
|
1668
|
-
var _a, _b, _c
|
|
1734
|
+
var _a, _b, _c;
|
|
1669
1735
|
switch (part.type) {
|
|
1670
1736
|
case "text": {
|
|
1671
1737
|
return { type: "input_text", text: part.text };
|
|
1672
1738
|
}
|
|
1673
|
-
case "image": {
|
|
1674
|
-
return {
|
|
1675
|
-
type: "input_image",
|
|
1676
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase642(part.image)}`,
|
|
1677
|
-
// OpenAI specific extension: image detail
|
|
1678
|
-
detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
1679
|
-
};
|
|
1680
|
-
}
|
|
1681
1739
|
case "file": {
|
|
1682
|
-
if (part.
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
}
|
|
1695
|
-
default: {
|
|
1696
|
-
throw new UnsupportedFunctionalityError6({
|
|
1697
|
-
functionality: "Only PDF files are supported in user messages"
|
|
1740
|
+
if (part.mediaType.startsWith("image/")) {
|
|
1741
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
1742
|
+
return {
|
|
1743
|
+
type: "input_image",
|
|
1744
|
+
image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`,
|
|
1745
|
+
// OpenAI specific extension: image detail
|
|
1746
|
+
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
1747
|
+
};
|
|
1748
|
+
} else if (part.mediaType === "application/pdf") {
|
|
1749
|
+
if (part.data instanceof URL) {
|
|
1750
|
+
throw new UnsupportedFunctionalityError4({
|
|
1751
|
+
functionality: "PDF file parts with URLs"
|
|
1698
1752
|
});
|
|
1699
1753
|
}
|
|
1754
|
+
return {
|
|
1755
|
+
type: "input_file",
|
|
1756
|
+
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
|
|
1757
|
+
file_data: `data:application/pdf;base64,${part.data}`
|
|
1758
|
+
};
|
|
1759
|
+
} else {
|
|
1760
|
+
throw new UnsupportedFunctionalityError4({
|
|
1761
|
+
functionality: `file part media type ${part.mediaType}`
|
|
1762
|
+
});
|
|
1700
1763
|
}
|
|
1701
1764
|
}
|
|
1702
1765
|
}
|
|
@@ -1766,19 +1829,18 @@ function mapOpenAIResponseFinishReason({
|
|
|
1766
1829
|
|
|
1767
1830
|
// src/responses/openai-responses-prepare-tools.ts
|
|
1768
1831
|
import {
|
|
1769
|
-
UnsupportedFunctionalityError as
|
|
1832
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1770
1833
|
} from "@ai-sdk/provider";
|
|
1771
1834
|
function prepareResponsesTools({
|
|
1772
|
-
|
|
1835
|
+
tools,
|
|
1836
|
+
toolChoice,
|
|
1773
1837
|
strict
|
|
1774
1838
|
}) {
|
|
1775
|
-
|
|
1776
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
1839
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1777
1840
|
const toolWarnings = [];
|
|
1778
1841
|
if (tools == null) {
|
|
1779
|
-
return { tools: void 0,
|
|
1842
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
1780
1843
|
}
|
|
1781
|
-
const toolChoice = mode.toolChoice;
|
|
1782
1844
|
const openaiTools2 = [];
|
|
1783
1845
|
for (const tool of tools) {
|
|
1784
1846
|
switch (tool.type) {
|
|
@@ -1811,37 +1873,24 @@ function prepareResponsesTools({
|
|
|
1811
1873
|
}
|
|
1812
1874
|
}
|
|
1813
1875
|
if (toolChoice == null) {
|
|
1814
|
-
return { tools: openaiTools2,
|
|
1876
|
+
return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
|
|
1815
1877
|
}
|
|
1816
1878
|
const type = toolChoice.type;
|
|
1817
1879
|
switch (type) {
|
|
1818
1880
|
case "auto":
|
|
1819
1881
|
case "none":
|
|
1820
1882
|
case "required":
|
|
1821
|
-
return { tools: openaiTools2,
|
|
1822
|
-
case "tool":
|
|
1823
|
-
if (toolChoice.toolName === "web_search_preview") {
|
|
1824
|
-
return {
|
|
1825
|
-
tools: openaiTools2,
|
|
1826
|
-
tool_choice: {
|
|
1827
|
-
type: "web_search_preview"
|
|
1828
|
-
},
|
|
1829
|
-
toolWarnings
|
|
1830
|
-
};
|
|
1831
|
-
}
|
|
1883
|
+
return { tools: openaiTools2, toolChoice: type, toolWarnings };
|
|
1884
|
+
case "tool":
|
|
1832
1885
|
return {
|
|
1833
1886
|
tools: openaiTools2,
|
|
1834
|
-
|
|
1835
|
-
type: "function",
|
|
1836
|
-
name: toolChoice.toolName
|
|
1837
|
-
},
|
|
1887
|
+
toolChoice: toolChoice.toolName === "web_search_preview" ? { type: "web_search_preview" } : { type: "function", name: toolChoice.toolName },
|
|
1838
1888
|
toolWarnings
|
|
1839
1889
|
};
|
|
1840
|
-
}
|
|
1841
1890
|
default: {
|
|
1842
1891
|
const _exhaustiveCheck = type;
|
|
1843
|
-
throw new
|
|
1844
|
-
functionality: `
|
|
1892
|
+
throw new UnsupportedFunctionalityError5({
|
|
1893
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1845
1894
|
});
|
|
1846
1895
|
}
|
|
1847
1896
|
}
|
|
@@ -1850,17 +1899,20 @@ function prepareResponsesTools({
|
|
|
1850
1899
|
// src/responses/openai-responses-language-model.ts
|
|
1851
1900
|
var OpenAIResponsesLanguageModel = class {
|
|
1852
1901
|
constructor(modelId, config) {
|
|
1853
|
-
this.specificationVersion = "
|
|
1854
|
-
this.defaultObjectGenerationMode = "json";
|
|
1902
|
+
this.specificationVersion = "v2";
|
|
1855
1903
|
this.modelId = modelId;
|
|
1856
1904
|
this.config = config;
|
|
1857
1905
|
}
|
|
1906
|
+
async getSupportedUrls() {
|
|
1907
|
+
return {
|
|
1908
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1909
|
+
};
|
|
1910
|
+
}
|
|
1858
1911
|
get provider() {
|
|
1859
1912
|
return this.config.provider;
|
|
1860
1913
|
}
|
|
1861
1914
|
getArgs({
|
|
1862
|
-
|
|
1863
|
-
maxTokens,
|
|
1915
|
+
maxOutputTokens,
|
|
1864
1916
|
temperature,
|
|
1865
1917
|
stopSequences,
|
|
1866
1918
|
topP,
|
|
@@ -1869,24 +1921,19 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1869
1921
|
frequencyPenalty,
|
|
1870
1922
|
seed,
|
|
1871
1923
|
prompt,
|
|
1872
|
-
|
|
1924
|
+
providerOptions,
|
|
1925
|
+
tools,
|
|
1926
|
+
toolChoice,
|
|
1873
1927
|
responseFormat
|
|
1874
1928
|
}) {
|
|
1875
|
-
var _a, _b
|
|
1929
|
+
var _a, _b;
|
|
1876
1930
|
const warnings = [];
|
|
1877
1931
|
const modelConfig = getResponsesModelConfig(this.modelId);
|
|
1878
|
-
const type = mode.type;
|
|
1879
1932
|
if (topK != null) {
|
|
1880
|
-
warnings.push({
|
|
1881
|
-
type: "unsupported-setting",
|
|
1882
|
-
setting: "topK"
|
|
1883
|
-
});
|
|
1933
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
|
1884
1934
|
}
|
|
1885
1935
|
if (seed != null) {
|
|
1886
|
-
warnings.push({
|
|
1887
|
-
type: "unsupported-setting",
|
|
1888
|
-
setting: "seed"
|
|
1889
|
-
});
|
|
1936
|
+
warnings.push({ type: "unsupported-setting", setting: "seed" });
|
|
1890
1937
|
}
|
|
1891
1938
|
if (presencePenalty != null) {
|
|
1892
1939
|
warnings.push({
|
|
@@ -1901,19 +1948,16 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1901
1948
|
});
|
|
1902
1949
|
}
|
|
1903
1950
|
if (stopSequences != null) {
|
|
1904
|
-
warnings.push({
|
|
1905
|
-
type: "unsupported-setting",
|
|
1906
|
-
setting: "stopSequences"
|
|
1907
|
-
});
|
|
1951
|
+
warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
|
|
1908
1952
|
}
|
|
1909
1953
|
const { messages, warnings: messageWarnings } = convertToOpenAIResponsesMessages({
|
|
1910
1954
|
prompt,
|
|
1911
1955
|
systemMessageMode: modelConfig.systemMessageMode
|
|
1912
1956
|
});
|
|
1913
1957
|
warnings.push(...messageWarnings);
|
|
1914
|
-
const openaiOptions =
|
|
1958
|
+
const openaiOptions = parseProviderOptions4({
|
|
1915
1959
|
provider: "openai",
|
|
1916
|
-
providerOptions
|
|
1960
|
+
providerOptions,
|
|
1917
1961
|
schema: openaiResponsesProviderOptionsSchema
|
|
1918
1962
|
});
|
|
1919
1963
|
const isStrict = (_a = openaiOptions == null ? void 0 : openaiOptions.strictSchemas) != null ? _a : true;
|
|
@@ -1922,7 +1966,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1922
1966
|
input: messages,
|
|
1923
1967
|
temperature,
|
|
1924
1968
|
top_p: topP,
|
|
1925
|
-
max_output_tokens:
|
|
1969
|
+
max_output_tokens: maxOutputTokens,
|
|
1926
1970
|
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
1927
1971
|
text: {
|
|
1928
1972
|
format: responseFormat.schema != null ? {
|
|
@@ -1967,65 +2011,26 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1967
2011
|
});
|
|
1968
2012
|
}
|
|
1969
2013
|
}
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
return {
|
|
1988
|
-
args: {
|
|
1989
|
-
...baseArgs,
|
|
1990
|
-
text: {
|
|
1991
|
-
format: mode.schema != null ? {
|
|
1992
|
-
type: "json_schema",
|
|
1993
|
-
strict: isStrict,
|
|
1994
|
-
name: (_c = mode.name) != null ? _c : "response",
|
|
1995
|
-
description: mode.description,
|
|
1996
|
-
schema: mode.schema
|
|
1997
|
-
} : { type: "json_object" }
|
|
1998
|
-
}
|
|
1999
|
-
},
|
|
2000
|
-
warnings
|
|
2001
|
-
};
|
|
2002
|
-
}
|
|
2003
|
-
case "object-tool": {
|
|
2004
|
-
return {
|
|
2005
|
-
args: {
|
|
2006
|
-
...baseArgs,
|
|
2007
|
-
tool_choice: { type: "function", name: mode.tool.name },
|
|
2008
|
-
tools: [
|
|
2009
|
-
{
|
|
2010
|
-
type: "function",
|
|
2011
|
-
name: mode.tool.name,
|
|
2012
|
-
description: mode.tool.description,
|
|
2013
|
-
parameters: mode.tool.parameters,
|
|
2014
|
-
strict: isStrict
|
|
2015
|
-
}
|
|
2016
|
-
]
|
|
2017
|
-
},
|
|
2018
|
-
warnings
|
|
2019
|
-
};
|
|
2020
|
-
}
|
|
2021
|
-
default: {
|
|
2022
|
-
const _exhaustiveCheck = type;
|
|
2023
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
2024
|
-
}
|
|
2025
|
-
}
|
|
2014
|
+
const {
|
|
2015
|
+
tools: openaiTools2,
|
|
2016
|
+
toolChoice: openaiToolChoice,
|
|
2017
|
+
toolWarnings
|
|
2018
|
+
} = prepareResponsesTools({
|
|
2019
|
+
tools,
|
|
2020
|
+
toolChoice,
|
|
2021
|
+
strict: isStrict
|
|
2022
|
+
});
|
|
2023
|
+
return {
|
|
2024
|
+
args: {
|
|
2025
|
+
...baseArgs,
|
|
2026
|
+
tools: openaiTools2,
|
|
2027
|
+
tool_choice: openaiToolChoice
|
|
2028
|
+
},
|
|
2029
|
+
warnings: [...warnings, ...toolWarnings]
|
|
2030
|
+
};
|
|
2026
2031
|
}
|
|
2027
2032
|
async doGenerate(options) {
|
|
2028
|
-
var _a, _b, _c, _d, _e;
|
|
2033
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2029
2034
|
const { args: body, warnings } = this.getArgs(options);
|
|
2030
2035
|
const {
|
|
2031
2036
|
responseHeaders,
|
|
@@ -2036,109 +2041,115 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2036
2041
|
path: "/responses",
|
|
2037
2042
|
modelId: this.modelId
|
|
2038
2043
|
}),
|
|
2039
|
-
headers:
|
|
2044
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
2040
2045
|
body,
|
|
2041
2046
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
2042
|
-
successfulResponseHandler:
|
|
2043
|
-
|
|
2044
|
-
id:
|
|
2045
|
-
created_at:
|
|
2046
|
-
model:
|
|
2047
|
-
output:
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
type:
|
|
2051
|
-
role:
|
|
2052
|
-
content:
|
|
2053
|
-
|
|
2054
|
-
type:
|
|
2055
|
-
text:
|
|
2056
|
-
annotations:
|
|
2057
|
-
|
|
2058
|
-
type:
|
|
2059
|
-
start_index:
|
|
2060
|
-
end_index:
|
|
2061
|
-
url:
|
|
2062
|
-
title:
|
|
2047
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
2048
|
+
z10.object({
|
|
2049
|
+
id: z10.string(),
|
|
2050
|
+
created_at: z10.number(),
|
|
2051
|
+
model: z10.string(),
|
|
2052
|
+
output: z10.array(
|
|
2053
|
+
z10.discriminatedUnion("type", [
|
|
2054
|
+
z10.object({
|
|
2055
|
+
type: z10.literal("message"),
|
|
2056
|
+
role: z10.literal("assistant"),
|
|
2057
|
+
content: z10.array(
|
|
2058
|
+
z10.object({
|
|
2059
|
+
type: z10.literal("output_text"),
|
|
2060
|
+
text: z10.string(),
|
|
2061
|
+
annotations: z10.array(
|
|
2062
|
+
z10.object({
|
|
2063
|
+
type: z10.literal("url_citation"),
|
|
2064
|
+
start_index: z10.number(),
|
|
2065
|
+
end_index: z10.number(),
|
|
2066
|
+
url: z10.string(),
|
|
2067
|
+
title: z10.string()
|
|
2063
2068
|
})
|
|
2064
2069
|
)
|
|
2065
2070
|
})
|
|
2066
2071
|
)
|
|
2067
2072
|
}),
|
|
2068
|
-
|
|
2069
|
-
type:
|
|
2070
|
-
call_id:
|
|
2071
|
-
name:
|
|
2072
|
-
arguments:
|
|
2073
|
+
z10.object({
|
|
2074
|
+
type: z10.literal("function_call"),
|
|
2075
|
+
call_id: z10.string(),
|
|
2076
|
+
name: z10.string(),
|
|
2077
|
+
arguments: z10.string()
|
|
2073
2078
|
}),
|
|
2074
|
-
|
|
2075
|
-
type:
|
|
2079
|
+
z10.object({
|
|
2080
|
+
type: z10.literal("web_search_call")
|
|
2076
2081
|
}),
|
|
2077
|
-
|
|
2078
|
-
type:
|
|
2082
|
+
z10.object({
|
|
2083
|
+
type: z10.literal("computer_call")
|
|
2079
2084
|
}),
|
|
2080
|
-
|
|
2081
|
-
type:
|
|
2085
|
+
z10.object({
|
|
2086
|
+
type: z10.literal("reasoning")
|
|
2082
2087
|
})
|
|
2083
2088
|
])
|
|
2084
2089
|
),
|
|
2085
|
-
incomplete_details:
|
|
2090
|
+
incomplete_details: z10.object({ reason: z10.string() }).nullable(),
|
|
2086
2091
|
usage: usageSchema
|
|
2087
2092
|
})
|
|
2088
2093
|
),
|
|
2089
2094
|
abortSignal: options.abortSignal,
|
|
2090
2095
|
fetch: this.config.fetch
|
|
2091
2096
|
});
|
|
2092
|
-
const
|
|
2093
|
-
const
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2097
|
+
const content = [];
|
|
2098
|
+
for (const part of response.output) {
|
|
2099
|
+
switch (part.type) {
|
|
2100
|
+
case "message": {
|
|
2101
|
+
for (const contentPart of part.content) {
|
|
2102
|
+
content.push({
|
|
2103
|
+
type: "text",
|
|
2104
|
+
text: contentPart.text
|
|
2105
|
+
});
|
|
2106
|
+
for (const annotation of contentPart.annotations) {
|
|
2107
|
+
content.push({
|
|
2108
|
+
type: "source",
|
|
2109
|
+
sourceType: "url",
|
|
2110
|
+
id: (_c = (_b = (_a = this.config).generateId) == null ? void 0 : _b.call(_a)) != null ? _c : generateId2(),
|
|
2111
|
+
url: annotation.url,
|
|
2112
|
+
title: annotation.title
|
|
2113
|
+
});
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
break;
|
|
2117
|
+
}
|
|
2118
|
+
case "function_call": {
|
|
2119
|
+
content.push({
|
|
2120
|
+
type: "tool-call",
|
|
2121
|
+
toolCallType: "function",
|
|
2122
|
+
toolCallId: part.call_id,
|
|
2123
|
+
toolName: part.name,
|
|
2124
|
+
args: part.arguments
|
|
2125
|
+
});
|
|
2126
|
+
break;
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2099
2130
|
return {
|
|
2100
|
-
|
|
2101
|
-
sources: outputTextElements.flatMap(
|
|
2102
|
-
(content) => content.annotations.map((annotation) => {
|
|
2103
|
-
var _a2, _b2, _c2;
|
|
2104
|
-
return {
|
|
2105
|
-
sourceType: "url",
|
|
2106
|
-
id: (_c2 = (_b2 = (_a2 = this.config).generateId) == null ? void 0 : _b2.call(_a2)) != null ? _c2 : generateId2(),
|
|
2107
|
-
url: annotation.url,
|
|
2108
|
-
title: annotation.title
|
|
2109
|
-
};
|
|
2110
|
-
})
|
|
2111
|
-
),
|
|
2131
|
+
content,
|
|
2112
2132
|
finishReason: mapOpenAIResponseFinishReason({
|
|
2113
|
-
finishReason: (
|
|
2114
|
-
hasToolCalls:
|
|
2133
|
+
finishReason: (_d = response.incomplete_details) == null ? void 0 : _d.reason,
|
|
2134
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
|
2115
2135
|
}),
|
|
2116
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
2117
2136
|
usage: {
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
},
|
|
2121
|
-
rawCall: {
|
|
2122
|
-
rawPrompt: void 0,
|
|
2123
|
-
rawSettings: {}
|
|
2124
|
-
},
|
|
2125
|
-
rawResponse: {
|
|
2126
|
-
headers: responseHeaders,
|
|
2127
|
-
body: rawResponse
|
|
2128
|
-
},
|
|
2129
|
-
request: {
|
|
2130
|
-
body: JSON.stringify(body)
|
|
2137
|
+
inputTokens: response.usage.input_tokens,
|
|
2138
|
+
outputTokens: response.usage.output_tokens
|
|
2131
2139
|
},
|
|
2140
|
+
request: { body },
|
|
2132
2141
|
response: {
|
|
2133
2142
|
id: response.id,
|
|
2134
2143
|
timestamp: new Date(response.created_at * 1e3),
|
|
2135
|
-
modelId: response.model
|
|
2144
|
+
modelId: response.model,
|
|
2145
|
+
headers: responseHeaders,
|
|
2146
|
+
body: rawResponse
|
|
2136
2147
|
},
|
|
2137
2148
|
providerMetadata: {
|
|
2138
2149
|
openai: {
|
|
2139
2150
|
responseId: response.id,
|
|
2140
|
-
cachedPromptTokens: (
|
|
2141
|
-
reasoningTokens: (
|
|
2151
|
+
cachedPromptTokens: (_f = (_e = response.usage.input_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : null,
|
|
2152
|
+
reasoningTokens: (_h = (_g = response.usage.output_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : null
|
|
2142
2153
|
}
|
|
2143
2154
|
},
|
|
2144
2155
|
warnings
|
|
@@ -2151,7 +2162,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2151
2162
|
path: "/responses",
|
|
2152
2163
|
modelId: this.modelId
|
|
2153
2164
|
}),
|
|
2154
|
-
headers:
|
|
2165
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
2155
2166
|
body: {
|
|
2156
2167
|
...body,
|
|
2157
2168
|
stream: true
|
|
@@ -2165,8 +2176,10 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2165
2176
|
});
|
|
2166
2177
|
const self = this;
|
|
2167
2178
|
let finishReason = "unknown";
|
|
2168
|
-
|
|
2169
|
-
|
|
2179
|
+
const usage = {
|
|
2180
|
+
inputTokens: void 0,
|
|
2181
|
+
outputTokens: void 0
|
|
2182
|
+
};
|
|
2170
2183
|
let cachedPromptTokens = null;
|
|
2171
2184
|
let reasoningTokens = null;
|
|
2172
2185
|
let responseId = null;
|
|
@@ -2175,6 +2188,9 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2175
2188
|
return {
|
|
2176
2189
|
stream: response.pipeThrough(
|
|
2177
2190
|
new TransformStream({
|
|
2191
|
+
start(controller) {
|
|
2192
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
2193
|
+
},
|
|
2178
2194
|
transform(chunk, controller) {
|
|
2179
2195
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2180
2196
|
if (!chunk.success) {
|
|
@@ -2218,8 +2234,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2218
2234
|
});
|
|
2219
2235
|
} else if (isTextDeltaChunk(value)) {
|
|
2220
2236
|
controller.enqueue({
|
|
2221
|
-
type: "text
|
|
2222
|
-
|
|
2237
|
+
type: "text",
|
|
2238
|
+
text: value.delta
|
|
2223
2239
|
});
|
|
2224
2240
|
} else if (isResponseOutputItemDoneChunk(value) && value.item.type === "function_call") {
|
|
2225
2241
|
ongoingToolCalls[value.output_index] = void 0;
|
|
@@ -2236,19 +2252,17 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2236
2252
|
finishReason: (_a = value.response.incomplete_details) == null ? void 0 : _a.reason,
|
|
2237
2253
|
hasToolCalls
|
|
2238
2254
|
});
|
|
2239
|
-
|
|
2240
|
-
|
|
2255
|
+
usage.inputTokens = value.response.usage.input_tokens;
|
|
2256
|
+
usage.outputTokens = value.response.usage.output_tokens;
|
|
2241
2257
|
cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
|
|
2242
2258
|
reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
|
|
2243
2259
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
2244
2260
|
controller.enqueue({
|
|
2245
2261
|
type: "source",
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
title: value.annotation.title
|
|
2251
|
-
}
|
|
2262
|
+
sourceType: "url",
|
|
2263
|
+
id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId2(),
|
|
2264
|
+
url: value.annotation.url,
|
|
2265
|
+
title: value.annotation.title
|
|
2252
2266
|
});
|
|
2253
2267
|
}
|
|
2254
2268
|
},
|
|
@@ -2256,7 +2270,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2256
2270
|
controller.enqueue({
|
|
2257
2271
|
type: "finish",
|
|
2258
2272
|
finishReason,
|
|
2259
|
-
usage
|
|
2273
|
+
usage,
|
|
2260
2274
|
...(cachedPromptTokens != null || reasoningTokens != null) && {
|
|
2261
2275
|
providerMetadata: {
|
|
2262
2276
|
openai: {
|
|
@@ -2270,89 +2284,84 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2270
2284
|
}
|
|
2271
2285
|
})
|
|
2272
2286
|
),
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
rawSettings: {}
|
|
2276
|
-
},
|
|
2277
|
-
rawResponse: { headers: responseHeaders },
|
|
2278
|
-
request: { body: JSON.stringify(body) },
|
|
2279
|
-
warnings
|
|
2287
|
+
request: { body },
|
|
2288
|
+
response: { headers: responseHeaders }
|
|
2280
2289
|
};
|
|
2281
2290
|
}
|
|
2282
2291
|
};
|
|
2283
|
-
var usageSchema =
|
|
2284
|
-
input_tokens:
|
|
2285
|
-
input_tokens_details:
|
|
2286
|
-
output_tokens:
|
|
2287
|
-
output_tokens_details:
|
|
2292
|
+
var usageSchema = z10.object({
|
|
2293
|
+
input_tokens: z10.number(),
|
|
2294
|
+
input_tokens_details: z10.object({ cached_tokens: z10.number().nullish() }).nullish(),
|
|
2295
|
+
output_tokens: z10.number(),
|
|
2296
|
+
output_tokens_details: z10.object({ reasoning_tokens: z10.number().nullish() }).nullish()
|
|
2288
2297
|
});
|
|
2289
|
-
var textDeltaChunkSchema =
|
|
2290
|
-
type:
|
|
2291
|
-
delta:
|
|
2298
|
+
var textDeltaChunkSchema = z10.object({
|
|
2299
|
+
type: z10.literal("response.output_text.delta"),
|
|
2300
|
+
delta: z10.string()
|
|
2292
2301
|
});
|
|
2293
|
-
var responseFinishedChunkSchema =
|
|
2294
|
-
type:
|
|
2295
|
-
response:
|
|
2296
|
-
incomplete_details:
|
|
2302
|
+
var responseFinishedChunkSchema = z10.object({
|
|
2303
|
+
type: z10.enum(["response.completed", "response.incomplete"]),
|
|
2304
|
+
response: z10.object({
|
|
2305
|
+
incomplete_details: z10.object({ reason: z10.string() }).nullish(),
|
|
2297
2306
|
usage: usageSchema
|
|
2298
2307
|
})
|
|
2299
2308
|
});
|
|
2300
|
-
var responseCreatedChunkSchema =
|
|
2301
|
-
type:
|
|
2302
|
-
response:
|
|
2303
|
-
id:
|
|
2304
|
-
created_at:
|
|
2305
|
-
model:
|
|
2309
|
+
var responseCreatedChunkSchema = z10.object({
|
|
2310
|
+
type: z10.literal("response.created"),
|
|
2311
|
+
response: z10.object({
|
|
2312
|
+
id: z10.string(),
|
|
2313
|
+
created_at: z10.number(),
|
|
2314
|
+
model: z10.string()
|
|
2306
2315
|
})
|
|
2307
2316
|
});
|
|
2308
|
-
var responseOutputItemDoneSchema =
|
|
2309
|
-
type:
|
|
2310
|
-
output_index:
|
|
2311
|
-
item:
|
|
2312
|
-
|
|
2313
|
-
type:
|
|
2317
|
+
var responseOutputItemDoneSchema = z10.object({
|
|
2318
|
+
type: z10.literal("response.output_item.done"),
|
|
2319
|
+
output_index: z10.number(),
|
|
2320
|
+
item: z10.discriminatedUnion("type", [
|
|
2321
|
+
z10.object({
|
|
2322
|
+
type: z10.literal("message")
|
|
2314
2323
|
}),
|
|
2315
|
-
|
|
2316
|
-
type:
|
|
2317
|
-
id:
|
|
2318
|
-
call_id:
|
|
2319
|
-
name:
|
|
2320
|
-
arguments:
|
|
2321
|
-
status:
|
|
2324
|
+
z10.object({
|
|
2325
|
+
type: z10.literal("function_call"),
|
|
2326
|
+
id: z10.string(),
|
|
2327
|
+
call_id: z10.string(),
|
|
2328
|
+
name: z10.string(),
|
|
2329
|
+
arguments: z10.string(),
|
|
2330
|
+
status: z10.literal("completed")
|
|
2322
2331
|
})
|
|
2323
2332
|
])
|
|
2324
2333
|
});
|
|
2325
|
-
var responseFunctionCallArgumentsDeltaSchema =
|
|
2326
|
-
type:
|
|
2327
|
-
item_id:
|
|
2328
|
-
output_index:
|
|
2329
|
-
delta:
|
|
2334
|
+
var responseFunctionCallArgumentsDeltaSchema = z10.object({
|
|
2335
|
+
type: z10.literal("response.function_call_arguments.delta"),
|
|
2336
|
+
item_id: z10.string(),
|
|
2337
|
+
output_index: z10.number(),
|
|
2338
|
+
delta: z10.string()
|
|
2330
2339
|
});
|
|
2331
|
-
var responseOutputItemAddedSchema =
|
|
2332
|
-
type:
|
|
2333
|
-
output_index:
|
|
2334
|
-
item:
|
|
2335
|
-
|
|
2336
|
-
type:
|
|
2340
|
+
var responseOutputItemAddedSchema = z10.object({
|
|
2341
|
+
type: z10.literal("response.output_item.added"),
|
|
2342
|
+
output_index: z10.number(),
|
|
2343
|
+
item: z10.discriminatedUnion("type", [
|
|
2344
|
+
z10.object({
|
|
2345
|
+
type: z10.literal("message")
|
|
2337
2346
|
}),
|
|
2338
|
-
|
|
2339
|
-
type:
|
|
2340
|
-
id:
|
|
2341
|
-
call_id:
|
|
2342
|
-
name:
|
|
2343
|
-
arguments:
|
|
2347
|
+
z10.object({
|
|
2348
|
+
type: z10.literal("function_call"),
|
|
2349
|
+
id: z10.string(),
|
|
2350
|
+
call_id: z10.string(),
|
|
2351
|
+
name: z10.string(),
|
|
2352
|
+
arguments: z10.string()
|
|
2344
2353
|
})
|
|
2345
2354
|
])
|
|
2346
2355
|
});
|
|
2347
|
-
var responseAnnotationAddedSchema =
|
|
2348
|
-
type:
|
|
2349
|
-
annotation:
|
|
2350
|
-
type:
|
|
2351
|
-
url:
|
|
2352
|
-
title:
|
|
2356
|
+
var responseAnnotationAddedSchema = z10.object({
|
|
2357
|
+
type: z10.literal("response.output_text.annotation.added"),
|
|
2358
|
+
annotation: z10.object({
|
|
2359
|
+
type: z10.literal("url_citation"),
|
|
2360
|
+
url: z10.string(),
|
|
2361
|
+
title: z10.string()
|
|
2353
2362
|
})
|
|
2354
2363
|
});
|
|
2355
|
-
var openaiResponsesChunkSchema =
|
|
2364
|
+
var openaiResponsesChunkSchema = z10.union([
|
|
2356
2365
|
textDeltaChunkSchema,
|
|
2357
2366
|
responseFinishedChunkSchema,
|
|
2358
2367
|
responseCreatedChunkSchema,
|
|
@@ -2360,7 +2369,7 @@ var openaiResponsesChunkSchema = z6.union([
|
|
|
2360
2369
|
responseFunctionCallArgumentsDeltaSchema,
|
|
2361
2370
|
responseOutputItemAddedSchema,
|
|
2362
2371
|
responseAnnotationAddedSchema,
|
|
2363
|
-
|
|
2372
|
+
z10.object({ type: z10.string() }).passthrough()
|
|
2364
2373
|
// fallback for unknown chunks
|
|
2365
2374
|
]);
|
|
2366
2375
|
function isTextDeltaChunk(chunk) {
|
|
@@ -2405,36 +2414,119 @@ function getResponsesModelConfig(modelId) {
|
|
|
2405
2414
|
requiredAutoTruncation: false
|
|
2406
2415
|
};
|
|
2407
2416
|
}
|
|
2408
|
-
var openaiResponsesProviderOptionsSchema =
|
|
2409
|
-
metadata:
|
|
2410
|
-
parallelToolCalls:
|
|
2411
|
-
previousResponseId:
|
|
2412
|
-
store:
|
|
2413
|
-
user:
|
|
2414
|
-
reasoningEffort:
|
|
2415
|
-
strictSchemas:
|
|
2416
|
-
instructions:
|
|
2417
|
+
var openaiResponsesProviderOptionsSchema = z10.object({
|
|
2418
|
+
metadata: z10.any().nullish(),
|
|
2419
|
+
parallelToolCalls: z10.boolean().nullish(),
|
|
2420
|
+
previousResponseId: z10.string().nullish(),
|
|
2421
|
+
store: z10.boolean().nullish(),
|
|
2422
|
+
user: z10.string().nullish(),
|
|
2423
|
+
reasoningEffort: z10.string().nullish(),
|
|
2424
|
+
strictSchemas: z10.boolean().nullish(),
|
|
2425
|
+
instructions: z10.string().nullish()
|
|
2417
2426
|
});
|
|
2418
2427
|
|
|
2419
|
-
// src/openai-
|
|
2420
|
-
import {
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
}
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2428
|
+
// src/openai-speech-model.ts
|
|
2429
|
+
import {
|
|
2430
|
+
combineHeaders as combineHeaders7,
|
|
2431
|
+
createBinaryResponseHandler,
|
|
2432
|
+
parseProviderOptions as parseProviderOptions5,
|
|
2433
|
+
postJsonToApi as postJsonToApi6
|
|
2434
|
+
} from "@ai-sdk/provider-utils";
|
|
2435
|
+
import { z as z11 } from "zod";
|
|
2436
|
+
var OpenAIProviderOptionsSchema = z11.object({
|
|
2437
|
+
instructions: z11.string().nullish(),
|
|
2438
|
+
speed: z11.number().min(0.25).max(4).default(1).nullish()
|
|
2439
|
+
});
|
|
2440
|
+
var OpenAISpeechModel = class {
|
|
2441
|
+
constructor(modelId, config) {
|
|
2442
|
+
this.modelId = modelId;
|
|
2443
|
+
this.config = config;
|
|
2444
|
+
this.specificationVersion = "v1";
|
|
2445
|
+
}
|
|
2446
|
+
get provider() {
|
|
2447
|
+
return this.config.provider;
|
|
2448
|
+
}
|
|
2449
|
+
getArgs({
|
|
2450
|
+
text,
|
|
2451
|
+
voice = "alloy",
|
|
2452
|
+
outputFormat = "mp3",
|
|
2453
|
+
speed,
|
|
2454
|
+
instructions,
|
|
2455
|
+
providerOptions
|
|
2456
|
+
}) {
|
|
2457
|
+
const warnings = [];
|
|
2458
|
+
const openAIOptions = parseProviderOptions5({
|
|
2459
|
+
provider: "openai",
|
|
2460
|
+
providerOptions,
|
|
2461
|
+
schema: OpenAIProviderOptionsSchema
|
|
2462
|
+
});
|
|
2463
|
+
const requestBody = {
|
|
2464
|
+
model: this.modelId,
|
|
2465
|
+
input: text,
|
|
2466
|
+
voice,
|
|
2467
|
+
response_format: "mp3",
|
|
2468
|
+
speed,
|
|
2469
|
+
instructions
|
|
2470
|
+
};
|
|
2471
|
+
if (outputFormat) {
|
|
2472
|
+
if (["mp3", "opus", "aac", "flac", "wav", "pcm"].includes(outputFormat)) {
|
|
2473
|
+
requestBody.response_format = outputFormat;
|
|
2474
|
+
} else {
|
|
2475
|
+
warnings.push({
|
|
2476
|
+
type: "unsupported-setting",
|
|
2477
|
+
setting: "outputFormat",
|
|
2478
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
|
|
2479
|
+
});
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
if (openAIOptions) {
|
|
2483
|
+
const speechModelOptions = {};
|
|
2484
|
+
for (const key in speechModelOptions) {
|
|
2485
|
+
const value = speechModelOptions[key];
|
|
2486
|
+
if (value !== void 0) {
|
|
2487
|
+
requestBody[key] = value;
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
return {
|
|
2492
|
+
requestBody,
|
|
2493
|
+
warnings
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
async doGenerate(options) {
|
|
2497
|
+
var _a, _b, _c;
|
|
2498
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2499
|
+
const { requestBody, warnings } = this.getArgs(options);
|
|
2500
|
+
const {
|
|
2501
|
+
value: audio,
|
|
2502
|
+
responseHeaders,
|
|
2503
|
+
rawValue: rawResponse
|
|
2504
|
+
} = await postJsonToApi6({
|
|
2505
|
+
url: this.config.url({
|
|
2506
|
+
path: "/audio/speech",
|
|
2507
|
+
modelId: this.modelId
|
|
2508
|
+
}),
|
|
2509
|
+
headers: combineHeaders7(this.config.headers(), options.headers),
|
|
2510
|
+
body: requestBody,
|
|
2511
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
2512
|
+
successfulResponseHandler: createBinaryResponseHandler(),
|
|
2513
|
+
abortSignal: options.abortSignal,
|
|
2514
|
+
fetch: this.config.fetch
|
|
2515
|
+
});
|
|
2516
|
+
return {
|
|
2517
|
+
audio,
|
|
2518
|
+
warnings,
|
|
2519
|
+
request: {
|
|
2520
|
+
body: JSON.stringify(requestBody)
|
|
2521
|
+
},
|
|
2522
|
+
response: {
|
|
2523
|
+
timestamp: currentDate,
|
|
2524
|
+
modelId: this.modelId,
|
|
2525
|
+
headers: responseHeaders,
|
|
2526
|
+
body: rawResponse
|
|
2527
|
+
}
|
|
2528
|
+
};
|
|
2529
|
+
}
|
|
2438
2530
|
};
|
|
2439
2531
|
|
|
2440
2532
|
// src/openai-provider.ts
|
|
@@ -2479,6 +2571,18 @@ function createOpenAI(options = {}) {
|
|
|
2479
2571
|
headers: getHeaders,
|
|
2480
2572
|
fetch: options.fetch
|
|
2481
2573
|
});
|
|
2574
|
+
const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
|
|
2575
|
+
provider: `${providerName}.transcription`,
|
|
2576
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2577
|
+
headers: getHeaders,
|
|
2578
|
+
fetch: options.fetch
|
|
2579
|
+
});
|
|
2580
|
+
const createSpeechModel = (modelId) => new OpenAISpeechModel(modelId, {
|
|
2581
|
+
provider: `${providerName}.speech`,
|
|
2582
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2583
|
+
headers: getHeaders,
|
|
2584
|
+
fetch: options.fetch
|
|
2585
|
+
});
|
|
2482
2586
|
const createLanguageModel = (modelId, settings) => {
|
|
2483
2587
|
if (new.target) {
|
|
2484
2588
|
throw new Error(
|
|
@@ -2513,6 +2617,10 @@ function createOpenAI(options = {}) {
|
|
|
2513
2617
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2514
2618
|
provider.image = createImageModel;
|
|
2515
2619
|
provider.imageModel = createImageModel;
|
|
2620
|
+
provider.transcription = createTranscriptionModel;
|
|
2621
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
2622
|
+
provider.speech = createSpeechModel;
|
|
2623
|
+
provider.speechModel = createSpeechModel;
|
|
2516
2624
|
provider.tools = openaiTools;
|
|
2517
2625
|
return provider;
|
|
2518
2626
|
}
|