mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(nodejs): batch OpenAI embedding requests to 2048 inputs (#4833)
`OpenAIEmbeddingFunction.computeSourceEmbeddings` in the Node SDK sent every row in a single `embeddings.create` call. `makeArrowTable` passes the whole source column at once, and the OpenAI embeddings endpoint accepts at most 2048 inputs per request, so `table.add` (or `createTable`) with more than 2048 rows and an `openai` embedding function fails with a 400. This splits the input into chunks of at most 2048 and concatenates the results in order. Inputs of 2048 rows or fewer still make one request, as before. Test: `nodejs/__test__/embedding_openai.test.ts` mocks `openai` (rejecting requests with more than 2048 inputs) and checks that 5000 rows produce 5000 embeddings, in order, over three calls (2048, 2048, 904). It fails without the change and passes with it. This change was prepared with AI assistance (Claude Code).
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
const create = jest.fn();
|
||||
|
||||
jest.mock("openai", () =>
|
||||
jest.fn().mockImplementation(() => ({ embeddings: { create } })),
|
||||
);
|
||||
|
||||
import { OpenAIEmbeddingFunction } from "../lancedb/embedding/openai";
|
||||
import { getRegistry } from "../lancedb/embedding/registry";
|
||||
|
||||
describe("OpenAIEmbeddingFunction", () => {
|
||||
beforeEach(() => {
|
||||
create.mockReset();
|
||||
create.mockImplementation(async ({ input }: { input: string[] }) => {
|
||||
if (input.length > 2048) {
|
||||
throw new Error("400 max 2048 inputs per request");
|
||||
}
|
||||
return { data: input.map((_, i) => ({ embedding: [i] })) };
|
||||
});
|
||||
});
|
||||
|
||||
it("splits source embeddings into batches of at most 2048 inputs", async () => {
|
||||
const registry = getRegistry();
|
||||
registry.setVar("openai_api_key", "sk-test");
|
||||
const func = new OpenAIEmbeddingFunction({
|
||||
apiKey: "$var:openai_api_key",
|
||||
model: "text-embedding-3-small",
|
||||
});
|
||||
const data = Array.from({ length: 5000 }, (_, i) => `row ${i}`);
|
||||
|
||||
const embeddings = await func.computeSourceEmbeddings(data);
|
||||
|
||||
expect(embeddings).toHaveLength(5000);
|
||||
expect(create.mock.calls.map(([args]) => args.input.length)).toEqual([
|
||||
2048, 2048, 904,
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,9 @@ import { Float, Float32 } from "../arrow";
|
||||
import { EmbeddingFunction } from "./embedding_function";
|
||||
import { registerBuiltIn } from "./registry";
|
||||
|
||||
// Maximum number of inputs the OpenAI embeddings API accepts per request.
|
||||
const MAX_INPUTS_PER_REQUEST = 2048;
|
||||
|
||||
export type OpenAIOptions = {
|
||||
apiKey: string;
|
||||
model: EmbeddingCreateParams["model"];
|
||||
@@ -75,14 +78,16 @@ export class OpenAIEmbeddingFunction extends EmbeddingFunction<
|
||||
}
|
||||
|
||||
async computeSourceEmbeddings(data: string[]): Promise<number[][]> {
|
||||
const response = await this.#openai.embeddings.create({
|
||||
model: this.#modelName,
|
||||
input: data,
|
||||
});
|
||||
|
||||
const embeddings: number[][] = [];
|
||||
for (let i = 0; i < response.data.length; i++) {
|
||||
embeddings.push(response.data[i].embedding);
|
||||
// The OpenAI embeddings endpoint accepts at most 2048 inputs per request.
|
||||
for (let start = 0; start < data.length; start += MAX_INPUTS_PER_REQUEST) {
|
||||
const response = await this.#openai.embeddings.create({
|
||||
model: this.#modelName,
|
||||
input: data.slice(start, start + MAX_INPUTS_PER_REQUEST),
|
||||
});
|
||||
for (let i = 0; i < response.data.length; i++) {
|
||||
embeddings.push(response.data[i].embedding);
|
||||
}
|
||||
}
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user