@langchain/openai 0.5.17 → 0.5.18
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/README.md +2 -2
- package/dist/azure/chat_models.cjs +1 -1
- package/dist/azure/chat_models.d.ts +6 -6
- package/dist/azure/chat_models.js +2 -2
- package/dist/chat_models.cjs +1922 -1861
- package/dist/chat_models.d.ts +233 -235
- package/dist/chat_models.js +1890 -1860
- package/dist/types.d.ts +7 -0
- package/dist/utils/openai.cjs +0 -49
- package/dist/utils/openai.d.ts +0 -6
- package/dist/utils/openai.js +1 -49
- package/dist/utils/prompts.cjs +1 -1
- package/dist/utils/prompts.d.ts +1 -1
- package/dist/utils/prompts.js +1 -1
- package/package.json +3 -3
package/dist/chat_models.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as z4 from "zod/v4/core";
|
|
1
2
|
import { OpenAI as OpenAIClient } from "openai";
|
|
2
3
|
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, isAIMessage, parseBase64DataUrl, parseMimeType, convertToProviderContentBlock, isDataContentBlock, } from "@langchain/core/messages";
|
|
3
4
|
import { ChatGenerationChunk, } from "@langchain/core/outputs";
|
|
@@ -7,33 +8,13 @@ import { isOpenAITool, } from "@langchain/core/language_models/base";
|
|
|
7
8
|
import { RunnableLambda, RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
8
9
|
import { JsonOutputParser, StructuredOutputParser, } from "@langchain/core/output_parsers";
|
|
9
10
|
import { JsonOutputKeyToolsParser, convertLangChainToolCallToOpenAI, makeInvalidToolCall, parseToolCall, } from "@langchain/core/output_parsers/openai_tools";
|
|
10
|
-
import {
|
|
11
|
+
import { zodResponseFormat } from "openai/helpers/zod";
|
|
12
|
+
import { getSchemaDescription, isInteropZodSchema, isZodSchemaV3, isZodSchemaV4, } from "@langchain/core/utils/types";
|
|
11
13
|
import { toJsonSchema, } from "@langchain/core/utils/json_schema";
|
|
12
14
|
import { getEndpoint } from "./utils/azure.js";
|
|
13
|
-
import { formatToOpenAIToolChoice,
|
|
15
|
+
import { formatToOpenAIToolChoice, wrapOpenAIClientError, } from "./utils/openai.js";
|
|
14
16
|
import { formatFunctionDefinitions, } from "./utils/openai-format-fndef.js";
|
|
15
17
|
import { _convertToOpenAITool } from "./utils/tools.js";
|
|
16
|
-
const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
|
|
17
|
-
function isBuiltInTool(tool) {
|
|
18
|
-
return "type" in tool && tool.type !== "function";
|
|
19
|
-
}
|
|
20
|
-
function isBuiltInToolChoice(tool_choice) {
|
|
21
|
-
return (tool_choice != null &&
|
|
22
|
-
typeof tool_choice === "object" &&
|
|
23
|
-
"type" in tool_choice &&
|
|
24
|
-
tool_choice.type !== "function");
|
|
25
|
-
}
|
|
26
|
-
function isReasoningModel(model) {
|
|
27
|
-
return model && /^o\d/.test(model);
|
|
28
|
-
}
|
|
29
|
-
function isStructuredOutputMethodParams(x
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
-
) {
|
|
32
|
-
return (x !== undefined &&
|
|
33
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
34
|
-
typeof x.schema ===
|
|
35
|
-
"object");
|
|
36
|
-
}
|
|
37
18
|
function extractGenericMessageCustomRole(message) {
|
|
38
19
|
if (message.role !== "system" &&
|
|
39
20
|
message.role !== "developer" &&
|
|
@@ -194,7 +175,6 @@ const completionsApiContentBlockConverter = {
|
|
|
194
175
|
},
|
|
195
176
|
};
|
|
196
177
|
// Used in LangSmith, export is important here
|
|
197
|
-
// TODO: put this conversion elsewhere
|
|
198
178
|
export function _convertMessagesToOpenAIParams(messages, model) {
|
|
199
179
|
// TODO: Function messages do not support array content, fix cast
|
|
200
180
|
return messages.flatMap((message) => {
|
|
@@ -243,1800 +223,497 @@ export function _convertMessagesToOpenAIParams(messages, model) {
|
|
|
243
223
|
id: message.additional_kwargs.audio.id,
|
|
244
224
|
},
|
|
245
225
|
};
|
|
246
|
-
return [
|
|
247
|
-
completionParam,
|
|
248
|
-
audioMessage,
|
|
249
|
-
];
|
|
226
|
+
return [completionParam, audioMessage];
|
|
250
227
|
}
|
|
251
228
|
return completionParam;
|
|
252
229
|
});
|
|
253
230
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
231
|
+
const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
|
|
232
|
+
function _convertReasoningSummaryToOpenAIResponsesParams(reasoning) {
|
|
233
|
+
// combine summary parts that have the the same index and then remove the indexes
|
|
234
|
+
const summary = (reasoning.summary.length > 1
|
|
235
|
+
? reasoning.summary.reduce((acc, curr) => {
|
|
236
|
+
const last = acc.at(-1);
|
|
237
|
+
if (last.index === curr.index) {
|
|
238
|
+
last.text += curr.text;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
acc.push(curr);
|
|
242
|
+
}
|
|
243
|
+
return acc;
|
|
244
|
+
}, [{ ...reasoning.summary[0] }])
|
|
245
|
+
: reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
|
|
246
|
+
return {
|
|
247
|
+
...reasoning,
|
|
248
|
+
summary,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function _convertMessagesToOpenAIResponsesParams(messages, model, zdrEnabled) {
|
|
252
|
+
return messages.flatMap((lcMsg) => {
|
|
253
|
+
const additional_kwargs = lcMsg.additional_kwargs;
|
|
254
|
+
let role = messageToOpenAIRole(lcMsg);
|
|
255
|
+
if (role === "system" && isReasoningModel(model))
|
|
256
|
+
role = "developer";
|
|
257
|
+
if (role === "function") {
|
|
258
|
+
throw new Error("Function messages are not supported in Responses API");
|
|
259
|
+
}
|
|
260
|
+
if (role === "tool") {
|
|
261
|
+
const toolMessage = lcMsg;
|
|
262
|
+
// Handle computer call output
|
|
263
|
+
if (additional_kwargs?.type === "computer_call_output") {
|
|
264
|
+
const output = (() => {
|
|
265
|
+
if (typeof toolMessage.content === "string") {
|
|
266
|
+
return {
|
|
267
|
+
type: "computer_screenshot",
|
|
268
|
+
image_url: toolMessage.content,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (Array.isArray(toolMessage.content)) {
|
|
272
|
+
const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
|
|
273
|
+
if (oaiScreenshot)
|
|
274
|
+
return oaiScreenshot;
|
|
275
|
+
const lcImage = toolMessage.content.find((i) => i.type === "image_url");
|
|
276
|
+
if (lcImage) {
|
|
277
|
+
return {
|
|
278
|
+
type: "computer_screenshot",
|
|
279
|
+
image_url: typeof lcImage.image_url === "string"
|
|
280
|
+
? lcImage.image_url
|
|
281
|
+
: lcImage.image_url.url,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
throw new Error("Invalid computer call output");
|
|
286
|
+
})();
|
|
287
|
+
return {
|
|
288
|
+
type: "computer_call_output",
|
|
289
|
+
output,
|
|
290
|
+
call_id: toolMessage.tool_call_id,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
type: "function_call_output",
|
|
295
|
+
call_id: toolMessage.tool_call_id,
|
|
296
|
+
id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : undefined,
|
|
297
|
+
output: typeof toolMessage.content !== "string"
|
|
298
|
+
? JSON.stringify(toolMessage.content)
|
|
299
|
+
: toolMessage.content,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
if (role === "assistant") {
|
|
303
|
+
// if we have the original response items, just reuse them
|
|
304
|
+
if (!zdrEnabled &&
|
|
305
|
+
lcMsg.response_metadata.output != null &&
|
|
306
|
+
Array.isArray(lcMsg.response_metadata.output) &&
|
|
307
|
+
lcMsg.response_metadata.output.length > 0 &&
|
|
308
|
+
lcMsg.response_metadata.output.every((item) => "type" in item)) {
|
|
309
|
+
return lcMsg.response_metadata.output;
|
|
310
|
+
}
|
|
311
|
+
// otherwise, try to reconstruct the response from what we have
|
|
312
|
+
const input = [];
|
|
313
|
+
// reasoning items
|
|
314
|
+
if (additional_kwargs?.reasoning && !zdrEnabled) {
|
|
315
|
+
const reasoningItem = _convertReasoningSummaryToOpenAIResponsesParams(additional_kwargs.reasoning);
|
|
316
|
+
input.push(reasoningItem);
|
|
317
|
+
}
|
|
318
|
+
// ai content
|
|
319
|
+
let { content } = lcMsg;
|
|
320
|
+
if (additional_kwargs?.refusal) {
|
|
321
|
+
if (typeof content === "string") {
|
|
322
|
+
content = [{ type: "output_text", text: content, annotations: [] }];
|
|
323
|
+
}
|
|
324
|
+
content = [
|
|
325
|
+
...content,
|
|
326
|
+
{ type: "refusal", refusal: additional_kwargs.refusal },
|
|
327
|
+
];
|
|
328
|
+
}
|
|
329
|
+
input.push({
|
|
330
|
+
type: "message",
|
|
331
|
+
role: "assistant",
|
|
332
|
+
...(lcMsg.id && !zdrEnabled && lcMsg.id.startsWith("msg_")
|
|
333
|
+
? { id: lcMsg.id }
|
|
334
|
+
: {}),
|
|
335
|
+
content: typeof content === "string"
|
|
336
|
+
? content
|
|
337
|
+
: content.flatMap((item) => {
|
|
338
|
+
if (item.type === "text") {
|
|
339
|
+
return {
|
|
340
|
+
type: "output_text",
|
|
341
|
+
text: item.text,
|
|
342
|
+
// @ts-expect-error TODO: add types for `annotations`
|
|
343
|
+
annotations: item.annotations ?? [],
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
if (item.type === "output_text" || item.type === "refusal") {
|
|
347
|
+
return item;
|
|
348
|
+
}
|
|
349
|
+
return [];
|
|
350
|
+
}),
|
|
351
|
+
});
|
|
352
|
+
const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
|
|
353
|
+
if (isAIMessage(lcMsg) && !!lcMsg.tool_calls?.length) {
|
|
354
|
+
input.push(...lcMsg.tool_calls.map((toolCall) => ({
|
|
355
|
+
type: "function_call",
|
|
356
|
+
name: toolCall.name,
|
|
357
|
+
arguments: JSON.stringify(toolCall.args),
|
|
358
|
+
call_id: toolCall.id,
|
|
359
|
+
...(zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}),
|
|
360
|
+
})));
|
|
361
|
+
}
|
|
362
|
+
else if (additional_kwargs?.tool_calls) {
|
|
363
|
+
input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
|
|
364
|
+
type: "function_call",
|
|
365
|
+
name: toolCall.function.name,
|
|
366
|
+
call_id: toolCall.id,
|
|
367
|
+
arguments: toolCall.function.arguments,
|
|
368
|
+
...(zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}),
|
|
369
|
+
})));
|
|
370
|
+
}
|
|
371
|
+
const toolOutputs = lcMsg.response_metadata.output?.length
|
|
372
|
+
? lcMsg.response_metadata.output
|
|
373
|
+
: additional_kwargs.tool_outputs;
|
|
374
|
+
const fallthroughCallTypes = [
|
|
375
|
+
"computer_call",
|
|
376
|
+
"mcp_call",
|
|
377
|
+
"code_interpreter_call",
|
|
378
|
+
"image_generation_call",
|
|
379
|
+
];
|
|
380
|
+
if (toolOutputs != null) {
|
|
381
|
+
const castToolOutputs = toolOutputs;
|
|
382
|
+
const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
|
|
383
|
+
if (fallthroughCalls.length > 0)
|
|
384
|
+
input.push(...fallthroughCalls);
|
|
385
|
+
}
|
|
386
|
+
return input;
|
|
387
|
+
}
|
|
388
|
+
if (role === "user" || role === "system" || role === "developer") {
|
|
389
|
+
if (typeof lcMsg.content === "string") {
|
|
390
|
+
return { type: "message", role, content: lcMsg.content };
|
|
391
|
+
}
|
|
392
|
+
const messages = [];
|
|
393
|
+
const content = lcMsg.content.flatMap((item) => {
|
|
394
|
+
if (item.type === "mcp_approval_response") {
|
|
395
|
+
messages.push({
|
|
396
|
+
type: "mcp_approval_response",
|
|
397
|
+
approval_request_id: item.approval_request_id,
|
|
398
|
+
approve: item.approve,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (isDataContentBlock(item)) {
|
|
402
|
+
return convertToProviderContentBlock(item, completionsApiContentBlockConverter);
|
|
403
|
+
}
|
|
404
|
+
if (item.type === "text") {
|
|
405
|
+
return {
|
|
406
|
+
type: "input_text",
|
|
407
|
+
text: item.text,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
if (item.type === "image_url") {
|
|
411
|
+
return {
|
|
412
|
+
type: "input_image",
|
|
413
|
+
image_url: typeof item.image_url === "string"
|
|
414
|
+
? item.image_url
|
|
415
|
+
: item.image_url.url,
|
|
416
|
+
detail: typeof item.image_url === "string"
|
|
417
|
+
? "auto"
|
|
418
|
+
: item.image_url.detail,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
if (item.type === "input_text" ||
|
|
422
|
+
item.type === "input_image" ||
|
|
423
|
+
item.type === "input_file") {
|
|
424
|
+
return item;
|
|
425
|
+
}
|
|
426
|
+
return [];
|
|
427
|
+
});
|
|
428
|
+
if (content.length > 0) {
|
|
429
|
+
messages.push({ type: "message", role, content });
|
|
430
|
+
}
|
|
431
|
+
return messages;
|
|
432
|
+
}
|
|
433
|
+
console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
|
|
434
|
+
return [];
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function _convertOpenAIResponsesMessageToBaseMessage(response) {
|
|
438
|
+
if (response.error) {
|
|
439
|
+
// TODO: add support for `addLangChainErrorFields`
|
|
440
|
+
const error = new Error(response.error.message);
|
|
441
|
+
error.name = response.error.code;
|
|
442
|
+
throw error;
|
|
258
443
|
}
|
|
259
|
-
|
|
260
|
-
|
|
444
|
+
let messageId;
|
|
445
|
+
const content = [];
|
|
446
|
+
const tool_calls = [];
|
|
447
|
+
const invalid_tool_calls = [];
|
|
448
|
+
const response_metadata = {
|
|
449
|
+
model: response.model,
|
|
450
|
+
created_at: response.created_at,
|
|
451
|
+
id: response.id,
|
|
452
|
+
incomplete_details: response.incomplete_details,
|
|
453
|
+
metadata: response.metadata,
|
|
454
|
+
object: response.object,
|
|
455
|
+
status: response.status,
|
|
456
|
+
user: response.user,
|
|
457
|
+
service_tier: response.service_tier,
|
|
458
|
+
// for compatibility with chat completion calls.
|
|
459
|
+
model_name: response.model,
|
|
460
|
+
};
|
|
461
|
+
const additional_kwargs = {};
|
|
462
|
+
for (const item of response.output) {
|
|
463
|
+
if (item.type === "message") {
|
|
464
|
+
messageId = item.id;
|
|
465
|
+
content.push(...item.content.flatMap((part) => {
|
|
466
|
+
if (part.type === "output_text") {
|
|
467
|
+
if ("parsed" in part && part.parsed != null) {
|
|
468
|
+
additional_kwargs.parsed = part.parsed;
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
type: "text",
|
|
472
|
+
text: part.text,
|
|
473
|
+
annotations: part.annotations,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
if (part.type === "refusal") {
|
|
477
|
+
additional_kwargs.refusal = part.refusal;
|
|
478
|
+
return [];
|
|
479
|
+
}
|
|
480
|
+
return part;
|
|
481
|
+
}));
|
|
482
|
+
}
|
|
483
|
+
else if (item.type === "function_call") {
|
|
484
|
+
const fnAdapter = {
|
|
485
|
+
function: { name: item.name, arguments: item.arguments },
|
|
486
|
+
id: item.call_id,
|
|
487
|
+
};
|
|
488
|
+
try {
|
|
489
|
+
tool_calls.push(parseToolCall(fnAdapter, { returnId: true }));
|
|
490
|
+
}
|
|
491
|
+
catch (e) {
|
|
492
|
+
let errMessage;
|
|
493
|
+
if (typeof e === "object" &&
|
|
494
|
+
e != null &&
|
|
495
|
+
"message" in e &&
|
|
496
|
+
typeof e.message === "string") {
|
|
497
|
+
errMessage = e.message;
|
|
498
|
+
}
|
|
499
|
+
invalid_tool_calls.push(makeInvalidToolCall(fnAdapter, errMessage));
|
|
500
|
+
}
|
|
501
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
|
|
502
|
+
if (item.id) {
|
|
503
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
else if (item.type === "reasoning") {
|
|
507
|
+
additional_kwargs.reasoning = item;
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
additional_kwargs.tool_outputs ??= [];
|
|
511
|
+
additional_kwargs.tool_outputs.push(item);
|
|
512
|
+
}
|
|
261
513
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
514
|
+
return new AIMessage({
|
|
515
|
+
id: messageId,
|
|
516
|
+
content,
|
|
517
|
+
tool_calls,
|
|
518
|
+
invalid_tool_calls,
|
|
519
|
+
usage_metadata: response.usage,
|
|
520
|
+
additional_kwargs,
|
|
521
|
+
response_metadata,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function _convertOpenAIResponsesDeltaToBaseMessageChunk(chunk) {
|
|
525
|
+
const content = [];
|
|
526
|
+
let generationInfo = {};
|
|
527
|
+
let usage_metadata;
|
|
528
|
+
const tool_call_chunks = [];
|
|
529
|
+
const response_metadata = {};
|
|
530
|
+
const additional_kwargs = {};
|
|
531
|
+
let id;
|
|
532
|
+
if (chunk.type === "response.output_text.delta") {
|
|
533
|
+
content.push({
|
|
534
|
+
type: "text",
|
|
535
|
+
text: chunk.delta,
|
|
536
|
+
index: chunk.content_index,
|
|
537
|
+
});
|
|
276
538
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
539
|
+
else if (chunk.type === "response.output_text_annotation.added") {
|
|
540
|
+
content.push({
|
|
541
|
+
type: "text",
|
|
542
|
+
text: "",
|
|
543
|
+
annotations: [chunk.annotation],
|
|
544
|
+
index: chunk.content_index,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
else if (chunk.type === "response.output_item.added" &&
|
|
548
|
+
chunk.item.type === "message") {
|
|
549
|
+
id = chunk.item.id;
|
|
550
|
+
}
|
|
551
|
+
else if (chunk.type === "response.output_item.added" &&
|
|
552
|
+
chunk.item.type === "function_call") {
|
|
553
|
+
tool_call_chunks.push({
|
|
554
|
+
type: "tool_call_chunk",
|
|
555
|
+
name: chunk.item.name,
|
|
556
|
+
args: chunk.item.arguments,
|
|
557
|
+
id: chunk.item.call_id,
|
|
558
|
+
index: chunk.output_index,
|
|
559
|
+
});
|
|
560
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = {
|
|
561
|
+
[chunk.item.call_id]: chunk.item.id,
|
|
281
562
|
};
|
|
282
563
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
564
|
+
else if (chunk.type === "response.output_item.done" &&
|
|
565
|
+
[
|
|
566
|
+
"web_search_call",
|
|
567
|
+
"file_search_call",
|
|
568
|
+
"computer_call",
|
|
569
|
+
"code_interpreter_call",
|
|
570
|
+
"mcp_call",
|
|
571
|
+
"mcp_list_tools",
|
|
572
|
+
"mcp_approval_request",
|
|
573
|
+
"image_generation_call",
|
|
574
|
+
].includes(chunk.item.type)) {
|
|
575
|
+
additional_kwargs.tool_outputs = [chunk.item];
|
|
576
|
+
}
|
|
577
|
+
else if (chunk.type === "response.created") {
|
|
578
|
+
response_metadata.id = chunk.response.id;
|
|
579
|
+
response_metadata.model_name = chunk.response.model;
|
|
580
|
+
response_metadata.model = chunk.response.model;
|
|
581
|
+
}
|
|
582
|
+
else if (chunk.type === "response.completed") {
|
|
583
|
+
const msg = _convertOpenAIResponsesMessageToBaseMessage(chunk.response);
|
|
584
|
+
usage_metadata = chunk.response.usage;
|
|
585
|
+
if (chunk.response.text?.format?.type === "json_schema") {
|
|
586
|
+
additional_kwargs.parsed ??= JSON.parse(msg.text);
|
|
587
|
+
}
|
|
588
|
+
for (const [key, value] of Object.entries(chunk.response)) {
|
|
589
|
+
if (key !== "id")
|
|
590
|
+
response_metadata[key] = value;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
else if (chunk.type === "response.function_call_arguments.delta") {
|
|
594
|
+
tool_call_chunks.push({
|
|
595
|
+
type: "tool_call_chunk",
|
|
596
|
+
args: chunk.delta,
|
|
597
|
+
index: chunk.output_index,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
else if (chunk.type === "response.web_search_call.completed" ||
|
|
601
|
+
chunk.type === "response.file_search_call.completed") {
|
|
602
|
+
generationInfo = {
|
|
603
|
+
tool_outputs: {
|
|
604
|
+
id: chunk.item_id,
|
|
605
|
+
type: chunk.type.replace("response.", "").replace(".completed", ""),
|
|
606
|
+
status: "completed",
|
|
607
|
+
},
|
|
286
608
|
};
|
|
287
609
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
"configuration",
|
|
291
|
-
"logprobs",
|
|
292
|
-
"topLogprobs",
|
|
293
|
-
"prefixMessages",
|
|
294
|
-
"supportsStrictToolCalling",
|
|
295
|
-
"modalities",
|
|
296
|
-
"audio",
|
|
297
|
-
"temperature",
|
|
298
|
-
"maxTokens",
|
|
299
|
-
"topP",
|
|
300
|
-
"frequencyPenalty",
|
|
301
|
-
"presencePenalty",
|
|
302
|
-
"n",
|
|
303
|
-
"logitBias",
|
|
304
|
-
"user",
|
|
305
|
-
"streaming",
|
|
306
|
-
"streamUsage",
|
|
307
|
-
"model",
|
|
308
|
-
"modelKwargs",
|
|
309
|
-
"stop",
|
|
310
|
-
"stopSequences",
|
|
311
|
-
"timeout",
|
|
312
|
-
"apiKey",
|
|
313
|
-
"cache",
|
|
314
|
-
"maxConcurrency",
|
|
315
|
-
"maxRetries",
|
|
316
|
-
"verbose",
|
|
317
|
-
"callbacks",
|
|
318
|
-
"tags",
|
|
319
|
-
"metadata",
|
|
320
|
-
"disableStreaming",
|
|
321
|
-
"zdrEnabled",
|
|
322
|
-
"reasoning",
|
|
323
|
-
];
|
|
610
|
+
else if (chunk.type === "response.refusal.done") {
|
|
611
|
+
additional_kwargs.refusal = chunk.refusal;
|
|
324
612
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
613
|
+
else if (chunk.type === "response.output_item.added" &&
|
|
614
|
+
"item" in chunk &&
|
|
615
|
+
chunk.item.type === "reasoning") {
|
|
616
|
+
const summary = chunk
|
|
617
|
+
.item.summary
|
|
618
|
+
? chunk.item.summary.map((s, index) => ({
|
|
619
|
+
...s,
|
|
620
|
+
index,
|
|
621
|
+
}))
|
|
622
|
+
: undefined;
|
|
623
|
+
additional_kwargs.reasoning = {
|
|
624
|
+
// We only capture ID in the first chunk or else the concatenated result of all chunks will
|
|
625
|
+
// have an ID field that is repeated once per chunk. There is special handling for the `type`
|
|
626
|
+
// field that prevents this, however.
|
|
627
|
+
id: chunk.item.id,
|
|
628
|
+
type: chunk.item.type,
|
|
629
|
+
...(summary ? { summary } : {}),
|
|
334
630
|
};
|
|
335
631
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
...this.invocationParams(),
|
|
341
|
-
...this.clientConfig,
|
|
632
|
+
else if (chunk.type === "response.reasoning_summary_part.added") {
|
|
633
|
+
additional_kwargs.reasoning = {
|
|
634
|
+
type: "reasoning",
|
|
635
|
+
summary: [{ ...chunk.part, index: chunk.summary_index }],
|
|
342
636
|
};
|
|
343
637
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
638
|
+
else if (chunk.type === "response.reasoning_summary_text.delta") {
|
|
639
|
+
additional_kwargs.reasoning = {
|
|
640
|
+
type: "reasoning",
|
|
641
|
+
summary: [
|
|
642
|
+
{ text: chunk.delta, type: "summary_text", index: chunk.summary_index },
|
|
643
|
+
],
|
|
644
|
+
};
|
|
349
645
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
configurable: true,
|
|
355
|
-
writable: true,
|
|
356
|
-
value: void 0
|
|
357
|
-
});
|
|
358
|
-
Object.defineProperty(this, "topP", {
|
|
359
|
-
enumerable: true,
|
|
360
|
-
configurable: true,
|
|
361
|
-
writable: true,
|
|
362
|
-
value: void 0
|
|
363
|
-
});
|
|
364
|
-
Object.defineProperty(this, "frequencyPenalty", {
|
|
365
|
-
enumerable: true,
|
|
366
|
-
configurable: true,
|
|
367
|
-
writable: true,
|
|
368
|
-
value: void 0
|
|
369
|
-
});
|
|
370
|
-
Object.defineProperty(this, "presencePenalty", {
|
|
371
|
-
enumerable: true,
|
|
372
|
-
configurable: true,
|
|
373
|
-
writable: true,
|
|
374
|
-
value: void 0
|
|
375
|
-
});
|
|
376
|
-
Object.defineProperty(this, "n", {
|
|
377
|
-
enumerable: true,
|
|
378
|
-
configurable: true,
|
|
379
|
-
writable: true,
|
|
380
|
-
value: void 0
|
|
381
|
-
});
|
|
382
|
-
Object.defineProperty(this, "logitBias", {
|
|
383
|
-
enumerable: true,
|
|
384
|
-
configurable: true,
|
|
385
|
-
writable: true,
|
|
386
|
-
value: void 0
|
|
387
|
-
});
|
|
388
|
-
Object.defineProperty(this, "model", {
|
|
389
|
-
enumerable: true,
|
|
390
|
-
configurable: true,
|
|
391
|
-
writable: true,
|
|
392
|
-
value: "gpt-3.5-turbo"
|
|
393
|
-
});
|
|
394
|
-
Object.defineProperty(this, "modelKwargs", {
|
|
395
|
-
enumerable: true,
|
|
396
|
-
configurable: true,
|
|
397
|
-
writable: true,
|
|
398
|
-
value: void 0
|
|
399
|
-
});
|
|
400
|
-
Object.defineProperty(this, "stop", {
|
|
401
|
-
enumerable: true,
|
|
402
|
-
configurable: true,
|
|
403
|
-
writable: true,
|
|
404
|
-
value: void 0
|
|
405
|
-
});
|
|
406
|
-
Object.defineProperty(this, "stopSequences", {
|
|
407
|
-
enumerable: true,
|
|
408
|
-
configurable: true,
|
|
409
|
-
writable: true,
|
|
410
|
-
value: void 0
|
|
411
|
-
});
|
|
412
|
-
Object.defineProperty(this, "user", {
|
|
413
|
-
enumerable: true,
|
|
414
|
-
configurable: true,
|
|
415
|
-
writable: true,
|
|
416
|
-
value: void 0
|
|
417
|
-
});
|
|
418
|
-
Object.defineProperty(this, "timeout", {
|
|
419
|
-
enumerable: true,
|
|
420
|
-
configurable: true,
|
|
421
|
-
writable: true,
|
|
422
|
-
value: void 0
|
|
423
|
-
});
|
|
424
|
-
Object.defineProperty(this, "streaming", {
|
|
425
|
-
enumerable: true,
|
|
426
|
-
configurable: true,
|
|
427
|
-
writable: true,
|
|
428
|
-
value: false
|
|
429
|
-
});
|
|
430
|
-
Object.defineProperty(this, "streamUsage", {
|
|
431
|
-
enumerable: true,
|
|
432
|
-
configurable: true,
|
|
433
|
-
writable: true,
|
|
434
|
-
value: true
|
|
435
|
-
});
|
|
436
|
-
Object.defineProperty(this, "maxTokens", {
|
|
437
|
-
enumerable: true,
|
|
438
|
-
configurable: true,
|
|
439
|
-
writable: true,
|
|
440
|
-
value: void 0
|
|
441
|
-
});
|
|
442
|
-
Object.defineProperty(this, "logprobs", {
|
|
443
|
-
enumerable: true,
|
|
444
|
-
configurable: true,
|
|
445
|
-
writable: true,
|
|
446
|
-
value: void 0
|
|
447
|
-
});
|
|
448
|
-
Object.defineProperty(this, "topLogprobs", {
|
|
449
|
-
enumerable: true,
|
|
450
|
-
configurable: true,
|
|
451
|
-
writable: true,
|
|
452
|
-
value: void 0
|
|
453
|
-
});
|
|
454
|
-
Object.defineProperty(this, "apiKey", {
|
|
455
|
-
enumerable: true,
|
|
456
|
-
configurable: true,
|
|
457
|
-
writable: true,
|
|
458
|
-
value: void 0
|
|
459
|
-
});
|
|
460
|
-
Object.defineProperty(this, "organization", {
|
|
461
|
-
enumerable: true,
|
|
462
|
-
configurable: true,
|
|
463
|
-
writable: true,
|
|
464
|
-
value: void 0
|
|
465
|
-
});
|
|
466
|
-
Object.defineProperty(this, "__includeRawResponse", {
|
|
467
|
-
enumerable: true,
|
|
468
|
-
configurable: true,
|
|
469
|
-
writable: true,
|
|
470
|
-
value: void 0
|
|
471
|
-
});
|
|
472
|
-
Object.defineProperty(this, "client", {
|
|
473
|
-
enumerable: true,
|
|
474
|
-
configurable: true,
|
|
475
|
-
writable: true,
|
|
476
|
-
value: void 0
|
|
477
|
-
});
|
|
478
|
-
Object.defineProperty(this, "clientConfig", {
|
|
479
|
-
enumerable: true,
|
|
480
|
-
configurable: true,
|
|
481
|
-
writable: true,
|
|
482
|
-
value: void 0
|
|
483
|
-
});
|
|
484
|
-
/**
|
|
485
|
-
* Whether the model supports the `strict` argument when passing in tools.
|
|
486
|
-
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
487
|
-
*/
|
|
488
|
-
Object.defineProperty(this, "supportsStrictToolCalling", {
|
|
489
|
-
enumerable: true,
|
|
490
|
-
configurable: true,
|
|
491
|
-
writable: true,
|
|
492
|
-
value: void 0
|
|
493
|
-
});
|
|
494
|
-
Object.defineProperty(this, "audio", {
|
|
495
|
-
enumerable: true,
|
|
496
|
-
configurable: true,
|
|
497
|
-
writable: true,
|
|
498
|
-
value: void 0
|
|
499
|
-
});
|
|
500
|
-
Object.defineProperty(this, "modalities", {
|
|
501
|
-
enumerable: true,
|
|
502
|
-
configurable: true,
|
|
503
|
-
writable: true,
|
|
504
|
-
value: void 0
|
|
505
|
-
});
|
|
506
|
-
Object.defineProperty(this, "reasoning", {
|
|
507
|
-
enumerable: true,
|
|
508
|
-
configurable: true,
|
|
509
|
-
writable: true,
|
|
510
|
-
value: void 0
|
|
511
|
-
});
|
|
512
|
-
/**
|
|
513
|
-
* Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
|
|
514
|
-
* output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
|
|
515
|
-
* OpenAI organization or project. This must be configured directly with OpenAI.
|
|
516
|
-
*
|
|
517
|
-
* See:
|
|
518
|
-
* https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
|
|
519
|
-
* https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
|
|
520
|
-
*
|
|
521
|
-
* @default false
|
|
522
|
-
*/
|
|
523
|
-
Object.defineProperty(this, "zdrEnabled", {
|
|
524
|
-
enumerable: true,
|
|
525
|
-
configurable: true,
|
|
526
|
-
writable: true,
|
|
527
|
-
value: void 0
|
|
528
|
-
});
|
|
529
|
-
/**
|
|
530
|
-
* Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
|
|
531
|
-
* Specifies the service tier for prioritization and latency optimization.
|
|
532
|
-
*/
|
|
533
|
-
Object.defineProperty(this, "service_tier", {
|
|
534
|
-
enumerable: true,
|
|
535
|
-
configurable: true,
|
|
536
|
-
writable: true,
|
|
537
|
-
value: void 0
|
|
538
|
-
});
|
|
539
|
-
Object.defineProperty(this, "lc_serializable", {
|
|
540
|
-
enumerable: true,
|
|
541
|
-
configurable: true,
|
|
542
|
-
writable: true,
|
|
543
|
-
value: true
|
|
544
|
-
});
|
|
545
|
-
this.apiKey =
|
|
546
|
-
fields?.apiKey ??
|
|
547
|
-
fields?.configuration?.apiKey ??
|
|
548
|
-
getEnvironmentVariable("OPENAI_API_KEY");
|
|
549
|
-
this.organization =
|
|
550
|
-
fields?.configuration?.organization ??
|
|
551
|
-
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
552
|
-
this.model = fields?.model ?? this.model;
|
|
553
|
-
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
554
|
-
this.timeout = fields?.timeout;
|
|
555
|
-
this.temperature = fields?.temperature ?? this.temperature;
|
|
556
|
-
this.topP = fields?.topP ?? this.topP;
|
|
557
|
-
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
558
|
-
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
559
|
-
this.logprobs = fields?.logprobs;
|
|
560
|
-
this.topLogprobs = fields?.topLogprobs;
|
|
561
|
-
this.n = fields?.n ?? this.n;
|
|
562
|
-
this.logitBias = fields?.logitBias;
|
|
563
|
-
this.stop = fields?.stopSequences ?? fields?.stop;
|
|
564
|
-
this.stopSequences = this.stop;
|
|
565
|
-
this.user = fields?.user;
|
|
566
|
-
this.__includeRawResponse = fields?.__includeRawResponse;
|
|
567
|
-
this.audio = fields?.audio;
|
|
568
|
-
this.modalities = fields?.modalities;
|
|
569
|
-
this.reasoning = fields?.reasoning;
|
|
570
|
-
this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
|
|
571
|
-
this.disableStreaming = fields?.disableStreaming ?? this.disableStreaming;
|
|
572
|
-
this.streaming = fields?.streaming ?? false;
|
|
573
|
-
if (this.disableStreaming)
|
|
574
|
-
this.streaming = false;
|
|
575
|
-
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
|
|
576
|
-
if (this.disableStreaming)
|
|
577
|
-
this.streamUsage = false;
|
|
578
|
-
this.clientConfig = {
|
|
579
|
-
apiKey: this.apiKey,
|
|
580
|
-
organization: this.organization,
|
|
581
|
-
dangerouslyAllowBrowser: true,
|
|
582
|
-
...fields?.configuration,
|
|
583
|
-
};
|
|
584
|
-
// If `supportsStrictToolCalling` is explicitly set, use that value.
|
|
585
|
-
// Else leave undefined so it's not passed to OpenAI.
|
|
586
|
-
if (fields?.supportsStrictToolCalling !== undefined) {
|
|
587
|
-
this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
|
|
588
|
-
}
|
|
589
|
-
if (fields?.service_tier !== undefined) {
|
|
590
|
-
this.service_tier = fields.service_tier;
|
|
591
|
-
}
|
|
592
|
-
this.zdrEnabled = fields?.zdrEnabled ?? false;
|
|
646
|
+
else if (chunk.type === "response.image_generation_call.partial_image") {
|
|
647
|
+
// noop/fixme: retaining partial images in a message chunk means that _all_
|
|
648
|
+
// partial images get kept in history, so we don't do anything here.
|
|
649
|
+
return null;
|
|
593
650
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
* @internal
|
|
597
|
-
*/
|
|
598
|
-
_getReasoningParams(options) {
|
|
599
|
-
if (!isReasoningModel(this.model)) {
|
|
600
|
-
return;
|
|
601
|
-
}
|
|
602
|
-
// apply options in reverse order of importance -- newer options supersede older options
|
|
603
|
-
let reasoning;
|
|
604
|
-
if (this.reasoning !== undefined) {
|
|
605
|
-
reasoning = {
|
|
606
|
-
...reasoning,
|
|
607
|
-
...this.reasoning,
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
if (options?.reasoning !== undefined) {
|
|
611
|
-
reasoning = {
|
|
612
|
-
...reasoning,
|
|
613
|
-
...options.reasoning,
|
|
614
|
-
};
|
|
615
|
-
}
|
|
616
|
-
return reasoning;
|
|
651
|
+
else {
|
|
652
|
+
return null;
|
|
617
653
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
654
|
+
return new ChatGenerationChunk({
|
|
655
|
+
// Legacy reasons, `onLLMNewToken` should pulls this out
|
|
656
|
+
text: content.map((part) => part.text).join(""),
|
|
657
|
+
message: new AIMessageChunk({
|
|
658
|
+
id,
|
|
659
|
+
content,
|
|
660
|
+
tool_call_chunks,
|
|
661
|
+
usage_metadata,
|
|
662
|
+
additional_kwargs,
|
|
663
|
+
response_metadata,
|
|
664
|
+
}),
|
|
665
|
+
generationInfo,
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
function isBuiltInTool(tool) {
|
|
669
|
+
return "type" in tool && tool.type !== "function";
|
|
670
|
+
}
|
|
671
|
+
function isBuiltInToolChoice(tool_choice) {
|
|
672
|
+
return (tool_choice != null &&
|
|
673
|
+
typeof tool_choice === "object" &&
|
|
674
|
+
"type" in tool_choice &&
|
|
675
|
+
tool_choice.type !== "function");
|
|
676
|
+
}
|
|
677
|
+
function _reduceChatOpenAIToolsForResponses(tools, fields) {
|
|
678
|
+
const reducedTools = [];
|
|
679
|
+
for (const tool of tools) {
|
|
680
|
+
if (isBuiltInTool(tool)) {
|
|
681
|
+
if (tool.type === "image_generation" && fields?.stream) {
|
|
682
|
+
// OpenAI sends a 400 error if partial_images is not set and we want to stream.
|
|
683
|
+
// We also set it to 1 since we don't support partial images yet.
|
|
684
|
+
tool.partial_images = 1;
|
|
685
|
+
}
|
|
686
|
+
reducedTools.push(tool);
|
|
687
|
+
}
|
|
688
|
+
else if (isOpenAITool(tool)) {
|
|
689
|
+
reducedTools.push({
|
|
690
|
+
type: "function",
|
|
691
|
+
name: tool.function.name,
|
|
692
|
+
parameters: tool.function.parameters,
|
|
693
|
+
description: tool.function.description,
|
|
694
|
+
strict: fields?.strict ?? null,
|
|
629
695
|
});
|
|
630
696
|
}
|
|
631
|
-
return resFormat;
|
|
632
697
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
698
|
+
return reducedTools;
|
|
699
|
+
}
|
|
700
|
+
function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
701
|
+
if (isOpenAITool(tool)) {
|
|
702
|
+
if (fields?.strict !== undefined) {
|
|
703
|
+
return {
|
|
704
|
+
...tool,
|
|
705
|
+
function: {
|
|
706
|
+
...tool.function,
|
|
707
|
+
strict: fields.strict,
|
|
708
|
+
},
|
|
644
709
|
};
|
|
645
|
-
if (!params.baseURL) {
|
|
646
|
-
delete params.baseURL;
|
|
647
|
-
}
|
|
648
|
-
this.client = new OpenAIClient(params);
|
|
649
|
-
}
|
|
650
|
-
const requestOptions = {
|
|
651
|
-
...this.clientConfig,
|
|
652
|
-
...options,
|
|
653
|
-
};
|
|
654
|
-
return requestOptions;
|
|
655
|
-
}
|
|
656
|
-
// TODO: move to completions class
|
|
657
|
-
_convertChatOpenAIToolToCompletionsTool(tool, fields) {
|
|
658
|
-
if (isOpenAITool(tool)) {
|
|
659
|
-
if (fields?.strict !== undefined) {
|
|
660
|
-
return {
|
|
661
|
-
...tool,
|
|
662
|
-
function: {
|
|
663
|
-
...tool.function,
|
|
664
|
-
strict: fields.strict,
|
|
665
|
-
},
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
|
-
return tool;
|
|
669
|
-
}
|
|
670
|
-
return _convertToOpenAITool(tool, fields);
|
|
671
|
-
}
|
|
672
|
-
bindTools(tools, kwargs) {
|
|
673
|
-
let strict;
|
|
674
|
-
if (kwargs?.strict !== undefined) {
|
|
675
|
-
strict = kwargs.strict;
|
|
676
|
-
}
|
|
677
|
-
else if (this.supportsStrictToolCalling !== undefined) {
|
|
678
|
-
strict = this.supportsStrictToolCalling;
|
|
679
|
-
}
|
|
680
|
-
return this.withConfig({
|
|
681
|
-
tools: tools.map((tool) => isBuiltInTool(tool)
|
|
682
|
-
? tool
|
|
683
|
-
: this._convertChatOpenAIToolToCompletionsTool(tool, { strict })),
|
|
684
|
-
...kwargs,
|
|
685
|
-
});
|
|
686
|
-
}
|
|
687
|
-
/** @ignore */
|
|
688
|
-
_combineLLMOutput(...llmOutputs) {
|
|
689
|
-
return llmOutputs.reduce((acc, llmOutput) => {
|
|
690
|
-
if (llmOutput && llmOutput.tokenUsage) {
|
|
691
|
-
acc.tokenUsage.completionTokens +=
|
|
692
|
-
llmOutput.tokenUsage.completionTokens ?? 0;
|
|
693
|
-
acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
|
|
694
|
-
acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
|
|
695
|
-
}
|
|
696
|
-
return acc;
|
|
697
|
-
}, {
|
|
698
|
-
tokenUsage: {
|
|
699
|
-
completionTokens: 0,
|
|
700
|
-
promptTokens: 0,
|
|
701
|
-
totalTokens: 0,
|
|
702
|
-
},
|
|
703
|
-
});
|
|
704
|
-
}
|
|
705
|
-
async getNumTokensFromMessages(messages) {
|
|
706
|
-
let totalCount = 0;
|
|
707
|
-
let tokensPerMessage = 0;
|
|
708
|
-
let tokensPerName = 0;
|
|
709
|
-
// From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
|
710
|
-
if (this.model === "gpt-3.5-turbo-0301") {
|
|
711
|
-
tokensPerMessage = 4;
|
|
712
|
-
tokensPerName = -1;
|
|
713
|
-
}
|
|
714
|
-
else {
|
|
715
|
-
tokensPerMessage = 3;
|
|
716
|
-
tokensPerName = 1;
|
|
717
|
-
}
|
|
718
|
-
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
719
|
-
const textCount = await this.getNumTokens(message.content);
|
|
720
|
-
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
721
|
-
const nameCount = message.name !== undefined
|
|
722
|
-
? tokensPerName + (await this.getNumTokens(message.name))
|
|
723
|
-
: 0;
|
|
724
|
-
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
725
|
-
// From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
|
|
726
|
-
const openAIMessage = message;
|
|
727
|
-
if (openAIMessage._getType() === "function") {
|
|
728
|
-
count -= 2;
|
|
729
|
-
}
|
|
730
|
-
if (openAIMessage.additional_kwargs?.function_call) {
|
|
731
|
-
count += 3;
|
|
732
|
-
}
|
|
733
|
-
if (openAIMessage?.additional_kwargs.function_call?.name) {
|
|
734
|
-
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
735
|
-
}
|
|
736
|
-
if (openAIMessage.additional_kwargs.function_call?.arguments) {
|
|
737
|
-
try {
|
|
738
|
-
count += await this.getNumTokens(
|
|
739
|
-
// Remove newlines and spaces
|
|
740
|
-
JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
741
|
-
}
|
|
742
|
-
catch (error) {
|
|
743
|
-
console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
|
|
744
|
-
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
totalCount += count;
|
|
748
|
-
return count;
|
|
749
|
-
}));
|
|
750
|
-
totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
|
|
751
|
-
return { totalCount, countPerMessage };
|
|
752
|
-
}
|
|
753
|
-
/** @internal */
|
|
754
|
-
async _getNumTokensFromGenerations(generations) {
|
|
755
|
-
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
756
|
-
if (generation.message.additional_kwargs?.function_call) {
|
|
757
|
-
return (await this.getNumTokensFromMessages([generation.message]))
|
|
758
|
-
.countPerMessage[0];
|
|
759
|
-
}
|
|
760
|
-
else {
|
|
761
|
-
return await this.getNumTokens(generation.message.content);
|
|
762
|
-
}
|
|
763
|
-
}));
|
|
764
|
-
return generationUsages.reduce((a, b) => a + b, 0);
|
|
765
|
-
}
|
|
766
|
-
/** @internal */
|
|
767
|
-
async _getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
768
|
-
// It appears that if functions are present, the first system message is padded with a trailing newline. This
|
|
769
|
-
// was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
|
|
770
|
-
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
771
|
-
// If there are functions, add the function definitions as they count towards token usage
|
|
772
|
-
if (functions && function_call !== "auto") {
|
|
773
|
-
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
774
|
-
tokens += await this.getNumTokens(promptDefinitions);
|
|
775
|
-
tokens += 9; // Add nine per completion
|
|
776
|
-
}
|
|
777
|
-
// If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
|
|
778
|
-
// functions typically add a system message, but reuse the first one if it's already there. This offsets
|
|
779
|
-
// the extra 9 tokens added by the function definitions.
|
|
780
|
-
if (functions && messages.find((m) => m._getType() === "system")) {
|
|
781
|
-
tokens -= 4;
|
|
782
|
-
}
|
|
783
|
-
// If function_call is 'none', add one token.
|
|
784
|
-
// If it's a FunctionCall object, add 4 + the number of tokens in the function name.
|
|
785
|
-
// If it's undefined or 'auto', don't add anything.
|
|
786
|
-
if (function_call === "none") {
|
|
787
|
-
tokens += 1;
|
|
788
|
-
}
|
|
789
|
-
else if (typeof function_call === "object") {
|
|
790
|
-
tokens += (await this.getNumTokens(function_call.name)) + 4;
|
|
791
|
-
}
|
|
792
|
-
return tokens;
|
|
793
|
-
}
|
|
794
|
-
withStructuredOutput(outputSchema, config) {
|
|
795
|
-
// ):
|
|
796
|
-
// | Runnable<BaseLanguageModelInput, RunOutput>
|
|
797
|
-
// | Runnable<
|
|
798
|
-
// BaseLanguageModelInput,
|
|
799
|
-
// { raw: BaseMessage; parsed: RunOutput }
|
|
800
|
-
// > {
|
|
801
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
802
|
-
let schema;
|
|
803
|
-
let name;
|
|
804
|
-
let method;
|
|
805
|
-
let includeRaw;
|
|
806
|
-
if (isStructuredOutputMethodParams(outputSchema)) {
|
|
807
|
-
schema = outputSchema.schema;
|
|
808
|
-
name = outputSchema.name;
|
|
809
|
-
method = outputSchema.method;
|
|
810
|
-
includeRaw = outputSchema.includeRaw;
|
|
811
|
-
}
|
|
812
|
-
else {
|
|
813
|
-
schema = outputSchema;
|
|
814
|
-
name = config?.name;
|
|
815
|
-
method = config?.method;
|
|
816
|
-
includeRaw = config?.includeRaw;
|
|
817
|
-
}
|
|
818
|
-
let llm;
|
|
819
|
-
let outputParser;
|
|
820
|
-
if (config?.strict !== undefined && method === "jsonMode") {
|
|
821
|
-
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
822
|
-
}
|
|
823
|
-
if (!this.model.startsWith("gpt-3") &&
|
|
824
|
-
!this.model.startsWith("gpt-4-") &&
|
|
825
|
-
this.model !== "gpt-4") {
|
|
826
|
-
if (method === undefined) {
|
|
827
|
-
method = "jsonSchema";
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
else if (method === "jsonSchema") {
|
|
831
|
-
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
832
|
-
}
|
|
833
|
-
if (method === "jsonMode") {
|
|
834
|
-
let outputFormatSchema;
|
|
835
|
-
if (isInteropZodSchema(schema)) {
|
|
836
|
-
outputParser = StructuredOutputParser.fromZodSchema(schema);
|
|
837
|
-
outputFormatSchema = toJsonSchema(schema);
|
|
838
|
-
}
|
|
839
|
-
else {
|
|
840
|
-
outputParser = new JsonOutputParser();
|
|
841
|
-
}
|
|
842
|
-
llm = this.withConfig({
|
|
843
|
-
response_format: { type: "json_object" },
|
|
844
|
-
ls_structured_output_format: {
|
|
845
|
-
kwargs: { method: "jsonMode" },
|
|
846
|
-
schema: outputFormatSchema,
|
|
847
|
-
},
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
else if (method === "jsonSchema") {
|
|
851
|
-
llm = this.withConfig({
|
|
852
|
-
response_format: {
|
|
853
|
-
type: "json_schema",
|
|
854
|
-
json_schema: {
|
|
855
|
-
name: name ?? "extract",
|
|
856
|
-
description: getSchemaDescription(schema),
|
|
857
|
-
schema,
|
|
858
|
-
strict: config?.strict,
|
|
859
|
-
},
|
|
860
|
-
},
|
|
861
|
-
ls_structured_output_format: {
|
|
862
|
-
kwargs: { method: "jsonSchema" },
|
|
863
|
-
schema: toJsonSchema(schema),
|
|
864
|
-
},
|
|
865
|
-
});
|
|
866
|
-
if (isInteropZodSchema(schema)) {
|
|
867
|
-
const altParser = StructuredOutputParser.fromZodSchema(schema);
|
|
868
|
-
outputParser = RunnableLambda.from((aiMessage) => {
|
|
869
|
-
if ("parsed" in aiMessage.additional_kwargs) {
|
|
870
|
-
return aiMessage.additional_kwargs.parsed;
|
|
871
|
-
}
|
|
872
|
-
return altParser;
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
else {
|
|
876
|
-
outputParser = new JsonOutputParser();
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
else {
|
|
880
|
-
let functionName = name ?? "extract";
|
|
881
|
-
// Is function calling
|
|
882
|
-
if (isInteropZodSchema(schema)) {
|
|
883
|
-
const asJsonSchema = toJsonSchema(schema);
|
|
884
|
-
llm = this.withConfig({
|
|
885
|
-
tools: [
|
|
886
|
-
{
|
|
887
|
-
type: "function",
|
|
888
|
-
function: {
|
|
889
|
-
name: functionName,
|
|
890
|
-
description: asJsonSchema.description,
|
|
891
|
-
parameters: asJsonSchema,
|
|
892
|
-
},
|
|
893
|
-
},
|
|
894
|
-
],
|
|
895
|
-
tool_choice: {
|
|
896
|
-
type: "function",
|
|
897
|
-
function: {
|
|
898
|
-
name: functionName,
|
|
899
|
-
},
|
|
900
|
-
},
|
|
901
|
-
ls_structured_output_format: {
|
|
902
|
-
kwargs: { method: "functionCalling" },
|
|
903
|
-
schema: asJsonSchema,
|
|
904
|
-
},
|
|
905
|
-
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
906
|
-
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
907
|
-
});
|
|
908
|
-
outputParser = new JsonOutputKeyToolsParser({
|
|
909
|
-
returnSingle: true,
|
|
910
|
-
keyName: functionName,
|
|
911
|
-
zodSchema: schema,
|
|
912
|
-
});
|
|
913
|
-
}
|
|
914
|
-
else {
|
|
915
|
-
let openAIFunctionDefinition;
|
|
916
|
-
if (typeof schema.name === "string" &&
|
|
917
|
-
typeof schema.parameters === "object" &&
|
|
918
|
-
schema.parameters != null) {
|
|
919
|
-
openAIFunctionDefinition = schema;
|
|
920
|
-
functionName = schema.name;
|
|
921
|
-
}
|
|
922
|
-
else {
|
|
923
|
-
functionName = schema.title ?? functionName;
|
|
924
|
-
openAIFunctionDefinition = {
|
|
925
|
-
name: functionName,
|
|
926
|
-
description: schema.description ?? "",
|
|
927
|
-
parameters: schema,
|
|
928
|
-
};
|
|
929
|
-
}
|
|
930
|
-
llm = this.withConfig({
|
|
931
|
-
tools: [
|
|
932
|
-
{
|
|
933
|
-
type: "function",
|
|
934
|
-
function: openAIFunctionDefinition,
|
|
935
|
-
},
|
|
936
|
-
],
|
|
937
|
-
tool_choice: {
|
|
938
|
-
type: "function",
|
|
939
|
-
function: {
|
|
940
|
-
name: functionName,
|
|
941
|
-
},
|
|
942
|
-
},
|
|
943
|
-
ls_structured_output_format: {
|
|
944
|
-
kwargs: { method: "functionCalling" },
|
|
945
|
-
schema: toJsonSchema(schema),
|
|
946
|
-
},
|
|
947
|
-
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
948
|
-
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
949
|
-
});
|
|
950
|
-
outputParser = new JsonOutputKeyToolsParser({
|
|
951
|
-
returnSingle: true,
|
|
952
|
-
keyName: functionName,
|
|
953
|
-
});
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
if (!includeRaw) {
|
|
957
|
-
return llm.pipe(outputParser);
|
|
958
|
-
}
|
|
959
|
-
const parserAssign = RunnablePassthrough.assign({
|
|
960
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
961
|
-
parsed: (input, config) => outputParser.invoke(input.raw, config),
|
|
962
|
-
});
|
|
963
|
-
const parserNone = RunnablePassthrough.assign({
|
|
964
|
-
parsed: () => null,
|
|
965
|
-
});
|
|
966
|
-
const parsedWithFallback = parserAssign.withFallbacks({
|
|
967
|
-
fallbacks: [parserNone],
|
|
968
|
-
});
|
|
969
|
-
return RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
/**
|
|
973
|
-
* OpenAI Responses API implementation.
|
|
974
|
-
*
|
|
975
|
-
* Will be exported in a later version of @langchain/openai.
|
|
976
|
-
*
|
|
977
|
-
* @internal
|
|
978
|
-
*/
|
|
979
|
-
export class ChatOpenAIResponses extends BaseChatOpenAI {
|
|
980
|
-
invocationParams(options) {
|
|
981
|
-
let strict;
|
|
982
|
-
if (options?.strict !== undefined) {
|
|
983
|
-
strict = options.strict;
|
|
984
|
-
}
|
|
985
|
-
else if (this.supportsStrictToolCalling !== undefined) {
|
|
986
|
-
strict = this.supportsStrictToolCalling;
|
|
987
|
-
}
|
|
988
|
-
const params = {
|
|
989
|
-
model: this.model,
|
|
990
|
-
temperature: this.temperature,
|
|
991
|
-
top_p: this.topP,
|
|
992
|
-
user: this.user,
|
|
993
|
-
// if include_usage is set or streamUsage then stream must be set to true.
|
|
994
|
-
stream: this.streaming,
|
|
995
|
-
previous_response_id: options?.previous_response_id,
|
|
996
|
-
truncation: options?.truncation,
|
|
997
|
-
include: options?.include,
|
|
998
|
-
tools: options?.tools?.length
|
|
999
|
-
? this._reduceChatOpenAITools(options.tools, {
|
|
1000
|
-
stream: this.streaming,
|
|
1001
|
-
strict,
|
|
1002
|
-
})
|
|
1003
|
-
: undefined,
|
|
1004
|
-
tool_choice: isBuiltInToolChoice(options?.tool_choice)
|
|
1005
|
-
? options?.tool_choice
|
|
1006
|
-
: (() => {
|
|
1007
|
-
const formatted = formatToOpenAIToolChoice(options?.tool_choice);
|
|
1008
|
-
if (typeof formatted === "object" && "type" in formatted) {
|
|
1009
|
-
return { type: "function", name: formatted.function.name };
|
|
1010
|
-
}
|
|
1011
|
-
else {
|
|
1012
|
-
return undefined;
|
|
1013
|
-
}
|
|
1014
|
-
})(),
|
|
1015
|
-
text: (() => {
|
|
1016
|
-
if (options?.text)
|
|
1017
|
-
return options.text;
|
|
1018
|
-
const format = this._getResponseFormat(options?.response_format);
|
|
1019
|
-
if (format?.type === "json_schema") {
|
|
1020
|
-
if (format.json_schema.schema != null) {
|
|
1021
|
-
return {
|
|
1022
|
-
format: {
|
|
1023
|
-
type: "json_schema",
|
|
1024
|
-
schema: format.json_schema.schema,
|
|
1025
|
-
description: format.json_schema.description,
|
|
1026
|
-
name: format.json_schema.name,
|
|
1027
|
-
strict: format.json_schema.strict,
|
|
1028
|
-
},
|
|
1029
|
-
};
|
|
1030
|
-
}
|
|
1031
|
-
return undefined;
|
|
1032
|
-
}
|
|
1033
|
-
return { format };
|
|
1034
|
-
})(),
|
|
1035
|
-
parallel_tool_calls: options?.parallel_tool_calls,
|
|
1036
|
-
max_output_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
|
|
1037
|
-
...(this.zdrEnabled ? { store: false } : {}),
|
|
1038
|
-
...this.modelKwargs,
|
|
1039
|
-
};
|
|
1040
|
-
const reasoning = this._getReasoningParams(options);
|
|
1041
|
-
if (reasoning !== undefined) {
|
|
1042
|
-
params.reasoning = reasoning;
|
|
1043
|
-
}
|
|
1044
|
-
return params;
|
|
1045
|
-
}
|
|
1046
|
-
async _generate(messages, options) {
|
|
1047
|
-
const invocationParams = this.invocationParams(options);
|
|
1048
|
-
if (invocationParams.stream) {
|
|
1049
|
-
const stream = this._streamResponseChunks(messages, options);
|
|
1050
|
-
let finalChunk;
|
|
1051
|
-
for await (const chunk of stream) {
|
|
1052
|
-
chunk.message.response_metadata = {
|
|
1053
|
-
...chunk.generationInfo,
|
|
1054
|
-
...chunk.message.response_metadata,
|
|
1055
|
-
};
|
|
1056
|
-
finalChunk = finalChunk?.concat(chunk) ?? chunk;
|
|
1057
|
-
}
|
|
1058
|
-
return {
|
|
1059
|
-
generations: finalChunk ? [finalChunk] : [],
|
|
1060
|
-
llmOutput: {
|
|
1061
|
-
estimatedTokenUsage: finalChunk?.message
|
|
1062
|
-
?.usage_metadata,
|
|
1063
|
-
},
|
|
1064
|
-
};
|
|
1065
|
-
}
|
|
1066
|
-
else {
|
|
1067
|
-
const input = this._convertMessagesToResponsesParams(messages);
|
|
1068
|
-
const data = await this.completionWithRetry({
|
|
1069
|
-
input,
|
|
1070
|
-
...invocationParams,
|
|
1071
|
-
stream: false,
|
|
1072
|
-
}, { signal: options?.signal, ...options?.options });
|
|
1073
|
-
return {
|
|
1074
|
-
generations: [
|
|
1075
|
-
{
|
|
1076
|
-
text: data.output_text,
|
|
1077
|
-
message: this._convertResponsesMessageToBaseMessage(data),
|
|
1078
|
-
},
|
|
1079
|
-
],
|
|
1080
|
-
llmOutput: {
|
|
1081
|
-
id: data.id,
|
|
1082
|
-
estimatedTokenUsage: data.usage
|
|
1083
|
-
? {
|
|
1084
|
-
promptTokens: data.usage.input_tokens,
|
|
1085
|
-
completionTokens: data.usage.output_tokens,
|
|
1086
|
-
totalTokens: data.usage.total_tokens,
|
|
1087
|
-
}
|
|
1088
|
-
: undefined,
|
|
1089
|
-
},
|
|
1090
|
-
};
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
async *_streamResponseChunks(messages, options) {
|
|
1094
|
-
const streamIterable = await this.completionWithRetry({
|
|
1095
|
-
...this.invocationParams(options),
|
|
1096
|
-
input: this._convertMessagesToResponsesParams(messages),
|
|
1097
|
-
stream: true,
|
|
1098
|
-
}, options);
|
|
1099
|
-
for await (const data of streamIterable) {
|
|
1100
|
-
const chunk = this._convertResponsesDeltaToBaseMessageChunk(data);
|
|
1101
|
-
if (chunk == null)
|
|
1102
|
-
continue;
|
|
1103
|
-
yield chunk;
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
async completionWithRetry(request, requestOptions) {
|
|
1107
|
-
return this.caller.call(async () => {
|
|
1108
|
-
const clientOptions = this._getClientOptions(requestOptions);
|
|
1109
|
-
try {
|
|
1110
|
-
// use parse if dealing with json_schema
|
|
1111
|
-
if (request.text?.format?.type === "json_schema" && !request.stream) {
|
|
1112
|
-
return await this.client.responses.parse(request, clientOptions);
|
|
1113
|
-
}
|
|
1114
|
-
return await this.client.responses.create(request, clientOptions);
|
|
1115
|
-
}
|
|
1116
|
-
catch (e) {
|
|
1117
|
-
const error = wrapOpenAIClientError(e);
|
|
1118
|
-
throw error;
|
|
1119
|
-
}
|
|
1120
|
-
});
|
|
1121
|
-
}
|
|
1122
|
-
/** @internal */
|
|
1123
|
-
_convertResponsesMessageToBaseMessage(response) {
|
|
1124
|
-
if (response.error) {
|
|
1125
|
-
// TODO: add support for `addLangChainErrorFields`
|
|
1126
|
-
const error = new Error(response.error.message);
|
|
1127
|
-
error.name = response.error.code;
|
|
1128
|
-
throw error;
|
|
1129
|
-
}
|
|
1130
|
-
let messageId;
|
|
1131
|
-
const content = [];
|
|
1132
|
-
const tool_calls = [];
|
|
1133
|
-
const invalid_tool_calls = [];
|
|
1134
|
-
const response_metadata = {
|
|
1135
|
-
model: response.model,
|
|
1136
|
-
created_at: response.created_at,
|
|
1137
|
-
id: response.id,
|
|
1138
|
-
incomplete_details: response.incomplete_details,
|
|
1139
|
-
metadata: response.metadata,
|
|
1140
|
-
object: response.object,
|
|
1141
|
-
status: response.status,
|
|
1142
|
-
user: response.user,
|
|
1143
|
-
service_tier: response.service_tier,
|
|
1144
|
-
// for compatibility with chat completion calls.
|
|
1145
|
-
model_name: response.model,
|
|
1146
|
-
};
|
|
1147
|
-
const additional_kwargs = {};
|
|
1148
|
-
for (const item of response.output) {
|
|
1149
|
-
if (item.type === "message") {
|
|
1150
|
-
messageId = item.id;
|
|
1151
|
-
content.push(...item.content.flatMap((part) => {
|
|
1152
|
-
if (part.type === "output_text") {
|
|
1153
|
-
if ("parsed" in part && part.parsed != null) {
|
|
1154
|
-
additional_kwargs.parsed = part.parsed;
|
|
1155
|
-
}
|
|
1156
|
-
return {
|
|
1157
|
-
type: "text",
|
|
1158
|
-
text: part.text,
|
|
1159
|
-
annotations: part.annotations,
|
|
1160
|
-
};
|
|
1161
|
-
}
|
|
1162
|
-
if (part.type === "refusal") {
|
|
1163
|
-
additional_kwargs.refusal = part.refusal;
|
|
1164
|
-
return [];
|
|
1165
|
-
}
|
|
1166
|
-
return part;
|
|
1167
|
-
}));
|
|
1168
|
-
}
|
|
1169
|
-
else if (item.type === "function_call") {
|
|
1170
|
-
const fnAdapter = {
|
|
1171
|
-
function: { name: item.name, arguments: item.arguments },
|
|
1172
|
-
id: item.call_id,
|
|
1173
|
-
};
|
|
1174
|
-
try {
|
|
1175
|
-
tool_calls.push(parseToolCall(fnAdapter, { returnId: true }));
|
|
1176
|
-
}
|
|
1177
|
-
catch (e) {
|
|
1178
|
-
let errMessage;
|
|
1179
|
-
if (typeof e === "object" &&
|
|
1180
|
-
e != null &&
|
|
1181
|
-
"message" in e &&
|
|
1182
|
-
typeof e.message === "string") {
|
|
1183
|
-
errMessage = e.message;
|
|
1184
|
-
}
|
|
1185
|
-
invalid_tool_calls.push(makeInvalidToolCall(fnAdapter, errMessage));
|
|
1186
|
-
}
|
|
1187
|
-
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
|
|
1188
|
-
if (item.id) {
|
|
1189
|
-
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
else if (item.type === "reasoning") {
|
|
1193
|
-
additional_kwargs.reasoning = item;
|
|
1194
|
-
}
|
|
1195
|
-
else {
|
|
1196
|
-
additional_kwargs.tool_outputs ??= [];
|
|
1197
|
-
additional_kwargs.tool_outputs.push(item);
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
return new AIMessage({
|
|
1201
|
-
id: messageId,
|
|
1202
|
-
content,
|
|
1203
|
-
tool_calls,
|
|
1204
|
-
invalid_tool_calls,
|
|
1205
|
-
usage_metadata: response.usage,
|
|
1206
|
-
additional_kwargs,
|
|
1207
|
-
response_metadata,
|
|
1208
|
-
});
|
|
1209
|
-
}
|
|
1210
|
-
/** @internal */
|
|
1211
|
-
_convertResponsesDeltaToBaseMessageChunk(chunk) {
|
|
1212
|
-
const content = [];
|
|
1213
|
-
let generationInfo = {};
|
|
1214
|
-
let usage_metadata;
|
|
1215
|
-
const tool_call_chunks = [];
|
|
1216
|
-
const response_metadata = {};
|
|
1217
|
-
const additional_kwargs = {};
|
|
1218
|
-
let id;
|
|
1219
|
-
if (chunk.type === "response.output_text.delta") {
|
|
1220
|
-
content.push({
|
|
1221
|
-
type: "text",
|
|
1222
|
-
text: chunk.delta,
|
|
1223
|
-
index: chunk.content_index,
|
|
1224
|
-
});
|
|
1225
|
-
}
|
|
1226
|
-
else if (chunk.type === "response.output_text_annotation.added") {
|
|
1227
|
-
content.push({
|
|
1228
|
-
type: "text",
|
|
1229
|
-
text: "",
|
|
1230
|
-
annotations: [chunk.annotation],
|
|
1231
|
-
index: chunk.content_index,
|
|
1232
|
-
});
|
|
1233
|
-
}
|
|
1234
|
-
else if (chunk.type === "response.output_item.added" &&
|
|
1235
|
-
chunk.item.type === "message") {
|
|
1236
|
-
id = chunk.item.id;
|
|
1237
|
-
}
|
|
1238
|
-
else if (chunk.type === "response.output_item.added" &&
|
|
1239
|
-
chunk.item.type === "function_call") {
|
|
1240
|
-
tool_call_chunks.push({
|
|
1241
|
-
type: "tool_call_chunk",
|
|
1242
|
-
name: chunk.item.name,
|
|
1243
|
-
args: chunk.item.arguments,
|
|
1244
|
-
id: chunk.item.call_id,
|
|
1245
|
-
index: chunk.output_index,
|
|
1246
|
-
});
|
|
1247
|
-
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = {
|
|
1248
|
-
[chunk.item.call_id]: chunk.item.id,
|
|
1249
|
-
};
|
|
1250
|
-
}
|
|
1251
|
-
else if (chunk.type === "response.output_item.done" &&
|
|
1252
|
-
[
|
|
1253
|
-
"web_search_call",
|
|
1254
|
-
"file_search_call",
|
|
1255
|
-
"computer_call",
|
|
1256
|
-
"code_interpreter_call",
|
|
1257
|
-
"mcp_call",
|
|
1258
|
-
"mcp_list_tools",
|
|
1259
|
-
"mcp_approval_request",
|
|
1260
|
-
"image_generation_call",
|
|
1261
|
-
].includes(chunk.item.type)) {
|
|
1262
|
-
additional_kwargs.tool_outputs = [chunk.item];
|
|
1263
|
-
}
|
|
1264
|
-
else if (chunk.type === "response.created") {
|
|
1265
|
-
response_metadata.id = chunk.response.id;
|
|
1266
|
-
response_metadata.model_name = chunk.response.model;
|
|
1267
|
-
response_metadata.model = chunk.response.model;
|
|
1268
|
-
}
|
|
1269
|
-
else if (chunk.type === "response.completed") {
|
|
1270
|
-
const msg = this._convertResponsesMessageToBaseMessage(chunk.response);
|
|
1271
|
-
usage_metadata = chunk.response.usage;
|
|
1272
|
-
if (chunk.response.text?.format?.type === "json_schema") {
|
|
1273
|
-
additional_kwargs.parsed ??= JSON.parse(msg.text);
|
|
1274
|
-
}
|
|
1275
|
-
for (const [key, value] of Object.entries(chunk.response)) {
|
|
1276
|
-
if (key !== "id")
|
|
1277
|
-
response_metadata[key] = value;
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
else if (chunk.type === "response.function_call_arguments.delta") {
|
|
1281
|
-
tool_call_chunks.push({
|
|
1282
|
-
type: "tool_call_chunk",
|
|
1283
|
-
args: chunk.delta,
|
|
1284
|
-
index: chunk.output_index,
|
|
1285
|
-
});
|
|
1286
|
-
}
|
|
1287
|
-
else if (chunk.type === "response.web_search_call.completed" ||
|
|
1288
|
-
chunk.type === "response.file_search_call.completed") {
|
|
1289
|
-
generationInfo = {
|
|
1290
|
-
tool_outputs: {
|
|
1291
|
-
id: chunk.item_id,
|
|
1292
|
-
type: chunk.type.replace("response.", "").replace(".completed", ""),
|
|
1293
|
-
status: "completed",
|
|
1294
|
-
},
|
|
1295
|
-
};
|
|
1296
|
-
}
|
|
1297
|
-
else if (chunk.type === "response.refusal.done") {
|
|
1298
|
-
additional_kwargs.refusal = chunk.refusal;
|
|
1299
|
-
}
|
|
1300
|
-
else if (chunk.type === "response.output_item.added" &&
|
|
1301
|
-
"item" in chunk &&
|
|
1302
|
-
chunk.item.type === "reasoning") {
|
|
1303
|
-
const summary = chunk
|
|
1304
|
-
.item.summary
|
|
1305
|
-
? chunk.item.summary.map((s, index) => ({
|
|
1306
|
-
...s,
|
|
1307
|
-
index,
|
|
1308
|
-
}))
|
|
1309
|
-
: undefined;
|
|
1310
|
-
additional_kwargs.reasoning = {
|
|
1311
|
-
// We only capture ID in the first chunk or else the concatenated result of all chunks will
|
|
1312
|
-
// have an ID field that is repeated once per chunk. There is special handling for the `type`
|
|
1313
|
-
// field that prevents this, however.
|
|
1314
|
-
id: chunk.item.id,
|
|
1315
|
-
type: chunk.item.type,
|
|
1316
|
-
...(summary ? { summary } : {}),
|
|
1317
|
-
};
|
|
1318
|
-
}
|
|
1319
|
-
else if (chunk.type === "response.reasoning_summary_part.added") {
|
|
1320
|
-
additional_kwargs.reasoning = {
|
|
1321
|
-
type: "reasoning",
|
|
1322
|
-
summary: [{ ...chunk.part, index: chunk.summary_index }],
|
|
1323
|
-
};
|
|
1324
|
-
}
|
|
1325
|
-
else if (chunk.type === "response.reasoning_summary_text.delta") {
|
|
1326
|
-
additional_kwargs.reasoning = {
|
|
1327
|
-
type: "reasoning",
|
|
1328
|
-
summary: [
|
|
1329
|
-
{
|
|
1330
|
-
text: chunk.delta,
|
|
1331
|
-
type: "summary_text",
|
|
1332
|
-
index: chunk.summary_index,
|
|
1333
|
-
},
|
|
1334
|
-
],
|
|
1335
|
-
};
|
|
1336
|
-
}
|
|
1337
|
-
else if (chunk.type === "response.image_generation_call.partial_image") {
|
|
1338
|
-
// noop/fixme: retaining partial images in a message chunk means that _all_
|
|
1339
|
-
// partial images get kept in history, so we don't do anything here.
|
|
1340
|
-
return null;
|
|
1341
|
-
}
|
|
1342
|
-
else {
|
|
1343
|
-
return null;
|
|
1344
|
-
}
|
|
1345
|
-
return new ChatGenerationChunk({
|
|
1346
|
-
// Legacy reasons, `onLLMNewToken` should pulls this out
|
|
1347
|
-
text: content.map((part) => part.text).join(""),
|
|
1348
|
-
message: new AIMessageChunk({
|
|
1349
|
-
id,
|
|
1350
|
-
content,
|
|
1351
|
-
tool_call_chunks,
|
|
1352
|
-
usage_metadata,
|
|
1353
|
-
additional_kwargs,
|
|
1354
|
-
response_metadata,
|
|
1355
|
-
}),
|
|
1356
|
-
generationInfo,
|
|
1357
|
-
});
|
|
1358
|
-
}
|
|
1359
|
-
/** @internal */
|
|
1360
|
-
_convertMessagesToResponsesParams(messages) {
|
|
1361
|
-
return messages.flatMap((lcMsg) => {
|
|
1362
|
-
const additional_kwargs = lcMsg.additional_kwargs;
|
|
1363
|
-
let role = messageToOpenAIRole(lcMsg);
|
|
1364
|
-
if (role === "system" && isReasoningModel(this.model))
|
|
1365
|
-
role = "developer";
|
|
1366
|
-
if (role === "function") {
|
|
1367
|
-
throw new Error("Function messages are not supported in Responses API");
|
|
1368
|
-
}
|
|
1369
|
-
if (role === "tool") {
|
|
1370
|
-
const toolMessage = lcMsg;
|
|
1371
|
-
// Handle computer call output
|
|
1372
|
-
if (additional_kwargs?.type === "computer_call_output") {
|
|
1373
|
-
const output = (() => {
|
|
1374
|
-
if (typeof toolMessage.content === "string") {
|
|
1375
|
-
return {
|
|
1376
|
-
type: "computer_screenshot",
|
|
1377
|
-
image_url: toolMessage.content,
|
|
1378
|
-
};
|
|
1379
|
-
}
|
|
1380
|
-
if (Array.isArray(toolMessage.content)) {
|
|
1381
|
-
const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
|
|
1382
|
-
if (oaiScreenshot)
|
|
1383
|
-
return oaiScreenshot;
|
|
1384
|
-
const lcImage = toolMessage.content.find((i) => i.type === "image_url");
|
|
1385
|
-
if (lcImage) {
|
|
1386
|
-
return {
|
|
1387
|
-
type: "computer_screenshot",
|
|
1388
|
-
image_url: typeof lcImage.image_url === "string"
|
|
1389
|
-
? lcImage.image_url
|
|
1390
|
-
: lcImage.image_url.url,
|
|
1391
|
-
};
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
throw new Error("Invalid computer call output");
|
|
1395
|
-
})();
|
|
1396
|
-
return {
|
|
1397
|
-
type: "computer_call_output",
|
|
1398
|
-
output,
|
|
1399
|
-
call_id: toolMessage.tool_call_id,
|
|
1400
|
-
};
|
|
1401
|
-
}
|
|
1402
|
-
return {
|
|
1403
|
-
type: "function_call_output",
|
|
1404
|
-
call_id: toolMessage.tool_call_id,
|
|
1405
|
-
id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : undefined,
|
|
1406
|
-
output: typeof toolMessage.content !== "string"
|
|
1407
|
-
? JSON.stringify(toolMessage.content)
|
|
1408
|
-
: toolMessage.content,
|
|
1409
|
-
};
|
|
1410
|
-
}
|
|
1411
|
-
if (role === "assistant") {
|
|
1412
|
-
// if we have the original response items, just reuse them
|
|
1413
|
-
if (!this.zdrEnabled &&
|
|
1414
|
-
lcMsg.response_metadata.output != null &&
|
|
1415
|
-
Array.isArray(lcMsg.response_metadata.output) &&
|
|
1416
|
-
lcMsg.response_metadata.output.length > 0 &&
|
|
1417
|
-
lcMsg.response_metadata.output.every((item) => "type" in item)) {
|
|
1418
|
-
return lcMsg.response_metadata.output;
|
|
1419
|
-
}
|
|
1420
|
-
// otherwise, try to reconstruct the response from what we have
|
|
1421
|
-
const input = [];
|
|
1422
|
-
// reasoning items
|
|
1423
|
-
if (additional_kwargs?.reasoning && !this.zdrEnabled) {
|
|
1424
|
-
const reasoningItem = this._convertReasoningSummary(additional_kwargs.reasoning);
|
|
1425
|
-
input.push(reasoningItem);
|
|
1426
|
-
}
|
|
1427
|
-
// ai content
|
|
1428
|
-
let { content } = lcMsg;
|
|
1429
|
-
if (additional_kwargs?.refusal) {
|
|
1430
|
-
if (typeof content === "string") {
|
|
1431
|
-
content = [
|
|
1432
|
-
{ type: "output_text", text: content, annotations: [] },
|
|
1433
|
-
];
|
|
1434
|
-
}
|
|
1435
|
-
content = [
|
|
1436
|
-
...content,
|
|
1437
|
-
{ type: "refusal", refusal: additional_kwargs.refusal },
|
|
1438
|
-
];
|
|
1439
|
-
}
|
|
1440
|
-
input.push({
|
|
1441
|
-
type: "message",
|
|
1442
|
-
role: "assistant",
|
|
1443
|
-
...(lcMsg.id && !this.zdrEnabled && lcMsg.id.startsWith("msg_")
|
|
1444
|
-
? { id: lcMsg.id }
|
|
1445
|
-
: {}),
|
|
1446
|
-
content: typeof content === "string"
|
|
1447
|
-
? content
|
|
1448
|
-
: content.flatMap((item) => {
|
|
1449
|
-
if (item.type === "text") {
|
|
1450
|
-
return {
|
|
1451
|
-
type: "output_text",
|
|
1452
|
-
text: item.text,
|
|
1453
|
-
// @ts-expect-error TODO: add types for `annotations`
|
|
1454
|
-
annotations: item.annotations ?? [],
|
|
1455
|
-
};
|
|
1456
|
-
}
|
|
1457
|
-
if (item.type === "output_text" ||
|
|
1458
|
-
item.type === "refusal") {
|
|
1459
|
-
return item;
|
|
1460
|
-
}
|
|
1461
|
-
return [];
|
|
1462
|
-
}),
|
|
1463
|
-
});
|
|
1464
|
-
const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
|
|
1465
|
-
if (isAIMessage(lcMsg) && !!lcMsg.tool_calls?.length) {
|
|
1466
|
-
input.push(...lcMsg.tool_calls.map((toolCall) => ({
|
|
1467
|
-
type: "function_call",
|
|
1468
|
-
name: toolCall.name,
|
|
1469
|
-
arguments: JSON.stringify(toolCall.args),
|
|
1470
|
-
call_id: toolCall.id,
|
|
1471
|
-
...(this.zdrEnabled
|
|
1472
|
-
? { id: functionCallIds?.[toolCall.id] }
|
|
1473
|
-
: {}),
|
|
1474
|
-
})));
|
|
1475
|
-
}
|
|
1476
|
-
else if (additional_kwargs?.tool_calls) {
|
|
1477
|
-
input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
|
|
1478
|
-
type: "function_call",
|
|
1479
|
-
name: toolCall.function.name,
|
|
1480
|
-
call_id: toolCall.id,
|
|
1481
|
-
arguments: toolCall.function.arguments,
|
|
1482
|
-
...(this.zdrEnabled
|
|
1483
|
-
? { id: functionCallIds?.[toolCall.id] }
|
|
1484
|
-
: {}),
|
|
1485
|
-
})));
|
|
1486
|
-
}
|
|
1487
|
-
const toolOutputs = lcMsg.response_metadata.output?.length
|
|
1488
|
-
? lcMsg.response_metadata.output
|
|
1489
|
-
: additional_kwargs.tool_outputs;
|
|
1490
|
-
const fallthroughCallTypes = [
|
|
1491
|
-
"computer_call",
|
|
1492
|
-
"mcp_call",
|
|
1493
|
-
"code_interpreter_call",
|
|
1494
|
-
"image_generation_call",
|
|
1495
|
-
];
|
|
1496
|
-
if (toolOutputs != null) {
|
|
1497
|
-
const castToolOutputs = toolOutputs;
|
|
1498
|
-
const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
|
|
1499
|
-
if (fallthroughCalls.length > 0)
|
|
1500
|
-
input.push(...fallthroughCalls);
|
|
1501
|
-
}
|
|
1502
|
-
return input;
|
|
1503
|
-
}
|
|
1504
|
-
if (role === "user" || role === "system" || role === "developer") {
|
|
1505
|
-
if (typeof lcMsg.content === "string") {
|
|
1506
|
-
return { type: "message", role, content: lcMsg.content };
|
|
1507
|
-
}
|
|
1508
|
-
const messages = [];
|
|
1509
|
-
const content = lcMsg.content.flatMap((item) => {
|
|
1510
|
-
if (item.type === "mcp_approval_response") {
|
|
1511
|
-
messages.push({
|
|
1512
|
-
type: "mcp_approval_response",
|
|
1513
|
-
approval_request_id: item.approval_request_id,
|
|
1514
|
-
approve: item.approve,
|
|
1515
|
-
});
|
|
1516
|
-
}
|
|
1517
|
-
if (isDataContentBlock(item)) {
|
|
1518
|
-
return convertToProviderContentBlock(item, completionsApiContentBlockConverter);
|
|
1519
|
-
}
|
|
1520
|
-
if (item.type === "text") {
|
|
1521
|
-
return {
|
|
1522
|
-
type: "input_text",
|
|
1523
|
-
text: item.text,
|
|
1524
|
-
};
|
|
1525
|
-
}
|
|
1526
|
-
if (item.type === "image_url") {
|
|
1527
|
-
return {
|
|
1528
|
-
type: "input_image",
|
|
1529
|
-
image_url: typeof item.image_url === "string"
|
|
1530
|
-
? item.image_url
|
|
1531
|
-
: item.image_url.url,
|
|
1532
|
-
detail: typeof item.image_url === "string"
|
|
1533
|
-
? "auto"
|
|
1534
|
-
: item.image_url.detail,
|
|
1535
|
-
};
|
|
1536
|
-
}
|
|
1537
|
-
if (item.type === "input_text" ||
|
|
1538
|
-
item.type === "input_image" ||
|
|
1539
|
-
item.type === "input_file") {
|
|
1540
|
-
return item;
|
|
1541
|
-
}
|
|
1542
|
-
return [];
|
|
1543
|
-
});
|
|
1544
|
-
if (content.length > 0) {
|
|
1545
|
-
messages.push({ type: "message", role, content });
|
|
1546
|
-
}
|
|
1547
|
-
return messages;
|
|
1548
|
-
}
|
|
1549
|
-
console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
|
|
1550
|
-
return [];
|
|
1551
|
-
});
|
|
1552
|
-
}
|
|
1553
|
-
/** @internal */
|
|
1554
|
-
_convertReasoningSummary(reasoning) {
|
|
1555
|
-
// combine summary parts that have the the same index and then remove the indexes
|
|
1556
|
-
const summary = (reasoning.summary.length > 1
|
|
1557
|
-
? reasoning.summary.reduce((acc, curr) => {
|
|
1558
|
-
const last = acc.at(-1);
|
|
1559
|
-
if (last.index === curr.index) {
|
|
1560
|
-
last.text += curr.text;
|
|
1561
|
-
}
|
|
1562
|
-
else {
|
|
1563
|
-
acc.push(curr);
|
|
1564
|
-
}
|
|
1565
|
-
return acc;
|
|
1566
|
-
}, [{ ...reasoning.summary[0] }])
|
|
1567
|
-
: reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
|
|
1568
|
-
return {
|
|
1569
|
-
...reasoning,
|
|
1570
|
-
summary,
|
|
1571
|
-
};
|
|
1572
|
-
}
|
|
1573
|
-
/** @internal */
|
|
1574
|
-
_reduceChatOpenAITools(tools, fields) {
|
|
1575
|
-
const reducedTools = [];
|
|
1576
|
-
for (const tool of tools) {
|
|
1577
|
-
if (isBuiltInTool(tool)) {
|
|
1578
|
-
if (tool.type === "image_generation" && fields?.stream) {
|
|
1579
|
-
// OpenAI sends a 400 error if partial_images is not set and we want to stream.
|
|
1580
|
-
// We also set it to 1 since we don't support partial images yet.
|
|
1581
|
-
tool.partial_images = 1;
|
|
1582
|
-
}
|
|
1583
|
-
reducedTools.push(tool);
|
|
1584
|
-
}
|
|
1585
|
-
else if (isOpenAITool(tool)) {
|
|
1586
|
-
reducedTools.push({
|
|
1587
|
-
type: "function",
|
|
1588
|
-
name: tool.function.name,
|
|
1589
|
-
parameters: tool.function.parameters,
|
|
1590
|
-
description: tool.function.description,
|
|
1591
|
-
strict: fields?.strict ?? null,
|
|
1592
|
-
});
|
|
1593
|
-
}
|
|
1594
|
-
}
|
|
1595
|
-
return reducedTools;
|
|
1596
|
-
}
|
|
1597
|
-
}
|
|
1598
|
-
/**
|
|
1599
|
-
* OpenAI Completions API implementation.
|
|
1600
|
-
* @internal
|
|
1601
|
-
*/
|
|
1602
|
-
export class ChatOpenAICompletions extends BaseChatOpenAI {
|
|
1603
|
-
/** @internal */
|
|
1604
|
-
invocationParams(options, extra) {
|
|
1605
|
-
let strict;
|
|
1606
|
-
if (options?.strict !== undefined) {
|
|
1607
|
-
strict = options.strict;
|
|
1608
|
-
}
|
|
1609
|
-
else if (this.supportsStrictToolCalling !== undefined) {
|
|
1610
|
-
strict = this.supportsStrictToolCalling;
|
|
1611
|
-
}
|
|
1612
|
-
let streamOptionsConfig = {};
|
|
1613
|
-
if (options?.stream_options !== undefined) {
|
|
1614
|
-
streamOptionsConfig = { stream_options: options.stream_options };
|
|
1615
|
-
}
|
|
1616
|
-
else if (this.streamUsage && (this.streaming || extra?.streaming)) {
|
|
1617
|
-
streamOptionsConfig = { stream_options: { include_usage: true } };
|
|
1618
|
-
}
|
|
1619
|
-
const params = {
|
|
1620
|
-
model: this.model,
|
|
1621
|
-
temperature: this.temperature,
|
|
1622
|
-
top_p: this.topP,
|
|
1623
|
-
frequency_penalty: this.frequencyPenalty,
|
|
1624
|
-
presence_penalty: this.presencePenalty,
|
|
1625
|
-
logprobs: this.logprobs,
|
|
1626
|
-
top_logprobs: this.topLogprobs,
|
|
1627
|
-
n: this.n,
|
|
1628
|
-
logit_bias: this.logitBias,
|
|
1629
|
-
stop: options?.stop ?? this.stopSequences,
|
|
1630
|
-
user: this.user,
|
|
1631
|
-
// if include_usage is set or streamUsage then stream must be set to true.
|
|
1632
|
-
stream: this.streaming,
|
|
1633
|
-
functions: options?.functions,
|
|
1634
|
-
function_call: options?.function_call,
|
|
1635
|
-
tools: options?.tools?.length
|
|
1636
|
-
? options.tools.map((tool) => this._convertChatOpenAIToolToCompletionsTool(tool, { strict }))
|
|
1637
|
-
: undefined,
|
|
1638
|
-
tool_choice: formatToOpenAIToolChoice(options?.tool_choice),
|
|
1639
|
-
response_format: this._getResponseFormat(options?.response_format),
|
|
1640
|
-
seed: options?.seed,
|
|
1641
|
-
...streamOptionsConfig,
|
|
1642
|
-
parallel_tool_calls: options?.parallel_tool_calls,
|
|
1643
|
-
...(this.audio || options?.audio
|
|
1644
|
-
? { audio: this.audio || options?.audio }
|
|
1645
|
-
: {}),
|
|
1646
|
-
...(this.modalities || options?.modalities
|
|
1647
|
-
? { modalities: this.modalities || options?.modalities }
|
|
1648
|
-
: {}),
|
|
1649
|
-
...this.modelKwargs,
|
|
1650
|
-
};
|
|
1651
|
-
if (options?.prediction !== undefined) {
|
|
1652
|
-
params.prediction = options.prediction;
|
|
1653
|
-
}
|
|
1654
|
-
if (this.service_tier !== undefined) {
|
|
1655
|
-
params.service_tier = this.service_tier;
|
|
1656
|
-
}
|
|
1657
|
-
if (options?.service_tier !== undefined) {
|
|
1658
|
-
params.service_tier = options.service_tier;
|
|
1659
|
-
}
|
|
1660
|
-
const reasoning = this._getReasoningParams(options);
|
|
1661
|
-
if (reasoning !== undefined && reasoning.effort !== undefined) {
|
|
1662
|
-
params.reasoning_effort = reasoning.effort;
|
|
1663
|
-
}
|
|
1664
|
-
if (isReasoningModel(params.model)) {
|
|
1665
|
-
params.max_completion_tokens =
|
|
1666
|
-
this.maxTokens === -1 ? undefined : this.maxTokens;
|
|
1667
|
-
}
|
|
1668
|
-
else {
|
|
1669
|
-
params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
|
|
1670
|
-
}
|
|
1671
|
-
return params;
|
|
1672
|
-
}
|
|
1673
|
-
async _generate(messages, options, runManager) {
|
|
1674
|
-
const usageMetadata = {};
|
|
1675
|
-
const params = this.invocationParams(options);
|
|
1676
|
-
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
1677
|
-
if (params.stream) {
|
|
1678
|
-
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
1679
|
-
const finalChunks = {};
|
|
1680
|
-
for await (const chunk of stream) {
|
|
1681
|
-
chunk.message.response_metadata = {
|
|
1682
|
-
...chunk.generationInfo,
|
|
1683
|
-
...chunk.message.response_metadata,
|
|
1684
|
-
};
|
|
1685
|
-
const index = chunk.generationInfo?.completion ?? 0;
|
|
1686
|
-
if (finalChunks[index] === undefined) {
|
|
1687
|
-
finalChunks[index] = chunk;
|
|
1688
|
-
}
|
|
1689
|
-
else {
|
|
1690
|
-
finalChunks[index] = finalChunks[index].concat(chunk);
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
const generations = Object.entries(finalChunks)
|
|
1694
|
-
.sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
|
|
1695
|
-
.map(([_, value]) => value);
|
|
1696
|
-
const { functions, function_call } = this.invocationParams(options);
|
|
1697
|
-
// OpenAI does not support token usage report under stream mode,
|
|
1698
|
-
// fallback to estimation.
|
|
1699
|
-
const promptTokenUsage = await this._getEstimatedTokenCountFromPrompt(messages, functions, function_call);
|
|
1700
|
-
const completionTokenUsage = await this._getNumTokensFromGenerations(generations);
|
|
1701
|
-
usageMetadata.input_tokens = promptTokenUsage;
|
|
1702
|
-
usageMetadata.output_tokens = completionTokenUsage;
|
|
1703
|
-
usageMetadata.total_tokens = promptTokenUsage + completionTokenUsage;
|
|
1704
|
-
return {
|
|
1705
|
-
generations,
|
|
1706
|
-
llmOutput: {
|
|
1707
|
-
estimatedTokenUsage: {
|
|
1708
|
-
promptTokens: usageMetadata.input_tokens,
|
|
1709
|
-
completionTokens: usageMetadata.output_tokens,
|
|
1710
|
-
totalTokens: usageMetadata.total_tokens,
|
|
1711
|
-
},
|
|
1712
|
-
},
|
|
1713
|
-
};
|
|
1714
|
-
}
|
|
1715
|
-
else {
|
|
1716
|
-
const data = await this.completionWithRetry({
|
|
1717
|
-
...params,
|
|
1718
|
-
stream: false,
|
|
1719
|
-
messages: messagesMapped,
|
|
1720
|
-
}, {
|
|
1721
|
-
signal: options?.signal,
|
|
1722
|
-
...options?.options,
|
|
1723
|
-
});
|
|
1724
|
-
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, prompt_tokens_details: promptTokensDetails, completion_tokens_details: completionTokensDetails, } = data?.usage ?? {};
|
|
1725
|
-
if (completionTokens) {
|
|
1726
|
-
usageMetadata.output_tokens =
|
|
1727
|
-
(usageMetadata.output_tokens ?? 0) + completionTokens;
|
|
1728
|
-
}
|
|
1729
|
-
if (promptTokens) {
|
|
1730
|
-
usageMetadata.input_tokens =
|
|
1731
|
-
(usageMetadata.input_tokens ?? 0) + promptTokens;
|
|
1732
|
-
}
|
|
1733
|
-
if (totalTokens) {
|
|
1734
|
-
usageMetadata.total_tokens =
|
|
1735
|
-
(usageMetadata.total_tokens ?? 0) + totalTokens;
|
|
1736
|
-
}
|
|
1737
|
-
if (promptTokensDetails?.audio_tokens !== null ||
|
|
1738
|
-
promptTokensDetails?.cached_tokens !== null) {
|
|
1739
|
-
usageMetadata.input_token_details = {
|
|
1740
|
-
...(promptTokensDetails?.audio_tokens !== null && {
|
|
1741
|
-
audio: promptTokensDetails?.audio_tokens,
|
|
1742
|
-
}),
|
|
1743
|
-
...(promptTokensDetails?.cached_tokens !== null && {
|
|
1744
|
-
cache_read: promptTokensDetails?.cached_tokens,
|
|
1745
|
-
}),
|
|
1746
|
-
};
|
|
1747
|
-
}
|
|
1748
|
-
if (completionTokensDetails?.audio_tokens !== null ||
|
|
1749
|
-
completionTokensDetails?.reasoning_tokens !== null) {
|
|
1750
|
-
usageMetadata.output_token_details = {
|
|
1751
|
-
...(completionTokensDetails?.audio_tokens !== null && {
|
|
1752
|
-
audio: completionTokensDetails?.audio_tokens,
|
|
1753
|
-
}),
|
|
1754
|
-
...(completionTokensDetails?.reasoning_tokens !== null && {
|
|
1755
|
-
reasoning: completionTokensDetails?.reasoning_tokens,
|
|
1756
|
-
}),
|
|
1757
|
-
};
|
|
1758
|
-
}
|
|
1759
|
-
const generations = [];
|
|
1760
|
-
for (const part of data?.choices ?? []) {
|
|
1761
|
-
const text = part.message?.content ?? "";
|
|
1762
|
-
const generation = {
|
|
1763
|
-
text,
|
|
1764
|
-
message: this._convertCompletionsMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
|
|
1765
|
-
};
|
|
1766
|
-
generation.generationInfo = {
|
|
1767
|
-
...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
|
|
1768
|
-
...(part.logprobs ? { logprobs: part.logprobs } : {}),
|
|
1769
|
-
};
|
|
1770
|
-
if (isAIMessage(generation.message)) {
|
|
1771
|
-
generation.message.usage_metadata = usageMetadata;
|
|
1772
|
-
}
|
|
1773
|
-
// Fields are not serialized unless passed to the constructor
|
|
1774
|
-
// Doing this ensures all fields on the message are serialized
|
|
1775
|
-
generation.message = new AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith("lc_"))));
|
|
1776
|
-
generations.push(generation);
|
|
1777
|
-
}
|
|
1778
|
-
return {
|
|
1779
|
-
generations,
|
|
1780
|
-
llmOutput: {
|
|
1781
|
-
tokenUsage: {
|
|
1782
|
-
promptTokens: usageMetadata.input_tokens,
|
|
1783
|
-
completionTokens: usageMetadata.output_tokens,
|
|
1784
|
-
totalTokens: usageMetadata.total_tokens,
|
|
1785
|
-
},
|
|
1786
|
-
},
|
|
1787
|
-
};
|
|
1788
|
-
}
|
|
1789
|
-
}
|
|
1790
|
-
async *_streamResponseChunks(messages, options, runManager) {
|
|
1791
|
-
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
1792
|
-
const params = {
|
|
1793
|
-
...this.invocationParams(options, {
|
|
1794
|
-
streaming: true,
|
|
1795
|
-
}),
|
|
1796
|
-
messages: messagesMapped,
|
|
1797
|
-
stream: true,
|
|
1798
|
-
};
|
|
1799
|
-
let defaultRole;
|
|
1800
|
-
const streamIterable = await this.completionWithRetry(params, options);
|
|
1801
|
-
let usage;
|
|
1802
|
-
for await (const data of streamIterable) {
|
|
1803
|
-
const choice = data?.choices?.[0];
|
|
1804
|
-
if (data.usage) {
|
|
1805
|
-
usage = data.usage;
|
|
1806
|
-
}
|
|
1807
|
-
if (!choice) {
|
|
1808
|
-
continue;
|
|
1809
|
-
}
|
|
1810
|
-
const { delta } = choice;
|
|
1811
|
-
if (!delta) {
|
|
1812
|
-
continue;
|
|
1813
|
-
}
|
|
1814
|
-
const chunk = this._convertCompletionsDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
1815
|
-
defaultRole = delta.role ?? defaultRole;
|
|
1816
|
-
const newTokenIndices = {
|
|
1817
|
-
prompt: options.promptIndex ?? 0,
|
|
1818
|
-
completion: choice.index ?? 0,
|
|
1819
|
-
};
|
|
1820
|
-
if (typeof chunk.content !== "string") {
|
|
1821
|
-
console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
|
|
1822
|
-
continue;
|
|
1823
|
-
}
|
|
1824
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1825
|
-
const generationInfo = { ...newTokenIndices };
|
|
1826
|
-
if (choice.finish_reason != null) {
|
|
1827
|
-
generationInfo.finish_reason = choice.finish_reason;
|
|
1828
|
-
// Only include system fingerprint in the last chunk for now
|
|
1829
|
-
// to avoid concatenation issues
|
|
1830
|
-
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
1831
|
-
generationInfo.model_name = data.model;
|
|
1832
|
-
generationInfo.service_tier = data.service_tier;
|
|
1833
|
-
}
|
|
1834
|
-
if (this.logprobs) {
|
|
1835
|
-
generationInfo.logprobs = choice.logprobs;
|
|
1836
|
-
}
|
|
1837
|
-
const generationChunk = new ChatGenerationChunk({
|
|
1838
|
-
message: chunk,
|
|
1839
|
-
text: chunk.content,
|
|
1840
|
-
generationInfo,
|
|
1841
|
-
});
|
|
1842
|
-
yield generationChunk;
|
|
1843
|
-
await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
1844
|
-
}
|
|
1845
|
-
if (usage) {
|
|
1846
|
-
const inputTokenDetails = {
|
|
1847
|
-
...(usage.prompt_tokens_details?.audio_tokens !== null && {
|
|
1848
|
-
audio: usage.prompt_tokens_details?.audio_tokens,
|
|
1849
|
-
}),
|
|
1850
|
-
...(usage.prompt_tokens_details?.cached_tokens !== null && {
|
|
1851
|
-
cache_read: usage.prompt_tokens_details?.cached_tokens,
|
|
1852
|
-
}),
|
|
1853
|
-
};
|
|
1854
|
-
const outputTokenDetails = {
|
|
1855
|
-
...(usage.completion_tokens_details?.audio_tokens !== null && {
|
|
1856
|
-
audio: usage.completion_tokens_details?.audio_tokens,
|
|
1857
|
-
}),
|
|
1858
|
-
...(usage.completion_tokens_details?.reasoning_tokens !== null && {
|
|
1859
|
-
reasoning: usage.completion_tokens_details?.reasoning_tokens,
|
|
1860
|
-
}),
|
|
1861
|
-
};
|
|
1862
|
-
const generationChunk = new ChatGenerationChunk({
|
|
1863
|
-
message: new AIMessageChunk({
|
|
1864
|
-
content: "",
|
|
1865
|
-
response_metadata: {
|
|
1866
|
-
usage: { ...usage },
|
|
1867
|
-
},
|
|
1868
|
-
usage_metadata: {
|
|
1869
|
-
input_tokens: usage.prompt_tokens,
|
|
1870
|
-
output_tokens: usage.completion_tokens,
|
|
1871
|
-
total_tokens: usage.total_tokens,
|
|
1872
|
-
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
1873
|
-
input_token_details: inputTokenDetails,
|
|
1874
|
-
}),
|
|
1875
|
-
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
1876
|
-
output_token_details: outputTokenDetails,
|
|
1877
|
-
}),
|
|
1878
|
-
},
|
|
1879
|
-
}),
|
|
1880
|
-
text: "",
|
|
1881
|
-
});
|
|
1882
|
-
yield generationChunk;
|
|
1883
|
-
}
|
|
1884
|
-
if (options.signal?.aborted) {
|
|
1885
|
-
throw new Error("AbortError");
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
async completionWithRetry(request, requestOptions) {
|
|
1889
|
-
const clientOptions = this._getClientOptions(requestOptions);
|
|
1890
|
-
const isParseableFormat = request.response_format && request.response_format.type === "json_schema";
|
|
1891
|
-
return this.caller.call(async () => {
|
|
1892
|
-
try {
|
|
1893
|
-
if (isParseableFormat && !request.stream) {
|
|
1894
|
-
return await this.client.chat.completions.parse(request, clientOptions);
|
|
1895
|
-
}
|
|
1896
|
-
else {
|
|
1897
|
-
return await this.client.chat.completions.create(request, clientOptions);
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
catch (e) {
|
|
1901
|
-
const error = wrapOpenAIClientError(e);
|
|
1902
|
-
throw error;
|
|
1903
|
-
}
|
|
1904
|
-
});
|
|
1905
|
-
}
|
|
1906
|
-
/** @internal */
|
|
1907
|
-
_convertCompletionsMessageToBaseMessage(message, rawResponse) {
|
|
1908
|
-
const rawToolCalls = message.tool_calls;
|
|
1909
|
-
switch (message.role) {
|
|
1910
|
-
case "assistant": {
|
|
1911
|
-
const toolCalls = [];
|
|
1912
|
-
const invalidToolCalls = [];
|
|
1913
|
-
for (const rawToolCall of rawToolCalls ?? []) {
|
|
1914
|
-
try {
|
|
1915
|
-
toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
|
|
1916
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1917
|
-
}
|
|
1918
|
-
catch (e) {
|
|
1919
|
-
invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
const additional_kwargs = {
|
|
1923
|
-
function_call: message.function_call,
|
|
1924
|
-
tool_calls: rawToolCalls,
|
|
1925
|
-
};
|
|
1926
|
-
if (this.__includeRawResponse !== undefined) {
|
|
1927
|
-
additional_kwargs.__raw_response = rawResponse;
|
|
1928
|
-
}
|
|
1929
|
-
const response_metadata = {
|
|
1930
|
-
model_name: rawResponse.model,
|
|
1931
|
-
...(rawResponse.system_fingerprint
|
|
1932
|
-
? {
|
|
1933
|
-
usage: { ...rawResponse.usage },
|
|
1934
|
-
system_fingerprint: rawResponse.system_fingerprint,
|
|
1935
|
-
}
|
|
1936
|
-
: {}),
|
|
1937
|
-
};
|
|
1938
|
-
if (message.audio) {
|
|
1939
|
-
additional_kwargs.audio = message.audio;
|
|
1940
|
-
}
|
|
1941
|
-
return new AIMessage({
|
|
1942
|
-
content: message.content || "",
|
|
1943
|
-
tool_calls: toolCalls,
|
|
1944
|
-
invalid_tool_calls: invalidToolCalls,
|
|
1945
|
-
additional_kwargs,
|
|
1946
|
-
response_metadata,
|
|
1947
|
-
id: rawResponse.id,
|
|
1948
|
-
});
|
|
1949
|
-
}
|
|
1950
|
-
default:
|
|
1951
|
-
return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
1952
|
-
}
|
|
1953
|
-
}
|
|
1954
|
-
/** @internal */
|
|
1955
|
-
_convertCompletionsDeltaToBaseMessageChunk(
|
|
1956
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1957
|
-
delta, rawResponse, defaultRole) {
|
|
1958
|
-
const role = delta.role ?? defaultRole;
|
|
1959
|
-
const content = delta.content ?? "";
|
|
1960
|
-
let additional_kwargs;
|
|
1961
|
-
if (delta.function_call) {
|
|
1962
|
-
additional_kwargs = {
|
|
1963
|
-
function_call: delta.function_call,
|
|
1964
|
-
};
|
|
1965
|
-
}
|
|
1966
|
-
else if (delta.tool_calls) {
|
|
1967
|
-
additional_kwargs = {
|
|
1968
|
-
tool_calls: delta.tool_calls,
|
|
1969
|
-
};
|
|
1970
|
-
}
|
|
1971
|
-
else {
|
|
1972
|
-
additional_kwargs = {};
|
|
1973
|
-
}
|
|
1974
|
-
if (this.__includeRawResponse) {
|
|
1975
|
-
additional_kwargs.__raw_response = rawResponse;
|
|
1976
|
-
}
|
|
1977
|
-
if (delta.audio) {
|
|
1978
|
-
additional_kwargs.audio = {
|
|
1979
|
-
...delta.audio,
|
|
1980
|
-
index: rawResponse.choices[0].index,
|
|
1981
|
-
};
|
|
1982
|
-
}
|
|
1983
|
-
const response_metadata = { usage: { ...rawResponse.usage } };
|
|
1984
|
-
if (role === "user") {
|
|
1985
|
-
return new HumanMessageChunk({ content, response_metadata });
|
|
1986
|
-
}
|
|
1987
|
-
else if (role === "assistant") {
|
|
1988
|
-
const toolCallChunks = [];
|
|
1989
|
-
if (Array.isArray(delta.tool_calls)) {
|
|
1990
|
-
for (const rawToolCall of delta.tool_calls) {
|
|
1991
|
-
toolCallChunks.push({
|
|
1992
|
-
name: rawToolCall.function?.name,
|
|
1993
|
-
args: rawToolCall.function?.arguments,
|
|
1994
|
-
id: rawToolCall.id,
|
|
1995
|
-
index: rawToolCall.index,
|
|
1996
|
-
type: "tool_call_chunk",
|
|
1997
|
-
});
|
|
1998
|
-
}
|
|
1999
|
-
}
|
|
2000
|
-
return new AIMessageChunk({
|
|
2001
|
-
content,
|
|
2002
|
-
tool_call_chunks: toolCallChunks,
|
|
2003
|
-
additional_kwargs,
|
|
2004
|
-
id: rawResponse.id,
|
|
2005
|
-
response_metadata,
|
|
2006
|
-
});
|
|
2007
|
-
}
|
|
2008
|
-
else if (role === "system") {
|
|
2009
|
-
return new SystemMessageChunk({ content, response_metadata });
|
|
2010
|
-
}
|
|
2011
|
-
else if (role === "developer") {
|
|
2012
|
-
return new SystemMessageChunk({
|
|
2013
|
-
content,
|
|
2014
|
-
response_metadata,
|
|
2015
|
-
additional_kwargs: {
|
|
2016
|
-
__openai_role__: "developer",
|
|
2017
|
-
},
|
|
2018
|
-
});
|
|
2019
|
-
}
|
|
2020
|
-
else if (role === "function") {
|
|
2021
|
-
return new FunctionMessageChunk({
|
|
2022
|
-
content,
|
|
2023
|
-
additional_kwargs,
|
|
2024
|
-
name: delta.name,
|
|
2025
|
-
response_metadata,
|
|
2026
|
-
});
|
|
2027
|
-
}
|
|
2028
|
-
else if (role === "tool") {
|
|
2029
|
-
return new ToolMessageChunk({
|
|
2030
|
-
content,
|
|
2031
|
-
additional_kwargs,
|
|
2032
|
-
tool_call_id: delta.tool_call_id,
|
|
2033
|
-
response_metadata,
|
|
2034
|
-
});
|
|
2035
|
-
}
|
|
2036
|
-
else {
|
|
2037
|
-
return new ChatMessageChunk({ content, role, response_metadata });
|
|
2038
710
|
}
|
|
711
|
+
return tool;
|
|
2039
712
|
}
|
|
713
|
+
return _convertToOpenAITool(tool, fields);
|
|
714
|
+
}
|
|
715
|
+
function isReasoningModel(model) {
|
|
716
|
+
return model && /^o\d/.test(model);
|
|
2040
717
|
}
|
|
2041
718
|
/**
|
|
2042
719
|
* OpenAI chat model integration.
|
|
@@ -2083,7 +760,7 @@ export class ChatOpenAICompletions extends BaseChatOpenAI {
|
|
|
2083
760
|
* import { ChatOpenAI } from '@langchain/openai';
|
|
2084
761
|
*
|
|
2085
762
|
* const llm = new ChatOpenAI({
|
|
2086
|
-
* model: "gpt-4o
|
|
763
|
+
* model: "gpt-4o",
|
|
2087
764
|
* temperature: 0,
|
|
2088
765
|
* maxTokens: undefined,
|
|
2089
766
|
* timeout: undefined,
|
|
@@ -2384,7 +1061,7 @@ export class ChatOpenAICompletions extends BaseChatOpenAI {
|
|
|
2384
1061
|
* <summary><strong>Logprobs</strong></summary>
|
|
2385
1062
|
*
|
|
2386
1063
|
* ```typescript
|
|
2387
|
-
* const logprobsLlm = new ChatOpenAI({
|
|
1064
|
+
* const logprobsLlm = new ChatOpenAI({ logprobs: true });
|
|
2388
1065
|
* const aiMsgForLogprobs = await logprobsLlm.invoke(input);
|
|
2389
1066
|
* console.log(aiMsgForLogprobs.response_metadata.logprobs);
|
|
2390
1067
|
* ```
|
|
@@ -2576,83 +1253,1436 @@ export class ChatOpenAICompletions extends BaseChatOpenAI {
|
|
|
2576
1253
|
*
|
|
2577
1254
|
* <br />
|
|
2578
1255
|
*/
|
|
2579
|
-
export class ChatOpenAI extends
|
|
1256
|
+
export class ChatOpenAI extends BaseChatModel {
|
|
1257
|
+
static lc_name() {
|
|
1258
|
+
return "ChatOpenAI";
|
|
1259
|
+
}
|
|
1260
|
+
get callKeys() {
|
|
1261
|
+
return [
|
|
1262
|
+
...super.callKeys,
|
|
1263
|
+
"options",
|
|
1264
|
+
"function_call",
|
|
1265
|
+
"functions",
|
|
1266
|
+
"tools",
|
|
1267
|
+
"tool_choice",
|
|
1268
|
+
"promptIndex",
|
|
1269
|
+
"response_format",
|
|
1270
|
+
"seed",
|
|
1271
|
+
"reasoning_effort",
|
|
1272
|
+
"service_tier",
|
|
1273
|
+
];
|
|
1274
|
+
}
|
|
1275
|
+
get lc_secrets() {
|
|
1276
|
+
return {
|
|
1277
|
+
openAIApiKey: "OPENAI_API_KEY",
|
|
1278
|
+
apiKey: "OPENAI_API_KEY",
|
|
1279
|
+
organization: "OPENAI_ORGANIZATION",
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
get lc_aliases() {
|
|
1283
|
+
return {
|
|
1284
|
+
modelName: "model",
|
|
1285
|
+
openAIApiKey: "openai_api_key",
|
|
1286
|
+
apiKey: "openai_api_key",
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
2580
1289
|
get lc_serializable_keys() {
|
|
2581
|
-
return [
|
|
1290
|
+
return [
|
|
1291
|
+
"configuration",
|
|
1292
|
+
"logprobs",
|
|
1293
|
+
"topLogprobs",
|
|
1294
|
+
"prefixMessages",
|
|
1295
|
+
"supportsStrictToolCalling",
|
|
1296
|
+
"modalities",
|
|
1297
|
+
"audio",
|
|
1298
|
+
"reasoningEffort",
|
|
1299
|
+
"temperature",
|
|
1300
|
+
"maxTokens",
|
|
1301
|
+
"topP",
|
|
1302
|
+
"frequencyPenalty",
|
|
1303
|
+
"presencePenalty",
|
|
1304
|
+
"n",
|
|
1305
|
+
"logitBias",
|
|
1306
|
+
"user",
|
|
1307
|
+
"streaming",
|
|
1308
|
+
"streamUsage",
|
|
1309
|
+
"modelName",
|
|
1310
|
+
"model",
|
|
1311
|
+
"modelKwargs",
|
|
1312
|
+
"stop",
|
|
1313
|
+
"stopSequences",
|
|
1314
|
+
"timeout",
|
|
1315
|
+
"openAIApiKey",
|
|
1316
|
+
"apiKey",
|
|
1317
|
+
"cache",
|
|
1318
|
+
"maxConcurrency",
|
|
1319
|
+
"maxRetries",
|
|
1320
|
+
"verbose",
|
|
1321
|
+
"callbacks",
|
|
1322
|
+
"tags",
|
|
1323
|
+
"metadata",
|
|
1324
|
+
"disableStreaming",
|
|
1325
|
+
"useResponsesApi",
|
|
1326
|
+
"zdrEnabled",
|
|
1327
|
+
"reasoning",
|
|
1328
|
+
];
|
|
1329
|
+
}
|
|
1330
|
+
constructor(fields) {
|
|
1331
|
+
super(fields ?? {});
|
|
1332
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
1333
|
+
enumerable: true,
|
|
1334
|
+
configurable: true,
|
|
1335
|
+
writable: true,
|
|
1336
|
+
value: true
|
|
1337
|
+
});
|
|
1338
|
+
Object.defineProperty(this, "temperature", {
|
|
1339
|
+
enumerable: true,
|
|
1340
|
+
configurable: true,
|
|
1341
|
+
writable: true,
|
|
1342
|
+
value: void 0
|
|
1343
|
+
});
|
|
1344
|
+
Object.defineProperty(this, "topP", {
|
|
1345
|
+
enumerable: true,
|
|
1346
|
+
configurable: true,
|
|
1347
|
+
writable: true,
|
|
1348
|
+
value: void 0
|
|
1349
|
+
});
|
|
1350
|
+
Object.defineProperty(this, "frequencyPenalty", {
|
|
1351
|
+
enumerable: true,
|
|
1352
|
+
configurable: true,
|
|
1353
|
+
writable: true,
|
|
1354
|
+
value: void 0
|
|
1355
|
+
});
|
|
1356
|
+
Object.defineProperty(this, "presencePenalty", {
|
|
1357
|
+
enumerable: true,
|
|
1358
|
+
configurable: true,
|
|
1359
|
+
writable: true,
|
|
1360
|
+
value: void 0
|
|
1361
|
+
});
|
|
1362
|
+
Object.defineProperty(this, "n", {
|
|
1363
|
+
enumerable: true,
|
|
1364
|
+
configurable: true,
|
|
1365
|
+
writable: true,
|
|
1366
|
+
value: void 0
|
|
1367
|
+
});
|
|
1368
|
+
Object.defineProperty(this, "logitBias", {
|
|
1369
|
+
enumerable: true,
|
|
1370
|
+
configurable: true,
|
|
1371
|
+
writable: true,
|
|
1372
|
+
value: void 0
|
|
1373
|
+
});
|
|
1374
|
+
/** @deprecated Use "model" instead */
|
|
1375
|
+
Object.defineProperty(this, "modelName", {
|
|
1376
|
+
enumerable: true,
|
|
1377
|
+
configurable: true,
|
|
1378
|
+
writable: true,
|
|
1379
|
+
value: void 0
|
|
1380
|
+
});
|
|
1381
|
+
Object.defineProperty(this, "model", {
|
|
1382
|
+
enumerable: true,
|
|
1383
|
+
configurable: true,
|
|
1384
|
+
writable: true,
|
|
1385
|
+
value: "gpt-3.5-turbo"
|
|
1386
|
+
});
|
|
1387
|
+
Object.defineProperty(this, "modelKwargs", {
|
|
1388
|
+
enumerable: true,
|
|
1389
|
+
configurable: true,
|
|
1390
|
+
writable: true,
|
|
1391
|
+
value: void 0
|
|
1392
|
+
});
|
|
1393
|
+
Object.defineProperty(this, "stop", {
|
|
1394
|
+
enumerable: true,
|
|
1395
|
+
configurable: true,
|
|
1396
|
+
writable: true,
|
|
1397
|
+
value: void 0
|
|
1398
|
+
});
|
|
1399
|
+
Object.defineProperty(this, "stopSequences", {
|
|
1400
|
+
enumerable: true,
|
|
1401
|
+
configurable: true,
|
|
1402
|
+
writable: true,
|
|
1403
|
+
value: void 0
|
|
1404
|
+
});
|
|
1405
|
+
Object.defineProperty(this, "user", {
|
|
1406
|
+
enumerable: true,
|
|
1407
|
+
configurable: true,
|
|
1408
|
+
writable: true,
|
|
1409
|
+
value: void 0
|
|
1410
|
+
});
|
|
1411
|
+
Object.defineProperty(this, "timeout", {
|
|
1412
|
+
enumerable: true,
|
|
1413
|
+
configurable: true,
|
|
1414
|
+
writable: true,
|
|
1415
|
+
value: void 0
|
|
1416
|
+
});
|
|
1417
|
+
Object.defineProperty(this, "streaming", {
|
|
1418
|
+
enumerable: true,
|
|
1419
|
+
configurable: true,
|
|
1420
|
+
writable: true,
|
|
1421
|
+
value: false
|
|
1422
|
+
});
|
|
1423
|
+
Object.defineProperty(this, "streamUsage", {
|
|
1424
|
+
enumerable: true,
|
|
1425
|
+
configurable: true,
|
|
1426
|
+
writable: true,
|
|
1427
|
+
value: true
|
|
1428
|
+
});
|
|
1429
|
+
Object.defineProperty(this, "maxTokens", {
|
|
1430
|
+
enumerable: true,
|
|
1431
|
+
configurable: true,
|
|
1432
|
+
writable: true,
|
|
1433
|
+
value: void 0
|
|
1434
|
+
});
|
|
1435
|
+
Object.defineProperty(this, "logprobs", {
|
|
1436
|
+
enumerable: true,
|
|
1437
|
+
configurable: true,
|
|
1438
|
+
writable: true,
|
|
1439
|
+
value: void 0
|
|
1440
|
+
});
|
|
1441
|
+
Object.defineProperty(this, "topLogprobs", {
|
|
1442
|
+
enumerable: true,
|
|
1443
|
+
configurable: true,
|
|
1444
|
+
writable: true,
|
|
1445
|
+
value: void 0
|
|
1446
|
+
});
|
|
1447
|
+
Object.defineProperty(this, "openAIApiKey", {
|
|
1448
|
+
enumerable: true,
|
|
1449
|
+
configurable: true,
|
|
1450
|
+
writable: true,
|
|
1451
|
+
value: void 0
|
|
1452
|
+
});
|
|
1453
|
+
Object.defineProperty(this, "apiKey", {
|
|
1454
|
+
enumerable: true,
|
|
1455
|
+
configurable: true,
|
|
1456
|
+
writable: true,
|
|
1457
|
+
value: void 0
|
|
1458
|
+
});
|
|
1459
|
+
Object.defineProperty(this, "organization", {
|
|
1460
|
+
enumerable: true,
|
|
1461
|
+
configurable: true,
|
|
1462
|
+
writable: true,
|
|
1463
|
+
value: void 0
|
|
1464
|
+
});
|
|
1465
|
+
Object.defineProperty(this, "__includeRawResponse", {
|
|
1466
|
+
enumerable: true,
|
|
1467
|
+
configurable: true,
|
|
1468
|
+
writable: true,
|
|
1469
|
+
value: void 0
|
|
1470
|
+
});
|
|
1471
|
+
Object.defineProperty(this, "client", {
|
|
1472
|
+
enumerable: true,
|
|
1473
|
+
configurable: true,
|
|
1474
|
+
writable: true,
|
|
1475
|
+
value: void 0
|
|
1476
|
+
});
|
|
1477
|
+
Object.defineProperty(this, "clientConfig", {
|
|
1478
|
+
enumerable: true,
|
|
1479
|
+
configurable: true,
|
|
1480
|
+
writable: true,
|
|
1481
|
+
value: void 0
|
|
1482
|
+
});
|
|
1483
|
+
/**
|
|
1484
|
+
* Whether the model supports the `strict` argument when passing in tools.
|
|
1485
|
+
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
1486
|
+
*/
|
|
1487
|
+
Object.defineProperty(this, "supportsStrictToolCalling", {
|
|
1488
|
+
enumerable: true,
|
|
1489
|
+
configurable: true,
|
|
1490
|
+
writable: true,
|
|
1491
|
+
value: void 0
|
|
1492
|
+
});
|
|
1493
|
+
Object.defineProperty(this, "audio", {
|
|
1494
|
+
enumerable: true,
|
|
1495
|
+
configurable: true,
|
|
1496
|
+
writable: true,
|
|
1497
|
+
value: void 0
|
|
1498
|
+
});
|
|
1499
|
+
Object.defineProperty(this, "modalities", {
|
|
1500
|
+
enumerable: true,
|
|
1501
|
+
configurable: true,
|
|
1502
|
+
writable: true,
|
|
1503
|
+
value: void 0
|
|
1504
|
+
});
|
|
1505
|
+
/**
|
|
1506
|
+
* @deprecated Use {@link reasoning} object instead.
|
|
1507
|
+
*/
|
|
1508
|
+
Object.defineProperty(this, "reasoningEffort", {
|
|
1509
|
+
enumerable: true,
|
|
1510
|
+
configurable: true,
|
|
1511
|
+
writable: true,
|
|
1512
|
+
value: void 0
|
|
1513
|
+
});
|
|
1514
|
+
/**
|
|
1515
|
+
* Options for reasoning models.
|
|
1516
|
+
*/
|
|
1517
|
+
Object.defineProperty(this, "reasoning", {
|
|
1518
|
+
enumerable: true,
|
|
1519
|
+
configurable: true,
|
|
1520
|
+
writable: true,
|
|
1521
|
+
value: void 0
|
|
1522
|
+
});
|
|
1523
|
+
/**
|
|
1524
|
+
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
1525
|
+
* only when required in order to fulfill the request.
|
|
1526
|
+
*/
|
|
1527
|
+
Object.defineProperty(this, "useResponsesApi", {
|
|
1528
|
+
enumerable: true,
|
|
1529
|
+
configurable: true,
|
|
1530
|
+
writable: true,
|
|
1531
|
+
value: false
|
|
1532
|
+
});
|
|
1533
|
+
/**
|
|
1534
|
+
* Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
|
|
1535
|
+
* output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
|
|
1536
|
+
* OpenAI organization or project. This must be configured directly with OpenAI.
|
|
1537
|
+
*
|
|
1538
|
+
* See:
|
|
1539
|
+
* https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
|
|
1540
|
+
* https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
|
|
1541
|
+
*
|
|
1542
|
+
* @default false
|
|
1543
|
+
*/
|
|
1544
|
+
Object.defineProperty(this, "zdrEnabled", {
|
|
1545
|
+
enumerable: true,
|
|
1546
|
+
configurable: true,
|
|
1547
|
+
writable: true,
|
|
1548
|
+
value: void 0
|
|
1549
|
+
});
|
|
1550
|
+
/**
|
|
1551
|
+
* Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
|
|
1552
|
+
* Specifies the service tier for prioritization and latency optimization.
|
|
1553
|
+
*/
|
|
1554
|
+
Object.defineProperty(this, "service_tier", {
|
|
1555
|
+
enumerable: true,
|
|
1556
|
+
configurable: true,
|
|
1557
|
+
writable: true,
|
|
1558
|
+
value: void 0
|
|
1559
|
+
});
|
|
1560
|
+
this.openAIApiKey =
|
|
1561
|
+
fields?.apiKey ??
|
|
1562
|
+
fields?.openAIApiKey ??
|
|
1563
|
+
fields?.configuration?.apiKey ??
|
|
1564
|
+
getEnvironmentVariable("OPENAI_API_KEY");
|
|
1565
|
+
this.apiKey = this.openAIApiKey;
|
|
1566
|
+
this.organization =
|
|
1567
|
+
fields?.configuration?.organization ??
|
|
1568
|
+
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
1569
|
+
this.model = fields?.model ?? fields?.modelName ?? this.model;
|
|
1570
|
+
this.modelName = this.model;
|
|
1571
|
+
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
1572
|
+
this.timeout = fields?.timeout;
|
|
1573
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
1574
|
+
this.topP = fields?.topP ?? this.topP;
|
|
1575
|
+
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
1576
|
+
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
1577
|
+
this.logprobs = fields?.logprobs;
|
|
1578
|
+
this.topLogprobs = fields?.topLogprobs;
|
|
1579
|
+
this.n = fields?.n ?? this.n;
|
|
1580
|
+
this.logitBias = fields?.logitBias;
|
|
1581
|
+
this.stop = fields?.stopSequences ?? fields?.stop;
|
|
1582
|
+
this.stopSequences = this.stop;
|
|
1583
|
+
this.user = fields?.user;
|
|
1584
|
+
this.__includeRawResponse = fields?.__includeRawResponse;
|
|
1585
|
+
this.audio = fields?.audio;
|
|
1586
|
+
this.modalities = fields?.modalities;
|
|
1587
|
+
this.reasoningEffort = fields?.reasoningEffort ?? fields?.reasoning?.effort;
|
|
1588
|
+
this.reasoning =
|
|
1589
|
+
fields?.reasoning ??
|
|
1590
|
+
(fields?.reasoningEffort
|
|
1591
|
+
? { effort: fields.reasoningEffort }
|
|
1592
|
+
: undefined);
|
|
1593
|
+
this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
|
|
1594
|
+
this.useResponsesApi = fields?.useResponsesApi ?? this.useResponsesApi;
|
|
1595
|
+
this.disableStreaming = fields?.disableStreaming ?? this.disableStreaming;
|
|
1596
|
+
this.streaming = fields?.streaming ?? false;
|
|
1597
|
+
if (this.disableStreaming)
|
|
1598
|
+
this.streaming = false;
|
|
1599
|
+
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
|
|
1600
|
+
if (this.disableStreaming)
|
|
1601
|
+
this.streamUsage = false;
|
|
1602
|
+
this.clientConfig = {
|
|
1603
|
+
apiKey: this.apiKey,
|
|
1604
|
+
organization: this.organization,
|
|
1605
|
+
dangerouslyAllowBrowser: true,
|
|
1606
|
+
...fields?.configuration,
|
|
1607
|
+
};
|
|
1608
|
+
// If `supportsStrictToolCalling` is explicitly set, use that value.
|
|
1609
|
+
// Else leave undefined so it's not passed to OpenAI.
|
|
1610
|
+
if (fields?.supportsStrictToolCalling !== undefined) {
|
|
1611
|
+
this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
|
|
1612
|
+
}
|
|
1613
|
+
if (fields?.service_tier !== undefined) {
|
|
1614
|
+
this.service_tier = fields.service_tier;
|
|
1615
|
+
}
|
|
1616
|
+
this.zdrEnabled = fields?.zdrEnabled ?? false;
|
|
1617
|
+
}
|
|
1618
|
+
getLsParams(options) {
|
|
1619
|
+
const params = this.invocationParams(options);
|
|
1620
|
+
return {
|
|
1621
|
+
ls_provider: "openai",
|
|
1622
|
+
ls_model_name: this.model,
|
|
1623
|
+
ls_model_type: "chat",
|
|
1624
|
+
ls_temperature: params.temperature ?? undefined,
|
|
1625
|
+
ls_max_tokens: params.max_tokens ?? undefined,
|
|
1626
|
+
ls_stop: options.stop,
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
bindTools(tools, kwargs) {
|
|
1630
|
+
let strict;
|
|
1631
|
+
if (kwargs?.strict !== undefined) {
|
|
1632
|
+
strict = kwargs.strict;
|
|
1633
|
+
}
|
|
1634
|
+
else if (this.supportsStrictToolCalling !== undefined) {
|
|
1635
|
+
strict = this.supportsStrictToolCalling;
|
|
1636
|
+
}
|
|
1637
|
+
return this.withConfig({
|
|
1638
|
+
tools: tools.map((tool) => isBuiltInTool(tool)
|
|
1639
|
+
? tool
|
|
1640
|
+
: _convertChatOpenAIToolTypeToOpenAITool(tool, { strict })),
|
|
1641
|
+
...kwargs,
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
createResponseFormat(resFormat) {
|
|
1645
|
+
if (resFormat &&
|
|
1646
|
+
resFormat.type === "json_schema" &&
|
|
1647
|
+
resFormat.json_schema.schema &&
|
|
1648
|
+
isInteropZodSchema(resFormat.json_schema.schema)) {
|
|
1649
|
+
return interopZodResponseFormat(resFormat.json_schema.schema, resFormat.json_schema.name, {
|
|
1650
|
+
description: resFormat.json_schema.description,
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
return resFormat;
|
|
1654
|
+
}
|
|
1655
|
+
getReasoningParams(options) {
|
|
1656
|
+
if (!isReasoningModel(this.model)) {
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1659
|
+
let reasoning;
|
|
1660
|
+
// apply reasoning options in reverse order of priority without else blocks so that highest priority overrides lowest priority
|
|
1661
|
+
if (this.reasoningEffort !== undefined) {
|
|
1662
|
+
reasoning = { effort: this.reasoningEffort };
|
|
1663
|
+
}
|
|
1664
|
+
if (this.reasoning !== undefined) {
|
|
1665
|
+
reasoning = {
|
|
1666
|
+
...reasoning,
|
|
1667
|
+
...this.reasoning,
|
|
1668
|
+
};
|
|
1669
|
+
}
|
|
1670
|
+
if (options?.reasoning_effort !== undefined) {
|
|
1671
|
+
reasoning = {
|
|
1672
|
+
...reasoning,
|
|
1673
|
+
effort: options.reasoning_effort,
|
|
1674
|
+
};
|
|
1675
|
+
}
|
|
1676
|
+
if (options?.reasoning !== undefined) {
|
|
1677
|
+
reasoning = {
|
|
1678
|
+
...reasoning,
|
|
1679
|
+
...options.reasoning,
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
return reasoning;
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Get the parameters used to invoke the model
|
|
1686
|
+
*/
|
|
1687
|
+
invocationParams(options, extra) {
|
|
1688
|
+
let strict;
|
|
1689
|
+
if (options?.strict !== undefined) {
|
|
1690
|
+
strict = options.strict;
|
|
1691
|
+
}
|
|
1692
|
+
else if (this.supportsStrictToolCalling !== undefined) {
|
|
1693
|
+
strict = this.supportsStrictToolCalling;
|
|
1694
|
+
}
|
|
1695
|
+
if (this._useResponseApi(options)) {
|
|
1696
|
+
const params = {
|
|
1697
|
+
model: this.model,
|
|
1698
|
+
temperature: this.temperature,
|
|
1699
|
+
top_p: this.topP,
|
|
1700
|
+
user: this.user,
|
|
1701
|
+
// if include_usage is set or streamUsage then stream must be set to true.
|
|
1702
|
+
stream: this.streaming,
|
|
1703
|
+
previous_response_id: options?.previous_response_id,
|
|
1704
|
+
truncation: options?.truncation,
|
|
1705
|
+
include: options?.include,
|
|
1706
|
+
tools: options?.tools?.length
|
|
1707
|
+
? _reduceChatOpenAIToolsForResponses(options.tools, {
|
|
1708
|
+
stream: this.streaming,
|
|
1709
|
+
strict,
|
|
1710
|
+
})
|
|
1711
|
+
: undefined,
|
|
1712
|
+
tool_choice: isBuiltInToolChoice(options?.tool_choice)
|
|
1713
|
+
? options?.tool_choice
|
|
1714
|
+
: (() => {
|
|
1715
|
+
const formatted = formatToOpenAIToolChoice(options?.tool_choice);
|
|
1716
|
+
if (typeof formatted === "object" && "type" in formatted) {
|
|
1717
|
+
return { type: "function", name: formatted.function.name };
|
|
1718
|
+
}
|
|
1719
|
+
else {
|
|
1720
|
+
return undefined;
|
|
1721
|
+
}
|
|
1722
|
+
})(),
|
|
1723
|
+
text: (() => {
|
|
1724
|
+
if (options?.text)
|
|
1725
|
+
return options.text;
|
|
1726
|
+
const format = this.createResponseFormat(options?.response_format);
|
|
1727
|
+
if (format?.type === "json_schema") {
|
|
1728
|
+
if (format.json_schema.schema != null) {
|
|
1729
|
+
return {
|
|
1730
|
+
format: {
|
|
1731
|
+
type: "json_schema",
|
|
1732
|
+
schema: format.json_schema.schema,
|
|
1733
|
+
description: format.json_schema.description,
|
|
1734
|
+
name: format.json_schema.name,
|
|
1735
|
+
strict: format.json_schema.strict,
|
|
1736
|
+
},
|
|
1737
|
+
};
|
|
1738
|
+
}
|
|
1739
|
+
return undefined;
|
|
1740
|
+
}
|
|
1741
|
+
return { format };
|
|
1742
|
+
})(),
|
|
1743
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
1744
|
+
max_output_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
|
|
1745
|
+
...(this.zdrEnabled ? { store: false } : {}),
|
|
1746
|
+
...this.modelKwargs,
|
|
1747
|
+
};
|
|
1748
|
+
const reasoning = this.getReasoningParams(options);
|
|
1749
|
+
if (reasoning !== undefined) {
|
|
1750
|
+
params.reasoning = reasoning;
|
|
1751
|
+
}
|
|
1752
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1753
|
+
return params;
|
|
1754
|
+
}
|
|
1755
|
+
let streamOptionsConfig = {};
|
|
1756
|
+
if (options?.stream_options !== undefined) {
|
|
1757
|
+
streamOptionsConfig = { stream_options: options.stream_options };
|
|
1758
|
+
}
|
|
1759
|
+
else if (this.streamUsage && (this.streaming || extra?.streaming)) {
|
|
1760
|
+
streamOptionsConfig = { stream_options: { include_usage: true } };
|
|
1761
|
+
}
|
|
1762
|
+
const params = {
|
|
1763
|
+
model: this.model,
|
|
1764
|
+
temperature: this.temperature,
|
|
1765
|
+
top_p: this.topP,
|
|
1766
|
+
frequency_penalty: this.frequencyPenalty,
|
|
1767
|
+
presence_penalty: this.presencePenalty,
|
|
1768
|
+
logprobs: this.logprobs,
|
|
1769
|
+
top_logprobs: this.topLogprobs,
|
|
1770
|
+
n: this.n,
|
|
1771
|
+
logit_bias: this.logitBias,
|
|
1772
|
+
stop: options?.stop ?? this.stopSequences,
|
|
1773
|
+
user: this.user,
|
|
1774
|
+
// if include_usage is set or streamUsage then stream must be set to true.
|
|
1775
|
+
stream: this.streaming,
|
|
1776
|
+
functions: options?.functions,
|
|
1777
|
+
function_call: options?.function_call,
|
|
1778
|
+
tools: options?.tools?.length
|
|
1779
|
+
? options.tools.map((tool) => _convertChatOpenAIToolTypeToOpenAITool(tool, { strict }))
|
|
1780
|
+
: undefined,
|
|
1781
|
+
tool_choice: formatToOpenAIToolChoice(options?.tool_choice),
|
|
1782
|
+
response_format: this.createResponseFormat(options?.response_format),
|
|
1783
|
+
seed: options?.seed,
|
|
1784
|
+
...streamOptionsConfig,
|
|
1785
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
1786
|
+
...(this.audio || options?.audio
|
|
1787
|
+
? { audio: this.audio || options?.audio }
|
|
1788
|
+
: {}),
|
|
1789
|
+
...(this.modalities || options?.modalities
|
|
1790
|
+
? { modalities: this.modalities || options?.modalities }
|
|
1791
|
+
: {}),
|
|
1792
|
+
...this.modelKwargs,
|
|
1793
|
+
};
|
|
1794
|
+
if (options?.prediction !== undefined) {
|
|
1795
|
+
params.prediction = options.prediction;
|
|
1796
|
+
}
|
|
1797
|
+
if (this.service_tier !== undefined) {
|
|
1798
|
+
params.service_tier = this.service_tier;
|
|
1799
|
+
}
|
|
1800
|
+
if (options?.service_tier !== undefined) {
|
|
1801
|
+
params.service_tier = options.service_tier;
|
|
1802
|
+
}
|
|
1803
|
+
const reasoning = this.getReasoningParams(options);
|
|
1804
|
+
if (reasoning !== undefined && reasoning.effort !== undefined) {
|
|
1805
|
+
params.reasoning_effort = reasoning.effort;
|
|
1806
|
+
}
|
|
1807
|
+
if (isReasoningModel(params.model)) {
|
|
1808
|
+
params.max_completion_tokens =
|
|
1809
|
+
this.maxTokens === -1 ? undefined : this.maxTokens;
|
|
1810
|
+
}
|
|
1811
|
+
else {
|
|
1812
|
+
params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
|
|
1813
|
+
}
|
|
1814
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1815
|
+
return params;
|
|
1816
|
+
}
|
|
1817
|
+
_convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
|
|
1818
|
+
const rawToolCalls = message.tool_calls;
|
|
1819
|
+
switch (message.role) {
|
|
1820
|
+
case "assistant": {
|
|
1821
|
+
const toolCalls = [];
|
|
1822
|
+
const invalidToolCalls = [];
|
|
1823
|
+
for (const rawToolCall of rawToolCalls ?? []) {
|
|
1824
|
+
try {
|
|
1825
|
+
toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
|
|
1826
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1827
|
+
}
|
|
1828
|
+
catch (e) {
|
|
1829
|
+
invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
const additional_kwargs = {
|
|
1833
|
+
function_call: message.function_call,
|
|
1834
|
+
tool_calls: rawToolCalls,
|
|
1835
|
+
};
|
|
1836
|
+
if (this.__includeRawResponse !== undefined) {
|
|
1837
|
+
additional_kwargs.__raw_response = rawResponse;
|
|
1838
|
+
}
|
|
1839
|
+
const response_metadata = {
|
|
1840
|
+
model_name: rawResponse.model,
|
|
1841
|
+
...(rawResponse.system_fingerprint
|
|
1842
|
+
? {
|
|
1843
|
+
usage: { ...rawResponse.usage },
|
|
1844
|
+
system_fingerprint: rawResponse.system_fingerprint,
|
|
1845
|
+
}
|
|
1846
|
+
: {}),
|
|
1847
|
+
};
|
|
1848
|
+
if (message.audio) {
|
|
1849
|
+
additional_kwargs.audio = message.audio;
|
|
1850
|
+
}
|
|
1851
|
+
return new AIMessage({
|
|
1852
|
+
content: message.content || "",
|
|
1853
|
+
tool_calls: toolCalls,
|
|
1854
|
+
invalid_tool_calls: invalidToolCalls,
|
|
1855
|
+
additional_kwargs,
|
|
1856
|
+
response_metadata,
|
|
1857
|
+
id: rawResponse.id,
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
default:
|
|
1861
|
+
return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
_convertOpenAIDeltaToBaseMessageChunk(
|
|
1865
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1866
|
+
delta, rawResponse, defaultRole) {
|
|
1867
|
+
const role = delta.role ?? defaultRole;
|
|
1868
|
+
const content = delta.content ?? "";
|
|
1869
|
+
let additional_kwargs;
|
|
1870
|
+
if (delta.function_call) {
|
|
1871
|
+
additional_kwargs = {
|
|
1872
|
+
function_call: delta.function_call,
|
|
1873
|
+
};
|
|
1874
|
+
}
|
|
1875
|
+
else if (delta.tool_calls) {
|
|
1876
|
+
additional_kwargs = {
|
|
1877
|
+
tool_calls: delta.tool_calls,
|
|
1878
|
+
};
|
|
1879
|
+
}
|
|
1880
|
+
else {
|
|
1881
|
+
additional_kwargs = {};
|
|
1882
|
+
}
|
|
1883
|
+
if (this.__includeRawResponse) {
|
|
1884
|
+
additional_kwargs.__raw_response = rawResponse;
|
|
1885
|
+
}
|
|
1886
|
+
if (delta.audio) {
|
|
1887
|
+
additional_kwargs.audio = {
|
|
1888
|
+
...delta.audio,
|
|
1889
|
+
index: rawResponse.choices[0].index,
|
|
1890
|
+
};
|
|
1891
|
+
}
|
|
1892
|
+
const response_metadata = { usage: { ...rawResponse.usage } };
|
|
1893
|
+
if (role === "user") {
|
|
1894
|
+
return new HumanMessageChunk({ content, response_metadata });
|
|
1895
|
+
}
|
|
1896
|
+
else if (role === "assistant") {
|
|
1897
|
+
const toolCallChunks = [];
|
|
1898
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
1899
|
+
for (const rawToolCall of delta.tool_calls) {
|
|
1900
|
+
toolCallChunks.push({
|
|
1901
|
+
name: rawToolCall.function?.name,
|
|
1902
|
+
args: rawToolCall.function?.arguments,
|
|
1903
|
+
id: rawToolCall.id,
|
|
1904
|
+
index: rawToolCall.index,
|
|
1905
|
+
type: "tool_call_chunk",
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
return new AIMessageChunk({
|
|
1910
|
+
content,
|
|
1911
|
+
tool_call_chunks: toolCallChunks,
|
|
1912
|
+
additional_kwargs,
|
|
1913
|
+
id: rawResponse.id,
|
|
1914
|
+
response_metadata,
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
else if (role === "system") {
|
|
1918
|
+
return new SystemMessageChunk({ content, response_metadata });
|
|
1919
|
+
}
|
|
1920
|
+
else if (role === "developer") {
|
|
1921
|
+
return new SystemMessageChunk({
|
|
1922
|
+
content,
|
|
1923
|
+
response_metadata,
|
|
1924
|
+
additional_kwargs: {
|
|
1925
|
+
__openai_role__: "developer",
|
|
1926
|
+
},
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
else if (role === "function") {
|
|
1930
|
+
return new FunctionMessageChunk({
|
|
1931
|
+
content,
|
|
1932
|
+
additional_kwargs,
|
|
1933
|
+
name: delta.name,
|
|
1934
|
+
response_metadata,
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
else if (role === "tool") {
|
|
1938
|
+
return new ToolMessageChunk({
|
|
1939
|
+
content,
|
|
1940
|
+
additional_kwargs,
|
|
1941
|
+
tool_call_id: delta.tool_call_id,
|
|
1942
|
+
response_metadata,
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
else {
|
|
1946
|
+
return new ChatMessageChunk({ content, role, response_metadata });
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
/** @ignore */
|
|
1950
|
+
_identifyingParams() {
|
|
1951
|
+
return {
|
|
1952
|
+
model_name: this.model,
|
|
1953
|
+
...this.invocationParams(),
|
|
1954
|
+
...this.clientConfig,
|
|
1955
|
+
};
|
|
1956
|
+
}
|
|
1957
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
1958
|
+
if (this._useResponseApi(options)) {
|
|
1959
|
+
const streamIterable = await this.responseApiWithRetry({
|
|
1960
|
+
...this.invocationParams(options, { streaming: true }),
|
|
1961
|
+
input: _convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
|
|
1962
|
+
stream: true,
|
|
1963
|
+
}, options);
|
|
1964
|
+
for await (const data of streamIterable) {
|
|
1965
|
+
const chunk = _convertOpenAIResponsesDeltaToBaseMessageChunk(data);
|
|
1966
|
+
if (chunk == null)
|
|
1967
|
+
continue;
|
|
1968
|
+
yield chunk;
|
|
1969
|
+
}
|
|
1970
|
+
return;
|
|
1971
|
+
}
|
|
1972
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
1973
|
+
const params = {
|
|
1974
|
+
...this.invocationParams(options, {
|
|
1975
|
+
streaming: true,
|
|
1976
|
+
}),
|
|
1977
|
+
messages: messagesMapped,
|
|
1978
|
+
stream: true,
|
|
1979
|
+
};
|
|
1980
|
+
let defaultRole;
|
|
1981
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
1982
|
+
let usage;
|
|
1983
|
+
for await (const data of streamIterable) {
|
|
1984
|
+
const choice = data?.choices?.[0];
|
|
1985
|
+
if (data.usage) {
|
|
1986
|
+
usage = data.usage;
|
|
1987
|
+
}
|
|
1988
|
+
if (!choice) {
|
|
1989
|
+
continue;
|
|
1990
|
+
}
|
|
1991
|
+
const { delta } = choice;
|
|
1992
|
+
if (!delta) {
|
|
1993
|
+
continue;
|
|
1994
|
+
}
|
|
1995
|
+
const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
1996
|
+
defaultRole = delta.role ?? defaultRole;
|
|
1997
|
+
const newTokenIndices = {
|
|
1998
|
+
prompt: options.promptIndex ?? 0,
|
|
1999
|
+
completion: choice.index ?? 0,
|
|
2000
|
+
};
|
|
2001
|
+
if (typeof chunk.content !== "string") {
|
|
2002
|
+
console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
|
|
2003
|
+
continue;
|
|
2004
|
+
}
|
|
2005
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2006
|
+
const generationInfo = { ...newTokenIndices };
|
|
2007
|
+
if (choice.finish_reason != null) {
|
|
2008
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
2009
|
+
// Only include system fingerprint in the last chunk for now
|
|
2010
|
+
// to avoid concatenation issues
|
|
2011
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
2012
|
+
generationInfo.model_name = data.model;
|
|
2013
|
+
generationInfo.service_tier = data.service_tier;
|
|
2014
|
+
}
|
|
2015
|
+
if (this.logprobs) {
|
|
2016
|
+
generationInfo.logprobs = choice.logprobs;
|
|
2017
|
+
}
|
|
2018
|
+
const generationChunk = new ChatGenerationChunk({
|
|
2019
|
+
message: chunk,
|
|
2020
|
+
text: chunk.content,
|
|
2021
|
+
generationInfo,
|
|
2022
|
+
});
|
|
2023
|
+
yield generationChunk;
|
|
2024
|
+
await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
2025
|
+
}
|
|
2026
|
+
if (usage) {
|
|
2027
|
+
const inputTokenDetails = {
|
|
2028
|
+
...(usage.prompt_tokens_details?.audio_tokens !== null && {
|
|
2029
|
+
audio: usage.prompt_tokens_details?.audio_tokens,
|
|
2030
|
+
}),
|
|
2031
|
+
...(usage.prompt_tokens_details?.cached_tokens !== null && {
|
|
2032
|
+
cache_read: usage.prompt_tokens_details?.cached_tokens,
|
|
2033
|
+
}),
|
|
2034
|
+
};
|
|
2035
|
+
const outputTokenDetails = {
|
|
2036
|
+
...(usage.completion_tokens_details?.audio_tokens !== null && {
|
|
2037
|
+
audio: usage.completion_tokens_details?.audio_tokens,
|
|
2038
|
+
}),
|
|
2039
|
+
...(usage.completion_tokens_details?.reasoning_tokens !== null && {
|
|
2040
|
+
reasoning: usage.completion_tokens_details?.reasoning_tokens,
|
|
2041
|
+
}),
|
|
2042
|
+
};
|
|
2043
|
+
const generationChunk = new ChatGenerationChunk({
|
|
2044
|
+
message: new AIMessageChunk({
|
|
2045
|
+
content: "",
|
|
2046
|
+
response_metadata: {
|
|
2047
|
+
usage: { ...usage },
|
|
2048
|
+
},
|
|
2049
|
+
usage_metadata: {
|
|
2050
|
+
input_tokens: usage.prompt_tokens,
|
|
2051
|
+
output_tokens: usage.completion_tokens,
|
|
2052
|
+
total_tokens: usage.total_tokens,
|
|
2053
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
2054
|
+
input_token_details: inputTokenDetails,
|
|
2055
|
+
}),
|
|
2056
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
2057
|
+
output_token_details: outputTokenDetails,
|
|
2058
|
+
}),
|
|
2059
|
+
},
|
|
2060
|
+
}),
|
|
2061
|
+
text: "",
|
|
2062
|
+
});
|
|
2063
|
+
yield generationChunk;
|
|
2064
|
+
}
|
|
2065
|
+
if (options.signal?.aborted) {
|
|
2066
|
+
throw new Error("AbortError");
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Get the identifying parameters for the model
|
|
2071
|
+
*
|
|
2072
|
+
*/
|
|
2073
|
+
identifyingParams() {
|
|
2074
|
+
return this._identifyingParams();
|
|
2075
|
+
}
|
|
2076
|
+
/** @ignore */
|
|
2077
|
+
async _responseApiGenerate(messages, options, runManager) {
|
|
2078
|
+
const invocationParams = this.invocationParams(options);
|
|
2079
|
+
if (invocationParams.stream) {
|
|
2080
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
2081
|
+
let finalChunk;
|
|
2082
|
+
for await (const chunk of stream) {
|
|
2083
|
+
chunk.message.response_metadata = {
|
|
2084
|
+
...chunk.generationInfo,
|
|
2085
|
+
...chunk.message.response_metadata,
|
|
2086
|
+
};
|
|
2087
|
+
finalChunk = finalChunk?.concat(chunk) ?? chunk;
|
|
2088
|
+
}
|
|
2089
|
+
return {
|
|
2090
|
+
generations: finalChunk ? [finalChunk] : [],
|
|
2091
|
+
llmOutput: {
|
|
2092
|
+
estimatedTokenUsage: finalChunk?.message
|
|
2093
|
+
?.usage_metadata,
|
|
2094
|
+
},
|
|
2095
|
+
};
|
|
2096
|
+
}
|
|
2097
|
+
const input = _convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled);
|
|
2098
|
+
const data = await this.responseApiWithRetry({
|
|
2099
|
+
input,
|
|
2100
|
+
...invocationParams,
|
|
2101
|
+
}, { signal: options?.signal, ...options?.options });
|
|
2102
|
+
return {
|
|
2103
|
+
generations: [
|
|
2104
|
+
{
|
|
2105
|
+
text: data.output_text,
|
|
2106
|
+
message: _convertOpenAIResponsesMessageToBaseMessage(data),
|
|
2107
|
+
},
|
|
2108
|
+
],
|
|
2109
|
+
llmOutput: {
|
|
2110
|
+
id: data.id,
|
|
2111
|
+
estimatedTokenUsage: data.usage
|
|
2112
|
+
? {
|
|
2113
|
+
promptTokens: data.usage.input_tokens,
|
|
2114
|
+
completionTokens: data.usage.output_tokens,
|
|
2115
|
+
totalTokens: data.usage.total_tokens,
|
|
2116
|
+
}
|
|
2117
|
+
: undefined,
|
|
2118
|
+
},
|
|
2119
|
+
};
|
|
2582
2120
|
}
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2121
|
+
/**
|
|
2122
|
+
* Determines whether the responses API should be used for the given request.
|
|
2123
|
+
*
|
|
2124
|
+
* @internal
|
|
2125
|
+
*
|
|
2126
|
+
* @param options The parsed call options for the request.
|
|
2127
|
+
* @returns `true` if the responses API should be used, either because it is explicitly enabled,
|
|
2128
|
+
* or because the request requires it.
|
|
2129
|
+
*/
|
|
2130
|
+
_useResponseApi(options) {
|
|
2131
|
+
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
|
2132
|
+
const hasResponsesOnlyKwargs = options?.previous_response_id != null ||
|
|
2133
|
+
options?.text != null ||
|
|
2134
|
+
options?.truncation != null ||
|
|
2135
|
+
options?.include != null ||
|
|
2136
|
+
options?.reasoning?.summary != null ||
|
|
2137
|
+
this.reasoning?.summary != null;
|
|
2138
|
+
return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs;
|
|
2139
|
+
}
|
|
2140
|
+
/** @ignore */
|
|
2141
|
+
async _generate(messages, options, runManager) {
|
|
2142
|
+
if (this._useResponseApi(options)) {
|
|
2143
|
+
return this._responseApiGenerate(messages, options, runManager);
|
|
2144
|
+
}
|
|
2145
|
+
const usageMetadata = {};
|
|
2146
|
+
const params = this.invocationParams(options);
|
|
2147
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
2148
|
+
if (params.stream) {
|
|
2149
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
2150
|
+
const finalChunks = {};
|
|
2151
|
+
for await (const chunk of stream) {
|
|
2152
|
+
chunk.message.response_metadata = {
|
|
2153
|
+
...chunk.generationInfo,
|
|
2154
|
+
...chunk.message.response_metadata,
|
|
2155
|
+
};
|
|
2156
|
+
const index = chunk.generationInfo?.completion ?? 0;
|
|
2157
|
+
if (finalChunks[index] === undefined) {
|
|
2158
|
+
finalChunks[index] = chunk;
|
|
2159
|
+
}
|
|
2160
|
+
else {
|
|
2161
|
+
finalChunks[index] = finalChunks[index].concat(chunk);
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
const generations = Object.entries(finalChunks)
|
|
2165
|
+
.sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
|
|
2166
|
+
.map(([_, value]) => value);
|
|
2167
|
+
const { functions, function_call } = this.invocationParams(options);
|
|
2168
|
+
// OpenAI does not support token usage report under stream mode,
|
|
2169
|
+
// fallback to estimation.
|
|
2170
|
+
const promptTokenUsage = await this.getEstimatedTokenCountFromPrompt(messages, functions, function_call);
|
|
2171
|
+
const completionTokenUsage = await this.getNumTokensFromGenerations(generations);
|
|
2172
|
+
usageMetadata.input_tokens = promptTokenUsage;
|
|
2173
|
+
usageMetadata.output_tokens = completionTokenUsage;
|
|
2174
|
+
usageMetadata.total_tokens = promptTokenUsage + completionTokenUsage;
|
|
2175
|
+
return {
|
|
2176
|
+
generations,
|
|
2177
|
+
llmOutput: {
|
|
2178
|
+
estimatedTokenUsage: {
|
|
2179
|
+
promptTokens: usageMetadata.input_tokens,
|
|
2180
|
+
completionTokens: usageMetadata.output_tokens,
|
|
2181
|
+
totalTokens: usageMetadata.total_tokens,
|
|
2182
|
+
},
|
|
2183
|
+
},
|
|
2184
|
+
};
|
|
2185
|
+
}
|
|
2186
|
+
else {
|
|
2187
|
+
let data;
|
|
2188
|
+
if (options.response_format &&
|
|
2189
|
+
options.response_format.type === "json_schema") {
|
|
2190
|
+
data = await this.betaParsedCompletionWithRetry({
|
|
2191
|
+
...params,
|
|
2192
|
+
stream: false,
|
|
2193
|
+
messages: messagesMapped,
|
|
2194
|
+
}, {
|
|
2195
|
+
signal: options?.signal,
|
|
2196
|
+
...options?.options,
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2199
|
+
else {
|
|
2200
|
+
data = await this.completionWithRetry({
|
|
2201
|
+
...params,
|
|
2202
|
+
stream: false,
|
|
2203
|
+
messages: messagesMapped,
|
|
2204
|
+
}, {
|
|
2205
|
+
signal: options?.signal,
|
|
2206
|
+
...options?.options,
|
|
2207
|
+
});
|
|
2208
|
+
}
|
|
2209
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, prompt_tokens_details: promptTokensDetails, completion_tokens_details: completionTokensDetails, } = data?.usage ?? {};
|
|
2210
|
+
if (completionTokens) {
|
|
2211
|
+
usageMetadata.output_tokens =
|
|
2212
|
+
(usageMetadata.output_tokens ?? 0) + completionTokens;
|
|
2213
|
+
}
|
|
2214
|
+
if (promptTokens) {
|
|
2215
|
+
usageMetadata.input_tokens =
|
|
2216
|
+
(usageMetadata.input_tokens ?? 0) + promptTokens;
|
|
2217
|
+
}
|
|
2218
|
+
if (totalTokens) {
|
|
2219
|
+
usageMetadata.total_tokens =
|
|
2220
|
+
(usageMetadata.total_tokens ?? 0) + totalTokens;
|
|
2221
|
+
}
|
|
2222
|
+
if (promptTokensDetails?.audio_tokens !== null ||
|
|
2223
|
+
promptTokensDetails?.cached_tokens !== null) {
|
|
2224
|
+
usageMetadata.input_token_details = {
|
|
2225
|
+
...(promptTokensDetails?.audio_tokens !== null && {
|
|
2226
|
+
audio: promptTokensDetails?.audio_tokens,
|
|
2227
|
+
}),
|
|
2228
|
+
...(promptTokensDetails?.cached_tokens !== null && {
|
|
2229
|
+
cache_read: promptTokensDetails?.cached_tokens,
|
|
2230
|
+
}),
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
if (completionTokensDetails?.audio_tokens !== null ||
|
|
2234
|
+
completionTokensDetails?.reasoning_tokens !== null) {
|
|
2235
|
+
usageMetadata.output_token_details = {
|
|
2236
|
+
...(completionTokensDetails?.audio_tokens !== null && {
|
|
2237
|
+
audio: completionTokensDetails?.audio_tokens,
|
|
2238
|
+
}),
|
|
2239
|
+
...(completionTokensDetails?.reasoning_tokens !== null && {
|
|
2240
|
+
reasoning: completionTokensDetails?.reasoning_tokens,
|
|
2241
|
+
}),
|
|
2242
|
+
};
|
|
2243
|
+
}
|
|
2244
|
+
const generations = [];
|
|
2245
|
+
for (const part of data?.choices ?? []) {
|
|
2246
|
+
const text = part.message?.content ?? "";
|
|
2247
|
+
const generation = {
|
|
2248
|
+
text,
|
|
2249
|
+
message: this._convertOpenAIChatCompletionMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
|
|
2250
|
+
};
|
|
2251
|
+
generation.generationInfo = {
|
|
2252
|
+
...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
|
|
2253
|
+
...(part.logprobs ? { logprobs: part.logprobs } : {}),
|
|
2254
|
+
};
|
|
2255
|
+
if (isAIMessage(generation.message)) {
|
|
2256
|
+
generation.message.usage_metadata = usageMetadata;
|
|
2257
|
+
}
|
|
2258
|
+
// Fields are not serialized unless passed to the constructor
|
|
2259
|
+
// Doing this ensures all fields on the message are serialized
|
|
2260
|
+
generation.message = new AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith("lc_"))));
|
|
2261
|
+
generations.push(generation);
|
|
2262
|
+
}
|
|
2263
|
+
return {
|
|
2264
|
+
generations,
|
|
2265
|
+
llmOutput: {
|
|
2266
|
+
tokenUsage: {
|
|
2267
|
+
promptTokens: usageMetadata.input_tokens,
|
|
2268
|
+
completionTokens: usageMetadata.output_tokens,
|
|
2269
|
+
totalTokens: usageMetadata.total_tokens,
|
|
2270
|
+
},
|
|
2271
|
+
},
|
|
2272
|
+
};
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
/**
|
|
2276
|
+
* Estimate the number of tokens a prompt will use.
|
|
2277
|
+
* Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
|
|
2278
|
+
*/
|
|
2279
|
+
async getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
2280
|
+
// It appears that if functions are present, the first system message is padded with a trailing newline. This
|
|
2281
|
+
// was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
|
|
2282
|
+
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
2283
|
+
// If there are functions, add the function definitions as they count towards token usage
|
|
2284
|
+
if (functions && function_call !== "auto") {
|
|
2285
|
+
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
2286
|
+
tokens += await this.getNumTokens(promptDefinitions);
|
|
2287
|
+
tokens += 9; // Add nine per completion
|
|
2288
|
+
}
|
|
2289
|
+
// If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
|
|
2290
|
+
// functions typically add a system message, but reuse the first one if it's already there. This offsets
|
|
2291
|
+
// the extra 9 tokens added by the function definitions.
|
|
2292
|
+
if (functions && messages.find((m) => m._getType() === "system")) {
|
|
2293
|
+
tokens -= 4;
|
|
2294
|
+
}
|
|
2295
|
+
// If function_call is 'none', add one token.
|
|
2296
|
+
// If it's a FunctionCall object, add 4 + the number of tokens in the function name.
|
|
2297
|
+
// If it's undefined or 'auto', don't add anything.
|
|
2298
|
+
if (function_call === "none") {
|
|
2299
|
+
tokens += 1;
|
|
2300
|
+
}
|
|
2301
|
+
else if (typeof function_call === "object") {
|
|
2302
|
+
tokens += (await this.getNumTokens(function_call.name)) + 4;
|
|
2303
|
+
}
|
|
2304
|
+
return tokens;
|
|
2305
|
+
}
|
|
2306
|
+
/**
|
|
2307
|
+
* Estimate the number of tokens an array of generations have used.
|
|
2308
|
+
*/
|
|
2309
|
+
async getNumTokensFromGenerations(generations) {
|
|
2310
|
+
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
2311
|
+
if (generation.message.additional_kwargs?.function_call) {
|
|
2312
|
+
return (await this.getNumTokensFromMessages([generation.message]))
|
|
2313
|
+
.countPerMessage[0];
|
|
2314
|
+
}
|
|
2315
|
+
else {
|
|
2316
|
+
return await this.getNumTokens(generation.message.content);
|
|
2317
|
+
}
|
|
2318
|
+
}));
|
|
2319
|
+
return generationUsages.reduce((a, b) => a + b, 0);
|
|
2320
|
+
}
|
|
2321
|
+
async getNumTokensFromMessages(messages) {
|
|
2322
|
+
let totalCount = 0;
|
|
2323
|
+
let tokensPerMessage = 0;
|
|
2324
|
+
let tokensPerName = 0;
|
|
2325
|
+
// From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
|
2326
|
+
if (this.model === "gpt-3.5-turbo-0301") {
|
|
2327
|
+
tokensPerMessage = 4;
|
|
2328
|
+
tokensPerName = -1;
|
|
2329
|
+
}
|
|
2330
|
+
else {
|
|
2331
|
+
tokensPerMessage = 3;
|
|
2332
|
+
tokensPerName = 1;
|
|
2333
|
+
}
|
|
2334
|
+
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
2335
|
+
const textCount = await this.getNumTokens(message.content);
|
|
2336
|
+
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
2337
|
+
const nameCount = message.name !== undefined
|
|
2338
|
+
? tokensPerName + (await this.getNumTokens(message.name))
|
|
2339
|
+
: 0;
|
|
2340
|
+
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
2341
|
+
// From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
|
|
2342
|
+
const openAIMessage = message;
|
|
2343
|
+
if (openAIMessage._getType() === "function") {
|
|
2344
|
+
count -= 2;
|
|
2345
|
+
}
|
|
2346
|
+
if (openAIMessage.additional_kwargs?.function_call) {
|
|
2347
|
+
count += 3;
|
|
2348
|
+
}
|
|
2349
|
+
if (openAIMessage?.additional_kwargs.function_call?.name) {
|
|
2350
|
+
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
2351
|
+
}
|
|
2352
|
+
if (openAIMessage.additional_kwargs.function_call?.arguments) {
|
|
2353
|
+
try {
|
|
2354
|
+
count += await this.getNumTokens(
|
|
2355
|
+
// Remove newlines and spaces
|
|
2356
|
+
JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
2357
|
+
}
|
|
2358
|
+
catch (error) {
|
|
2359
|
+
console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
|
|
2360
|
+
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
totalCount += count;
|
|
2364
|
+
return count;
|
|
2365
|
+
}));
|
|
2366
|
+
totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
|
|
2367
|
+
return { totalCount, countPerMessage };
|
|
2368
|
+
}
|
|
2369
|
+
async completionWithRetry(request, options) {
|
|
2370
|
+
const requestOptions = this._getClientOptions(options);
|
|
2371
|
+
return this.caller.call(async () => {
|
|
2372
|
+
try {
|
|
2373
|
+
const res = await this.client.chat.completions.create(request, requestOptions);
|
|
2374
|
+
return res;
|
|
2375
|
+
}
|
|
2376
|
+
catch (e) {
|
|
2377
|
+
const error = wrapOpenAIClientError(e);
|
|
2378
|
+
throw error;
|
|
2379
|
+
}
|
|
2594
2380
|
});
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2381
|
+
}
|
|
2382
|
+
async responseApiWithRetry(request, options) {
|
|
2383
|
+
return this.caller.call(async () => {
|
|
2384
|
+
const requestOptions = this._getClientOptions(options);
|
|
2385
|
+
try {
|
|
2386
|
+
// use parse if dealing with json_schema
|
|
2387
|
+
if (request.text?.format?.type === "json_schema" && !request.stream) {
|
|
2388
|
+
return await this.client.responses.parse(request, requestOptions);
|
|
2389
|
+
}
|
|
2390
|
+
return await this.client.responses.create(request, requestOptions);
|
|
2391
|
+
}
|
|
2392
|
+
catch (e) {
|
|
2393
|
+
const error = wrapOpenAIClientError(e);
|
|
2394
|
+
throw error;
|
|
2395
|
+
}
|
|
2600
2396
|
});
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2397
|
+
}
|
|
2398
|
+
/**
|
|
2399
|
+
* Call the beta chat completions parse endpoint. This should only be called if
|
|
2400
|
+
* response_format is set to "json_object".
|
|
2401
|
+
* @param {OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming} request
|
|
2402
|
+
* @param {OpenAICoreRequestOptions | undefined} options
|
|
2403
|
+
*/
|
|
2404
|
+
async betaParsedCompletionWithRetry(request, options
|
|
2405
|
+
// Avoid relying importing a beta type with no official entrypoint
|
|
2406
|
+
) {
|
|
2407
|
+
const requestOptions = this._getClientOptions(options);
|
|
2408
|
+
return this.caller.call(async () => {
|
|
2409
|
+
try {
|
|
2410
|
+
const res = await this.client.chat.completions.parse(request, requestOptions);
|
|
2411
|
+
return res;
|
|
2412
|
+
}
|
|
2413
|
+
catch (e) {
|
|
2414
|
+
const error = wrapOpenAIClientError(e);
|
|
2415
|
+
throw error;
|
|
2416
|
+
}
|
|
2606
2417
|
});
|
|
2607
|
-
this.useResponsesApi = fields?.useResponsesApi ?? false;
|
|
2608
|
-
this.responses = new ChatOpenAIResponses(fields);
|
|
2609
|
-
this.completions = new ChatOpenAICompletions(fields);
|
|
2610
2418
|
}
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2419
|
+
_getClientOptions(options) {
|
|
2420
|
+
if (!this.client) {
|
|
2421
|
+
const openAIEndpointConfig = {
|
|
2422
|
+
baseURL: this.clientConfig.baseURL,
|
|
2423
|
+
};
|
|
2424
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
2425
|
+
const params = {
|
|
2426
|
+
...this.clientConfig,
|
|
2427
|
+
baseURL: endpoint,
|
|
2428
|
+
timeout: this.timeout,
|
|
2429
|
+
maxRetries: 0,
|
|
2430
|
+
};
|
|
2431
|
+
if (!params.baseURL) {
|
|
2432
|
+
delete params.baseURL;
|
|
2433
|
+
}
|
|
2434
|
+
this.client = new OpenAIClient(params);
|
|
2435
|
+
}
|
|
2436
|
+
const requestOptions = {
|
|
2437
|
+
...this.clientConfig,
|
|
2438
|
+
...options,
|
|
2439
|
+
};
|
|
2440
|
+
return requestOptions;
|
|
2441
|
+
}
|
|
2442
|
+
_llmType() {
|
|
2443
|
+
return "openai";
|
|
2620
2444
|
}
|
|
2621
2445
|
/** @ignore */
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2446
|
+
_combineLLMOutput(...llmOutputs) {
|
|
2447
|
+
return llmOutputs.reduce((acc, llmOutput) => {
|
|
2448
|
+
if (llmOutput && llmOutput.tokenUsage) {
|
|
2449
|
+
acc.tokenUsage.completionTokens +=
|
|
2450
|
+
llmOutput.tokenUsage.completionTokens ?? 0;
|
|
2451
|
+
acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
|
|
2452
|
+
acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
|
|
2453
|
+
}
|
|
2454
|
+
return acc;
|
|
2455
|
+
}, {
|
|
2456
|
+
tokenUsage: {
|
|
2457
|
+
completionTokens: 0,
|
|
2458
|
+
promptTokens: 0,
|
|
2459
|
+
totalTokens: 0,
|
|
2460
|
+
},
|
|
2461
|
+
});
|
|
2627
2462
|
}
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2463
|
+
withStructuredOutput(outputSchema, config) {
|
|
2464
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2465
|
+
let schema;
|
|
2466
|
+
let name;
|
|
2467
|
+
let method;
|
|
2468
|
+
let includeRaw;
|
|
2469
|
+
if (isStructuredOutputMethodParams(outputSchema)) {
|
|
2470
|
+
schema = outputSchema.schema;
|
|
2471
|
+
name = outputSchema.name;
|
|
2472
|
+
method = outputSchema.method;
|
|
2473
|
+
includeRaw = outputSchema.includeRaw;
|
|
2632
2474
|
}
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2475
|
+
else {
|
|
2476
|
+
schema = outputSchema;
|
|
2477
|
+
name = config?.name;
|
|
2478
|
+
method = config?.method;
|
|
2479
|
+
includeRaw = config?.includeRaw;
|
|
2480
|
+
}
|
|
2481
|
+
let llm;
|
|
2482
|
+
let outputParser;
|
|
2483
|
+
if (config?.strict !== undefined && method === "jsonMode") {
|
|
2484
|
+
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
2485
|
+
}
|
|
2486
|
+
if (!this.model.startsWith("gpt-3") &&
|
|
2487
|
+
!this.model.startsWith("gpt-4-") &&
|
|
2488
|
+
this.model !== "gpt-4") {
|
|
2489
|
+
if (method === undefined) {
|
|
2490
|
+
method = "jsonSchema";
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
else if (method === "jsonSchema") {
|
|
2494
|
+
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
2495
|
+
}
|
|
2496
|
+
if (method === "jsonMode") {
|
|
2497
|
+
let outputFormatSchema;
|
|
2498
|
+
if (isInteropZodSchema(schema)) {
|
|
2499
|
+
outputParser = StructuredOutputParser.fromZodSchema(schema);
|
|
2500
|
+
outputFormatSchema = toJsonSchema(schema);
|
|
2501
|
+
}
|
|
2502
|
+
else {
|
|
2503
|
+
outputParser = new JsonOutputParser();
|
|
2504
|
+
}
|
|
2505
|
+
llm = this.withConfig({
|
|
2506
|
+
response_format: { type: "json_object" },
|
|
2507
|
+
ls_structured_output_format: {
|
|
2508
|
+
kwargs: { method: "jsonMode" },
|
|
2509
|
+
schema: outputFormatSchema,
|
|
2510
|
+
},
|
|
2511
|
+
});
|
|
2512
|
+
}
|
|
2513
|
+
else if (method === "jsonSchema") {
|
|
2514
|
+
llm = this.withConfig({
|
|
2515
|
+
response_format: {
|
|
2516
|
+
type: "json_schema",
|
|
2517
|
+
json_schema: {
|
|
2518
|
+
name: name ?? "extract",
|
|
2519
|
+
description: getSchemaDescription(schema),
|
|
2520
|
+
schema,
|
|
2521
|
+
strict: config?.strict,
|
|
2522
|
+
},
|
|
2647
2523
|
},
|
|
2648
|
-
|
|
2649
|
-
|
|
2524
|
+
ls_structured_output_format: {
|
|
2525
|
+
kwargs: { method: "jsonSchema" },
|
|
2526
|
+
schema: toJsonSchema(schema),
|
|
2650
2527
|
},
|
|
2651
2528
|
});
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2529
|
+
if (isInteropZodSchema(schema)) {
|
|
2530
|
+
const altParser = StructuredOutputParser.fromZodSchema(schema);
|
|
2531
|
+
outputParser = RunnableLambda.from((aiMessage) => {
|
|
2532
|
+
if ("parsed" in aiMessage.additional_kwargs) {
|
|
2533
|
+
return aiMessage.additional_kwargs.parsed;
|
|
2534
|
+
}
|
|
2535
|
+
return altParser;
|
|
2536
|
+
});
|
|
2537
|
+
}
|
|
2538
|
+
else {
|
|
2539
|
+
outputParser = new JsonOutputParser();
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
else {
|
|
2543
|
+
let functionName = name ?? "extract";
|
|
2544
|
+
// Is function calling
|
|
2545
|
+
if (isInteropZodSchema(schema)) {
|
|
2546
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
2547
|
+
llm = this.withConfig({
|
|
2548
|
+
tools: [
|
|
2549
|
+
{
|
|
2550
|
+
type: "function",
|
|
2551
|
+
function: {
|
|
2552
|
+
name: functionName,
|
|
2553
|
+
description: asJsonSchema.description,
|
|
2554
|
+
parameters: asJsonSchema,
|
|
2555
|
+
},
|
|
2556
|
+
},
|
|
2557
|
+
],
|
|
2558
|
+
tool_choice: {
|
|
2559
|
+
type: "function",
|
|
2560
|
+
function: {
|
|
2561
|
+
name: functionName,
|
|
2562
|
+
},
|
|
2563
|
+
},
|
|
2564
|
+
ls_structured_output_format: {
|
|
2565
|
+
kwargs: { method: "functionCalling" },
|
|
2566
|
+
schema: asJsonSchema,
|
|
2567
|
+
},
|
|
2568
|
+
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
2569
|
+
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
2570
|
+
});
|
|
2571
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
2572
|
+
returnSingle: true,
|
|
2573
|
+
keyName: functionName,
|
|
2574
|
+
zodSchema: schema,
|
|
2575
|
+
});
|
|
2576
|
+
}
|
|
2577
|
+
else {
|
|
2578
|
+
let openAIFunctionDefinition;
|
|
2579
|
+
if (typeof schema.name === "string" &&
|
|
2580
|
+
typeof schema.parameters === "object" &&
|
|
2581
|
+
schema.parameters != null) {
|
|
2582
|
+
openAIFunctionDefinition = schema;
|
|
2583
|
+
functionName = schema.name;
|
|
2584
|
+
}
|
|
2585
|
+
else {
|
|
2586
|
+
functionName = schema.title ?? functionName;
|
|
2587
|
+
openAIFunctionDefinition = {
|
|
2588
|
+
name: functionName,
|
|
2589
|
+
description: schema.description ?? "",
|
|
2590
|
+
parameters: schema,
|
|
2591
|
+
};
|
|
2592
|
+
}
|
|
2593
|
+
llm = this.withConfig({
|
|
2594
|
+
tools: [
|
|
2595
|
+
{
|
|
2596
|
+
type: "function",
|
|
2597
|
+
function: openAIFunctionDefinition,
|
|
2598
|
+
},
|
|
2599
|
+
],
|
|
2600
|
+
tool_choice: {
|
|
2601
|
+
type: "function",
|
|
2602
|
+
function: {
|
|
2603
|
+
name: functionName,
|
|
2604
|
+
},
|
|
2605
|
+
},
|
|
2606
|
+
ls_structured_output_format: {
|
|
2607
|
+
kwargs: { method: "functionCalling" },
|
|
2608
|
+
schema: toJsonSchema(schema),
|
|
2609
|
+
},
|
|
2610
|
+
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
2611
|
+
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
2612
|
+
});
|
|
2613
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
2614
|
+
returnSingle: true,
|
|
2615
|
+
keyName: functionName,
|
|
2616
|
+
});
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
if (!includeRaw) {
|
|
2620
|
+
return llm.pipe(outputParser);
|
|
2621
|
+
}
|
|
2622
|
+
const parserAssign = RunnablePassthrough.assign({
|
|
2623
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2624
|
+
parsed: (input, config) => outputParser.invoke(input.raw, config),
|
|
2625
|
+
});
|
|
2626
|
+
const parserNone = RunnablePassthrough.assign({
|
|
2627
|
+
parsed: () => null,
|
|
2628
|
+
});
|
|
2629
|
+
const parsedWithFallback = parserAssign.withFallbacks({
|
|
2630
|
+
fallbacks: [parserNone],
|
|
2631
|
+
});
|
|
2632
|
+
return RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
function isStructuredOutputMethodParams(x
|
|
2636
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2637
|
+
) {
|
|
2638
|
+
return (x !== undefined &&
|
|
2639
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2640
|
+
typeof x.schema ===
|
|
2641
|
+
"object");
|
|
2642
|
+
}
|
|
2643
|
+
// inlined from openai/lib/parser.ts
|
|
2644
|
+
function makeParseableResponseFormat(response_format, parser) {
|
|
2645
|
+
const obj = { ...response_format };
|
|
2646
|
+
Object.defineProperties(obj, {
|
|
2647
|
+
$brand: {
|
|
2648
|
+
value: "auto-parseable-response-format",
|
|
2649
|
+
enumerable: false,
|
|
2650
|
+
},
|
|
2651
|
+
$parseRaw: {
|
|
2652
|
+
value: parser,
|
|
2653
|
+
enumerable: false,
|
|
2654
|
+
},
|
|
2655
|
+
});
|
|
2656
|
+
return obj;
|
|
2657
|
+
}
|
|
2658
|
+
function interopZodResponseFormat(zodSchema, name, props) {
|
|
2659
|
+
if (isZodSchemaV3(zodSchema)) {
|
|
2660
|
+
return zodResponseFormat(zodSchema, name, props);
|
|
2661
|
+
}
|
|
2662
|
+
if (isZodSchemaV4(zodSchema)) {
|
|
2663
|
+
return makeParseableResponseFormat({
|
|
2664
|
+
type: "json_schema",
|
|
2665
|
+
json_schema: {
|
|
2666
|
+
...props,
|
|
2667
|
+
name,
|
|
2668
|
+
strict: true,
|
|
2669
|
+
schema: z4.toJSONSchema(zodSchema, {
|
|
2670
|
+
cycles: "ref", // equivalent to nameStrategy: 'duplicate-ref'
|
|
2671
|
+
reused: "ref", // equivalent to $refStrategy: 'extract-to-root'
|
|
2672
|
+
override(ctx) {
|
|
2673
|
+
ctx.jsonSchema.title = name; // equivalent to `name` property
|
|
2674
|
+
// TODO: implement `nullableStrategy` patch-fix (zod doesn't support openApi3 json schema target)
|
|
2675
|
+
// TODO: implement `openaiStrictMode` patch-fix (where optional properties without `nullable` are not supported)
|
|
2676
|
+
},
|
|
2677
|
+
/// property equivalents from native `zodResponseFormat` fn
|
|
2678
|
+
// openaiStrictMode: true,
|
|
2679
|
+
// name,
|
|
2680
|
+
// nameStrategy: 'duplicate-ref',
|
|
2681
|
+
// $refStrategy: 'extract-to-root',
|
|
2682
|
+
// nullableStrategy: 'property',
|
|
2683
|
+
}),
|
|
2684
|
+
},
|
|
2685
|
+
}, (content) => z4.parse(zodSchema, JSON.parse(content)));
|
|
2657
2686
|
}
|
|
2687
|
+
throw new Error("Unsupported schema response format");
|
|
2658
2688
|
}
|