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