langchain 0.0.197-rc.1 → 0.0.198
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/llama_cpp.cjs +24 -0
- package/dist/chat_models/llama_cpp.d.ts +3 -1
- package/dist/chat_models/llama_cpp.js +24 -0
- 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/document_loaders/fs/pptx.cjs +39 -0
- package/dist/document_loaders/fs/pptx.d.ts +23 -0
- package/dist/document_loaders/fs/pptx.js +35 -0
- 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 +35 -3
- package/dist/experimental/openai_assistant/index.d.ts +27 -1
- package/dist/experimental/openai_assistant/index.js +33 -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/experimental/tools/pyinterpreter.cjs +248 -0
- package/dist/experimental/tools/pyinterpreter.d.ts +18 -0
- package/dist/experimental/tools/pyinterpreter.js +244 -0
- package/dist/graphs/neo4j_graph.cjs +49 -14
- package/dist/graphs/neo4j_graph.d.ts +30 -0
- package/dist/graphs/neo4j_graph.js +49 -14
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/llms/hf.cjs +13 -2
- package/dist/llms/hf.d.ts +5 -0
- package/dist/llms/hf.js +13 -2
- package/dist/llms/llama_cpp.cjs +17 -3
- package/dist/llms/llama_cpp.d.ts +4 -1
- package/dist/llms/llama_cpp.js +17 -3
- 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/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/output_parsers/json.cjs +4 -0
- package/dist/output_parsers/json.js +4 -0
- 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/dist/vectorstores/clickhouse.cjs +286 -0
- package/dist/vectorstores/clickhouse.d.ts +126 -0
- package/dist/vectorstores/clickhouse.js +259 -0
- package/dist/vectorstores/pgvector.cjs +142 -18
- package/dist/vectorstores/pgvector.d.ts +21 -0
- package/dist/vectorstores/pgvector.js +142 -18
- package/dist/vectorstores/weaviate.cjs +45 -2
- package/dist/vectorstores/weaviate.d.ts +27 -1
- package/dist/vectorstores/weaviate.js +45 -2
- package/document_loaders/fs/pptx.cjs +1 -0
- package/document_loaders/fs/pptx.d.ts +1 -0
- package/document_loaders/fs/pptx.js +1 -0
- package/experimental/tools/pyinterpreter.cjs +1 -0
- package/experimental/tools/pyinterpreter.d.ts +1 -0
- package/experimental/tools/pyinterpreter.js +1 -0
- package/package.json +41 -9
- package/vectorstores/clickhouse.cjs +1 -0
- package/vectorstores/clickhouse.d.ts +1 -0
- package/vectorstores/clickhouse.js +1 -0
|
@@ -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;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PPTXLoader = void 0;
|
|
4
|
+
const officeparser_1 = require("officeparser");
|
|
5
|
+
const document_js_1 = require("../../document.cjs");
|
|
6
|
+
const buffer_js_1 = require("./buffer.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* A class that extends the `BufferLoader` class. It represents a document
|
|
9
|
+
* loader that loads documents from PDF files.
|
|
10
|
+
*/
|
|
11
|
+
class PPTXLoader extends buffer_js_1.BufferLoader {
|
|
12
|
+
constructor(filePathOrBlob) {
|
|
13
|
+
super(filePathOrBlob);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A method that takes a `raw` buffer and `metadata` as parameters and
|
|
17
|
+
* returns a promise that resolves to an array of `Document` instances. It
|
|
18
|
+
* uses the `parseOfficeAsync` function from the `officeparser` module to extract
|
|
19
|
+
* the raw text content from the buffer. If the extracted powerpoint content is
|
|
20
|
+
* empty, it returns an empty array. Otherwise, it creates a new
|
|
21
|
+
* `Document` instance with the extracted powerpoint content and the provided
|
|
22
|
+
* metadata, and returns it as an array.
|
|
23
|
+
* @param raw The buffer to be parsed.
|
|
24
|
+
* @param metadata The metadata of the document.
|
|
25
|
+
* @returns A promise that resolves to an array of `Document` instances.
|
|
26
|
+
*/
|
|
27
|
+
async parse(raw, metadata) {
|
|
28
|
+
const pptx = await (0, officeparser_1.parseOfficeAsync)(raw, { outputErrorToConsole: true });
|
|
29
|
+
if (!pptx)
|
|
30
|
+
return [];
|
|
31
|
+
return [
|
|
32
|
+
new document_js_1.Document({
|
|
33
|
+
pageContent: pptx,
|
|
34
|
+
metadata,
|
|
35
|
+
}),
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.PPTXLoader = PPTXLoader;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Document } from "../../document.js";
|
|
3
|
+
import { BufferLoader } from "./buffer.js";
|
|
4
|
+
/**
|
|
5
|
+
* A class that extends the `BufferLoader` class. It represents a document
|
|
6
|
+
* loader that loads documents from PDF files.
|
|
7
|
+
*/
|
|
8
|
+
export declare class PPTXLoader extends BufferLoader {
|
|
9
|
+
constructor(filePathOrBlob: string | Blob);
|
|
10
|
+
/**
|
|
11
|
+
* A method that takes a `raw` buffer and `metadata` as parameters and
|
|
12
|
+
* returns a promise that resolves to an array of `Document` instances. It
|
|
13
|
+
* uses the `parseOfficeAsync` function from the `officeparser` module to extract
|
|
14
|
+
* the raw text content from the buffer. If the extracted powerpoint content is
|
|
15
|
+
* empty, it returns an empty array. Otherwise, it creates a new
|
|
16
|
+
* `Document` instance with the extracted powerpoint content and the provided
|
|
17
|
+
* metadata, and returns it as an array.
|
|
18
|
+
* @param raw The buffer to be parsed.
|
|
19
|
+
* @param metadata The metadata of the document.
|
|
20
|
+
* @returns A promise that resolves to an array of `Document` instances.
|
|
21
|
+
*/
|
|
22
|
+
parse(raw: Buffer, metadata: Document["metadata"]): Promise<Document[]>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parseOfficeAsync } from "officeparser";
|
|
2
|
+
import { Document } from "../../document.js";
|
|
3
|
+
import { BufferLoader } from "./buffer.js";
|
|
4
|
+
/**
|
|
5
|
+
* A class that extends the `BufferLoader` class. It represents a document
|
|
6
|
+
* loader that loads documents from PDF files.
|
|
7
|
+
*/
|
|
8
|
+
export class PPTXLoader extends BufferLoader {
|
|
9
|
+
constructor(filePathOrBlob) {
|
|
10
|
+
super(filePathOrBlob);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A method that takes a `raw` buffer and `metadata` as parameters and
|
|
14
|
+
* returns a promise that resolves to an array of `Document` instances. It
|
|
15
|
+
* uses the `parseOfficeAsync` function from the `officeparser` module to extract
|
|
16
|
+
* the raw text content from the buffer. If the extracted powerpoint content is
|
|
17
|
+
* empty, it returns an empty array. Otherwise, it creates a new
|
|
18
|
+
* `Document` instance with the extracted powerpoint content and the provided
|
|
19
|
+
* metadata, and returns it as an array.
|
|
20
|
+
* @param raw The buffer to be parsed.
|
|
21
|
+
* @param metadata The metadata of the document.
|
|
22
|
+
* @returns A promise that resolves to an array of `Document` instances.
|
|
23
|
+
*/
|
|
24
|
+
async parse(raw, metadata) {
|
|
25
|
+
const pptx = await parseOfficeAsync(raw, { outputErrorToConsole: true });
|
|
26
|
+
if (!pptx)
|
|
27
|
+
return [];
|
|
28
|
+
return [
|
|
29
|
+
new Document({
|
|
30
|
+
pageContent: pptx,
|
|
31
|
+
metadata,
|
|
32
|
+
}),
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -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
|
+
}
|