@langchain/openai 1.0.0-alpha.2 → 1.0.0

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 (110) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +6 -6
  3. package/dist/azure/chat_models/common.cjs +130 -0
  4. package/dist/azure/chat_models/common.cjs.map +1 -0
  5. package/dist/azure/chat_models/common.d.cts +16 -0
  6. package/dist/azure/chat_models/common.d.cts.map +1 -0
  7. package/dist/azure/chat_models/common.d.ts +16 -0
  8. package/dist/azure/chat_models/common.d.ts.map +1 -0
  9. package/dist/azure/chat_models/common.js +124 -0
  10. package/dist/azure/chat_models/common.js.map +1 -0
  11. package/dist/azure/chat_models/completions.cjs +50 -0
  12. package/dist/azure/chat_models/completions.cjs.map +1 -0
  13. package/dist/azure/chat_models/completions.d.cts +29 -0
  14. package/dist/azure/chat_models/completions.d.cts.map +1 -0
  15. package/dist/azure/chat_models/completions.d.ts +29 -0
  16. package/dist/azure/chat_models/completions.d.ts.map +1 -0
  17. package/dist/azure/chat_models/completions.js +50 -0
  18. package/dist/azure/chat_models/completions.js.map +1 -0
  19. package/dist/azure/{chat_models.cjs → chat_models/index.cjs} +14 -214
  20. package/dist/azure/chat_models/index.cjs.map +1 -0
  21. package/dist/azure/{chat_models.d.ts → chat_models/index.d.cts} +6 -11
  22. package/dist/azure/chat_models/index.d.cts.map +1 -0
  23. package/dist/azure/{chat_models.d.cts → chat_models/index.d.ts} +6 -11
  24. package/dist/azure/chat_models/index.d.ts.map +1 -0
  25. package/dist/azure/{chat_models.js → chat_models/index.js} +6 -205
  26. package/dist/azure/chat_models/index.js.map +1 -0
  27. package/dist/azure/chat_models/responses.cjs +50 -0
  28. package/dist/azure/chat_models/responses.cjs.map +1 -0
  29. package/dist/azure/chat_models/responses.d.cts +29 -0
  30. package/dist/azure/chat_models/responses.d.cts.map +1 -0
  31. package/dist/azure/chat_models/responses.d.ts +29 -0
  32. package/dist/azure/chat_models/responses.d.ts.map +1 -0
  33. package/dist/azure/chat_models/responses.js +50 -0
  34. package/dist/azure/chat_models/responses.js.map +1 -0
  35. package/dist/chat_models/base.cjs +533 -0
  36. package/dist/chat_models/base.cjs.map +1 -0
  37. package/dist/chat_models/base.d.cts +272 -0
  38. package/dist/chat_models/base.d.cts.map +1 -0
  39. package/dist/chat_models/base.d.ts +272 -0
  40. package/dist/chat_models/base.d.ts.map +1 -0
  41. package/dist/chat_models/base.js +532 -0
  42. package/dist/chat_models/base.js.map +1 -0
  43. package/dist/chat_models/completions.cjs +328 -0
  44. package/dist/chat_models/completions.cjs.map +1 -0
  45. package/dist/chat_models/completions.d.cts +33 -0
  46. package/dist/chat_models/completions.d.cts.map +1 -0
  47. package/dist/chat_models/completions.d.ts +33 -0
  48. package/dist/chat_models/completions.d.ts.map +1 -0
  49. package/dist/chat_models/completions.js +327 -0
  50. package/dist/chat_models/completions.js.map +1 -0
  51. package/dist/chat_models/index.cjs +608 -0
  52. package/dist/chat_models/index.cjs.map +1 -0
  53. package/dist/chat_models/index.d.cts +593 -0
  54. package/dist/chat_models/index.d.cts.map +1 -0
  55. package/dist/chat_models/index.d.ts +593 -0
  56. package/dist/chat_models/index.d.ts.map +1 -0
  57. package/dist/chat_models/index.js +608 -0
  58. package/dist/chat_models/index.js.map +1 -0
  59. package/dist/chat_models/responses.cjs +550 -0
  60. package/dist/chat_models/responses.cjs.map +1 -0
  61. package/dist/chat_models/responses.d.cts +77 -0
  62. package/dist/chat_models/responses.d.cts.map +1 -0
  63. package/dist/chat_models/responses.d.ts +77 -0
  64. package/dist/chat_models/responses.d.ts.map +1 -0
  65. package/dist/chat_models/responses.js +549 -0
  66. package/dist/chat_models/responses.js.map +1 -0
  67. package/dist/index.cjs +14 -7
  68. package/dist/index.d.cts +9 -3
  69. package/dist/index.d.ts +9 -3
  70. package/dist/index.js +8 -3
  71. package/dist/langchain-core/dist/callbacks/base.d.ts +1 -0
  72. package/dist/langchain-core/dist/callbacks/manager.d.ts +2 -0
  73. package/dist/langchain-core/dist/language_models/base.d.ts +5 -0
  74. package/dist/langchain-core/dist/language_models/chat_models.d.cts +13 -0
  75. package/dist/langchain-core/dist/language_models/chat_models.d.cts.map +1 -0
  76. package/dist/langchain-core/dist/language_models/chat_models.d.ts +20 -0
  77. package/dist/langchain-core/dist/language_models/chat_models.d.ts.map +1 -0
  78. package/dist/langchain-core/dist/runnables/base.d.ts +7 -0
  79. package/dist/langchain-core/dist/runnables/graph.d.ts +1 -0
  80. package/dist/langchain-core/dist/runnables/types.d.ts +2 -0
  81. package/dist/langchain-core/dist/tools/types.d.ts +5 -0
  82. package/dist/langchain-core/dist/tracers/base.d.ts +2 -0
  83. package/dist/langchain-core/dist/tracers/event_stream.d.ts +1 -0
  84. package/dist/langchain-core/dist/tracers/log_stream.d.ts +3 -0
  85. package/dist/langchain-core/dist/utils/types/index.d.ts +1 -0
  86. package/dist/langchain-core/dist/utils/types/zod.d.ts +1 -0
  87. package/dist/llms.cjs +1 -1
  88. package/dist/llms.d.cts +1 -1
  89. package/dist/llms.js +1 -1
  90. package/dist/utils/message_inputs.cjs +1 -1
  91. package/dist/utils/message_inputs.js +1 -1
  92. package/dist/utils/misc.cjs +4 -1
  93. package/dist/utils/misc.cjs.map +1 -1
  94. package/dist/utils/misc.d.cts.map +1 -1
  95. package/dist/utils/misc.d.ts.map +1 -1
  96. package/dist/utils/misc.js +4 -1
  97. package/dist/utils/misc.js.map +1 -1
  98. package/package.json +11 -10
  99. package/dist/azure/chat_models.cjs.map +0 -1
  100. package/dist/azure/chat_models.d.cts.map +0 -1
  101. package/dist/azure/chat_models.d.ts.map +0 -1
  102. package/dist/azure/chat_models.js.map +0 -1
  103. package/dist/chat_models.cjs +0 -1965
  104. package/dist/chat_models.cjs.map +0 -1
  105. package/dist/chat_models.d.cts +0 -931
  106. package/dist/chat_models.d.cts.map +0 -1
  107. package/dist/chat_models.d.ts +0 -931
  108. package/dist/chat_models.d.ts.map +0 -1
  109. package/dist/chat_models.js +0 -1961
  110. package/dist/chat_models.js.map +0 -1
@@ -1,931 +0,0 @@
1
- import { IterableReadableStream } from "./langchain-core/dist/utils/stream.js";
2
- import { ChatOpenAIReasoningSummary, ChatOpenAIResponseFormat, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, OpenAIVerbosityParam, ResponseFormatConfiguration } from "./types.js";
3
- import { ChatOpenAIToolType, OpenAIToolChoice, ResponsesTool, ResponsesToolChoice } from "./utils/tools.js";
4
- import { ClientOptions, OpenAI as OpenAI$1 } from "openai";
5
- import * as _langchain_core_messages0 from "@langchain/core/messages";
6
- import { AIMessageChunk, BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk } from "@langchain/core/messages";
7
- import { ChatGeneration, ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
8
- import { BaseChatModel, BaseChatModelParams, LangSmithParams } from "@langchain/core/language_models/chat_models";
9
- import { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
10
- import { Runnable } from "@langchain/core/runnables";
11
- import { InteropZodType } from "@langchain/core/utils/types";
12
- import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
13
-
14
- //#region src/chat_models.d.ts
15
- interface OpenAILLMOutput {
16
- tokenUsage: {
17
- completionTokens?: number;
18
- promptTokens?: number;
19
- totalTokens?: number;
20
- };
21
- }
22
- interface BaseChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
23
- /**
24
- * A list of tools that the model may use to generate responses.
25
- * Each tool can be a function, a built-in tool, or a custom tool definition.
26
- * If not provided, the model will not use any tools.
27
- */
28
- tools?: ChatOpenAIToolType[];
29
- /**
30
- * Specifies which tool the model should use to respond.
31
- * Can be an {@link OpenAIToolChoice} or a {@link ResponsesToolChoice}.
32
- * If not set, the model will decide which tool to use automatically.
33
- */
34
- // TODO: break OpenAIToolChoice and ResponsesToolChoice into options sub classes
35
- tool_choice?: OpenAIToolChoice | ResponsesToolChoice;
36
- /**
37
- * Adds a prompt index to prompts passed to the model to track
38
- * what prompt is being used for a given generation.
39
- */
40
- promptIndex?: number;
41
- /**
42
- * An object specifying the format that the model must output.
43
- */
44
- response_format?: ChatOpenAIResponseFormat;
45
- /**
46
- * When provided, the completions API will make a best effort to sample
47
- * deterministically, such that repeated requests with the same `seed`
48
- * and parameters should return the same result.
49
- */
50
- seed?: number;
51
- /**
52
- * Additional options to pass to streamed completions.
53
- * If provided, this takes precedence over "streamUsage" set at
54
- * initialization time.
55
- */
56
- stream_options?: OpenAI$1.Chat.ChatCompletionStreamOptions;
57
- /**
58
- * The model may choose to call multiple functions in a single turn. You can
59
- * set parallel_tool_calls to false which ensures only one tool is called at most.
60
- * [Learn more](https://platform.openai.com/docs/guides/function-calling#parallel-function-calling)
61
- */
62
- parallel_tool_calls?: boolean;
63
- /**
64
- * If `true`, model output is guaranteed to exactly match the JSON Schema
65
- * provided in the tool definition. If `true`, the input schema will also be
66
- * validated according to
67
- * https://platform.openai.com/docs/guides/structured-outputs/supported-schemas.
68
- *
69
- * If `false`, input schema will not be validated and model output will not
70
- * be validated.
71
- *
72
- * If `undefined`, `strict` argument will not be passed to the model.
73
- */
74
- strict?: boolean;
75
- /**
76
- * Output types that you would like the model to generate for this request. Most
77
- * models are capable of generating text, which is the default:
78
- *
79
- * `["text"]`
80
- *
81
- * The `gpt-4o-audio-preview` model can also be used to
82
- * [generate audio](https://platform.openai.com/docs/guides/audio). To request that
83
- * this model generate both text and audio responses, you can use:
84
- *
85
- * `["text", "audio"]`
86
- */
87
- modalities?: Array<OpenAI$1.Chat.ChatCompletionModality>;
88
- /**
89
- * Parameters for audio output. Required when audio output is requested with
90
- * `modalities: ["audio"]`.
91
- * [Learn more](https://platform.openai.com/docs/guides/audio).
92
- */
93
- audio?: OpenAI$1.Chat.ChatCompletionAudioParam;
94
- /**
95
- * Static predicted output content, such as the content of a text file that is being regenerated.
96
- * [Learn more](https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs).
97
- */
98
- prediction?: OpenAI$1.ChatCompletionPredictionContent;
99
- /**
100
- * Options for reasoning models.
101
- *
102
- * Note that some options, like reasoning summaries, are only available when using the responses
103
- * API. If these options are set, the responses API will be used to fulfill the request.
104
- *
105
- * These options will be ignored when not using a reasoning model.
106
- */
107
- reasoning?: OpenAI$1.Reasoning;
108
- /**
109
- * Service tier to use for this request. Can be "auto", "default", or "flex"
110
- * Specifies the service tier for prioritization and latency optimization.
111
- */
112
- service_tier?: OpenAI$1.Chat.ChatCompletionCreateParams["service_tier"];
113
- /**
114
- * Used by OpenAI to cache responses for similar requests to optimize your cache
115
- * hit rates. Replaces the `user` field.
116
- * [Learn more](https://platform.openai.com/docs/guides/prompt-caching).
117
- */
118
- promptCacheKey?: string;
119
- /**
120
- * The verbosity of the model's response.
121
- */
122
- verbosity?: OpenAIVerbosityParam;
123
- }
124
- interface BaseChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChatModelParams {
125
- /**
126
- * Optional configuration options for the OpenAI client.
127
- */
128
- configuration?: ClientOptions;
129
- }
130
- /** @internal */
131
- declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements Partial<OpenAIChatInput> {
132
- temperature?: number;
133
- topP?: number;
134
- frequencyPenalty?: number;
135
- presencePenalty?: number;
136
- n?: number;
137
- logitBias?: Record<string, number>;
138
- model: string;
139
- modelKwargs?: OpenAIChatInput["modelKwargs"];
140
- stop?: string[];
141
- stopSequences?: string[];
142
- user?: string;
143
- timeout?: number;
144
- streaming: boolean;
145
- streamUsage: boolean;
146
- maxTokens?: number;
147
- logprobs?: boolean;
148
- topLogprobs?: number;
149
- apiKey?: string;
150
- organization?: string;
151
- __includeRawResponse?: boolean;
152
- /** @internal */
153
- client: OpenAI$1;
154
- /** @internal */
155
- clientConfig: ClientOptions;
156
- /**
157
- * Whether the model supports the `strict` argument when passing in tools.
158
- * If `undefined` the `strict` argument will not be passed to OpenAI.
159
- */
160
- supportsStrictToolCalling?: boolean;
161
- audio?: OpenAI$1.Chat.ChatCompletionAudioParam;
162
- modalities?: Array<OpenAI$1.Chat.ChatCompletionModality>;
163
- reasoning?: OpenAI$1.Reasoning;
164
- /**
165
- * Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
166
- * output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
167
- * OpenAI organization or project. This must be configured directly with OpenAI.
168
- *
169
- * See:
170
- * https://platform.openai.com/docs/guides/your-data
171
- * https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
172
- *
173
- * @default false
174
- */
175
- zdrEnabled?: boolean | undefined;
176
- /**
177
- * Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
178
- * Specifies the service tier for prioritization and latency optimization.
179
- */
180
- service_tier?: OpenAI$1.Chat.ChatCompletionCreateParams["service_tier"];
181
- /**
182
- * Used by OpenAI to cache responses for similar requests to optimize your cache
183
- * hit rates.
184
- * [Learn more](https://platform.openai.com/docs/guides/prompt-caching).
185
- */
186
- promptCacheKey: string;
187
- /**
188
- * The verbosity of the model's response.
189
- */
190
- verbosity?: OpenAIVerbosityParam;
191
- protected defaultOptions: CallOptions;
192
- _llmType(): string;
193
- static lc_name(): string;
194
- get callKeys(): string[];
195
- lc_serializable: boolean;
196
- get lc_secrets(): {
197
- [key: string]: string;
198
- } | undefined;
199
- get lc_aliases(): Record<string, string>;
200
- get lc_serializable_keys(): string[];
201
- getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
202
- /** @ignore */
203
- _identifyingParams(): Omit<OpenAI$1.Chat.ChatCompletionCreateParams, "messages"> & {
204
- model_name: string;
205
- } & ClientOptions;
206
- /**
207
- * Get the identifying parameters for the model
208
- */
209
- identifyingParams(): Omit<OpenAI$1.ChatCompletionCreateParams, "messages"> & {
210
- model_name: string;
211
- } & ClientOptions;
212
- constructor(fields?: BaseChatOpenAIFields);
213
- /**
214
- * Returns backwards compatible reasoning parameters from constructor params and call options
215
- * @internal
216
- */
217
- protected _getReasoningParams(options?: this["ParsedCallOptions"]): OpenAI$1.Reasoning | undefined;
218
- /**
219
- * Returns an openai compatible response format from a set of options
220
- * @internal
221
- */
222
- protected _getResponseFormat(resFormat?: CallOptions["response_format"]): ResponseFormatConfiguration | undefined;
223
- protected _combineCallOptions(additionalOptions?: this["ParsedCallOptions"]): this["ParsedCallOptions"];
224
- /** @internal */
225
- _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
226
- // TODO: move to completions class
227
- protected _convertChatOpenAIToolToCompletionsTool(tool: ChatOpenAIToolType, fields?: {
228
- strict?: boolean;
229
- }): OpenAI$1.ChatCompletionTool;
230
- bindTools(tools: ChatOpenAIToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
231
- stream(input: BaseLanguageModelInput, options?: CallOptions): Promise<IterableReadableStream<AIMessageChunk<_langchain_core_messages0.MessageStructure>>>;
232
- invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<AIMessageChunk<_langchain_core_messages0.MessageStructure>>;
233
- /** @ignore */
234
- _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
235
- getNumTokensFromMessages(messages: BaseMessage[]): Promise<{
236
- totalCount: number;
237
- countPerMessage: number[];
238
- }>;
239
- /** @internal */
240
- protected _getNumTokensFromGenerations(generations: ChatGeneration[]): Promise<number>;
241
- /** @internal */
242
- protected _getEstimatedTokenCountFromPrompt(messages: BaseMessage[], functions?: OpenAI$1.Chat.ChatCompletionCreateParams.Function[], function_call?: "none" | "auto" | OpenAI$1.Chat.ChatCompletionFunctionCallOption): Promise<number>;
243
- /** @internal */
244
- protected _getStructuredOutputMethod(config: StructuredOutputMethodOptions<boolean>): string | undefined;
245
- withStructuredOutput<
246
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
247
- RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput>
248
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
249
- | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
250
- withStructuredOutput<
251
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
252
- RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput>
253
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
254
- | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
255
- raw: BaseMessage;
256
- parsed: RunOutput;
257
- }>;
258
- withStructuredOutput<
259
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
260
- RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput>
261
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
- | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
263
- raw: BaseMessage;
264
- parsed: RunOutput;
265
- }>;
266
- }
267
- interface ChatOpenAIResponsesCallOptions extends BaseChatOpenAICallOptions {
268
- /**
269
- * Configuration options for a text response from the model. Can be plain text or
270
- * structured JSON data.
271
- */
272
- text?: OpenAI$1.Responses.ResponseCreateParams["text"];
273
- /**
274
- * The truncation strategy to use for the model response.
275
- */
276
- truncation?: OpenAI$1.Responses.ResponseCreateParams["truncation"];
277
- /**
278
- * Specify additional output data to include in the model response.
279
- */
280
- include?: OpenAI$1.Responses.ResponseCreateParams["include"];
281
- /**
282
- * The unique ID of the previous response to the model. Use this to create multi-turn
283
- * conversations.
284
- */
285
- previous_response_id?: OpenAI$1.Responses.ResponseCreateParams["previous_response_id"];
286
- /**
287
- * The verbosity of the model's response.
288
- */
289
- verbosity?: OpenAIVerbosityParam;
290
- }
291
- type ChatResponsesInvocationParams = Omit<OpenAI$1.Responses.ResponseCreateParams, "input">;
292
- type ExcludeController<T> = T extends {
293
- controller: unknown;
294
- } ? never : T;
295
- type ResponsesCreate = OpenAI$1.Responses["create"];
296
- type ResponsesParse = OpenAI$1.Responses["parse"];
297
- type ResponsesCreateInvoke = ExcludeController<Awaited<ReturnType<ResponsesCreate>>>;
298
- type ResponsesParseInvoke = ExcludeController<Awaited<ReturnType<ResponsesParse>>>;
299
- /**
300
- * OpenAI Responses API implementation.
301
- *
302
- * Will be exported in a later version of @langchain/openai.
303
- *
304
- * @internal
305
- */
306
- declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponsesCallOptions = ChatOpenAIResponsesCallOptions> extends BaseChatOpenAI<CallOptions> {
307
- invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams;
308
- _generate(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<ChatResult>;
309
- _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
310
- /**
311
- * Calls the Responses API with retry logic in case of failures.
312
- * @param request The request to send to the OpenAI API.
313
- * @param options Optional configuration for the API call.
314
- * @returns The response from the OpenAI API.
315
- */
316
- completionWithRetry(request: OpenAI$1.Responses.ResponseCreateParamsStreaming, requestOptions?: OpenAI$1.RequestOptions): Promise<AsyncIterable<OpenAI$1.Responses.ResponseStreamEvent>>;
317
- completionWithRetry(request: OpenAI$1.Responses.ResponseCreateParamsNonStreaming, requestOptions?: OpenAI$1.RequestOptions): Promise<OpenAI$1.Responses.Response>;
318
- /** @internal */
319
- protected _convertResponsesMessageToBaseMessage(response: ResponsesCreateInvoke | ResponsesParseInvoke): BaseMessage;
320
- /** @internal */
321
- protected _convertResponsesDeltaToBaseMessageChunk(chunk: OpenAI$1.Responses.ResponseStreamEvent): ChatGenerationChunk | null;
322
- /** @internal */
323
- protected _convertMessagesToResponsesParams(messages: BaseMessage[]): any[];
324
- /** @internal */
325
- protected _convertReasoningSummary(reasoning: ChatOpenAIReasoningSummary): OpenAI$1.Responses.ResponseReasoningItem;
326
- /** @internal */
327
- protected _reduceChatOpenAITools(tools: ChatOpenAIToolType[], fields: {
328
- stream?: boolean;
329
- strict?: boolean;
330
- }): ResponsesTool[];
331
- }
332
- interface ChatOpenAICompletionsCallOptions extends BaseChatOpenAICallOptions {}
333
- type ChatCompletionsInvocationParams = Omit<OpenAI$1.Chat.Completions.ChatCompletionCreateParams, "messages">;
334
- /**
335
- * OpenAI Completions API implementation.
336
- * @internal
337
- */
338
- declare class ChatOpenAICompletions<CallOptions extends ChatOpenAICompletionsCallOptions = ChatOpenAICompletionsCallOptions> extends BaseChatOpenAI<CallOptions> {
339
- /** @internal */
340
- invocationParams(options?: this["ParsedCallOptions"], extra?: {
341
- streaming?: boolean;
342
- }): ChatCompletionsInvocationParams;
343
- _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
344
- _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
345
- completionWithRetry(request: OpenAI$1.Chat.ChatCompletionCreateParamsStreaming, requestOptions?: OpenAI$1.RequestOptions): Promise<AsyncIterable<OpenAI$1.Chat.Completions.ChatCompletionChunk>>;
346
- completionWithRetry(request: OpenAI$1.Chat.ChatCompletionCreateParamsNonStreaming, requestOptions?: OpenAI$1.RequestOptions): Promise<OpenAI$1.Chat.Completions.ChatCompletion>;
347
- /** @internal */
348
- protected _convertCompletionsMessageToBaseMessage(message: OpenAI$1.Chat.Completions.ChatCompletionMessage, rawResponse: OpenAI$1.Chat.Completions.ChatCompletion): BaseMessage;
349
- /** @internal */
350
- protected _convertCompletionsDeltaToBaseMessageChunk(
351
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
352
- delta: Record<string, any>, rawResponse: OpenAI$1.Chat.Completions.ChatCompletionChunk, defaultRole?: OpenAI$1.Chat.ChatCompletionRole): AIMessageChunk<_langchain_core_messages0.MessageStructure> | ChatMessageChunk<_langchain_core_messages0.MessageStructure> | FunctionMessageChunk<_langchain_core_messages0.MessageStructure> | HumanMessageChunk<_langchain_core_messages0.MessageStructure> | SystemMessageChunk<_langchain_core_messages0.MessageStructure> | ToolMessageChunk<_langchain_core_messages0.MessageStructure>;
353
- }
354
- type ChatOpenAICallOptions = ChatOpenAICompletionsCallOptions & ChatOpenAIResponsesCallOptions;
355
- interface ChatOpenAIFields extends BaseChatOpenAIFields {
356
- /**
357
- * Whether to use the responses API for all requests. If `false` the responses API will be used
358
- * only when required in order to fulfill the request.
359
- */
360
- useResponsesApi?: boolean;
361
- /**
362
- * The completions chat instance
363
- * @internal
364
- */
365
- completions?: ChatOpenAICompletions;
366
- /**
367
- * The responses chat instance
368
- * @internal
369
- */
370
- responses?: ChatOpenAIResponses;
371
- }
372
- /**
373
- * OpenAI chat model integration.
374
- *
375
- * To use with Azure, import the `AzureChatOpenAI` class.
376
- *
377
- * Setup:
378
- * Install `@langchain/openai` and set an environment variable named `OPENAI_API_KEY`.
379
- *
380
- * ```bash
381
- * npm install @langchain/openai
382
- * export OPENAI_API_KEY="your-api-key"
383
- * ```
384
- *
385
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html#constructor)
386
- *
387
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_openai.ChatOpenAICallOptions.html)
388
- *
389
- * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
390
- * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
391
- *
392
- * ```typescript
393
- * // When calling `.withConfig`, call options should be passed via the first argument
394
- * const llmWithArgsBound = llm.withConfig({
395
- * stop: ["\n"],
396
- * tools: [...],
397
- * });
398
- *
399
- * // When calling `.bindTools`, call options should be passed via the second argument
400
- * const llmWithTools = llm.bindTools(
401
- * [...],
402
- * {
403
- * tool_choice: "auto",
404
- * }
405
- * );
406
- * ```
407
- *
408
- * ## Examples
409
- *
410
- * <details open>
411
- * <summary><strong>Instantiate</strong></summary>
412
- *
413
- * ```typescript
414
- * import { ChatOpenAI } from '@langchain/openai';
415
- *
416
- * const llm = new ChatOpenAI({
417
- * model: "gpt-4o-mini",
418
- * temperature: 0,
419
- * maxTokens: undefined,
420
- * timeout: undefined,
421
- * maxRetries: 2,
422
- * // apiKey: "...",
423
- * // baseUrl: "...",
424
- * // organization: "...",
425
- * // other params...
426
- * });
427
- * ```
428
- * </details>
429
- *
430
- * <br />
431
- *
432
- * <details>
433
- * <summary><strong>Invoking</strong></summary>
434
- *
435
- * ```typescript
436
- * const input = `Translate "I love programming" into French.`;
437
- *
438
- * // Models also accept a list of chat messages or a formatted prompt
439
- * const result = await llm.invoke(input);
440
- * console.log(result);
441
- * ```
442
- *
443
- * ```txt
444
- * AIMessage {
445
- * "id": "chatcmpl-9u4Mpu44CbPjwYFkTbeoZgvzB00Tz",
446
- * "content": "J'adore la programmation.",
447
- * "response_metadata": {
448
- * "tokenUsage": {
449
- * "completionTokens": 5,
450
- * "promptTokens": 28,
451
- * "totalTokens": 33
452
- * },
453
- * "finish_reason": "stop",
454
- * "system_fingerprint": "fp_3aa7262c27"
455
- * },
456
- * "usage_metadata": {
457
- * "input_tokens": 28,
458
- * "output_tokens": 5,
459
- * "total_tokens": 33
460
- * }
461
- * }
462
- * ```
463
- * </details>
464
- *
465
- * <br />
466
- *
467
- * <details>
468
- * <summary><strong>Streaming Chunks</strong></summary>
469
- *
470
- * ```typescript
471
- * for await (const chunk of await llm.stream(input)) {
472
- * console.log(chunk);
473
- * }
474
- * ```
475
- *
476
- * ```txt
477
- * AIMessageChunk {
478
- * "id": "chatcmpl-9u4NWB7yUeHCKdLr6jP3HpaOYHTqs",
479
- * "content": ""
480
- * }
481
- * AIMessageChunk {
482
- * "content": "J"
483
- * }
484
- * AIMessageChunk {
485
- * "content": "'adore"
486
- * }
487
- * AIMessageChunk {
488
- * "content": " la"
489
- * }
490
- * AIMessageChunk {
491
- * "content": " programmation",,
492
- * }
493
- * AIMessageChunk {
494
- * "content": ".",,
495
- * }
496
- * AIMessageChunk {
497
- * "content": "",
498
- * "response_metadata": {
499
- * "finish_reason": "stop",
500
- * "system_fingerprint": "fp_c9aa9c0491"
501
- * },
502
- * }
503
- * AIMessageChunk {
504
- * "content": "",
505
- * "usage_metadata": {
506
- * "input_tokens": 28,
507
- * "output_tokens": 5,
508
- * "total_tokens": 33
509
- * }
510
- * }
511
- * ```
512
- * </details>
513
- *
514
- * <br />
515
- *
516
- * <details>
517
- * <summary><strong>Aggregate Streamed Chunks</strong></summary>
518
- *
519
- * ```typescript
520
- * import { AIMessageChunk } from '@langchain/core/messages';
521
- * import { concat } from '@langchain/core/utils/stream';
522
- *
523
- * const stream = await llm.stream(input);
524
- * let full: AIMessageChunk | undefined;
525
- * for await (const chunk of stream) {
526
- * full = !full ? chunk : concat(full, chunk);
527
- * }
528
- * console.log(full);
529
- * ```
530
- *
531
- * ```txt
532
- * AIMessageChunk {
533
- * "id": "chatcmpl-9u4PnX6Fy7OmK46DASy0bH6cxn5Xu",
534
- * "content": "J'adore la programmation.",
535
- * "response_metadata": {
536
- * "prompt": 0,
537
- * "completion": 0,
538
- * "finish_reason": "stop",
539
- * },
540
- * "usage_metadata": {
541
- * "input_tokens": 28,
542
- * "output_tokens": 5,
543
- * "total_tokens": 33
544
- * }
545
- * }
546
- * ```
547
- * </details>
548
- *
549
- * <br />
550
- *
551
- * <details>
552
- * <summary><strong>Bind tools</strong></summary>
553
- *
554
- * ```typescript
555
- * import { z } from 'zod';
556
- *
557
- * const GetWeather = {
558
- * name: "GetWeather",
559
- * description: "Get the current weather in a given location",
560
- * schema: z.object({
561
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
562
- * }),
563
- * }
564
- *
565
- * const GetPopulation = {
566
- * name: "GetPopulation",
567
- * description: "Get the current population in a given location",
568
- * schema: z.object({
569
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
570
- * }),
571
- * }
572
- *
573
- * const llmWithTools = llm.bindTools(
574
- * [GetWeather, GetPopulation],
575
- * {
576
- * // strict: true // enforce tool args schema is respected
577
- * }
578
- * );
579
- * const aiMsg = await llmWithTools.invoke(
580
- * "Which city is hotter today and which is bigger: LA or NY?"
581
- * );
582
- * console.log(aiMsg.tool_calls);
583
- * ```
584
- *
585
- * ```txt
586
- * [
587
- * {
588
- * name: 'GetWeather',
589
- * args: { location: 'Los Angeles, CA' },
590
- * type: 'tool_call',
591
- * id: 'call_uPU4FiFzoKAtMxfmPnfQL6UK'
592
- * },
593
- * {
594
- * name: 'GetWeather',
595
- * args: { location: 'New York, NY' },
596
- * type: 'tool_call',
597
- * id: 'call_UNkEwuQsHrGYqgDQuH9nPAtX'
598
- * },
599
- * {
600
- * name: 'GetPopulation',
601
- * args: { location: 'Los Angeles, CA' },
602
- * type: 'tool_call',
603
- * id: 'call_kL3OXxaq9OjIKqRTpvjaCH14'
604
- * },
605
- * {
606
- * name: 'GetPopulation',
607
- * args: { location: 'New York, NY' },
608
- * type: 'tool_call',
609
- * id: 'call_s9KQB1UWj45LLGaEnjz0179q'
610
- * }
611
- * ]
612
- * ```
613
- * </details>
614
- *
615
- * <br />
616
- *
617
- * <details>
618
- * <summary><strong>Structured Output</strong></summary>
619
- *
620
- * ```typescript
621
- * import { z } from 'zod';
622
- *
623
- * const Joke = z.object({
624
- * setup: z.string().describe("The setup of the joke"),
625
- * punchline: z.string().describe("The punchline to the joke"),
626
- * rating: z.number().nullable().describe("How funny the joke is, from 1 to 10")
627
- * }).describe('Joke to tell user.');
628
- *
629
- * const structuredLlm = llm.withStructuredOutput(Joke, {
630
- * name: "Joke",
631
- * strict: true, // Optionally enable OpenAI structured outputs
632
- * });
633
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
634
- * console.log(jokeResult);
635
- * ```
636
- *
637
- * ```txt
638
- * {
639
- * setup: 'Why was the cat sitting on the computer?',
640
- * punchline: 'Because it wanted to keep an eye on the mouse!',
641
- * rating: 7
642
- * }
643
- * ```
644
- * </details>
645
- *
646
- * <br />
647
- *
648
- * <details>
649
- * <summary><strong>JSON Object Response Format</strong></summary>
650
- *
651
- * ```typescript
652
- * const jsonLlm = llm.withConfig({ response_format: { type: "json_object" } });
653
- * const jsonLlmAiMsg = await jsonLlm.invoke(
654
- * "Return a JSON object with key 'randomInts' and a value of 10 random ints in [0-99]"
655
- * );
656
- * console.log(jsonLlmAiMsg.content);
657
- * ```
658
- *
659
- * ```txt
660
- * {
661
- * "randomInts": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]
662
- * }
663
- * ```
664
- * </details>
665
- *
666
- * <br />
667
- *
668
- * <details>
669
- * <summary><strong>Multimodal</strong></summary>
670
- *
671
- * ```typescript
672
- * import { HumanMessage } from '@langchain/core/messages';
673
- *
674
- * const imageUrl = "https://example.com/image.jpg";
675
- * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
676
- * const base64Image = Buffer.from(imageData).toString('base64');
677
- *
678
- * const message = new HumanMessage({
679
- * content: [
680
- * { type: "text", text: "describe the weather in this image" },
681
- * {
682
- * type: "image_url",
683
- * image_url: { url: `data:image/jpeg;base64,${base64Image}` },
684
- * },
685
- * ]
686
- * });
687
- *
688
- * const imageDescriptionAiMsg = await llm.invoke([message]);
689
- * console.log(imageDescriptionAiMsg.content);
690
- * ```
691
- *
692
- * ```txt
693
- * 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.
694
- * ```
695
- * </details>
696
- *
697
- * <br />
698
- *
699
- * <details>
700
- * <summary><strong>Usage Metadata</strong></summary>
701
- *
702
- * ```typescript
703
- * const aiMsgForMetadata = await llm.invoke(input);
704
- * console.log(aiMsgForMetadata.usage_metadata);
705
- * ```
706
- *
707
- * ```txt
708
- * { input_tokens: 28, output_tokens: 5, total_tokens: 33 }
709
- * ```
710
- * </details>
711
- *
712
- * <br />
713
- *
714
- * <details>
715
- * <summary><strong>Logprobs</strong></summary>
716
- *
717
- * ```typescript
718
- * const logprobsLlm = new ChatOpenAI({ model: "gpt-4o-mini", logprobs: true });
719
- * const aiMsgForLogprobs = await logprobsLlm.invoke(input);
720
- * console.log(aiMsgForLogprobs.response_metadata.logprobs);
721
- * ```
722
- *
723
- * ```txt
724
- * {
725
- * content: [
726
- * {
727
- * token: 'J',
728
- * logprob: -0.000050616763,
729
- * bytes: [Array],
730
- * top_logprobs: []
731
- * },
732
- * {
733
- * token: "'",
734
- * logprob: -0.01868736,
735
- * bytes: [Array],
736
- * top_logprobs: []
737
- * },
738
- * {
739
- * token: 'ad',
740
- * logprob: -0.0000030545007,
741
- * bytes: [Array],
742
- * top_logprobs: []
743
- * },
744
- * { token: 'ore', logprob: 0, bytes: [Array], top_logprobs: [] },
745
- * {
746
- * token: ' la',
747
- * logprob: -0.515404,
748
- * bytes: [Array],
749
- * top_logprobs: []
750
- * },
751
- * {
752
- * token: ' programm',
753
- * logprob: -0.0000118755715,
754
- * bytes: [Array],
755
- * top_logprobs: []
756
- * },
757
- * { token: 'ation', logprob: 0, bytes: [Array], top_logprobs: [] },
758
- * {
759
- * token: '.',
760
- * logprob: -0.0000037697225,
761
- * bytes: [Array],
762
- * top_logprobs: []
763
- * }
764
- * ],
765
- * refusal: null
766
- * }
767
- * ```
768
- * </details>
769
- *
770
- * <br />
771
- *
772
- * <details>
773
- * <summary><strong>Response Metadata</strong></summary>
774
- *
775
- * ```typescript
776
- * const aiMsgForResponseMetadata = await llm.invoke(input);
777
- * console.log(aiMsgForResponseMetadata.response_metadata);
778
- * ```
779
- *
780
- * ```txt
781
- * {
782
- * tokenUsage: { completionTokens: 5, promptTokens: 28, totalTokens: 33 },
783
- * finish_reason: 'stop',
784
- * system_fingerprint: 'fp_3aa7262c27'
785
- * }
786
- * ```
787
- * </details>
788
- *
789
- * <br />
790
- *
791
- * <details>
792
- * <summary><strong>JSON Schema Structured Output</strong></summary>
793
- *
794
- * ```typescript
795
- * const llmForJsonSchema = new ChatOpenAI({
796
- * model: "gpt-4o-2024-08-06",
797
- * }).withStructuredOutput(
798
- * z.object({
799
- * command: z.string().describe("The command to execute"),
800
- * expectedOutput: z.string().describe("The expected output of the command"),
801
- * options: z
802
- * .array(z.string())
803
- * .describe("The options you can pass to the command"),
804
- * }),
805
- * {
806
- * method: "jsonSchema",
807
- * strict: true, // Optional when using the `jsonSchema` method
808
- * }
809
- * );
810
- *
811
- * const jsonSchemaRes = await llmForJsonSchema.invoke(
812
- * "What is the command to list files in a directory?"
813
- * );
814
- * console.log(jsonSchemaRes);
815
- * ```
816
- *
817
- * ```txt
818
- * {
819
- * command: 'ls',
820
- * expectedOutput: 'A list of files and subdirectories within the specified directory.',
821
- * options: [
822
- * '-a: include directory entries whose names begin with a dot (.).',
823
- * '-l: use a long listing format.',
824
- * '-h: with -l, print sizes in human readable format (e.g., 1K, 234M, 2G).',
825
- * '-t: sort by time, newest first.',
826
- * '-r: reverse order while sorting.',
827
- * '-S: sort by file size, largest first.',
828
- * '-R: list subdirectories recursively.'
829
- * ]
830
- * }
831
- * ```
832
- * </details>
833
- *
834
- * <br />
835
- *
836
- * <details>
837
- * <summary><strong>Audio Outputs</strong></summary>
838
- *
839
- * ```typescript
840
- * import { ChatOpenAI } from "@langchain/openai";
841
- *
842
- * const modelWithAudioOutput = new ChatOpenAI({
843
- * model: "gpt-4o-audio-preview",
844
- * // You may also pass these fields to `.withConfig` as a call argument.
845
- * modalities: ["text", "audio"], // Specifies that the model should output audio.
846
- * audio: {
847
- * voice: "alloy",
848
- * format: "wav",
849
- * },
850
- * });
851
- *
852
- * const audioOutputResult = await modelWithAudioOutput.invoke("Tell me a joke about cats.");
853
- * const castMessageContent = audioOutputResult.content[0] as Record<string, any>;
854
- *
855
- * console.log({
856
- * ...castMessageContent,
857
- * data: castMessageContent.data.slice(0, 100) // Sliced for brevity
858
- * })
859
- * ```
860
- *
861
- * ```txt
862
- * {
863
- * id: 'audio_67117718c6008190a3afad3e3054b9b6',
864
- * data: 'UklGRqYwBgBXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAATElTVBoAAABJTkZPSVNGVA4AAABMYXZmNTguMjkuMTAwAGRhdGFg',
865
- * expires_at: 1729201448,
866
- * transcript: 'Sure! Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'
867
- * }
868
- * ```
869
- * </details>
870
- *
871
- * <br />
872
- *
873
- * <details>
874
- * <summary><strong>Audio Outputs</strong></summary>
875
- *
876
- * ```typescript
877
- * import { ChatOpenAI } from "@langchain/openai";
878
- *
879
- * const modelWithAudioOutput = new ChatOpenAI({
880
- * model: "gpt-4o-audio-preview",
881
- * // You may also pass these fields to `.withConfig` as a call argument.
882
- * modalities: ["text", "audio"], // Specifies that the model should output audio.
883
- * audio: {
884
- * voice: "alloy",
885
- * format: "wav",
886
- * },
887
- * });
888
- *
889
- * const audioOutputResult = await modelWithAudioOutput.invoke("Tell me a joke about cats.");
890
- * const castAudioContent = audioOutputResult.additional_kwargs.audio as Record<string, any>;
891
- *
892
- * console.log({
893
- * ...castAudioContent,
894
- * data: castAudioContent.data.slice(0, 100) // Sliced for brevity
895
- * })
896
- * ```
897
- *
898
- * ```txt
899
- * {
900
- * id: 'audio_67117718c6008190a3afad3e3054b9b6',
901
- * data: 'UklGRqYwBgBXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAATElTVBoAAABJTkZPSVNGVA4AAABMYXZmNTguMjkuMTAwAGRhdGFg',
902
- * expires_at: 1729201448,
903
- * transcript: 'Sure! Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'
904
- * }
905
- * ```
906
- * </details>
907
- *
908
- * <br />
909
- */
910
- declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatOpenAI<CallOptions> {
911
- protected fields?: ChatOpenAIFields | undefined;
912
- /**
913
- * Whether to use the responses API for all requests. If `false` the responses API will be used
914
- * only when required in order to fulfill the request.
915
- */
916
- useResponsesApi: boolean;
917
- protected responses: ChatOpenAIResponses;
918
- protected completions: ChatOpenAICompletions;
919
- get lc_serializable_keys(): string[];
920
- constructor(fields?: ChatOpenAIFields | undefined);
921
- protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean | undefined;
922
- getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
923
- invocationParams(options?: this["ParsedCallOptions"]): ChatCompletionsInvocationParams | ChatResponsesInvocationParams;
924
- /** @ignore */
925
- _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
926
- _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
927
- withConfig(config: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
928
- }
929
- //#endregion
930
- export { BaseChatOpenAI, BaseChatOpenAICallOptions, BaseChatOpenAIFields, ChatOpenAI, ChatOpenAICallOptions, ChatOpenAICompletions, ChatOpenAICompletionsCallOptions, ChatOpenAIFields, ChatOpenAIResponses, ChatOpenAIResponsesCallOptions };
931
- //# sourceMappingURL=chat_models.d.ts.map