@langchain/openai 0.0.12 → 0.0.14
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/README.md +1 -1
- package/dist/embeddings.cjs +27 -6
- package/dist/embeddings.d.ts +10 -0
- package/dist/embeddings.js +27 -6
- package/dist/tests/embeddings.int.test.js +21 -0
- package/index.d.cts +1 -0
- package/package.json +22 -13
package/README.md
CHANGED
|
@@ -125,4 +125,4 @@ yarn lint && yarn format
|
|
|
125
125
|
|
|
126
126
|
### Adding new entrypoints
|
|
127
127
|
|
|
128
|
-
If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to `
|
|
128
|
+
If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to the `entrypoints` field in the `config` variable located inside `langchain.config.js` and run `yarn build` to generate the new entrypoint.
|
package/dist/embeddings.cjs
CHANGED
|
@@ -45,6 +45,16 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
45
45
|
writable: true,
|
|
46
46
|
value: true
|
|
47
47
|
});
|
|
48
|
+
/**
|
|
49
|
+
* The number of dimensions the resulting output embeddings should have.
|
|
50
|
+
* Only supported in `text-embedding-3` and later models.
|
|
51
|
+
*/
|
|
52
|
+
Object.defineProperty(this, "dimensions", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: void 0
|
|
57
|
+
});
|
|
48
58
|
Object.defineProperty(this, "timeout", {
|
|
49
59
|
enumerable: true,
|
|
50
60
|
configurable: true,
|
|
@@ -126,6 +136,7 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
126
136
|
this.stripNewLines =
|
|
127
137
|
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
128
138
|
this.timeout = fieldsWithDefaults?.timeout;
|
|
139
|
+
this.dimensions = fieldsWithDefaults?.dimensions;
|
|
129
140
|
this.azureOpenAIApiVersion = azureApiVersion;
|
|
130
141
|
this.azureOpenAIApiKey = azureApiKey;
|
|
131
142
|
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
@@ -162,10 +173,16 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
162
173
|
*/
|
|
163
174
|
async embedDocuments(texts) {
|
|
164
175
|
const batches = (0, chunk_array_1.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
165
|
-
const batchRequests = batches.map((batch) =>
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
176
|
+
const batchRequests = batches.map((batch) => {
|
|
177
|
+
const params = {
|
|
178
|
+
model: this.modelName,
|
|
179
|
+
input: batch,
|
|
180
|
+
};
|
|
181
|
+
if (this.dimensions) {
|
|
182
|
+
params.dimensions = this.dimensions;
|
|
183
|
+
}
|
|
184
|
+
return this.embeddingWithRetry(params);
|
|
185
|
+
});
|
|
169
186
|
const batchResponses = await Promise.all(batchRequests);
|
|
170
187
|
const embeddings = [];
|
|
171
188
|
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
@@ -184,10 +201,14 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
|
|
|
184
201
|
* @returns Promise that resolves to an embedding for the document.
|
|
185
202
|
*/
|
|
186
203
|
async embedQuery(text) {
|
|
187
|
-
const
|
|
204
|
+
const params = {
|
|
188
205
|
model: this.modelName,
|
|
189
206
|
input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
|
|
190
|
-
}
|
|
207
|
+
};
|
|
208
|
+
if (this.dimensions) {
|
|
209
|
+
params.dimensions = this.dimensions;
|
|
210
|
+
}
|
|
211
|
+
const { data } = await this.embeddingWithRetry(params);
|
|
191
212
|
return data[0].embedding;
|
|
192
213
|
}
|
|
193
214
|
/**
|
package/dist/embeddings.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ import { AzureOpenAIInput, LegacyOpenAIInput } from "./types.js";
|
|
|
8
8
|
export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
|
|
9
9
|
/** Model name to use */
|
|
10
10
|
modelName: string;
|
|
11
|
+
/**
|
|
12
|
+
* The number of dimensions the resulting output embeddings should have.
|
|
13
|
+
* Only supported in `text-embedding-3` and later models.
|
|
14
|
+
*/
|
|
15
|
+
dimensions?: number;
|
|
11
16
|
/**
|
|
12
17
|
* Timeout to use when making requests to OpenAI.
|
|
13
18
|
*/
|
|
@@ -43,6 +48,11 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
|
|
|
43
48
|
modelName: string;
|
|
44
49
|
batchSize: number;
|
|
45
50
|
stripNewLines: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* The number of dimensions the resulting output embeddings should have.
|
|
53
|
+
* Only supported in `text-embedding-3` and later models.
|
|
54
|
+
*/
|
|
55
|
+
dimensions?: number;
|
|
46
56
|
timeout?: number;
|
|
47
57
|
azureOpenAIApiVersion?: string;
|
|
48
58
|
azureOpenAIApiKey?: string;
|
package/dist/embeddings.js
CHANGED
|
@@ -42,6 +42,16 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
42
42
|
writable: true,
|
|
43
43
|
value: true
|
|
44
44
|
});
|
|
45
|
+
/**
|
|
46
|
+
* The number of dimensions the resulting output embeddings should have.
|
|
47
|
+
* Only supported in `text-embedding-3` and later models.
|
|
48
|
+
*/
|
|
49
|
+
Object.defineProperty(this, "dimensions", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: void 0
|
|
54
|
+
});
|
|
45
55
|
Object.defineProperty(this, "timeout", {
|
|
46
56
|
enumerable: true,
|
|
47
57
|
configurable: true,
|
|
@@ -123,6 +133,7 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
123
133
|
this.stripNewLines =
|
|
124
134
|
fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
|
|
125
135
|
this.timeout = fieldsWithDefaults?.timeout;
|
|
136
|
+
this.dimensions = fieldsWithDefaults?.dimensions;
|
|
126
137
|
this.azureOpenAIApiVersion = azureApiVersion;
|
|
127
138
|
this.azureOpenAIApiKey = azureApiKey;
|
|
128
139
|
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
@@ -159,10 +170,16 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
159
170
|
*/
|
|
160
171
|
async embedDocuments(texts) {
|
|
161
172
|
const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
162
|
-
const batchRequests = batches.map((batch) =>
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
173
|
+
const batchRequests = batches.map((batch) => {
|
|
174
|
+
const params = {
|
|
175
|
+
model: this.modelName,
|
|
176
|
+
input: batch,
|
|
177
|
+
};
|
|
178
|
+
if (this.dimensions) {
|
|
179
|
+
params.dimensions = this.dimensions;
|
|
180
|
+
}
|
|
181
|
+
return this.embeddingWithRetry(params);
|
|
182
|
+
});
|
|
166
183
|
const batchResponses = await Promise.all(batchRequests);
|
|
167
184
|
const embeddings = [];
|
|
168
185
|
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
@@ -181,10 +198,14 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
181
198
|
* @returns Promise that resolves to an embedding for the document.
|
|
182
199
|
*/
|
|
183
200
|
async embedQuery(text) {
|
|
184
|
-
const
|
|
201
|
+
const params = {
|
|
185
202
|
model: this.modelName,
|
|
186
203
|
input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
|
|
187
|
-
}
|
|
204
|
+
};
|
|
205
|
+
if (this.dimensions) {
|
|
206
|
+
params.dimensions = this.dimensions;
|
|
207
|
+
}
|
|
208
|
+
const { data } = await this.embeddingWithRetry(params);
|
|
188
209
|
return data[0].embedding;
|
|
189
210
|
}
|
|
190
211
|
/**
|
|
@@ -60,3 +60,24 @@ test("Test OpenAI embeddings with an invalid org throws", async () => {
|
|
|
60
60
|
]);
|
|
61
61
|
}).rejects.toThrow();
|
|
62
62
|
});
|
|
63
|
+
test("Test OpenAIEmbeddings.embedQuery with v3 and dimensions", async () => {
|
|
64
|
+
const embeddings = new OpenAIEmbeddings({
|
|
65
|
+
modelName: "text-embedding-3-small",
|
|
66
|
+
dimensions: 127,
|
|
67
|
+
});
|
|
68
|
+
const res = await embeddings.embedQuery("Hello world");
|
|
69
|
+
expect(typeof res[0]).toBe("number");
|
|
70
|
+
expect(res.length).toBe(127);
|
|
71
|
+
});
|
|
72
|
+
test("Test OpenAIEmbeddings.embedDocuments with v3 and dimensions", async () => {
|
|
73
|
+
const embeddings = new OpenAIEmbeddings({
|
|
74
|
+
modelName: "text-embedding-3-small",
|
|
75
|
+
dimensions: 127,
|
|
76
|
+
});
|
|
77
|
+
const res = await embeddings.embedDocuments(["Hello world", "Bye bye"]);
|
|
78
|
+
expect(res).toHaveLength(2);
|
|
79
|
+
expect(typeof res[0][0]).toBe("number");
|
|
80
|
+
expect(typeof res[1][0]).toBe("number");
|
|
81
|
+
expect(res[0].length).toBe(127);
|
|
82
|
+
expect(res[1].length).toBe(127);
|
|
83
|
+
});
|
package/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -16,33 +16,37 @@
|
|
|
16
16
|
"build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts",
|
|
17
17
|
"build:deps": "yarn run turbo:command build --filter=@langchain/core",
|
|
18
18
|
"build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/",
|
|
19
|
-
"build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json &&
|
|
20
|
-
"build:watch": "
|
|
21
|
-
"build:scripts": "
|
|
22
|
-
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src",
|
|
19
|
+
"build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
|
|
20
|
+
"build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
|
|
21
|
+
"build:scripts": "yarn create-entrypoints && yarn check-tree-shaking",
|
|
22
|
+
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
|
|
23
23
|
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
24
24
|
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
25
25
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
|
|
26
|
-
"clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096
|
|
26
|
+
"clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn create-entrypoints -- --pre",
|
|
27
27
|
"prepack": "yarn build",
|
|
28
28
|
"test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
29
29
|
"test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
|
|
30
30
|
"test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
|
|
31
31
|
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
|
|
32
|
-
"format": "prettier --write \"src\"",
|
|
33
|
-
"format:check": "prettier --check \"src\""
|
|
32
|
+
"format": "prettier --config .prettierrc --write \"src\"",
|
|
33
|
+
"format:check": "prettier --config .prettierrc --check \"src\"",
|
|
34
|
+
"move-cjs-to-dist": "yarn lc-build --config ./langchain.config.js --move-cjs-dist",
|
|
35
|
+
"create-entrypoints": "yarn lc-build --config ./langchain.config.js --create-entrypoints",
|
|
36
|
+
"check-tree-shaking": "yarn lc-build --config ./langchain.config.js --tree-shaking"
|
|
34
37
|
},
|
|
35
38
|
"author": "LangChain",
|
|
36
39
|
"license": "MIT",
|
|
37
40
|
"dependencies": {
|
|
38
41
|
"@langchain/core": "~0.1.13",
|
|
39
42
|
"js-tiktoken": "^1.0.7",
|
|
40
|
-
"openai": "^4.
|
|
41
|
-
"zod": "^3.22.
|
|
42
|
-
"zod-to-json-schema": "3.
|
|
43
|
+
"openai": "^4.26.0",
|
|
44
|
+
"zod": "^3.22.4",
|
|
45
|
+
"zod-to-json-schema": "^3.22.3"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"@jest/globals": "^29.5.0",
|
|
49
|
+
"@langchain/scripts": "~0.0",
|
|
46
50
|
"@swc/core": "^1.3.90",
|
|
47
51
|
"@swc/jest": "^0.2.29",
|
|
48
52
|
"dpdm": "^3.12.0",
|
|
@@ -79,7 +83,11 @@
|
|
|
79
83
|
],
|
|
80
84
|
"exports": {
|
|
81
85
|
".": {
|
|
82
|
-
"types":
|
|
86
|
+
"types": {
|
|
87
|
+
"import": "./index.d.ts",
|
|
88
|
+
"require": "./index.d.cts",
|
|
89
|
+
"default": "./index.d.ts"
|
|
90
|
+
},
|
|
83
91
|
"import": "./index.js",
|
|
84
92
|
"require": "./index.cjs"
|
|
85
93
|
},
|
|
@@ -89,6 +97,7 @@
|
|
|
89
97
|
"dist/",
|
|
90
98
|
"index.cjs",
|
|
91
99
|
"index.js",
|
|
92
|
-
"index.d.ts"
|
|
100
|
+
"index.d.ts",
|
|
101
|
+
"index.d.cts"
|
|
93
102
|
]
|
|
94
103
|
}
|