@langchain/openai 0.4.0-rc.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/azure/chat_models.cjs +77 -16
- package/dist/azure/chat_models.d.ts +28 -4
- package/dist/azure/chat_models.js +78 -17
- 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 -112
- package/dist/chat_models.d.ts +18 -20
- package/dist/chat_models.js +22 -112
- 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,21 +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 = fields?.azureOpenAIApiKey;
|
|
1046
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
1047
|
-
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
1048
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
1049
|
-
}
|
|
1050
|
-
this.azureOpenAIApiInstanceName = fields?.azureOpenAIApiInstanceName;
|
|
1051
|
-
this.azureOpenAIApiDeploymentName = fields?.azureOpenAIApiDeploymentName;
|
|
1052
|
-
this.azureOpenAIApiVersion = fields?.azureOpenAIApiVersion;
|
|
1053
|
-
this.azureOpenAIBasePath = fields?.azureOpenAIBasePath;
|
|
1054
|
-
this.azureOpenAIEndpoint = fields?.azureOpenAIEndpoint;
|
|
1055
999
|
this.organization =
|
|
1056
1000
|
fields?.configuration?.organization ??
|
|
1057
1001
|
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
1058
|
-
this.
|
|
1059
|
-
this.
|
|
1002
|
+
this.model = fields?.model ?? fields?.modelName ?? this.model;
|
|
1003
|
+
this.modelName = this.model;
|
|
1060
1004
|
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
1061
1005
|
this.timeout = fields?.timeout;
|
|
1062
1006
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
@@ -1075,29 +1019,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1075
1019
|
this.audio = fields?.audio;
|
|
1076
1020
|
this.modalities = fields?.modalities;
|
|
1077
1021
|
this.reasoningEffort = fields?.reasoningEffort;
|
|
1078
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
1079
|
-
if (!this.azureOpenAIApiInstanceName &&
|
|
1080
|
-
!this.azureOpenAIBasePath &&
|
|
1081
|
-
!this.azureOpenAIEndpoint) {
|
|
1082
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
1083
|
-
}
|
|
1084
|
-
if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
|
|
1085
|
-
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
1086
|
-
if (parts.length === 2) {
|
|
1087
|
-
const [, deployment] = parts;
|
|
1088
|
-
this.azureOpenAIApiDeploymentName = deployment;
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
1092
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
1093
|
-
}
|
|
1094
|
-
if (!this.azureOpenAIApiVersion) {
|
|
1095
|
-
throw new Error("Azure OpenAI API version not found");
|
|
1096
|
-
}
|
|
1097
|
-
this.apiKey = this.apiKey ?? "";
|
|
1098
|
-
// Streaming usage is not supported by Azure deployments, so default to false
|
|
1099
|
-
this.streamUsage = false;
|
|
1100
|
-
}
|
|
1101
1022
|
if (this.model === "o1") {
|
|
1102
1023
|
this.disableStreaming = true;
|
|
1103
1024
|
}
|
|
@@ -1106,13 +1027,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1106
1027
|
this.clientConfig = {
|
|
1107
1028
|
apiKey: this.apiKey,
|
|
1108
1029
|
organization: this.organization,
|
|
1109
|
-
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
1110
1030
|
dangerouslyAllowBrowser: true,
|
|
1111
|
-
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
1112
|
-
fields?.configuration?.baseOptions?.headers,
|
|
1113
|
-
defaultQuery: configuration?.baseOptions?.params ??
|
|
1114
|
-
fields?.configuration?.baseOptions?.params,
|
|
1115
|
-
...configuration,
|
|
1116
1031
|
...fields?.configuration,
|
|
1117
1032
|
};
|
|
1118
1033
|
// If `supportsStrictToolCalling` is explicitly set, use that value.
|
|
@@ -1591,12 +1506,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1591
1506
|
_getClientOptions(options) {
|
|
1592
1507
|
if (!this.client) {
|
|
1593
1508
|
const openAIEndpointConfig = {
|
|
1594
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
1595
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
1596
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
1597
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
1598
1509
|
baseURL: this.clientConfig.baseURL,
|
|
1599
|
-
azureOpenAIEndpoint: this.azureOpenAIEndpoint,
|
|
1600
1510
|
};
|
|
1601
1511
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
1602
1512
|
const params = {
|
|
@@ -1614,16 +1524,6 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1614
1524
|
...this.clientConfig,
|
|
1615
1525
|
...options,
|
|
1616
1526
|
};
|
|
1617
|
-
if (this.azureOpenAIApiKey) {
|
|
1618
|
-
requestOptions.headers = {
|
|
1619
|
-
"api-key": this.azureOpenAIApiKey,
|
|
1620
|
-
...requestOptions.headers,
|
|
1621
|
-
};
|
|
1622
|
-
requestOptions.query = {
|
|
1623
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
1624
|
-
...requestOptions.query,
|
|
1625
|
-
};
|
|
1626
|
-
}
|
|
1627
1527
|
return requestOptions;
|
|
1628
1528
|
}
|
|
1629
1529
|
_llmType() {
|
|
@@ -1670,6 +1570,16 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1670
1570
|
if (config?.strict !== undefined && method === "jsonMode") {
|
|
1671
1571
|
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
1672
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
|
+
}
|
|
1673
1583
|
if (method === "jsonMode") {
|
|
1674
1584
|
llm = this.bind({
|
|
1675
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
|
}
|
package/dist/embeddings.js
CHANGED
|
@@ -5,9 +5,10 @@ import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
|
5
5
|
import { getEndpoint } from "./utils/azure.js";
|
|
6
6
|
import { wrapOpenAIClientError } from "./utils/openai.js";
|
|
7
7
|
/**
|
|
8
|
-
* Class for generating embeddings using the OpenAI API.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Class for generating embeddings using the OpenAI API.
|
|
9
|
+
*
|
|
10
|
+
* To use with Azure, import the `AzureOpenAIEmbeddings` class.
|
|
11
|
+
*
|
|
11
12
|
* @example
|
|
12
13
|
* ```typescript
|
|
13
14
|
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
@@ -20,20 +21,21 @@ import { wrapOpenAIClientError } from "./utils/openai.js";
|
|
|
20
21
|
* ```
|
|
21
22
|
*/
|
|
22
23
|
export class OpenAIEmbeddings extends Embeddings {
|
|
23
|
-
constructor(fields
|
|
24
|
+
constructor(fields) {
|
|
24
25
|
const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
|
|
25
26
|
super(fieldsWithDefaults);
|
|
26
|
-
Object.defineProperty(this, "
|
|
27
|
+
Object.defineProperty(this, "model", {
|
|
27
28
|
enumerable: true,
|
|
28
29
|
configurable: true,
|
|
29
30
|
writable: true,
|
|
30
31
|
value: "text-embedding-ada-002"
|
|
31
32
|
});
|
|
32
|
-
|
|
33
|
+
/** @deprecated Use "model" instead */
|
|
34
|
+
Object.defineProperty(this, "modelName", {
|
|
33
35
|
enumerable: true,
|
|
34
36
|
configurable: true,
|
|
35
37
|
writable: true,
|
|
36
|
-
value:
|
|
38
|
+
value: void 0
|
|
37
39
|
});
|
|
38
40
|
Object.defineProperty(this, "batchSize", {
|
|
39
41
|
enumerable: true,
|
|
@@ -64,42 +66,6 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
64
66
|
writable: true,
|
|
65
67
|
value: void 0
|
|
66
68
|
});
|
|
67
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
68
|
-
enumerable: true,
|
|
69
|
-
configurable: true,
|
|
70
|
-
writable: true,
|
|
71
|
-
value: void 0
|
|
72
|
-
});
|
|
73
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
74
|
-
enumerable: true,
|
|
75
|
-
configurable: true,
|
|
76
|
-
writable: true,
|
|
77
|
-
value: void 0
|
|
78
|
-
});
|
|
79
|
-
Object.defineProperty(this, "azureADTokenProvider", {
|
|
80
|
-
enumerable: true,
|
|
81
|
-
configurable: true,
|
|
82
|
-
writable: true,
|
|
83
|
-
value: void 0
|
|
84
|
-
});
|
|
85
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
configurable: true,
|
|
88
|
-
writable: true,
|
|
89
|
-
value: void 0
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
92
|
-
enumerable: true,
|
|
93
|
-
configurable: true,
|
|
94
|
-
writable: true,
|
|
95
|
-
value: void 0
|
|
96
|
-
});
|
|
97
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
98
|
-
enumerable: true,
|
|
99
|
-
configurable: true,
|
|
100
|
-
writable: true,
|
|
101
|
-
value: void 0
|
|
102
|
-
});
|
|
103
69
|
Object.defineProperty(this, "organization", {
|
|
104
70
|
enumerable: true,
|
|
105
71
|
configurable: true,
|
|
@@ -118,62 +84,24 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
118
84
|
writable: true,
|
|
119
85
|
value: void 0
|
|
120
86
|
});
|
|
121
|
-
|
|
87
|
+
const apiKey = fieldsWithDefaults?.apiKey ??
|
|
122
88
|
fieldsWithDefaults?.openAIApiKey ??
|
|
123
89
|
getEnvironmentVariable("OPENAI_API_KEY");
|
|
124
|
-
const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
|
|
125
|
-
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
126
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
127
|
-
if (!azureApiKey && !apiKey && !this.azureADTokenProvider) {
|
|
128
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
129
|
-
}
|
|
130
|
-
const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
|
|
131
|
-
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
132
|
-
const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
|
|
133
|
-
fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
|
|
134
|
-
(getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
|
|
135
|
-
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
136
|
-
const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
|
|
137
|
-
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
138
|
-
this.azureOpenAIBasePath =
|
|
139
|
-
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
140
|
-
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
141
90
|
this.organization =
|
|
142
91
|
fieldsWithDefaults?.configuration?.organization ??
|
|
143
92
|
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
144
|
-
this.
|
|
93
|
+
this.model =
|
|
145
94
|
fieldsWithDefaults?.model ?? fieldsWithDefaults?.modelName ?? this.model;
|
|
146
|
-
this.
|
|
147
|
-
this.batchSize =
|
|
148
|
-
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
95
|
+
this.modelName = this.model;
|
|
96
|
+
this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;
|
|
149
97
|
this.stripNewLines =
|
|
150
98
|
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
151
99
|
this.timeout = fieldsWithDefaults?.timeout;
|
|
152
100
|
this.dimensions = fieldsWithDefaults?.dimensions;
|
|
153
|
-
this.azureOpenAIApiVersion = azureApiVersion;
|
|
154
|
-
this.azureOpenAIApiKey = azureApiKey;
|
|
155
|
-
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
156
|
-
this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
|
|
157
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
158
|
-
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
159
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
160
|
-
}
|
|
161
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
162
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
163
|
-
}
|
|
164
|
-
if (!this.azureOpenAIApiVersion) {
|
|
165
|
-
throw new Error("Azure OpenAI API version not found");
|
|
166
|
-
}
|
|
167
|
-
apiKey = apiKey ?? "";
|
|
168
|
-
}
|
|
169
101
|
this.clientConfig = {
|
|
170
102
|
apiKey,
|
|
171
103
|
organization: this.organization,
|
|
172
|
-
baseURL: configuration?.basePath,
|
|
173
104
|
dangerouslyAllowBrowser: true,
|
|
174
|
-
defaultHeaders: configuration?.baseOptions?.headers,
|
|
175
|
-
defaultQuery: configuration?.baseOptions?.params,
|
|
176
|
-
...configuration,
|
|
177
105
|
...fields?.configuration,
|
|
178
106
|
};
|
|
179
107
|
}
|
|
@@ -234,10 +162,6 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
234
162
|
async embeddingWithRetry(request) {
|
|
235
163
|
if (!this.client) {
|
|
236
164
|
const openAIEndpointConfig = {
|
|
237
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
238
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
239
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
240
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
241
165
|
baseURL: this.clientConfig.baseURL,
|
|
242
166
|
};
|
|
243
167
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
@@ -253,16 +177,6 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
253
177
|
this.client = new OpenAIClient(params);
|
|
254
178
|
}
|
|
255
179
|
const requestOptions = {};
|
|
256
|
-
if (this.azureOpenAIApiKey) {
|
|
257
|
-
requestOptions.headers = {
|
|
258
|
-
"api-key": this.azureOpenAIApiKey,
|
|
259
|
-
...requestOptions.headers,
|
|
260
|
-
};
|
|
261
|
-
requestOptions.query = {
|
|
262
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
263
|
-
...requestOptions.query,
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
180
|
return this.caller.call(async () => {
|
|
267
181
|
try {
|
|
268
182
|
const res = await this.client.embeddings.create(request, requestOptions);
|