@langchain/openai 0.3.17 → 0.4.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/dist/azure/chat_models.cjs +77 -11
- package/dist/azure/chat_models.d.ts +14 -3
- package/dist/azure/chat_models.js +77 -11
- package/dist/azure/embeddings.cjs +60 -12
- package/dist/azure/embeddings.d.ts +8 -2
- package/dist/azure/embeddings.js +60 -12
- package/dist/azure/llms.cjs +82 -10
- package/dist/azure/llms.d.ts +12 -2
- package/dist/azure/llms.js +82 -10
- package/dist/chat_models.cjs +22 -124
- package/dist/chat_models.d.ts +14 -20
- package/dist/chat_models.js +22 -124
- package/dist/embeddings.cjs +13 -99
- package/dist/embeddings.d.ts +11 -18
- package/dist/embeddings.js +13 -99
- package/dist/llms.cjs +28 -131
- package/dist/llms.d.ts +14 -27
- package/dist/llms.js +27 -130
- package/dist/types.d.ts +1 -12
- package/package.json +1 -1
- package/dist/legacy.cjs +0 -458
- package/dist/legacy.d.ts +0 -127
- package/dist/legacy.js +0 -454
package/dist/chat_models.js
CHANGED
|
@@ -242,6 +242,8 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
|
242
242
|
/**
|
|
243
243
|
* OpenAI chat model integration.
|
|
244
244
|
*
|
|
245
|
+
* To use with Azure, import the `AzureChatOpenAI` class.
|
|
246
|
+
*
|
|
245
247
|
* Setup:
|
|
246
248
|
* Install `@langchain/openai` and set an environment variable named `OPENAI_API_KEY`.
|
|
247
249
|
*
|
|
@@ -491,7 +493,7 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
|
491
493
|
* const Joke = z.object({
|
|
492
494
|
* setup: z.string().describe("The setup of the joke"),
|
|
493
495
|
* punchline: z.string().describe("The punchline to the joke"),
|
|
494
|
-
* rating: z.number().
|
|
496
|
+
* rating: z.number().nullable().describe("How funny the joke is, from 1 to 10")
|
|
495
497
|
* }).describe('Joke to tell user.');
|
|
496
498
|
*
|
|
497
499
|
* const structuredLlm = llm.withStructuredOutput(Joke, {
|
|
@@ -797,7 +799,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
797
799
|
return {
|
|
798
800
|
openAIApiKey: "OPENAI_API_KEY",
|
|
799
801
|
apiKey: "OPENAI_API_KEY",
|
|
800
|
-
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
801
802
|
organization: "OPENAI_ORGANIZATION",
|
|
802
803
|
};
|
|
803
804
|
}
|
|
@@ -806,15 +807,9 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
806
807
|
modelName: "model",
|
|
807
808
|
openAIApiKey: "openai_api_key",
|
|
808
809
|
apiKey: "openai_api_key",
|
|
809
|
-
azureOpenAIApiVersion: "azure_openai_api_version",
|
|
810
|
-
azureOpenAIApiKey: "azure_openai_api_key",
|
|
811
|
-
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
|
|
812
|
-
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
|
|
813
810
|
};
|
|
814
811
|
}
|
|
815
|
-
constructor(fields
|
|
816
|
-
/** @deprecated */
|
|
817
|
-
configuration) {
|
|
812
|
+
constructor(fields) {
|
|
818
813
|
super(fields ?? {});
|
|
819
814
|
Object.defineProperty(this, "lc_serializable", {
|
|
820
815
|
enumerable: true,
|
|
@@ -826,25 +821,25 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
826
821
|
enumerable: true,
|
|
827
822
|
configurable: true,
|
|
828
823
|
writable: true,
|
|
829
|
-
value:
|
|
824
|
+
value: void 0
|
|
830
825
|
});
|
|
831
826
|
Object.defineProperty(this, "topP", {
|
|
832
827
|
enumerable: true,
|
|
833
828
|
configurable: true,
|
|
834
829
|
writable: true,
|
|
835
|
-
value:
|
|
830
|
+
value: void 0
|
|
836
831
|
});
|
|
837
832
|
Object.defineProperty(this, "frequencyPenalty", {
|
|
838
833
|
enumerable: true,
|
|
839
834
|
configurable: true,
|
|
840
835
|
writable: true,
|
|
841
|
-
value: 0
|
|
836
|
+
value: void 0
|
|
842
837
|
});
|
|
843
838
|
Object.defineProperty(this, "presencePenalty", {
|
|
844
839
|
enumerable: true,
|
|
845
840
|
configurable: true,
|
|
846
841
|
writable: true,
|
|
847
|
-
value: 0
|
|
842
|
+
value: void 0
|
|
848
843
|
});
|
|
849
844
|
Object.defineProperty(this, "n", {
|
|
850
845
|
enumerable: true,
|
|
@@ -858,11 +853,12 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
858
853
|
writable: true,
|
|
859
854
|
value: void 0
|
|
860
855
|
});
|
|
856
|
+
/** @deprecated Use "model" instead */
|
|
861
857
|
Object.defineProperty(this, "modelName", {
|
|
862
858
|
enumerable: true,
|
|
863
859
|
configurable: true,
|
|
864
860
|
writable: true,
|
|
865
|
-
value:
|
|
861
|
+
value: void 0
|
|
866
862
|
});
|
|
867
863
|
Object.defineProperty(this, "model", {
|
|
868
864
|
enumerable: true,
|
|
@@ -942,48 +938,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
942
938
|
writable: true,
|
|
943
939
|
value: void 0
|
|
944
940
|
});
|
|
945
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
946
|
-
enumerable: true,
|
|
947
|
-
configurable: true,
|
|
948
|
-
writable: true,
|
|
949
|
-
value: void 0
|
|
950
|
-
});
|
|
951
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
952
|
-
enumerable: true,
|
|
953
|
-
configurable: true,
|
|
954
|
-
writable: true,
|
|
955
|
-
value: void 0
|
|
956
|
-
});
|
|
957
|
-
Object.defineProperty(this, "azureADTokenProvider", {
|
|
958
|
-
enumerable: true,
|
|
959
|
-
configurable: true,
|
|
960
|
-
writable: true,
|
|
961
|
-
value: void 0
|
|
962
|
-
});
|
|
963
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
964
|
-
enumerable: true,
|
|
965
|
-
configurable: true,
|
|
966
|
-
writable: true,
|
|
967
|
-
value: void 0
|
|
968
|
-
});
|
|
969
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
970
|
-
enumerable: true,
|
|
971
|
-
configurable: true,
|
|
972
|
-
writable: true,
|
|
973
|
-
value: void 0
|
|
974
|
-
});
|
|
975
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
976
|
-
enumerable: true,
|
|
977
|
-
configurable: true,
|
|
978
|
-
writable: true,
|
|
979
|
-
value: void 0
|
|
980
|
-
});
|
|
981
|
-
Object.defineProperty(this, "azureOpenAIEndpoint", {
|
|
982
|
-
enumerable: true,
|
|
983
|
-
configurable: true,
|
|
984
|
-
writable: true,
|
|
985
|
-
value: void 0
|
|
986
|
-
});
|
|
987
941
|
Object.defineProperty(this, "organization", {
|
|
988
942
|
enumerable: true,
|
|
989
943
|
configurable: true,
|
|
@@ -1042,33 +996,11 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1042
996
|
fields?.configuration?.apiKey ??
|
|
1043
997
|
getEnvironmentVariable("OPENAI_API_KEY");
|
|
1044
998
|
this.apiKey = this.openAIApiKey;
|
|
1045
|
-
this.azureOpenAIApiKey =
|
|
1046
|
-
fields?.azureOpenAIApiKey ??
|
|
1047
|
-
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
1048
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
1049
|
-
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
1050
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
1051
|
-
}
|
|
1052
|
-
this.azureOpenAIApiInstanceName =
|
|
1053
|
-
fields?.azureOpenAIApiInstanceName ??
|
|
1054
|
-
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
1055
|
-
this.azureOpenAIApiDeploymentName =
|
|
1056
|
-
fields?.azureOpenAIApiDeploymentName ??
|
|
1057
|
-
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME");
|
|
1058
|
-
this.azureOpenAIApiVersion =
|
|
1059
|
-
fields?.azureOpenAIApiVersion ??
|
|
1060
|
-
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
1061
|
-
this.azureOpenAIBasePath =
|
|
1062
|
-
fields?.azureOpenAIBasePath ??
|
|
1063
|
-
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
1064
999
|
this.organization =
|
|
1065
1000
|
fields?.configuration?.organization ??
|
|
1066
1001
|
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
1067
|
-
this.
|
|
1068
|
-
|
|
1069
|
-
getEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
|
|
1070
|
-
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
|
|
1071
|
-
this.model = this.modelName;
|
|
1002
|
+
this.model = fields?.model ?? fields?.modelName ?? this.model;
|
|
1003
|
+
this.modelName = this.model;
|
|
1072
1004
|
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
1073
1005
|
this.timeout = fields?.timeout;
|
|
1074
1006
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
@@ -1087,29 +1019,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1087
1019
|
this.audio = fields?.audio;
|
|
1088
1020
|
this.modalities = fields?.modalities;
|
|
1089
1021
|
this.reasoningEffort = fields?.reasoningEffort;
|
|
1090
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
1091
|
-
if (!this.azureOpenAIApiInstanceName &&
|
|
1092
|
-
!this.azureOpenAIBasePath &&
|
|
1093
|
-
!this.azureOpenAIEndpoint) {
|
|
1094
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
1095
|
-
}
|
|
1096
|
-
if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
|
|
1097
|
-
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
1098
|
-
if (parts.length === 2) {
|
|
1099
|
-
const [, deployment] = parts;
|
|
1100
|
-
this.azureOpenAIApiDeploymentName = deployment;
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
1104
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
1105
|
-
}
|
|
1106
|
-
if (!this.azureOpenAIApiVersion) {
|
|
1107
|
-
throw new Error("Azure OpenAI API version not found");
|
|
1108
|
-
}
|
|
1109
|
-
this.apiKey = this.apiKey ?? "";
|
|
1110
|
-
// Streaming usage is not supported by Azure deployments, so default to false
|
|
1111
|
-
this.streamUsage = false;
|
|
1112
|
-
}
|
|
1113
1022
|
if (this.model === "o1") {
|
|
1114
1023
|
this.disableStreaming = true;
|
|
1115
1024
|
}
|
|
@@ -1118,13 +1027,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1118
1027
|
this.clientConfig = {
|
|
1119
1028
|
apiKey: this.apiKey,
|
|
1120
1029
|
organization: this.organization,
|
|
1121
|
-
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
1122
1030
|
dangerouslyAllowBrowser: true,
|
|
1123
|
-
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
1124
|
-
fields?.configuration?.baseOptions?.headers,
|
|
1125
|
-
defaultQuery: configuration?.baseOptions?.params ??
|
|
1126
|
-
fields?.configuration?.baseOptions?.params,
|
|
1127
|
-
...configuration,
|
|
1128
1031
|
...fields?.configuration,
|
|
1129
1032
|
};
|
|
1130
1033
|
// If `supportsStrictToolCalling` is explicitly set, use that value.
|
|
@@ -1603,12 +1506,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1603
1506
|
_getClientOptions(options) {
|
|
1604
1507
|
if (!this.client) {
|
|
1605
1508
|
const openAIEndpointConfig = {
|
|
1606
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
1607
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
1608
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
1609
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
1610
1509
|
baseURL: this.clientConfig.baseURL,
|
|
1611
|
-
azureOpenAIEndpoint: this.azureOpenAIEndpoint,
|
|
1612
1510
|
};
|
|
1613
1511
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
1614
1512
|
const params = {
|
|
@@ -1626,16 +1524,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1626
1524
|
...this.clientConfig,
|
|
1627
1525
|
...options,
|
|
1628
1526
|
};
|
|
1629
|
-
if (this.azureOpenAIApiKey) {
|
|
1630
|
-
requestOptions.headers = {
|
|
1631
|
-
"api-key": this.azureOpenAIApiKey,
|
|
1632
|
-
...requestOptions.headers,
|
|
1633
|
-
};
|
|
1634
|
-
requestOptions.query = {
|
|
1635
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
1636
|
-
...requestOptions.query,
|
|
1637
|
-
};
|
|
1638
|
-
}
|
|
1639
1527
|
return requestOptions;
|
|
1640
1528
|
}
|
|
1641
1529
|
_llmType() {
|
|
@@ -1682,6 +1570,16 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1682
1570
|
if (config?.strict !== undefined && method === "jsonMode") {
|
|
1683
1571
|
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
1684
1572
|
}
|
|
1573
|
+
if (!this.model.startsWith("gpt-3") &&
|
|
1574
|
+
!this.model.startsWith("gpt-4-") &&
|
|
1575
|
+
this.model !== "gpt-4") {
|
|
1576
|
+
if (method === undefined) {
|
|
1577
|
+
method = "jsonSchema";
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
else if (method === "jsonSchema") {
|
|
1581
|
+
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
1582
|
+
}
|
|
1685
1583
|
if (method === "jsonMode") {
|
|
1686
1584
|
llm = this.bind({
|
|
1687
1585
|
response_format: { type: "json_object" },
|
package/dist/embeddings.cjs
CHANGED
|
@@ -8,9 +8,10 @@ const chunk_array_1 = require("@langchain/core/utils/chunk_array");
|
|
|
8
8
|
const azure_js_1 = require("./utils/azure.cjs");
|
|
9
9
|
const openai_js_1 = require("./utils/openai.cjs");
|
|
10
10
|
/**
|
|
11
|
-
* Class for generating embeddings using the OpenAI API.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* Class for generating embeddings using the OpenAI API.
|
|
12
|
+
*
|
|
13
|
+
* To use with Azure, import the `AzureOpenAIEmbeddings` class.
|
|
14
|
+
*
|
|
14
15
|
* @example
|
|
15
16
|
* ```typescript
|
|
16
17
|
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
@@ -23,20 +24,21 @@ const openai_js_1 = require("./utils/openai.cjs");
|
|
|
23
24
|
* ```
|
|
24
25
|
*/
|
|
25
26
|
class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
26
|
-
constructor(fields
|
|
27
|
+
constructor(fields) {
|
|
27
28
|
const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
|
|
28
29
|
super(fieldsWithDefaults);
|
|
29
|
-
Object.defineProperty(this, "
|
|
30
|
+
Object.defineProperty(this, "model", {
|
|
30
31
|
enumerable: true,
|
|
31
32
|
configurable: true,
|
|
32
33
|
writable: true,
|
|
33
34
|
value: "text-embedding-ada-002"
|
|
34
35
|
});
|
|
35
|
-
|
|
36
|
+
/** @deprecated Use "model" instead */
|
|
37
|
+
Object.defineProperty(this, "modelName", {
|
|
36
38
|
enumerable: true,
|
|
37
39
|
configurable: true,
|
|
38
40
|
writable: true,
|
|
39
|
-
value:
|
|
41
|
+
value: void 0
|
|
40
42
|
});
|
|
41
43
|
Object.defineProperty(this, "batchSize", {
|
|
42
44
|
enumerable: true,
|
|
@@ -67,42 +69,6 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
67
69
|
writable: true,
|
|
68
70
|
value: void 0
|
|
69
71
|
});
|
|
70
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
71
|
-
enumerable: true,
|
|
72
|
-
configurable: true,
|
|
73
|
-
writable: true,
|
|
74
|
-
value: void 0
|
|
75
|
-
});
|
|
76
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
77
|
-
enumerable: true,
|
|
78
|
-
configurable: true,
|
|
79
|
-
writable: true,
|
|
80
|
-
value: void 0
|
|
81
|
-
});
|
|
82
|
-
Object.defineProperty(this, "azureADTokenProvider", {
|
|
83
|
-
enumerable: true,
|
|
84
|
-
configurable: true,
|
|
85
|
-
writable: true,
|
|
86
|
-
value: void 0
|
|
87
|
-
});
|
|
88
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
89
|
-
enumerable: true,
|
|
90
|
-
configurable: true,
|
|
91
|
-
writable: true,
|
|
92
|
-
value: void 0
|
|
93
|
-
});
|
|
94
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
95
|
-
enumerable: true,
|
|
96
|
-
configurable: true,
|
|
97
|
-
writable: true,
|
|
98
|
-
value: void 0
|
|
99
|
-
});
|
|
100
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
101
|
-
enumerable: true,
|
|
102
|
-
configurable: true,
|
|
103
|
-
writable: true,
|
|
104
|
-
value: void 0
|
|
105
|
-
});
|
|
106
72
|
Object.defineProperty(this, "organization", {
|
|
107
73
|
enumerable: true,
|
|
108
74
|
configurable: true,
|
|
@@ -121,62 +87,24 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
121
87
|
writable: true,
|
|
122
88
|
value: void 0
|
|
123
89
|
});
|
|
124
|
-
|
|
90
|
+
const apiKey = fieldsWithDefaults?.apiKey ??
|
|
125
91
|
fieldsWithDefaults?.openAIApiKey ??
|
|
126
92
|
(0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
|
|
127
|
-
const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
|
|
128
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
|
129
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
130
|
-
if (!azureApiKey && !apiKey && !this.azureADTokenProvider) {
|
|
131
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
132
|
-
}
|
|
133
|
-
const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
|
|
134
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
135
|
-
const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
|
|
136
|
-
fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
|
|
137
|
-
((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
|
|
138
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
139
|
-
const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
|
|
140
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
|
|
141
|
-
this.azureOpenAIBasePath =
|
|
142
|
-
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
143
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
144
93
|
this.organization =
|
|
145
94
|
fieldsWithDefaults?.configuration?.organization ??
|
|
146
95
|
(0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
|
|
147
|
-
this.
|
|
96
|
+
this.model =
|
|
148
97
|
fieldsWithDefaults?.model ?? fieldsWithDefaults?.modelName ?? this.model;
|
|
149
|
-
this.
|
|
150
|
-
this.batchSize =
|
|
151
|
-
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
98
|
+
this.modelName = this.model;
|
|
99
|
+
this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;
|
|
152
100
|
this.stripNewLines =
|
|
153
101
|
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
154
102
|
this.timeout = fieldsWithDefaults?.timeout;
|
|
155
103
|
this.dimensions = fieldsWithDefaults?.dimensions;
|
|
156
|
-
this.azureOpenAIApiVersion = azureApiVersion;
|
|
157
|
-
this.azureOpenAIApiKey = azureApiKey;
|
|
158
|
-
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
159
|
-
this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
|
|
160
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
161
|
-
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
162
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
163
|
-
}
|
|
164
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
165
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
166
|
-
}
|
|
167
|
-
if (!this.azureOpenAIApiVersion) {
|
|
168
|
-
throw new Error("Azure OpenAI API version not found");
|
|
169
|
-
}
|
|
170
|
-
apiKey = apiKey ?? "";
|
|
171
|
-
}
|
|
172
104
|
this.clientConfig = {
|
|
173
105
|
apiKey,
|
|
174
106
|
organization: this.organization,
|
|
175
|
-
baseURL: configuration?.basePath,
|
|
176
107
|
dangerouslyAllowBrowser: true,
|
|
177
|
-
defaultHeaders: configuration?.baseOptions?.headers,
|
|
178
|
-
defaultQuery: configuration?.baseOptions?.params,
|
|
179
|
-
...configuration,
|
|
180
108
|
...fields?.configuration,
|
|
181
109
|
};
|
|
182
110
|
}
|
|
@@ -237,10 +165,6 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
237
165
|
async embeddingWithRetry(request) {
|
|
238
166
|
if (!this.client) {
|
|
239
167
|
const openAIEndpointConfig = {
|
|
240
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
241
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
242
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
243
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
244
168
|
baseURL: this.clientConfig.baseURL,
|
|
245
169
|
};
|
|
246
170
|
const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
|
|
@@ -256,16 +180,6 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
256
180
|
this.client = new openai_1.OpenAI(params);
|
|
257
181
|
}
|
|
258
182
|
const requestOptions = {};
|
|
259
|
-
if (this.azureOpenAIApiKey) {
|
|
260
|
-
requestOptions.headers = {
|
|
261
|
-
"api-key": this.azureOpenAIApiKey,
|
|
262
|
-
...requestOptions.headers,
|
|
263
|
-
};
|
|
264
|
-
requestOptions.query = {
|
|
265
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
266
|
-
...requestOptions.query,
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
183
|
return this.caller.call(async () => {
|
|
270
184
|
try {
|
|
271
185
|
const res = await this.client.embeddings.create(request, requestOptions);
|
package/dist/embeddings.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
|
|
3
|
-
import { AzureOpenAIInput, LegacyOpenAIInput } from "./types.js";
|
|
4
3
|
/**
|
|
5
4
|
* Interface for OpenAIEmbeddings parameters. Extends EmbeddingsParams and
|
|
6
5
|
* defines additional parameters specific to the OpenAIEmbeddings class.
|
|
@@ -9,6 +8,7 @@ export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
|
|
|
9
8
|
/**
|
|
10
9
|
* Model name to use
|
|
11
10
|
* Alias for `model`
|
|
11
|
+
* @deprecated Use "model" instead.
|
|
12
12
|
*/
|
|
13
13
|
modelName: string;
|
|
14
14
|
/** Model name to use */
|
|
@@ -35,9 +35,10 @@ export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
|
|
|
35
35
|
stripNewLines?: boolean;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
* Class for generating embeddings using the OpenAI API.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* Class for generating embeddings using the OpenAI API.
|
|
39
|
+
*
|
|
40
|
+
* To use with Azure, import the `AzureOpenAIEmbeddings` class.
|
|
41
|
+
*
|
|
41
42
|
* @example
|
|
42
43
|
* ```typescript
|
|
43
44
|
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
@@ -49,9 +50,10 @@ export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
|
|
|
49
50
|
*
|
|
50
51
|
* ```
|
|
51
52
|
*/
|
|
52
|
-
export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbeddingsParams
|
|
53
|
-
modelName: string;
|
|
53
|
+
export declare class OpenAIEmbeddings extends Embeddings implements Partial<OpenAIEmbeddingsParams> {
|
|
54
54
|
model: string;
|
|
55
|
+
/** @deprecated Use "model" instead */
|
|
56
|
+
modelName: string;
|
|
55
57
|
batchSize: number;
|
|
56
58
|
stripNewLines: boolean;
|
|
57
59
|
/**
|
|
@@ -60,16 +62,10 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
|
|
|
60
62
|
*/
|
|
61
63
|
dimensions?: number;
|
|
62
64
|
timeout?: number;
|
|
63
|
-
azureOpenAIApiVersion?: string;
|
|
64
|
-
azureOpenAIApiKey?: string;
|
|
65
|
-
azureADTokenProvider?: () => Promise<string>;
|
|
66
|
-
azureOpenAIApiInstanceName?: string;
|
|
67
|
-
azureOpenAIApiDeploymentName?: string;
|
|
68
|
-
azureOpenAIBasePath?: string;
|
|
69
65
|
organization?: string;
|
|
70
66
|
protected client: OpenAIClient;
|
|
71
67
|
protected clientConfig: ClientOptions;
|
|
72
|
-
constructor(fields?: Partial<OpenAIEmbeddingsParams> &
|
|
68
|
+
constructor(fields?: Partial<OpenAIEmbeddingsParams> & {
|
|
73
69
|
verbose?: boolean;
|
|
74
70
|
/**
|
|
75
71
|
* The OpenAI API key to use.
|
|
@@ -79,7 +75,7 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
|
|
|
79
75
|
/** The OpenAI API key to use. */
|
|
80
76
|
apiKey?: string;
|
|
81
77
|
configuration?: ClientOptions;
|
|
82
|
-
}
|
|
78
|
+
});
|
|
83
79
|
/**
|
|
84
80
|
* Method to generate embeddings for an array of documents. Splits the
|
|
85
81
|
* documents into batches and makes requests to the OpenAI API to generate
|
|
@@ -103,9 +99,6 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
|
|
|
103
99
|
* @returns Promise that resolves to the response from the API.
|
|
104
100
|
*/
|
|
105
101
|
protected embeddingWithRetry(request: OpenAIClient.EmbeddingCreateParams): Promise<OpenAIClient.Embeddings.CreateEmbeddingResponse & {
|
|
106
|
-
_request_id?: string | null | undefined;
|
|
107
|
-
* The number of dimensions the resulting output embeddings should have.
|
|
108
|
-
* Only supported in `text-embedding-3` and later models.
|
|
109
|
-
*/
|
|
102
|
+
_request_id?: string | null | undefined;
|
|
110
103
|
}>;
|
|
111
104
|
}
|