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