@langchain/openai 0.6.2 → 0.6.4
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/dist/azure/chat_models.cjs +330 -161
- package/dist/azure/chat_models.d.ts +16 -28
- package/dist/azure/chat_models.js +331 -162
- package/dist/chat_models.cjs +60 -43
- package/dist/chat_models.d.ts +26 -11
- package/dist/chat_models.js +60 -44
- package/dist/types.d.ts +7 -1
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ChatOpenAI = exports.ChatOpenAICompletions = exports.ChatOpenAIResponses = void 0;
|
|
3
|
+
exports.ChatOpenAI = exports.ChatOpenAICompletions = exports.ChatOpenAIResponses = exports.BaseChatOpenAI = void 0;
|
|
4
4
|
exports.messageToOpenAIRole = messageToOpenAIRole;
|
|
5
5
|
exports._convertMessagesToOpenAIParams = _convertMessagesToOpenAIParams;
|
|
6
6
|
const openai_1 = require("openai");
|
|
@@ -476,12 +476,14 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
476
476
|
writable: true,
|
|
477
477
|
value: void 0
|
|
478
478
|
});
|
|
479
|
+
/** @internal */
|
|
479
480
|
Object.defineProperty(this, "client", {
|
|
480
481
|
enumerable: true,
|
|
481
482
|
configurable: true,
|
|
482
483
|
writable: true,
|
|
483
484
|
value: void 0
|
|
484
485
|
});
|
|
486
|
+
/** @internal */
|
|
485
487
|
Object.defineProperty(this, "clientConfig", {
|
|
486
488
|
enumerable: true,
|
|
487
489
|
configurable: true,
|
|
@@ -817,13 +819,22 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
817
819
|
}
|
|
818
820
|
return tokens;
|
|
819
821
|
}
|
|
822
|
+
/** @internal */
|
|
823
|
+
_getStructuredOutputMethod(config) {
|
|
824
|
+
const ensuredConfig = { ...config };
|
|
825
|
+
if (!this.model.startsWith("gpt-3") &&
|
|
826
|
+
!this.model.startsWith("gpt-4-") &&
|
|
827
|
+
this.model !== "gpt-4") {
|
|
828
|
+
if (ensuredConfig?.method === undefined) {
|
|
829
|
+
return "jsonSchema";
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
else if (ensuredConfig.method === "jsonSchema") {
|
|
833
|
+
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
834
|
+
}
|
|
835
|
+
return ensuredConfig.method;
|
|
836
|
+
}
|
|
820
837
|
withStructuredOutput(outputSchema, config) {
|
|
821
|
-
// ):
|
|
822
|
-
// | Runnable<BaseLanguageModelInput, RunOutput>
|
|
823
|
-
// | Runnable<
|
|
824
|
-
// BaseLanguageModelInput,
|
|
825
|
-
// { raw: BaseMessage; parsed: RunOutput }
|
|
826
|
-
// > {
|
|
827
838
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
828
839
|
let schema;
|
|
829
840
|
let name;
|
|
@@ -846,47 +857,43 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
846
857
|
if (config?.strict !== undefined && method === "jsonMode") {
|
|
847
858
|
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
848
859
|
}
|
|
849
|
-
|
|
850
|
-
!this.model.startsWith("gpt-4-") &&
|
|
851
|
-
this.model !== "gpt-4") {
|
|
852
|
-
if (method === undefined) {
|
|
853
|
-
method = "jsonSchema";
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
else if (method === "jsonSchema") {
|
|
857
|
-
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
858
|
-
}
|
|
860
|
+
method = this._getStructuredOutputMethod({ ...config, method });
|
|
859
861
|
if (method === "jsonMode") {
|
|
860
|
-
let outputFormatSchema;
|
|
861
862
|
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
862
863
|
outputParser = output_parsers_1.StructuredOutputParser.fromZodSchema(schema);
|
|
863
|
-
outputFormatSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
864
864
|
}
|
|
865
865
|
else {
|
|
866
866
|
outputParser = new output_parsers_1.JsonOutputParser();
|
|
867
867
|
}
|
|
868
|
+
const asJsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
868
869
|
llm = this.withConfig({
|
|
869
870
|
response_format: { type: "json_object" },
|
|
870
871
|
ls_structured_output_format: {
|
|
871
|
-
kwargs: { method: "
|
|
872
|
-
schema:
|
|
872
|
+
kwargs: { method: "json_mode" },
|
|
873
|
+
schema: { title: name ?? "extract", ...asJsonSchema },
|
|
873
874
|
},
|
|
874
875
|
});
|
|
875
876
|
}
|
|
876
877
|
else if (method === "jsonSchema") {
|
|
878
|
+
const openaiJsonSchemaParams = {
|
|
879
|
+
name: name ?? "extract",
|
|
880
|
+
description: (0, types_1.getSchemaDescription)(schema),
|
|
881
|
+
schema,
|
|
882
|
+
strict: config?.strict,
|
|
883
|
+
};
|
|
884
|
+
const asJsonSchema = (0, json_schema_1.toJsonSchema)(openaiJsonSchemaParams.schema);
|
|
877
885
|
llm = this.withConfig({
|
|
878
886
|
response_format: {
|
|
879
887
|
type: "json_schema",
|
|
880
|
-
json_schema:
|
|
881
|
-
name: name ?? "extract",
|
|
882
|
-
description: (0, types_1.getSchemaDescription)(schema),
|
|
883
|
-
schema,
|
|
884
|
-
strict: config?.strict,
|
|
885
|
-
},
|
|
888
|
+
json_schema: openaiJsonSchemaParams,
|
|
886
889
|
},
|
|
887
890
|
ls_structured_output_format: {
|
|
888
|
-
kwargs: { method: "
|
|
889
|
-
schema:
|
|
891
|
+
kwargs: { method: "json_schema" },
|
|
892
|
+
schema: {
|
|
893
|
+
title: openaiJsonSchemaParams.name,
|
|
894
|
+
description: openaiJsonSchemaParams.description,
|
|
895
|
+
...asJsonSchema,
|
|
896
|
+
},
|
|
890
897
|
},
|
|
891
898
|
});
|
|
892
899
|
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
@@ -925,8 +932,8 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
925
932
|
},
|
|
926
933
|
},
|
|
927
934
|
ls_structured_output_format: {
|
|
928
|
-
kwargs: { method: "
|
|
929
|
-
schema: asJsonSchema,
|
|
935
|
+
kwargs: { method: "function_calling" },
|
|
936
|
+
schema: { title: functionName, ...asJsonSchema },
|
|
930
937
|
},
|
|
931
938
|
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
932
939
|
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
@@ -953,6 +960,7 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
953
960
|
parameters: schema,
|
|
954
961
|
};
|
|
955
962
|
}
|
|
963
|
+
const asJsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
956
964
|
llm = this.withConfig({
|
|
957
965
|
tools: [
|
|
958
966
|
{
|
|
@@ -967,8 +975,8 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
967
975
|
},
|
|
968
976
|
},
|
|
969
977
|
ls_structured_output_format: {
|
|
970
|
-
kwargs: { method: "
|
|
971
|
-
schema:
|
|
978
|
+
kwargs: { method: "function_calling" },
|
|
979
|
+
schema: { title: functionName, ...asJsonSchema },
|
|
972
980
|
},
|
|
973
981
|
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
974
982
|
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
@@ -995,6 +1003,7 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
995
1003
|
return runnables_1.RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
|
|
996
1004
|
}
|
|
997
1005
|
}
|
|
1006
|
+
exports.BaseChatOpenAI = BaseChatOpenAI;
|
|
998
1007
|
/**
|
|
999
1008
|
* OpenAI Responses API implementation.
|
|
1000
1009
|
*
|
|
@@ -1116,7 +1125,7 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
|
|
|
1116
1125
|
};
|
|
1117
1126
|
}
|
|
1118
1127
|
}
|
|
1119
|
-
async *_streamResponseChunks(messages, options) {
|
|
1128
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
1120
1129
|
const streamIterable = await this.completionWithRetry({
|
|
1121
1130
|
...this.invocationParams(options),
|
|
1122
1131
|
input: this._convertMessagesToResponsesParams(messages),
|
|
@@ -1127,6 +1136,10 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
|
|
|
1127
1136
|
if (chunk == null)
|
|
1128
1137
|
continue;
|
|
1129
1138
|
yield chunk;
|
|
1139
|
+
await runManager?.handleLLMNewToken(chunk.text || "", {
|
|
1140
|
+
prompt: options.promptIndex ?? 0,
|
|
1141
|
+
completion: 0,
|
|
1142
|
+
}, undefined, undefined, undefined, { chunk });
|
|
1130
1143
|
}
|
|
1131
1144
|
}
|
|
1132
1145
|
async completionWithRetry(request, requestOptions) {
|
|
@@ -2609,7 +2622,13 @@ class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2609
2622
|
return [...super.lc_serializable_keys, "useResponsesApi"];
|
|
2610
2623
|
}
|
|
2611
2624
|
constructor(fields) {
|
|
2612
|
-
super(fields
|
|
2625
|
+
super(fields);
|
|
2626
|
+
Object.defineProperty(this, "fields", {
|
|
2627
|
+
enumerable: true,
|
|
2628
|
+
configurable: true,
|
|
2629
|
+
writable: true,
|
|
2630
|
+
value: fields
|
|
2631
|
+
});
|
|
2613
2632
|
/**
|
|
2614
2633
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
2615
2634
|
* only when required in order to fulfill the request.
|
|
@@ -2633,11 +2652,8 @@ class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2633
2652
|
value: void 0
|
|
2634
2653
|
});
|
|
2635
2654
|
this.useResponsesApi = fields?.useResponsesApi ?? false;
|
|
2636
|
-
this.responses = new ChatOpenAIResponses(fields);
|
|
2637
|
-
this.completions = new ChatOpenAICompletions(fields);
|
|
2638
|
-
// Override `_getClientOptions` so sub classes of ChatOpenAI use the same client config
|
|
2639
|
-
this.responses._getClientOptions = this._getClientOptions;
|
|
2640
|
-
this.completions._getClientOptions = this._getClientOptions;
|
|
2655
|
+
this.responses = fields?.responses ?? new ChatOpenAIResponses(fields);
|
|
2656
|
+
this.completions = fields?.completions ?? new ChatOpenAICompletions(fields);
|
|
2641
2657
|
}
|
|
2642
2658
|
_useResponsesApi(options) {
|
|
2643
2659
|
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
|
@@ -2672,14 +2688,15 @@ class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2672
2688
|
}
|
|
2673
2689
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
2674
2690
|
if (this._useResponsesApi(options)) {
|
|
2675
|
-
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
|
|
2691
|
+
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2676
2692
|
return;
|
|
2677
2693
|
}
|
|
2678
2694
|
yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2679
2695
|
}
|
|
2680
2696
|
withConfig(config) {
|
|
2681
|
-
|
|
2682
|
-
|
|
2697
|
+
const newModel = new ChatOpenAI(this.fields);
|
|
2698
|
+
newModel.defaultOptions = { ...this.defaultOptions, ...config };
|
|
2699
|
+
return newModel;
|
|
2683
2700
|
}
|
|
2684
2701
|
}
|
|
2685
2702
|
exports.ChatOpenAI = ChatOpenAI;
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ interface OpenAILLMOutput {
|
|
|
22
22
|
export type { OpenAICallOptions, OpenAIChatInput };
|
|
23
23
|
export declare function messageToOpenAIRole(message: BaseMessage): OpenAIClient.ChatCompletionRole;
|
|
24
24
|
export declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];
|
|
25
|
-
interface BaseChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
|
|
25
|
+
export interface BaseChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
|
|
26
26
|
/**
|
|
27
27
|
* A list of tools that the model may use to generate responses.
|
|
28
28
|
* Each tool can be a function, a built-in tool, or a custom tool definition.
|
|
@@ -120,7 +120,7 @@ export interface BaseChatOpenAIFields extends Partial<OpenAIChatInput>, BaseChat
|
|
|
120
120
|
configuration?: ClientOptions;
|
|
121
121
|
}
|
|
122
122
|
/** @internal */
|
|
123
|
-
declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements Partial<OpenAIChatInput> {
|
|
123
|
+
export declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements Partial<OpenAIChatInput> {
|
|
124
124
|
temperature?: number;
|
|
125
125
|
topP?: number;
|
|
126
126
|
frequencyPenalty?: number;
|
|
@@ -141,8 +141,10 @@ declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOpti
|
|
|
141
141
|
apiKey?: string;
|
|
142
142
|
organization?: string;
|
|
143
143
|
__includeRawResponse?: boolean;
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
/** @internal */
|
|
145
|
+
client: OpenAIClient;
|
|
146
|
+
/** @internal */
|
|
147
|
+
clientConfig: ClientOptions;
|
|
146
148
|
/**
|
|
147
149
|
* Whether the model supports the `strict` argument when passing in tools.
|
|
148
150
|
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
@@ -219,6 +221,8 @@ declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOpti
|
|
|
219
221
|
protected _getNumTokensFromGenerations(generations: ChatGeneration[]): Promise<number>;
|
|
220
222
|
/** @internal */
|
|
221
223
|
protected _getEstimatedTokenCountFromPrompt(messages: BaseMessage[], functions?: OpenAIClient.Chat.ChatCompletionCreateParams.Function[], function_call?: "none" | "auto" | OpenAIClient.Chat.ChatCompletionFunctionCallOption): Promise<number>;
|
|
224
|
+
/** @internal */
|
|
225
|
+
protected _getStructuredOutputMethod(config: StructuredOutputMethodOptions<boolean>): string | undefined;
|
|
222
226
|
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
223
227
|
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
224
228
|
raw: BaseMessage;
|
|
@@ -236,7 +240,7 @@ type ResponsesCreate = OpenAIClient.Responses["create"];
|
|
|
236
240
|
type ResponsesParse = OpenAIClient.Responses["parse"];
|
|
237
241
|
type ResponsesCreateInvoke = ExcludeController<Awaited<ReturnType<ResponsesCreate>>>;
|
|
238
242
|
type ResponsesParseInvoke = ExcludeController<Awaited<ReturnType<ResponsesParse>>>;
|
|
239
|
-
interface ChatOpenAIResponsesCallOptions extends BaseChatOpenAICallOptions {
|
|
243
|
+
export interface ChatOpenAIResponsesCallOptions extends BaseChatOpenAICallOptions {
|
|
240
244
|
/**
|
|
241
245
|
* Configuration options for a text response from the model. Can be plain text or
|
|
242
246
|
* structured JSON data.
|
|
@@ -267,7 +271,7 @@ type ChatResponsesInvocationParams = Omit<OpenAIClient.Responses.ResponseCreateP
|
|
|
267
271
|
export declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponsesCallOptions = ChatOpenAIResponsesCallOptions> extends BaseChatOpenAI<CallOptions> {
|
|
268
272
|
invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams;
|
|
269
273
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<ChatResult>;
|
|
270
|
-
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"]): AsyncGenerator<ChatGenerationChunk>;
|
|
274
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
271
275
|
/**
|
|
272
276
|
* Calls the Responses API with retry logic in case of failures.
|
|
273
277
|
* @param request The request to send to the OpenAI API.
|
|
@@ -290,7 +294,7 @@ export declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponses
|
|
|
290
294
|
strict?: boolean;
|
|
291
295
|
}): ResponsesTool[];
|
|
292
296
|
}
|
|
293
|
-
interface ChatOpenAICompletionsCallOptions extends BaseChatOpenAICallOptions {
|
|
297
|
+
export interface ChatOpenAICompletionsCallOptions extends BaseChatOpenAICallOptions {
|
|
294
298
|
}
|
|
295
299
|
type ChatCompletionsInvocationParams = Omit<OpenAIClient.Chat.Completions.ChatCompletionCreateParams, "messages">;
|
|
296
300
|
/**
|
|
@@ -318,6 +322,16 @@ export interface ChatOpenAIFields extends BaseChatOpenAIFields {
|
|
|
318
322
|
* only when required in order to fulfill the request.
|
|
319
323
|
*/
|
|
320
324
|
useResponsesApi?: boolean;
|
|
325
|
+
/**
|
|
326
|
+
* The completions chat instance
|
|
327
|
+
* @internal
|
|
328
|
+
*/
|
|
329
|
+
completions?: ChatOpenAICompletions;
|
|
330
|
+
/**
|
|
331
|
+
* The responses chat instance
|
|
332
|
+
* @internal
|
|
333
|
+
*/
|
|
334
|
+
responses?: ChatOpenAIResponses;
|
|
321
335
|
}
|
|
322
336
|
/**
|
|
323
337
|
* OpenAI chat model integration.
|
|
@@ -857,16 +871,17 @@ export interface ChatOpenAIFields extends BaseChatOpenAIFields {
|
|
|
857
871
|
*
|
|
858
872
|
* <br />
|
|
859
873
|
*/
|
|
860
|
-
export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatOpenAI<CallOptions>
|
|
874
|
+
export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatOpenAI<CallOptions> {
|
|
875
|
+
protected fields?: ChatOpenAIFields | undefined;
|
|
861
876
|
/**
|
|
862
877
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
863
878
|
* only when required in order to fulfill the request.
|
|
864
879
|
*/
|
|
865
880
|
useResponsesApi: boolean;
|
|
866
|
-
|
|
867
|
-
|
|
881
|
+
protected responses: ChatOpenAIResponses;
|
|
882
|
+
protected completions: ChatOpenAICompletions;
|
|
868
883
|
get lc_serializable_keys(): string[];
|
|
869
|
-
constructor(fields?: ChatOpenAIFields);
|
|
884
|
+
constructor(fields?: ChatOpenAIFields | undefined);
|
|
870
885
|
protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean;
|
|
871
886
|
getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
|
|
872
887
|
invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams | ChatCompletionsInvocationParams;
|
package/dist/chat_models.js
CHANGED
|
@@ -8,7 +8,7 @@ import { RunnableLambda, RunnablePassthrough, RunnableSequence, } from "@langcha
|
|
|
8
8
|
import { JsonOutputParser, StructuredOutputParser, } from "@langchain/core/output_parsers";
|
|
9
9
|
import { JsonOutputKeyToolsParser, convertLangChainToolCallToOpenAI, makeInvalidToolCall, parseToolCall, } from "@langchain/core/output_parsers/openai_tools";
|
|
10
10
|
import { getSchemaDescription, isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
11
|
-
import { toJsonSchema
|
|
11
|
+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
12
12
|
import { getEndpoint } from "./utils/azure.js";
|
|
13
13
|
import { formatToOpenAIToolChoice, interopZodResponseFormat, wrapOpenAIClientError, } from "./utils/openai.js";
|
|
14
14
|
import { formatFunctionDefinitions, } from "./utils/openai-format-fndef.js";
|
|
@@ -252,7 +252,7 @@ export function _convertMessagesToOpenAIParams(messages, model) {
|
|
|
252
252
|
});
|
|
253
253
|
}
|
|
254
254
|
/** @internal */
|
|
255
|
-
class BaseChatOpenAI extends BaseChatModel {
|
|
255
|
+
export class BaseChatOpenAI extends BaseChatModel {
|
|
256
256
|
_llmType() {
|
|
257
257
|
return "openai";
|
|
258
258
|
}
|
|
@@ -471,12 +471,14 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
471
471
|
writable: true,
|
|
472
472
|
value: void 0
|
|
473
473
|
});
|
|
474
|
+
/** @internal */
|
|
474
475
|
Object.defineProperty(this, "client", {
|
|
475
476
|
enumerable: true,
|
|
476
477
|
configurable: true,
|
|
477
478
|
writable: true,
|
|
478
479
|
value: void 0
|
|
479
480
|
});
|
|
481
|
+
/** @internal */
|
|
480
482
|
Object.defineProperty(this, "clientConfig", {
|
|
481
483
|
enumerable: true,
|
|
482
484
|
configurable: true,
|
|
@@ -812,13 +814,22 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
812
814
|
}
|
|
813
815
|
return tokens;
|
|
814
816
|
}
|
|
817
|
+
/** @internal */
|
|
818
|
+
_getStructuredOutputMethod(config) {
|
|
819
|
+
const ensuredConfig = { ...config };
|
|
820
|
+
if (!this.model.startsWith("gpt-3") &&
|
|
821
|
+
!this.model.startsWith("gpt-4-") &&
|
|
822
|
+
this.model !== "gpt-4") {
|
|
823
|
+
if (ensuredConfig?.method === undefined) {
|
|
824
|
+
return "jsonSchema";
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
else if (ensuredConfig.method === "jsonSchema") {
|
|
828
|
+
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
829
|
+
}
|
|
830
|
+
return ensuredConfig.method;
|
|
831
|
+
}
|
|
815
832
|
withStructuredOutput(outputSchema, config) {
|
|
816
|
-
// ):
|
|
817
|
-
// | Runnable<BaseLanguageModelInput, RunOutput>
|
|
818
|
-
// | Runnable<
|
|
819
|
-
// BaseLanguageModelInput,
|
|
820
|
-
// { raw: BaseMessage; parsed: RunOutput }
|
|
821
|
-
// > {
|
|
822
833
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
823
834
|
let schema;
|
|
824
835
|
let name;
|
|
@@ -841,47 +852,43 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
841
852
|
if (config?.strict !== undefined && method === "jsonMode") {
|
|
842
853
|
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
843
854
|
}
|
|
844
|
-
|
|
845
|
-
!this.model.startsWith("gpt-4-") &&
|
|
846
|
-
this.model !== "gpt-4") {
|
|
847
|
-
if (method === undefined) {
|
|
848
|
-
method = "jsonSchema";
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
else if (method === "jsonSchema") {
|
|
852
|
-
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
853
|
-
}
|
|
855
|
+
method = this._getStructuredOutputMethod({ ...config, method });
|
|
854
856
|
if (method === "jsonMode") {
|
|
855
|
-
let outputFormatSchema;
|
|
856
857
|
if (isInteropZodSchema(schema)) {
|
|
857
858
|
outputParser = StructuredOutputParser.fromZodSchema(schema);
|
|
858
|
-
outputFormatSchema = toJsonSchema(schema);
|
|
859
859
|
}
|
|
860
860
|
else {
|
|
861
861
|
outputParser = new JsonOutputParser();
|
|
862
862
|
}
|
|
863
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
863
864
|
llm = this.withConfig({
|
|
864
865
|
response_format: { type: "json_object" },
|
|
865
866
|
ls_structured_output_format: {
|
|
866
|
-
kwargs: { method: "
|
|
867
|
-
schema:
|
|
867
|
+
kwargs: { method: "json_mode" },
|
|
868
|
+
schema: { title: name ?? "extract", ...asJsonSchema },
|
|
868
869
|
},
|
|
869
870
|
});
|
|
870
871
|
}
|
|
871
872
|
else if (method === "jsonSchema") {
|
|
873
|
+
const openaiJsonSchemaParams = {
|
|
874
|
+
name: name ?? "extract",
|
|
875
|
+
description: getSchemaDescription(schema),
|
|
876
|
+
schema,
|
|
877
|
+
strict: config?.strict,
|
|
878
|
+
};
|
|
879
|
+
const asJsonSchema = toJsonSchema(openaiJsonSchemaParams.schema);
|
|
872
880
|
llm = this.withConfig({
|
|
873
881
|
response_format: {
|
|
874
882
|
type: "json_schema",
|
|
875
|
-
json_schema:
|
|
876
|
-
name: name ?? "extract",
|
|
877
|
-
description: getSchemaDescription(schema),
|
|
878
|
-
schema,
|
|
879
|
-
strict: config?.strict,
|
|
880
|
-
},
|
|
883
|
+
json_schema: openaiJsonSchemaParams,
|
|
881
884
|
},
|
|
882
885
|
ls_structured_output_format: {
|
|
883
|
-
kwargs: { method: "
|
|
884
|
-
schema:
|
|
886
|
+
kwargs: { method: "json_schema" },
|
|
887
|
+
schema: {
|
|
888
|
+
title: openaiJsonSchemaParams.name,
|
|
889
|
+
description: openaiJsonSchemaParams.description,
|
|
890
|
+
...asJsonSchema,
|
|
891
|
+
},
|
|
885
892
|
},
|
|
886
893
|
});
|
|
887
894
|
if (isInteropZodSchema(schema)) {
|
|
@@ -920,8 +927,8 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
920
927
|
},
|
|
921
928
|
},
|
|
922
929
|
ls_structured_output_format: {
|
|
923
|
-
kwargs: { method: "
|
|
924
|
-
schema: asJsonSchema,
|
|
930
|
+
kwargs: { method: "function_calling" },
|
|
931
|
+
schema: { title: functionName, ...asJsonSchema },
|
|
925
932
|
},
|
|
926
933
|
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
927
934
|
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
@@ -948,6 +955,7 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
948
955
|
parameters: schema,
|
|
949
956
|
};
|
|
950
957
|
}
|
|
958
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
951
959
|
llm = this.withConfig({
|
|
952
960
|
tools: [
|
|
953
961
|
{
|
|
@@ -962,8 +970,8 @@ class BaseChatOpenAI extends BaseChatModel {
|
|
|
962
970
|
},
|
|
963
971
|
},
|
|
964
972
|
ls_structured_output_format: {
|
|
965
|
-
kwargs: { method: "
|
|
966
|
-
schema:
|
|
973
|
+
kwargs: { method: "function_calling" },
|
|
974
|
+
schema: { title: functionName, ...asJsonSchema },
|
|
967
975
|
},
|
|
968
976
|
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
969
977
|
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
@@ -1111,7 +1119,7 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
|
|
|
1111
1119
|
};
|
|
1112
1120
|
}
|
|
1113
1121
|
}
|
|
1114
|
-
async *_streamResponseChunks(messages, options) {
|
|
1122
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
1115
1123
|
const streamIterable = await this.completionWithRetry({
|
|
1116
1124
|
...this.invocationParams(options),
|
|
1117
1125
|
input: this._convertMessagesToResponsesParams(messages),
|
|
@@ -1122,6 +1130,10 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
|
|
|
1122
1130
|
if (chunk == null)
|
|
1123
1131
|
continue;
|
|
1124
1132
|
yield chunk;
|
|
1133
|
+
await runManager?.handleLLMNewToken(chunk.text || "", {
|
|
1134
|
+
prompt: options.promptIndex ?? 0,
|
|
1135
|
+
completion: 0,
|
|
1136
|
+
}, undefined, undefined, undefined, { chunk });
|
|
1125
1137
|
}
|
|
1126
1138
|
}
|
|
1127
1139
|
async completionWithRetry(request, requestOptions) {
|
|
@@ -2602,7 +2614,13 @@ export class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2602
2614
|
return [...super.lc_serializable_keys, "useResponsesApi"];
|
|
2603
2615
|
}
|
|
2604
2616
|
constructor(fields) {
|
|
2605
|
-
super(fields
|
|
2617
|
+
super(fields);
|
|
2618
|
+
Object.defineProperty(this, "fields", {
|
|
2619
|
+
enumerable: true,
|
|
2620
|
+
configurable: true,
|
|
2621
|
+
writable: true,
|
|
2622
|
+
value: fields
|
|
2623
|
+
});
|
|
2606
2624
|
/**
|
|
2607
2625
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
2608
2626
|
* only when required in order to fulfill the request.
|
|
@@ -2626,11 +2644,8 @@ export class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2626
2644
|
value: void 0
|
|
2627
2645
|
});
|
|
2628
2646
|
this.useResponsesApi = fields?.useResponsesApi ?? false;
|
|
2629
|
-
this.responses = new ChatOpenAIResponses(fields);
|
|
2630
|
-
this.completions = new ChatOpenAICompletions(fields);
|
|
2631
|
-
// Override `_getClientOptions` so sub classes of ChatOpenAI use the same client config
|
|
2632
|
-
this.responses._getClientOptions = this._getClientOptions;
|
|
2633
|
-
this.completions._getClientOptions = this._getClientOptions;
|
|
2647
|
+
this.responses = fields?.responses ?? new ChatOpenAIResponses(fields);
|
|
2648
|
+
this.completions = fields?.completions ?? new ChatOpenAICompletions(fields);
|
|
2634
2649
|
}
|
|
2635
2650
|
_useResponsesApi(options) {
|
|
2636
2651
|
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
|
@@ -2665,13 +2680,14 @@ export class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2665
2680
|
}
|
|
2666
2681
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
2667
2682
|
if (this._useResponsesApi(options)) {
|
|
2668
|
-
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
|
|
2683
|
+
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2669
2684
|
return;
|
|
2670
2685
|
}
|
|
2671
2686
|
yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2672
2687
|
}
|
|
2673
2688
|
withConfig(config) {
|
|
2674
|
-
|
|
2675
|
-
|
|
2689
|
+
const newModel = new ChatOpenAI(this.fields);
|
|
2690
|
+
newModel.defaultOptions = { ...this.defaultOptions, ...config };
|
|
2691
|
+
return newModel;
|
|
2676
2692
|
}
|
|
2677
2693
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -155,7 +155,7 @@ export interface OpenAIChatInput extends OpenAIBaseInput {
|
|
|
155
155
|
*/
|
|
156
156
|
service_tier?: OpenAIClient.Responses.ResponseCreateParams["service_tier"];
|
|
157
157
|
}
|
|
158
|
-
export
|
|
158
|
+
export interface AzureOpenAIInput {
|
|
159
159
|
/**
|
|
160
160
|
* API version to use when making requests to Azure OpenAI.
|
|
161
161
|
*/
|
|
@@ -213,6 +213,12 @@ export declare interface AzureOpenAIInput {
|
|
|
213
213
|
*/
|
|
214
214
|
azureADTokenProvider?: () => Promise<string>;
|
|
215
215
|
}
|
|
216
|
+
export interface AzureOpenAIChatInput extends OpenAIChatInput, AzureOpenAIInput {
|
|
217
|
+
openAIApiKey?: string;
|
|
218
|
+
openAIApiVersion?: string;
|
|
219
|
+
openAIBasePath?: string;
|
|
220
|
+
deploymentName?: string;
|
|
221
|
+
}
|
|
216
222
|
type ChatOpenAIResponseFormatJSONSchema = Omit<ResponseFormatJSONSchema, "json_schema"> & {
|
|
217
223
|
json_schema: Omit<ResponseFormatJSONSchema["json_schema"], "schema"> & {
|
|
218
224
|
/**
|