@langchain/openai
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -12,2 +12,7 @@ import { type ClientOptions } from "openai"; | ||
/** | ||
* The number of dimensions the resulting output embeddings should have. | ||
* Only supported in `text-embedding-3` and later models. | ||
*/ | ||
dimensions?: number; | ||
/** | ||
* Timeout to use when making requests to OpenAI. | ||
@@ -47,2 +52,7 @@ */ | ||
stripNewLines: boolean; | ||
/** | ||
* The number of dimensions the resulting output embeddings should have. | ||
* Only supported in `text-embedding-3` and later models. | ||
*/ | ||
dimensions?: number; | ||
timeout?: number; | ||
@@ -49,0 +59,0 @@ azureOpenAIApiVersion?: string; |
@@ -45,2 +45,12 @@ import { OpenAI as OpenAIClient } from "openai"; | ||
}); | ||
/** | ||
* The number of dimensions the resulting output embeddings should have. | ||
* Only supported in `text-embedding-3` and later models. | ||
*/ | ||
Object.defineProperty(this, "dimensions", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "timeout", { | ||
@@ -127,2 +137,3 @@ enumerable: true, | ||
this.timeout = fieldsWithDefaults?.timeout; | ||
this.dimensions = fieldsWithDefaults?.dimensions; | ||
this.azureOpenAIApiVersion = azureApiVersion; | ||
@@ -164,6 +175,12 @@ this.azureOpenAIApiKey = azureApiKey; | ||
const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize); | ||
const batchRequests = batches.map((batch) => this.embeddingWithRetry({ | ||
model: this.modelName, | ||
input: batch, | ||
})); | ||
const batchRequests = batches.map((batch) => { | ||
const params = { | ||
model: this.modelName, | ||
input: batch, | ||
}; | ||
if (this.dimensions) { | ||
params.dimensions = this.dimensions; | ||
} | ||
return this.embeddingWithRetry(params); | ||
}); | ||
const batchResponses = await Promise.all(batchRequests); | ||
@@ -187,6 +204,10 @@ const embeddings = []; | ||
async embedQuery(text) { | ||
const { data } = await this.embeddingWithRetry({ | ||
const params = { | ||
model: this.modelName, | ||
input: this.stripNewLines ? text.replace(/\n/g, " ") : text, | ||
}); | ||
}; | ||
if (this.dimensions) { | ||
params.dimensions = this.dimensions; | ||
} | ||
const { data } = await this.embeddingWithRetry(params); | ||
return data[0].embedding; | ||
@@ -193,0 +214,0 @@ } |
@@ -63,1 +63,22 @@ import { test, expect } from "@jest/globals"; | ||
}); | ||
test("Test OpenAIEmbeddings.embedQuery with v3 and dimensions", async () => { | ||
const embeddings = new OpenAIEmbeddings({ | ||
modelName: "text-embedding-3-small", | ||
dimensions: 127, | ||
}); | ||
const res = await embeddings.embedQuery("Hello world"); | ||
expect(typeof res[0]).toBe("number"); | ||
expect(res.length).toBe(127); | ||
}); | ||
test("Test OpenAIEmbeddings.embedDocuments with v3 and dimensions", async () => { | ||
const embeddings = new OpenAIEmbeddings({ | ||
modelName: "text-embedding-3-small", | ||
dimensions: 127, | ||
}); | ||
const res = await embeddings.embedDocuments(["Hello world", "Bye bye"]); | ||
expect(res).toHaveLength(2); | ||
expect(typeof res[0][0]).toBe("number"); | ||
expect(typeof res[1][0]).toBe("number"); | ||
expect(res[0].length).toBe(127); | ||
expect(res[1].length).toBe(127); | ||
}); |
{ | ||
"name": "@langchain/openai", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "OpenAI integrations for LangChain.js", | ||
@@ -22,3 +22,3 @@ "type": "module", | ||
"build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js", | ||
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src", | ||
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/", | ||
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts", | ||
@@ -33,4 +33,4 @@ "lint": "yarn lint:eslint && yarn lint:dpdm", | ||
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", | ||
"format": "prettier --write \"src\"", | ||
"format:check": "prettier --check \"src\"" | ||
"format": "prettier --config .prettierrc --write \"src\" \"scripts\"", | ||
"format:check": "prettier --config .prettierrc --check \"src\" \"scripts\"" | ||
}, | ||
@@ -42,5 +42,5 @@ "author": "LangChain", | ||
"js-tiktoken": "^1.0.7", | ||
"openai": "^4.24.2", | ||
"zod": "^3.22.3", | ||
"zod-to-json-schema": "3.20.3" | ||
"openai": "^4.26.0", | ||
"zod": "^3.22.4", | ||
"zod-to-json-schema": "^3.22.3" | ||
}, | ||
@@ -47,0 +47,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
255259
6528
- Removedzod-to-json-schema@3.20.3(transitive)
Updatedopenai@^4.26.0
Updatedzod@^3.22.4
Updatedzod-to-json-schema@^3.22.3