@langchain/openai 0.0.12 → 0.0.13

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.
@@ -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) => this.embeddingWithRetry({
166
- model: this.modelName,
167
- input: batch,
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 { data } = await this.embeddingWithRetry({
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
  /**
@@ -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;
@@ -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) => this.embeddingWithRetry({
163
- model: this.modelName,
164
- input: batch,
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 { data } = await this.embeddingWithRetry({
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -19,7 +19,7 @@
19
19
  "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs",
20
20
  "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
21
21
  "build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
22
- "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src",
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",
@@ -29,17 +29,17 @@
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\" \"scripts\"",
33
+ "format:check": "prettier --config .prettierrc --check \"src\" \"scripts\""
34
34
  },
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "@langchain/core": "~0.1.13",
39
39
  "js-tiktoken": "^1.0.7",
40
- "openai": "^4.24.2",
41
- "zod": "^3.22.3",
42
- "zod-to-json-schema": "3.20.3"
40
+ "openai": "^4.26.0",
41
+ "zod": "^3.22.4",
42
+ "zod-to-json-schema": "^3.22.3"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@jest/globals": "^29.5.0",