@langchain/openai 0.5.18 → 0.6.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.
- package/README.md +2 -2
- package/dist/azure/chat_models.cjs +1 -1
- package/dist/azure/chat_models.d.ts +6 -6
- package/dist/azure/chat_models.js +2 -2
- package/dist/chat_models.cjs +2031 -2091
- package/dist/chat_models.d.ts +235 -233
- package/dist/chat_models.js +2030 -2059
- package/dist/types.d.ts +0 -7
- package/dist/utils/openai.cjs +49 -0
- package/dist/utils/openai.d.ts +6 -0
- package/dist/utils/openai.js +49 -1
- package/dist/utils/prompts.cjs +1 -1
- package/dist/utils/prompts.d.ts +1 -1
- package/dist/utils/prompts.js +1 -1
- package/package.json +1 -1
package/dist/chat_models.d.ts
CHANGED
|
@@ -1,79 +1,65 @@
|
|
|
1
1
|
import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
3
3
|
import { AIMessageChunk, type BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk } from "@langchain/core/messages";
|
|
4
|
-
import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
|
|
4
|
+
import { ChatGenerationChunk, type ChatGeneration, type ChatResult } from "@langchain/core/outputs";
|
|
5
5
|
import { BaseChatModel, type BindToolsInput, type LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
6
6
|
import { type BaseFunctionCallOptions, type BaseLanguageModelInput, type StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
|
|
7
7
|
import { Runnable } from "@langchain/core/runnables";
|
|
8
|
+
import type { ResponseFormatText, ResponseFormatJSONObject, ResponseFormatJSONSchema } from "openai/resources/shared";
|
|
8
9
|
import { InteropZodType } from "@langchain/core/utils/types";
|
|
9
|
-
import { type OpenAICallOptions, type OpenAIChatInput, type OpenAICoreRequestOptions, type ChatOpenAIResponseFormat } from "./types.js";
|
|
10
|
+
import { type OpenAICallOptions, type OpenAIChatInput, type OpenAICoreRequestOptions, type ChatOpenAIResponseFormat, ChatOpenAIReasoningSummary } from "./types.js";
|
|
10
11
|
import { OpenAIToolChoice } from "./utils/openai.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
promptTokens?: number;
|
|
15
|
-
totalTokens?: number;
|
|
16
|
-
}
|
|
12
|
+
type ResponsesTool = NonNullable<OpenAIClient.Responses.ResponseCreateParams["tools"]>[number];
|
|
13
|
+
type ResponsesToolChoice = NonNullable<OpenAIClient.Responses.ResponseCreateParams["tool_choice"]>;
|
|
14
|
+
type ChatOpenAIToolType = BindToolsInput | OpenAIClient.Chat.ChatCompletionTool | ResponsesTool;
|
|
17
15
|
interface OpenAILLMOutput {
|
|
18
|
-
tokenUsage:
|
|
16
|
+
tokenUsage: {
|
|
17
|
+
completionTokens?: number;
|
|
18
|
+
promptTokens?: number;
|
|
19
|
+
totalTokens?: number;
|
|
20
|
+
};
|
|
19
21
|
}
|
|
20
|
-
type
|
|
21
|
-
|
|
22
|
-
export declare function
|
|
23
|
-
|
|
24
|
-
type ExcludeController<T> = T extends {
|
|
25
|
-
controller: unknown;
|
|
26
|
-
} ? never : T;
|
|
27
|
-
type ExcludeNonController<T> = T extends {
|
|
28
|
-
controller: unknown;
|
|
29
|
-
} ? T : never;
|
|
30
|
-
type ResponsesCreate = OpenAIClient.Responses["create"];
|
|
31
|
-
type ResponsesCreateParams = Parameters<OpenAIClient.Responses["create"]>[0];
|
|
32
|
-
type ResponsesTool = Exclude<ResponsesCreateParams["tools"], undefined>[number];
|
|
33
|
-
type ResponsesToolChoice = Exclude<ResponsesCreateParams["tool_choice"], undefined>;
|
|
34
|
-
type ResponsesCreateInvoke = ExcludeController<Awaited<ReturnType<ResponsesCreate>>>;
|
|
35
|
-
type ResponsesCreateStream = ExcludeNonController<Awaited<ReturnType<ResponsesCreate>>>;
|
|
36
|
-
type ResponseInvocationParams = Omit<ResponsesCreateParams, "input">;
|
|
37
|
-
type ChatCompletionInvocationParams = Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
|
|
38
|
-
type ChatOpenAIToolType = BindToolsInput | OpenAIClient.ChatCompletionTool | ResponsesTool;
|
|
39
|
-
export interface ChatOpenAIStructuredOutputMethodOptions<IncludeRaw extends boolean> extends StructuredOutputMethodOptions<IncludeRaw> {
|
|
22
|
+
export type { OpenAICallOptions, OpenAIChatInput };
|
|
23
|
+
export declare function messageToOpenAIRole(message: BaseMessage): OpenAIClient.ChatCompletionRole;
|
|
24
|
+
export declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];
|
|
25
|
+
interface BaseChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
|
|
40
26
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* will
|
|
44
|
-
* https://platform.openai.com/docs/guides/structured-outputs/supported-schemas.
|
|
45
|
-
* If `false`, input schema will not be validated and model output will not
|
|
46
|
-
* be validated.
|
|
47
|
-
* If `undefined`, `strict` argument will not be passed to the model.
|
|
48
|
-
*
|
|
49
|
-
* @version 0.2.6
|
|
50
|
-
* @note Planned breaking change in version `0.3.0`:
|
|
51
|
-
* `strict` will default to `true` when `method` is
|
|
52
|
-
* "function_calling" as of version `0.3.0`.
|
|
27
|
+
* A list of tools that the model may use to generate responses.
|
|
28
|
+
* Each tool can be a function, a built-in tool, or a custom tool definition.
|
|
29
|
+
* If not provided, the model will not use any tools.
|
|
53
30
|
*/
|
|
54
|
-
strict?: boolean;
|
|
55
|
-
}
|
|
56
|
-
export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
|
|
57
31
|
tools?: ChatOpenAIToolType[];
|
|
32
|
+
/**
|
|
33
|
+
* Specifies which tool the model should use to respond.
|
|
34
|
+
* Can be an {@link OpenAIToolChoice} or a {@link ResponsesToolChoice}.
|
|
35
|
+
* If not set, the model will decide which tool to use automatically.
|
|
36
|
+
*/
|
|
58
37
|
tool_choice?: OpenAIToolChoice | ResponsesToolChoice;
|
|
38
|
+
/**
|
|
39
|
+
* Adds a prompt index to prompts passed to the model to track
|
|
40
|
+
* what prompt is being used for a given generation.
|
|
41
|
+
*/
|
|
59
42
|
promptIndex?: number;
|
|
43
|
+
/**
|
|
44
|
+
* An object specifying the format that the model must output.
|
|
45
|
+
*/
|
|
60
46
|
response_format?: ChatOpenAIResponseFormat;
|
|
47
|
+
/**
|
|
48
|
+
* When provided, the completions API will make a best effort to sample
|
|
49
|
+
* deterministically, such that repeated requests with the same `seed`
|
|
50
|
+
* and parameters should return the same result.
|
|
51
|
+
*/
|
|
61
52
|
seed?: number;
|
|
62
53
|
/**
|
|
63
54
|
* Additional options to pass to streamed completions.
|
|
64
|
-
* If provided takes precedence over "streamUsage" set at
|
|
55
|
+
* If provided, this takes precedence over "streamUsage" set at
|
|
56
|
+
* initialization time.
|
|
65
57
|
*/
|
|
66
|
-
stream_options?:
|
|
67
|
-
/**
|
|
68
|
-
* Whether or not to include token usage in the stream.
|
|
69
|
-
* If set to `true`, this will include an additional
|
|
70
|
-
* chunk at the end of the stream with the token usage.
|
|
71
|
-
*/
|
|
72
|
-
include_usage: boolean;
|
|
73
|
-
};
|
|
58
|
+
stream_options?: OpenAIClient.Chat.ChatCompletionStreamOptions;
|
|
74
59
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
60
|
+
* The model may choose to call multiple functions in a single turn. You can
|
|
61
|
+
* set parallel_tool_calls to false which ensures only one tool is called at most.
|
|
62
|
+
* [Learn more](https://platform.openai.com/docs/guides/function-calling#parallel-function-calling)
|
|
77
63
|
*/
|
|
78
64
|
parallel_tool_calls?: boolean;
|
|
79
65
|
/**
|
|
@@ -86,8 +72,6 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
|
|
|
86
72
|
* be validated.
|
|
87
73
|
*
|
|
88
74
|
* If `undefined`, `strict` argument will not be passed to the model.
|
|
89
|
-
*
|
|
90
|
-
* @version 0.2.6
|
|
91
75
|
*/
|
|
92
76
|
strict?: boolean;
|
|
93
77
|
/**
|
|
@@ -114,13 +98,6 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
|
|
|
114
98
|
* [Learn more](https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs).
|
|
115
99
|
*/
|
|
116
100
|
prediction?: OpenAIClient.ChatCompletionPredictionContent;
|
|
117
|
-
/**
|
|
118
|
-
* Constrains effort on reasoning for reasoning models. Currently supported values are low, medium, and high.
|
|
119
|
-
* Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
|
|
120
|
-
*
|
|
121
|
-
* @deprecated Use {@link reasoning} object instead.
|
|
122
|
-
*/
|
|
123
|
-
reasoning_effort?: OpenAIClient.Chat.ChatCompletionReasoningEffort;
|
|
124
101
|
/**
|
|
125
102
|
* Options for reasoning models.
|
|
126
103
|
*
|
|
@@ -130,42 +107,207 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
|
|
|
130
107
|
* These options will be ignored when not using a reasoning model.
|
|
131
108
|
*/
|
|
132
109
|
reasoning?: OpenAIClient.Reasoning;
|
|
110
|
+
/**
|
|
111
|
+
* Service tier to use for this request. Can be "auto", "default", or "flex"
|
|
112
|
+
* Specifies the service tier for prioritization and latency optimization.
|
|
113
|
+
*/
|
|
114
|
+
service_tier?: OpenAIClient.Chat.ChatCompletionCreateParams["service_tier"];
|
|
115
|
+
}
|
|
116
|
+
export interface BaseChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChatModelParams {
|
|
117
|
+
/**
|
|
118
|
+
* Optional configuration options for the OpenAI client.
|
|
119
|
+
*/
|
|
120
|
+
configuration?: ClientOptions;
|
|
121
|
+
}
|
|
122
|
+
/** @internal */
|
|
123
|
+
declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements Partial<OpenAIChatInput> {
|
|
124
|
+
temperature?: number;
|
|
125
|
+
topP?: number;
|
|
126
|
+
frequencyPenalty?: number;
|
|
127
|
+
presencePenalty?: number;
|
|
128
|
+
n?: number;
|
|
129
|
+
logitBias?: Record<string, number>;
|
|
130
|
+
model: string;
|
|
131
|
+
modelKwargs?: OpenAIChatInput["modelKwargs"];
|
|
132
|
+
stop?: string[];
|
|
133
|
+
stopSequences?: string[];
|
|
134
|
+
user?: string;
|
|
135
|
+
timeout?: number;
|
|
136
|
+
streaming: boolean;
|
|
137
|
+
streamUsage: boolean;
|
|
138
|
+
maxTokens?: number;
|
|
139
|
+
logprobs?: boolean;
|
|
140
|
+
topLogprobs?: number;
|
|
141
|
+
apiKey?: string;
|
|
142
|
+
organization?: string;
|
|
143
|
+
__includeRawResponse?: boolean;
|
|
144
|
+
protected client: OpenAIClient;
|
|
145
|
+
protected clientConfig: ClientOptions;
|
|
146
|
+
/**
|
|
147
|
+
* Whether the model supports the `strict` argument when passing in tools.
|
|
148
|
+
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
149
|
+
*/
|
|
150
|
+
supportsStrictToolCalling?: boolean;
|
|
151
|
+
audio?: OpenAIClient.Chat.ChatCompletionAudioParam;
|
|
152
|
+
modalities?: Array<OpenAIClient.Chat.ChatCompletionModality>;
|
|
153
|
+
reasoning?: OpenAIClient.Reasoning;
|
|
154
|
+
/**
|
|
155
|
+
* Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
|
|
156
|
+
* output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
|
|
157
|
+
* OpenAI organization or project. This must be configured directly with OpenAI.
|
|
158
|
+
*
|
|
159
|
+
* See:
|
|
160
|
+
* https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
|
|
161
|
+
* https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
|
|
162
|
+
*
|
|
163
|
+
* @default false
|
|
164
|
+
*/
|
|
165
|
+
zdrEnabled?: boolean | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
|
|
168
|
+
* Specifies the service tier for prioritization and latency optimization.
|
|
169
|
+
*/
|
|
170
|
+
service_tier?: OpenAIClient.Chat.ChatCompletionCreateParams["service_tier"];
|
|
171
|
+
_llmType(): string;
|
|
172
|
+
static lc_name(): string;
|
|
173
|
+
get callKeys(): string[];
|
|
174
|
+
lc_serializable: boolean;
|
|
175
|
+
get lc_secrets(): {
|
|
176
|
+
[key: string]: string;
|
|
177
|
+
} | undefined;
|
|
178
|
+
get lc_aliases(): Record<string, string>;
|
|
179
|
+
get lc_serializable_keys(): string[];
|
|
180
|
+
getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
|
|
181
|
+
/** @ignore */
|
|
182
|
+
_identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
|
|
183
|
+
model_name: string;
|
|
184
|
+
} & ClientOptions;
|
|
185
|
+
/**
|
|
186
|
+
* Get the identifying parameters for the model
|
|
187
|
+
*/
|
|
188
|
+
identifyingParams(): Omit<OpenAIClient.Chat.Completions.ChatCompletionCreateParams, "messages"> & {
|
|
189
|
+
model_name: string;
|
|
190
|
+
} & ClientOptions;
|
|
191
|
+
constructor(fields?: BaseChatOpenAIFields);
|
|
192
|
+
/**
|
|
193
|
+
* Returns backwards compatible reasoning parameters from constructor params and call options
|
|
194
|
+
* @internal
|
|
195
|
+
*/
|
|
196
|
+
protected _getReasoningParams(options?: this["ParsedCallOptions"]): OpenAIClient.Reasoning | undefined;
|
|
197
|
+
/**
|
|
198
|
+
* Returns an openai compatible response format from a set of options
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
protected _getResponseFormat(resFormat?: CallOptions["response_format"]): ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | undefined;
|
|
202
|
+
protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
|
|
203
|
+
protected _convertChatOpenAIToolToCompletionsTool(tool: ChatOpenAIToolType, fields?: {
|
|
204
|
+
strict?: boolean;
|
|
205
|
+
}): OpenAIClient.ChatCompletionTool;
|
|
206
|
+
bindTools(tools: ChatOpenAIToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
|
|
207
|
+
/** @ignore */
|
|
208
|
+
_combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
|
|
209
|
+
getNumTokensFromMessages(messages: BaseMessage[]): Promise<{
|
|
210
|
+
totalCount: number;
|
|
211
|
+
countPerMessage: number[];
|
|
212
|
+
}>;
|
|
213
|
+
/** @internal */
|
|
214
|
+
protected _getNumTokensFromGenerations(generations: ChatGeneration[]): Promise<number>;
|
|
215
|
+
/** @internal */
|
|
216
|
+
protected _getEstimatedTokenCountFromPrompt(messages: BaseMessage[], functions?: OpenAIClient.Chat.ChatCompletionCreateParams.Function[], function_call?: "none" | "auto" | OpenAIClient.Chat.ChatCompletionFunctionCallOption): Promise<number>;
|
|
217
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
218
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
219
|
+
raw: BaseMessage;
|
|
220
|
+
parsed: RunOutput;
|
|
221
|
+
}>;
|
|
222
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
223
|
+
raw: BaseMessage;
|
|
224
|
+
parsed: RunOutput;
|
|
225
|
+
}>;
|
|
226
|
+
}
|
|
227
|
+
type ExcludeController<T> = T extends {
|
|
228
|
+
controller: unknown;
|
|
229
|
+
} ? never : T;
|
|
230
|
+
type ResponsesCreate = OpenAIClient.Responses["create"];
|
|
231
|
+
type ResponsesParse = OpenAIClient.Responses["parse"];
|
|
232
|
+
type ResponsesCreateInvoke = ExcludeController<Awaited<ReturnType<ResponsesCreate>>>;
|
|
233
|
+
type ResponsesParseInvoke = ExcludeController<Awaited<ReturnType<ResponsesParse>>>;
|
|
234
|
+
interface ChatOpenAIResponsesCallOptions extends BaseChatOpenAICallOptions {
|
|
133
235
|
/**
|
|
134
236
|
* Configuration options for a text response from the model. Can be plain text or
|
|
135
237
|
* structured JSON data.
|
|
136
|
-
*
|
|
137
|
-
* If set, the Responses API will be used to fulfill the request.
|
|
138
238
|
*/
|
|
139
|
-
text?:
|
|
239
|
+
text?: OpenAIClient.Responses.ResponseCreateParams["text"];
|
|
140
240
|
/**
|
|
141
241
|
* The truncation strategy to use for the model response.
|
|
142
|
-
*
|
|
143
|
-
* If set, the Responses API will be used to fulfill the request.
|
|
144
242
|
*/
|
|
145
|
-
truncation?:
|
|
243
|
+
truncation?: OpenAIClient.Responses.ResponseCreateParams["truncation"];
|
|
146
244
|
/**
|
|
147
245
|
* Specify additional output data to include in the model response.
|
|
148
|
-
*
|
|
149
|
-
* If set, the Responses API will be used to fulfill the request.
|
|
150
246
|
*/
|
|
151
|
-
include?:
|
|
247
|
+
include?: OpenAIClient.Responses.ResponseCreateParams["include"];
|
|
152
248
|
/**
|
|
153
249
|
* The unique ID of the previous response to the model. Use this to create multi-turn
|
|
154
|
-
* conversations.
|
|
155
|
-
* that AIMessages included in the request have a `response_metadata.id` property. Ignored unless
|
|
156
|
-
* {@link ChatOpenAI.useResponsesApi} is `true`.
|
|
157
|
-
*
|
|
158
|
-
* If set, the Responses API will be used to fulfill the request.
|
|
250
|
+
* conversations.
|
|
159
251
|
*/
|
|
160
|
-
previous_response_id?:
|
|
252
|
+
previous_response_id?: OpenAIClient.Responses.ResponseCreateParams["previous_response_id"];
|
|
253
|
+
}
|
|
254
|
+
type ChatResponsesInvocationParams = Omit<OpenAIClient.Responses.ResponseCreateParams, "input">;
|
|
255
|
+
/**
|
|
256
|
+
* OpenAI Responses API implementation.
|
|
257
|
+
*
|
|
258
|
+
* Will be exported in a later version of @langchain/openai.
|
|
259
|
+
*
|
|
260
|
+
* @internal
|
|
261
|
+
*/
|
|
262
|
+
export declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponsesCallOptions = ChatOpenAIResponsesCallOptions> extends BaseChatOpenAI<CallOptions> {
|
|
263
|
+
invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams;
|
|
264
|
+
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<ChatResult>;
|
|
265
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"]): AsyncGenerator<ChatGenerationChunk>;
|
|
161
266
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
267
|
+
* Calls the Responses API with retry logic in case of failures.
|
|
268
|
+
* @param request The request to send to the OpenAI API.
|
|
269
|
+
* @param options Optional configuration for the API call.
|
|
270
|
+
* @returns The response from the OpenAI API.
|
|
164
271
|
*/
|
|
165
|
-
|
|
272
|
+
completionWithRetry(request: OpenAIClient.Responses.ResponseCreateParamsStreaming, requestOptions?: OpenAIClient.RequestOptions): Promise<AsyncIterable<OpenAIClient.Responses.ResponseStreamEvent>>;
|
|
273
|
+
completionWithRetry(request: OpenAIClient.Responses.ResponseCreateParamsNonStreaming, requestOptions?: OpenAIClient.RequestOptions): Promise<OpenAIClient.Responses.Response>;
|
|
274
|
+
/** @internal */
|
|
275
|
+
protected _convertResponsesMessageToBaseMessage(response: ResponsesCreateInvoke | ResponsesParseInvoke): BaseMessage;
|
|
276
|
+
/** @internal */
|
|
277
|
+
protected _convertResponsesDeltaToBaseMessageChunk(chunk: OpenAIClient.Responses.ResponseStreamEvent): ChatGenerationChunk | null;
|
|
278
|
+
/** @internal */
|
|
279
|
+
protected _convertMessagesToResponsesParams(messages: BaseMessage[]): (OpenAIClient.Responses.ResponseReasoningItem | OpenAIClient.Responses.EasyInputMessage | OpenAIClient.Responses.ResponseInputItem.Message | OpenAIClient.Responses.ResponseOutputMessage | OpenAIClient.Responses.ResponseFileSearchToolCall | OpenAIClient.Responses.ResponseComputerToolCall | OpenAIClient.Responses.ResponseInputItem.ComputerCallOutput | OpenAIClient.Responses.ResponseFunctionWebSearch | OpenAIClient.Responses.ResponseFunctionToolCall | OpenAIClient.Responses.ResponseInputItem.FunctionCallOutput | OpenAIClient.Responses.ResponseInputItem.ImageGenerationCall | OpenAIClient.Responses.ResponseCodeInterpreterToolCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCallOutput | OpenAIClient.Responses.ResponseInputItem.McpListTools | OpenAIClient.Responses.ResponseInputItem.McpApprovalRequest | OpenAIClient.Responses.ResponseInputItem.McpApprovalResponse | OpenAIClient.Responses.ResponseInputItem.McpCall | OpenAIClient.Responses.ResponseInputItem.ItemReference)[];
|
|
280
|
+
/** @internal */
|
|
281
|
+
protected _convertReasoningSummary(reasoning: ChatOpenAIReasoningSummary): OpenAIClient.Responses.ResponseReasoningItem;
|
|
282
|
+
/** @internal */
|
|
283
|
+
protected _reduceChatOpenAITools(tools: ChatOpenAIToolType[], fields: {
|
|
284
|
+
stream?: boolean;
|
|
285
|
+
strict?: boolean;
|
|
286
|
+
}): ResponsesTool[];
|
|
166
287
|
}
|
|
167
|
-
|
|
168
|
-
|
|
288
|
+
interface ChatOpenAICompletionsCallOptions extends BaseChatOpenAICallOptions {
|
|
289
|
+
}
|
|
290
|
+
type ChatCompletionsInvocationParams = Omit<OpenAIClient.Chat.Completions.ChatCompletionCreateParams, "messages">;
|
|
291
|
+
/**
|
|
292
|
+
* OpenAI Completions API implementation.
|
|
293
|
+
* @internal
|
|
294
|
+
*/
|
|
295
|
+
export declare class ChatOpenAICompletions<CallOptions extends ChatOpenAICompletionsCallOptions = ChatOpenAICompletionsCallOptions> extends BaseChatOpenAI<CallOptions> {
|
|
296
|
+
/** @internal */
|
|
297
|
+
invocationParams(options?: this["ParsedCallOptions"], extra?: {
|
|
298
|
+
streaming?: boolean;
|
|
299
|
+
}): ChatCompletionsInvocationParams;
|
|
300
|
+
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
301
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
302
|
+
completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, requestOptions?: OpenAIClient.RequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
|
|
303
|
+
completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, requestOptions?: OpenAIClient.RequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
|
|
304
|
+
/** @internal */
|
|
305
|
+
protected _convertCompletionsMessageToBaseMessage(message: OpenAIClient.Chat.Completions.ChatCompletionMessage, rawResponse: OpenAIClient.Chat.Completions.ChatCompletion): BaseMessage;
|
|
306
|
+
/** @internal */
|
|
307
|
+
protected _convertCompletionsDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk, defaultRole?: OpenAIClient.Chat.ChatCompletionRole): AIMessageChunk | HumanMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
|
|
308
|
+
}
|
|
309
|
+
export type ChatOpenAICallOptions = ChatOpenAICompletionsCallOptions & ChatOpenAIResponsesCallOptions;
|
|
310
|
+
export interface ChatOpenAIFields extends BaseChatOpenAIFields {
|
|
169
311
|
/**
|
|
170
312
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
171
313
|
* only when required in order to fulfill the request.
|
|
@@ -217,7 +359,7 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChatMode
|
|
|
217
359
|
* import { ChatOpenAI } from '@langchain/openai';
|
|
218
360
|
*
|
|
219
361
|
* const llm = new ChatOpenAI({
|
|
220
|
-
* model: "gpt-4o",
|
|
362
|
+
* model: "gpt-4o-mini",
|
|
221
363
|
* temperature: 0,
|
|
222
364
|
* maxTokens: undefined,
|
|
223
365
|
* timeout: undefined,
|
|
@@ -518,7 +660,7 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChatMode
|
|
|
518
660
|
* <summary><strong>Logprobs</strong></summary>
|
|
519
661
|
*
|
|
520
662
|
* ```typescript
|
|
521
|
-
* const logprobsLlm = new ChatOpenAI({ logprobs: true });
|
|
663
|
+
* const logprobsLlm = new ChatOpenAI({ model: "gpt-4o-mini", logprobs: true });
|
|
522
664
|
* const aiMsgForLogprobs = await logprobsLlm.invoke(input);
|
|
523
665
|
* console.log(aiMsgForLogprobs.response_metadata.logprobs);
|
|
524
666
|
* ```
|
|
@@ -710,159 +852,19 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChatMode
|
|
|
710
852
|
*
|
|
711
853
|
* <br />
|
|
712
854
|
*/
|
|
713
|
-
export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends
|
|
714
|
-
static lc_name(): string;
|
|
715
|
-
get callKeys(): string[];
|
|
716
|
-
lc_serializable: boolean;
|
|
717
|
-
get lc_secrets(): {
|
|
718
|
-
[key: string]: string;
|
|
719
|
-
} | undefined;
|
|
720
|
-
get lc_aliases(): Record<string, string>;
|
|
721
|
-
get lc_serializable_keys(): string[];
|
|
722
|
-
temperature?: number;
|
|
723
|
-
topP?: number;
|
|
724
|
-
frequencyPenalty?: number;
|
|
725
|
-
presencePenalty?: number;
|
|
726
|
-
n?: number;
|
|
727
|
-
logitBias?: Record<string, number>;
|
|
728
|
-
/** @deprecated Use "model" instead */
|
|
729
|
-
modelName: string;
|
|
730
|
-
model: string;
|
|
731
|
-
modelKwargs?: OpenAIChatInput["modelKwargs"];
|
|
732
|
-
stop?: string[];
|
|
733
|
-
stopSequences?: string[];
|
|
734
|
-
user?: string;
|
|
735
|
-
timeout?: number;
|
|
736
|
-
streaming: boolean;
|
|
737
|
-
streamUsage: boolean;
|
|
738
|
-
maxTokens?: number;
|
|
739
|
-
logprobs?: boolean;
|
|
740
|
-
topLogprobs?: number;
|
|
741
|
-
openAIApiKey?: string;
|
|
742
|
-
apiKey?: string;
|
|
743
|
-
organization?: string;
|
|
744
|
-
__includeRawResponse?: boolean;
|
|
745
|
-
protected client: OpenAIClient;
|
|
746
|
-
protected clientConfig: ClientOptions;
|
|
747
|
-
/**
|
|
748
|
-
* Whether the model supports the `strict` argument when passing in tools.
|
|
749
|
-
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
750
|
-
*/
|
|
751
|
-
supportsStrictToolCalling?: boolean;
|
|
752
|
-
audio?: OpenAIClient.Chat.ChatCompletionAudioParam;
|
|
753
|
-
modalities?: Array<OpenAIClient.Chat.ChatCompletionModality>;
|
|
754
|
-
/**
|
|
755
|
-
* @deprecated Use {@link reasoning} object instead.
|
|
756
|
-
*/
|
|
757
|
-
reasoningEffort?: OpenAIClient.Chat.ChatCompletionReasoningEffort;
|
|
758
|
-
/**
|
|
759
|
-
* Options for reasoning models.
|
|
760
|
-
*/
|
|
761
|
-
reasoning?: OpenAIClient.Reasoning;
|
|
855
|
+
export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatOpenAI<CallOptions> implements Partial<OpenAIChatInput> {
|
|
762
856
|
/**
|
|
763
857
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
764
858
|
* only when required in order to fulfill the request.
|
|
765
859
|
*/
|
|
766
860
|
useResponsesApi: boolean;
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
* OpenAI organization or project. This must be configured directly with OpenAI.
|
|
771
|
-
*
|
|
772
|
-
* See:
|
|
773
|
-
* https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
|
|
774
|
-
* https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
|
|
775
|
-
*
|
|
776
|
-
* @default false
|
|
777
|
-
*/
|
|
778
|
-
zdrEnabled?: boolean | undefined;
|
|
779
|
-
/**
|
|
780
|
-
* Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
|
|
781
|
-
* Specifies the service tier for prioritization and latency optimization.
|
|
782
|
-
*/
|
|
783
|
-
service_tier?: ResponsesCreateParams["service_tier"];
|
|
861
|
+
private responses;
|
|
862
|
+
private completions;
|
|
863
|
+
get lc_serializable_keys(): string[];
|
|
784
864
|
constructor(fields?: ChatOpenAIFields);
|
|
785
|
-
|
|
786
|
-
bindTools(tools: ChatOpenAIToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
|
|
787
|
-
private createResponseFormat;
|
|
788
|
-
private getReasoningParams;
|
|
789
|
-
/**
|
|
790
|
-
* Get the parameters used to invoke the model
|
|
791
|
-
*/
|
|
792
|
-
invocationParams<Type extends "responses" | "completion" = "completion">(options?: this["ParsedCallOptions"], extra?: {
|
|
793
|
-
streaming?: boolean;
|
|
794
|
-
}): Type extends "responses" ? ResponseInvocationParams : ChatCompletionInvocationParams;
|
|
795
|
-
protected _convertOpenAIChatCompletionMessageToBaseMessage(message: OpenAIClient.Chat.Completions.ChatCompletionMessage, rawResponse: OpenAIClient.Chat.Completions.ChatCompletion): BaseMessage;
|
|
796
|
-
protected _convertOpenAIDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk, defaultRole?: OpenAIRoleEnum): AIMessageChunk | HumanMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
|
|
797
|
-
/** @ignore */
|
|
798
|
-
_identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
|
|
799
|
-
model_name: string;
|
|
800
|
-
} & ClientOptions;
|
|
801
|
-
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
802
|
-
/**
|
|
803
|
-
* Get the identifying parameters for the model
|
|
804
|
-
*
|
|
805
|
-
*/
|
|
806
|
-
identifyingParams(): Omit<OpenAIClient.Chat.Completions.ChatCompletionCreateParams, "messages"> & {
|
|
807
|
-
model_name: string;
|
|
808
|
-
} & ClientOptions;
|
|
809
|
-
/** @ignore */
|
|
810
|
-
_responseApiGenerate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
811
|
-
/**
|
|
812
|
-
* Determines whether the responses API should be used for the given request.
|
|
813
|
-
*
|
|
814
|
-
* @internal
|
|
815
|
-
*
|
|
816
|
-
* @param options The parsed call options for the request.
|
|
817
|
-
* @returns `true` if the responses API should be used, either because it is explicitly enabled,
|
|
818
|
-
* or because the request requires it.
|
|
819
|
-
*/
|
|
820
|
-
protected _useResponseApi(options: this["ParsedCallOptions"] | undefined): boolean;
|
|
865
|
+
protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean;
|
|
821
866
|
/** @ignore */
|
|
822
867
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
* Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
|
|
826
|
-
*/
|
|
827
|
-
private getEstimatedTokenCountFromPrompt;
|
|
828
|
-
/**
|
|
829
|
-
* Estimate the number of tokens an array of generations have used.
|
|
830
|
-
*/
|
|
831
|
-
private getNumTokensFromGenerations;
|
|
832
|
-
getNumTokensFromMessages(messages: BaseMessage[]): Promise<{
|
|
833
|
-
totalCount: number;
|
|
834
|
-
countPerMessage: number[];
|
|
835
|
-
}>;
|
|
836
|
-
/**
|
|
837
|
-
* Calls the OpenAI API with retry logic in case of failures.
|
|
838
|
-
* @param request The request to send to the OpenAI API.
|
|
839
|
-
* @param options Optional configuration for the API call.
|
|
840
|
-
* @returns The response from the OpenAI API.
|
|
841
|
-
*/
|
|
842
|
-
completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
|
|
843
|
-
completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
|
|
844
|
-
protected responseApiWithRetry(request: ResponsesCreateParams & {
|
|
845
|
-
stream: true;
|
|
846
|
-
}, options?: OpenAICoreRequestOptions): Promise<ResponsesCreateStream>;
|
|
847
|
-
protected responseApiWithRetry(request: ResponsesCreateParams, options?: OpenAICoreRequestOptions): Promise<ResponsesCreateInvoke>;
|
|
848
|
-
/**
|
|
849
|
-
* Call the beta chat completions parse endpoint. This should only be called if
|
|
850
|
-
* response_format is set to "json_object".
|
|
851
|
-
* @param {OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming} request
|
|
852
|
-
* @param {OpenAICoreRequestOptions | undefined} options
|
|
853
|
-
*/
|
|
854
|
-
betaParsedCompletionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<ReturnType<OpenAIClient["chat"]["completions"]["parse"]>>;
|
|
855
|
-
protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
|
|
856
|
-
_llmType(): string;
|
|
857
|
-
/** @ignore */
|
|
858
|
-
_combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
|
|
859
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
860
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
861
|
-
raw: BaseMessage;
|
|
862
|
-
parsed: RunOutput;
|
|
863
|
-
}>;
|
|
864
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
865
|
-
raw: BaseMessage;
|
|
866
|
-
parsed: RunOutput;
|
|
867
|
-
}>;
|
|
868
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
869
|
+
withConfig(config: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
|
|
868
870
|
}
|