langchain 0.0.197-rc.1 → 0.0.197
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/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.cjs +351 -15
- package/dist/chat_models/anthropic.d.ts +157 -1
- package/dist/chat_models/anthropic.js +348 -1
- package/dist/chat_models/cloudflare_workersai.cjs +5 -0
- package/dist/chat_models/cloudflare_workersai.d.ts +3 -0
- package/dist/chat_models/cloudflare_workersai.js +5 -0
- package/dist/chat_models/fireworks.d.ts +1 -1
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
- package/dist/chat_models/minimax.d.ts +1 -1
- package/dist/chat_models/openai.cjs +698 -4
- package/dist/chat_models/openai.d.ts +137 -4
- package/dist/chat_models/openai.js +695 -2
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/embeddings/openai.cjs +240 -2
- package/dist/embeddings/openai.d.ts +82 -1
- package/dist/embeddings/openai.js +239 -1
- package/dist/experimental/openai_assistant/index.cjs +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.js +1 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/llms/openai-chat.cjs +445 -3
- package/dist/llms/openai-chat.d.ts +123 -4
- package/dist/llms/openai-chat.js +443 -2
- package/dist/llms/openai.cjs +530 -6
- package/dist/llms/openai.d.ts +123 -4
- package/dist/llms/openai.js +525 -2
- package/dist/schema/index.d.ts +1 -1
- package/dist/tools/convert_to_openai.cjs +38 -4
- package/dist/tools/convert_to_openai.d.ts +11 -1
- package/dist/tools/convert_to_openai.js +35 -1
- package/dist/types/openai-types.d.ts +133 -1
- package/dist/util/env.cjs +9 -70
- package/dist/util/env.d.ts +1 -21
- package/dist/util/env.js +1 -62
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openai.cjs +18 -2
- package/dist/util/openai.d.ts +1 -1
- package/dist/util/openai.js +17 -1
- package/dist/util/openapi.d.ts +2 -2
- package/dist/util/prompt-layer.d.ts +1 -1
- package/package.json +3 -6
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAIWhisperAudio = void 0;
|
|
4
|
-
const openai_1 = require("
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
5
|
const document_js_1 = require("../../document.cjs");
|
|
6
6
|
const buffer_js_1 = require("./buffer.cjs");
|
|
7
7
|
const MODEL_NAME = "whisper-1";
|
|
@@ -24,7 +24,7 @@ class OpenAIWhisperAudio extends buffer_js_1.BufferLoader {
|
|
|
24
24
|
writable: true,
|
|
25
25
|
value: void 0
|
|
26
26
|
});
|
|
27
|
-
this.openAIClient = new openai_1.
|
|
27
|
+
this.openAIClient = new openai_1.OpenAI(fields?.clientOptions);
|
|
28
28
|
}
|
|
29
29
|
async parse(raw, metadata) {
|
|
30
30
|
const fileName = metadata.source === "blob" ? metadata.blobType : metadata.source;
|
|
@@ -1,5 +1,243 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAIEmbeddings = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
|
+
const env_js_1 = require("../util/env.cjs");
|
|
6
|
+
const chunk_js_1 = require("../util/chunk.cjs");
|
|
7
|
+
const base_js_1 = require("./base.cjs");
|
|
8
|
+
const azure_js_1 = require("../util/azure.cjs");
|
|
9
|
+
const openai_js_1 = require("../util/openai.cjs");
|
|
10
|
+
/**
|
|
11
|
+
* Class for generating embeddings using the OpenAI API. Extends the
|
|
12
|
+
* Embeddings class and implements OpenAIEmbeddingsParams and
|
|
13
|
+
* AzureOpenAIInput.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
17
|
+
* const model = new OpenAIEmbeddings();
|
|
18
|
+
* const res = await model.embedQuery(
|
|
19
|
+
* "What would be a good company name for a company that makes colorful socks?",
|
|
20
|
+
* );
|
|
21
|
+
* console.log({ res });
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
class OpenAIEmbeddings extends base_js_1.Embeddings {
|
|
26
|
+
constructor(fields, configuration) {
|
|
27
|
+
const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
|
|
28
|
+
super(fieldsWithDefaults);
|
|
29
|
+
Object.defineProperty(this, "modelName", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: "text-embedding-ada-002"
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "batchSize", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: 512
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "stripNewLines", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: true
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "timeout", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: void 0
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: void 0
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: void 0
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: void 0
|
|
76
|
+
});
|
|
77
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: void 0
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "organization", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "client", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: void 0
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "clientConfig", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: void 0
|
|
100
|
+
});
|
|
101
|
+
let apiKey = fieldsWithDefaults?.openAIApiKey ??
|
|
102
|
+
(0, env_js_1.getEnvironmentVariable)("OPENAI_API_KEY");
|
|
103
|
+
const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
|
|
104
|
+
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
|
105
|
+
if (!azureApiKey && !apiKey) {
|
|
106
|
+
throw new Error("OpenAI or Azure OpenAI API key not found");
|
|
107
|
+
}
|
|
108
|
+
const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
|
|
109
|
+
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
110
|
+
const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
|
|
111
|
+
fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
|
|
112
|
+
((0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
|
|
113
|
+
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
114
|
+
const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
|
|
115
|
+
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
|
|
116
|
+
this.azureOpenAIBasePath =
|
|
117
|
+
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
118
|
+
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
119
|
+
this.organization =
|
|
120
|
+
fieldsWithDefaults?.configuration?.organization ??
|
|
121
|
+
(0, env_js_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
|
|
122
|
+
this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
|
|
123
|
+
this.batchSize =
|
|
124
|
+
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
125
|
+
this.stripNewLines =
|
|
126
|
+
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
127
|
+
this.timeout = fieldsWithDefaults?.timeout;
|
|
128
|
+
this.azureOpenAIApiVersion = azureApiVersion;
|
|
129
|
+
this.azureOpenAIApiKey = azureApiKey;
|
|
130
|
+
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
131
|
+
this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
|
|
132
|
+
if (this.azureOpenAIApiKey) {
|
|
133
|
+
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
134
|
+
throw new Error("Azure OpenAI API instance name not found");
|
|
135
|
+
}
|
|
136
|
+
if (!this.azureOpenAIApiDeploymentName) {
|
|
137
|
+
throw new Error("Azure OpenAI API deployment name not found");
|
|
138
|
+
}
|
|
139
|
+
if (!this.azureOpenAIApiVersion) {
|
|
140
|
+
throw new Error("Azure OpenAI API version not found");
|
|
141
|
+
}
|
|
142
|
+
apiKey = apiKey ?? "";
|
|
143
|
+
}
|
|
144
|
+
this.clientConfig = {
|
|
145
|
+
apiKey,
|
|
146
|
+
organization: this.organization,
|
|
147
|
+
baseURL: configuration?.basePath,
|
|
148
|
+
dangerouslyAllowBrowser: true,
|
|
149
|
+
defaultHeaders: configuration?.baseOptions?.headers,
|
|
150
|
+
defaultQuery: configuration?.baseOptions?.params,
|
|
151
|
+
...configuration,
|
|
152
|
+
...fields?.configuration,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
157
|
+
* documents into batches and makes requests to the OpenAI API to generate
|
|
158
|
+
* embeddings.
|
|
159
|
+
* @param texts Array of documents to generate embeddings for.
|
|
160
|
+
* @returns Promise that resolves to a 2D array of embeddings for each document.
|
|
161
|
+
*/
|
|
162
|
+
async embedDocuments(texts) {
|
|
163
|
+
const batches = (0, chunk_js_1.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
164
|
+
const batchRequests = batches.map((batch) => this.embeddingWithRetry({
|
|
165
|
+
model: this.modelName,
|
|
166
|
+
input: batch,
|
|
167
|
+
}));
|
|
168
|
+
const batchResponses = await Promise.all(batchRequests);
|
|
169
|
+
const embeddings = [];
|
|
170
|
+
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
171
|
+
const batch = batches[i];
|
|
172
|
+
const { data: batchResponse } = batchResponses[i];
|
|
173
|
+
for (let j = 0; j < batch.length; j += 1) {
|
|
174
|
+
embeddings.push(batchResponse[j].embedding);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return embeddings;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Method to generate an embedding for a single document. Calls the
|
|
181
|
+
* embeddingWithRetry method with the document as the input.
|
|
182
|
+
* @param text Document to generate an embedding for.
|
|
183
|
+
* @returns Promise that resolves to an embedding for the document.
|
|
184
|
+
*/
|
|
185
|
+
async embedQuery(text) {
|
|
186
|
+
const { data } = await this.embeddingWithRetry({
|
|
187
|
+
model: this.modelName,
|
|
188
|
+
input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
|
|
189
|
+
});
|
|
190
|
+
return data[0].embedding;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Private method to make a request to the OpenAI API to generate
|
|
194
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
195
|
+
* API.
|
|
196
|
+
* @param request Request to send to the OpenAI API.
|
|
197
|
+
* @returns Promise that resolves to the response from the API.
|
|
198
|
+
*/
|
|
199
|
+
async embeddingWithRetry(request) {
|
|
200
|
+
if (!this.client) {
|
|
201
|
+
const openAIEndpointConfig = {
|
|
202
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
203
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
204
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
205
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
206
|
+
baseURL: this.clientConfig.baseURL,
|
|
207
|
+
};
|
|
208
|
+
const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
|
|
209
|
+
const params = {
|
|
210
|
+
...this.clientConfig,
|
|
211
|
+
baseURL: endpoint,
|
|
212
|
+
timeout: this.timeout,
|
|
213
|
+
maxRetries: 0,
|
|
214
|
+
};
|
|
215
|
+
if (!params.baseURL) {
|
|
216
|
+
delete params.baseURL;
|
|
217
|
+
}
|
|
218
|
+
this.client = new openai_1.OpenAI(params);
|
|
219
|
+
}
|
|
220
|
+
const requestOptions = {};
|
|
221
|
+
if (this.azureOpenAIApiKey) {
|
|
222
|
+
requestOptions.headers = {
|
|
223
|
+
"api-key": this.azureOpenAIApiKey,
|
|
224
|
+
...requestOptions.headers,
|
|
225
|
+
};
|
|
226
|
+
requestOptions.query = {
|
|
227
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
228
|
+
...requestOptions.query,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return this.caller.call(async () => {
|
|
232
|
+
try {
|
|
233
|
+
const res = await this.client.embeddings.create(request, requestOptions);
|
|
234
|
+
return res;
|
|
235
|
+
}
|
|
236
|
+
catch (e) {
|
|
237
|
+
const error = (0, openai_js_1.wrapOpenAIClientError)(e);
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
exports.OpenAIEmbeddings = OpenAIEmbeddings;
|
|
@@ -1 +1,82 @@
|
|
|
1
|
-
|
|
1
|
+
import { type ClientOptions } from "openai";
|
|
2
|
+
import { AzureOpenAIInput, LegacyOpenAIInput } from "../types/openai-types.js";
|
|
3
|
+
import { Embeddings, EmbeddingsParams } from "./base.js";
|
|
4
|
+
/**
|
|
5
|
+
* Interface for OpenAIEmbeddings parameters. Extends EmbeddingsParams and
|
|
6
|
+
* defines additional parameters specific to the OpenAIEmbeddings class.
|
|
7
|
+
*/
|
|
8
|
+
export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
|
|
9
|
+
/** Model name to use */
|
|
10
|
+
modelName: string;
|
|
11
|
+
/**
|
|
12
|
+
* Timeout to use when making requests to OpenAI.
|
|
13
|
+
*/
|
|
14
|
+
timeout?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The maximum number of documents to embed in a single request. This is
|
|
17
|
+
* limited by the OpenAI API to a maximum of 2048.
|
|
18
|
+
*/
|
|
19
|
+
batchSize?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Whether to strip new lines from the input text. This is recommended by
|
|
22
|
+
* OpenAI, but may not be suitable for all use cases.
|
|
23
|
+
*/
|
|
24
|
+
stripNewLines?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Class for generating embeddings using the OpenAI API. Extends the
|
|
28
|
+
* Embeddings class and implements OpenAIEmbeddingsParams and
|
|
29
|
+
* AzureOpenAIInput.
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
33
|
+
* const model = new OpenAIEmbeddings();
|
|
34
|
+
* const res = await model.embedQuery(
|
|
35
|
+
* "What would be a good company name for a company that makes colorful socks?",
|
|
36
|
+
* );
|
|
37
|
+
* console.log({ res });
|
|
38
|
+
*
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbeddingsParams, AzureOpenAIInput {
|
|
42
|
+
modelName: string;
|
|
43
|
+
batchSize: number;
|
|
44
|
+
stripNewLines: boolean;
|
|
45
|
+
timeout?: number;
|
|
46
|
+
azureOpenAIApiVersion?: string;
|
|
47
|
+
azureOpenAIApiKey?: string;
|
|
48
|
+
azureOpenAIApiInstanceName?: string;
|
|
49
|
+
azureOpenAIApiDeploymentName?: string;
|
|
50
|
+
azureOpenAIBasePath?: string;
|
|
51
|
+
organization?: string;
|
|
52
|
+
private client;
|
|
53
|
+
private clientConfig;
|
|
54
|
+
constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
|
|
55
|
+
verbose?: boolean;
|
|
56
|
+
openAIApiKey?: string;
|
|
57
|
+
configuration?: ClientOptions;
|
|
58
|
+
}, configuration?: ClientOptions & LegacyOpenAIInput);
|
|
59
|
+
/**
|
|
60
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
61
|
+
* documents into batches and makes requests to the OpenAI API to generate
|
|
62
|
+
* embeddings.
|
|
63
|
+
* @param texts Array of documents to generate embeddings for.
|
|
64
|
+
* @returns Promise that resolves to a 2D array of embeddings for each document.
|
|
65
|
+
*/
|
|
66
|
+
embedDocuments(texts: string[]): Promise<number[][]>;
|
|
67
|
+
/**
|
|
68
|
+
* Method to generate an embedding for a single document. Calls the
|
|
69
|
+
* embeddingWithRetry method with the document as the input.
|
|
70
|
+
* @param text Document to generate an embedding for.
|
|
71
|
+
* @returns Promise that resolves to an embedding for the document.
|
|
72
|
+
*/
|
|
73
|
+
embedQuery(text: string): Promise<number[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Private method to make a request to the OpenAI API to generate
|
|
76
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
77
|
+
* API.
|
|
78
|
+
* @param request Request to send to the OpenAI API.
|
|
79
|
+
* @returns Promise that resolves to the response from the API.
|
|
80
|
+
*/
|
|
81
|
+
private embeddingWithRetry;
|
|
82
|
+
}
|
|
@@ -1 +1,239 @@
|
|
|
1
|
-
|
|
1
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import { getEnvironmentVariable } from "../util/env.js";
|
|
3
|
+
import { chunkArray } from "../util/chunk.js";
|
|
4
|
+
import { Embeddings } from "./base.js";
|
|
5
|
+
import { getEndpoint } from "../util/azure.js";
|
|
6
|
+
import { wrapOpenAIClientError } from "../util/openai.js";
|
|
7
|
+
/**
|
|
8
|
+
* Class for generating embeddings using the OpenAI API. Extends the
|
|
9
|
+
* Embeddings class and implements OpenAIEmbeddingsParams and
|
|
10
|
+
* AzureOpenAIInput.
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
|
|
14
|
+
* const model = new OpenAIEmbeddings();
|
|
15
|
+
* const res = await model.embedQuery(
|
|
16
|
+
* "What would be a good company name for a company that makes colorful socks?",
|
|
17
|
+
* );
|
|
18
|
+
* console.log({ res });
|
|
19
|
+
*
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class OpenAIEmbeddings extends Embeddings {
|
|
23
|
+
constructor(fields, configuration) {
|
|
24
|
+
const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
|
|
25
|
+
super(fieldsWithDefaults);
|
|
26
|
+
Object.defineProperty(this, "modelName", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: "text-embedding-ada-002"
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "batchSize", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: 512
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "stripNewLines", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: true
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "timeout", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: void 0
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "organization", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "client", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "clientConfig", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: void 0
|
|
97
|
+
});
|
|
98
|
+
let apiKey = fieldsWithDefaults?.openAIApiKey ??
|
|
99
|
+
getEnvironmentVariable("OPENAI_API_KEY");
|
|
100
|
+
const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
|
|
101
|
+
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
102
|
+
if (!azureApiKey && !apiKey) {
|
|
103
|
+
throw new Error("OpenAI or Azure OpenAI API key not found");
|
|
104
|
+
}
|
|
105
|
+
const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
|
|
106
|
+
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
107
|
+
const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
|
|
108
|
+
fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
|
|
109
|
+
(getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
|
|
110
|
+
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
111
|
+
const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
|
|
112
|
+
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
113
|
+
this.azureOpenAIBasePath =
|
|
114
|
+
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
115
|
+
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
116
|
+
this.organization =
|
|
117
|
+
fieldsWithDefaults?.configuration?.organization ??
|
|
118
|
+
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
119
|
+
this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
|
|
120
|
+
this.batchSize =
|
|
121
|
+
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
122
|
+
this.stripNewLines =
|
|
123
|
+
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
124
|
+
this.timeout = fieldsWithDefaults?.timeout;
|
|
125
|
+
this.azureOpenAIApiVersion = azureApiVersion;
|
|
126
|
+
this.azureOpenAIApiKey = azureApiKey;
|
|
127
|
+
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
128
|
+
this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
|
|
129
|
+
if (this.azureOpenAIApiKey) {
|
|
130
|
+
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
131
|
+
throw new Error("Azure OpenAI API instance name not found");
|
|
132
|
+
}
|
|
133
|
+
if (!this.azureOpenAIApiDeploymentName) {
|
|
134
|
+
throw new Error("Azure OpenAI API deployment name not found");
|
|
135
|
+
}
|
|
136
|
+
if (!this.azureOpenAIApiVersion) {
|
|
137
|
+
throw new Error("Azure OpenAI API version not found");
|
|
138
|
+
}
|
|
139
|
+
apiKey = apiKey ?? "";
|
|
140
|
+
}
|
|
141
|
+
this.clientConfig = {
|
|
142
|
+
apiKey,
|
|
143
|
+
organization: this.organization,
|
|
144
|
+
baseURL: configuration?.basePath,
|
|
145
|
+
dangerouslyAllowBrowser: true,
|
|
146
|
+
defaultHeaders: configuration?.baseOptions?.headers,
|
|
147
|
+
defaultQuery: configuration?.baseOptions?.params,
|
|
148
|
+
...configuration,
|
|
149
|
+
...fields?.configuration,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
154
|
+
* documents into batches and makes requests to the OpenAI API to generate
|
|
155
|
+
* embeddings.
|
|
156
|
+
* @param texts Array of documents to generate embeddings for.
|
|
157
|
+
* @returns Promise that resolves to a 2D array of embeddings for each document.
|
|
158
|
+
*/
|
|
159
|
+
async embedDocuments(texts) {
|
|
160
|
+
const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
161
|
+
const batchRequests = batches.map((batch) => this.embeddingWithRetry({
|
|
162
|
+
model: this.modelName,
|
|
163
|
+
input: batch,
|
|
164
|
+
}));
|
|
165
|
+
const batchResponses = await Promise.all(batchRequests);
|
|
166
|
+
const embeddings = [];
|
|
167
|
+
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
168
|
+
const batch = batches[i];
|
|
169
|
+
const { data: batchResponse } = batchResponses[i];
|
|
170
|
+
for (let j = 0; j < batch.length; j += 1) {
|
|
171
|
+
embeddings.push(batchResponse[j].embedding);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return embeddings;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Method to generate an embedding for a single document. Calls the
|
|
178
|
+
* embeddingWithRetry method with the document as the input.
|
|
179
|
+
* @param text Document to generate an embedding for.
|
|
180
|
+
* @returns Promise that resolves to an embedding for the document.
|
|
181
|
+
*/
|
|
182
|
+
async embedQuery(text) {
|
|
183
|
+
const { data } = await this.embeddingWithRetry({
|
|
184
|
+
model: this.modelName,
|
|
185
|
+
input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
|
|
186
|
+
});
|
|
187
|
+
return data[0].embedding;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Private method to make a request to the OpenAI API to generate
|
|
191
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
192
|
+
* API.
|
|
193
|
+
* @param request Request to send to the OpenAI API.
|
|
194
|
+
* @returns Promise that resolves to the response from the API.
|
|
195
|
+
*/
|
|
196
|
+
async embeddingWithRetry(request) {
|
|
197
|
+
if (!this.client) {
|
|
198
|
+
const openAIEndpointConfig = {
|
|
199
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
200
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
201
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
202
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
203
|
+
baseURL: this.clientConfig.baseURL,
|
|
204
|
+
};
|
|
205
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
206
|
+
const params = {
|
|
207
|
+
...this.clientConfig,
|
|
208
|
+
baseURL: endpoint,
|
|
209
|
+
timeout: this.timeout,
|
|
210
|
+
maxRetries: 0,
|
|
211
|
+
};
|
|
212
|
+
if (!params.baseURL) {
|
|
213
|
+
delete params.baseURL;
|
|
214
|
+
}
|
|
215
|
+
this.client = new OpenAIClient(params);
|
|
216
|
+
}
|
|
217
|
+
const requestOptions = {};
|
|
218
|
+
if (this.azureOpenAIApiKey) {
|
|
219
|
+
requestOptions.headers = {
|
|
220
|
+
"api-key": this.azureOpenAIApiKey,
|
|
221
|
+
...requestOptions.headers,
|
|
222
|
+
};
|
|
223
|
+
requestOptions.query = {
|
|
224
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
225
|
+
...requestOptions.query,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return this.caller.call(async () => {
|
|
229
|
+
try {
|
|
230
|
+
const res = await this.client.embeddings.create(request, requestOptions);
|
|
231
|
+
return res;
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
const error = wrapOpenAIClientError(e);
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAIAssistantRunnable = void 0;
|
|
4
|
-
const openai_1 = require("
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
5
|
const base_js_1 = require("../../schema/runnable/base.cjs");
|
|
6
6
|
const time_js_1 = require("../../util/time.cjs");
|
|
7
7
|
const base_js_2 = require("../../tools/base.cjs");
|
|
@@ -39,7 +39,7 @@ class OpenAIAssistantRunnable extends base_js_1.Runnable {
|
|
|
39
39
|
writable: true,
|
|
40
40
|
value: void 0
|
|
41
41
|
});
|
|
42
|
-
this.client = fields.client ?? new openai_1.
|
|
42
|
+
this.client = fields.client ?? new openai_1.OpenAI(fields?.clientOptions);
|
|
43
43
|
this.assistantId = fields.assistantId;
|
|
44
44
|
this.asAgent = fields.asAgent ?? this.asAgent;
|
|
45
45
|
}
|
|
@@ -51,7 +51,7 @@ class OpenAIAssistantRunnable extends base_js_1.Runnable {
|
|
|
51
51
|
}
|
|
52
52
|
return tool;
|
|
53
53
|
}) ?? [];
|
|
54
|
-
const oaiClient = client ?? new openai_1.
|
|
54
|
+
const oaiClient = client ?? new openai_1.OpenAI(clientOptions);
|
|
55
55
|
const assistant = await oaiClient.beta.assistants.create({
|
|
56
56
|
name,
|
|
57
57
|
instructions,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ClientOptions, OpenAIClient } from "
|
|
1
|
+
import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { Runnable } from "../../schema/runnable/base.js";
|
|
3
3
|
import type { RunnableConfig } from "../../schema/runnable/config.js";
|
|
4
4
|
import type { OpenAIAssistantFinish, OpenAIAssistantAction, OpenAIToolType } from "./schema.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAIFiles = void 0;
|
|
4
|
-
const openai_1 = require("
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
5
|
const serializable_js_1 = require("../../load/serializable.cjs");
|
|
6
6
|
class OpenAIFiles extends serializable_js_1.Serializable {
|
|
7
7
|
constructor(fields) {
|
|
@@ -18,7 +18,7 @@ class OpenAIFiles extends serializable_js_1.Serializable {
|
|
|
18
18
|
writable: true,
|
|
19
19
|
value: void 0
|
|
20
20
|
});
|
|
21
|
-
this.oaiClient = fields?.client ?? new openai_1.
|
|
21
|
+
this.oaiClient = fields?.client ?? new openai_1.OpenAI(fields?.clientOptions);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Upload file
|