@langchain/openai 0.6.2 → 0.6.3
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 +45 -39
- package/dist/chat_models.d.ts +23 -9
- package/dist/chat_models.js +45 -40
- 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
|
*
|
|
@@ -2609,7 +2618,7 @@ class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2609
2618
|
return [...super.lc_serializable_keys, "useResponsesApi"];
|
|
2610
2619
|
}
|
|
2611
2620
|
constructor(fields) {
|
|
2612
|
-
super(fields
|
|
2621
|
+
super(fields);
|
|
2613
2622
|
/**
|
|
2614
2623
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
2615
2624
|
* only when required in order to fulfill the request.
|
|
@@ -2633,11 +2642,8 @@ class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2633
2642
|
value: void 0
|
|
2634
2643
|
});
|
|
2635
2644
|
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;
|
|
2645
|
+
this.responses = fields?.responses ?? new ChatOpenAIResponses(fields);
|
|
2646
|
+
this.completions = fields?.completions ?? new ChatOpenAICompletions(fields);
|
|
2641
2647
|
}
|
|
2642
2648
|
_useResponsesApi(options) {
|
|
2643
2649
|
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
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.
|
|
@@ -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,14 +871,14 @@ 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> {
|
|
861
875
|
/**
|
|
862
876
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
863
877
|
* only when required in order to fulfill the request.
|
|
864
878
|
*/
|
|
865
879
|
useResponsesApi: boolean;
|
|
866
|
-
|
|
867
|
-
|
|
880
|
+
protected responses: ChatOpenAIResponses;
|
|
881
|
+
protected completions: ChatOpenAICompletions;
|
|
868
882
|
get lc_serializable_keys(): string[];
|
|
869
883
|
constructor(fields?: ChatOpenAIFields);
|
|
870
884
|
protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean;
|
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 } : {}),
|
|
@@ -2602,7 +2610,7 @@ export class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2602
2610
|
return [...super.lc_serializable_keys, "useResponsesApi"];
|
|
2603
2611
|
}
|
|
2604
2612
|
constructor(fields) {
|
|
2605
|
-
super(fields
|
|
2613
|
+
super(fields);
|
|
2606
2614
|
/**
|
|
2607
2615
|
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
2608
2616
|
* only when required in order to fulfill the request.
|
|
@@ -2626,11 +2634,8 @@ export class ChatOpenAI extends BaseChatOpenAI {
|
|
|
2626
2634
|
value: void 0
|
|
2627
2635
|
});
|
|
2628
2636
|
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;
|
|
2637
|
+
this.responses = fields?.responses ?? new ChatOpenAIResponses(fields);
|
|
2638
|
+
this.completions = fields?.completions ?? new ChatOpenAICompletions(fields);
|
|
2634
2639
|
}
|
|
2635
2640
|
_useResponsesApi(options) {
|
|
2636
2641
|
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
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
|
/**
|