@langchain/openai 0.6.8 → 1.0.0-alpha.1

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