@langchain/openai 0.5.16 → 0.5.17

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