Merge pull request #2163 from YouJiacheng/embed-add-maxBatchSize-option
Add `maxBatchSize` option for EmbeddingsProvider.
This commit is contained in:
commit
401b0e71f5
7 changed files with 21 additions and 49 deletions
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -812,6 +812,7 @@ export interface EmbedOptions {
|
|||
apiVersion?: string;
|
||||
requestOptions?: RequestOptions;
|
||||
maxChunkSize?: number;
|
||||
maxBatchSize?: number;
|
||||
// AWS options
|
||||
profile?: string;
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,9 @@ abstract class BaseEmbeddingsProvider implements IBaseEmbeddingsProvider {
|
|||
}
|
||||
}
|
||||
defaultOptions?: EmbedOptions | undefined;
|
||||
maxBatchSize?: number | undefined;
|
||||
get maxBatchSize(): number | undefined {
|
||||
return this.options.maxBatchSize ?? (this.constructor as typeof BaseEmbeddingsProvider).maxBatchSize;
|
||||
}
|
||||
|
||||
abstract embed(chunks: string[]): Promise<number[][]>;
|
||||
|
||||
|
|
@ -53,7 +55,7 @@ abstract class BaseEmbeddingsProvider implements IBaseEmbeddingsProvider {
|
|||
return this.options.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;
|
||||
}
|
||||
|
||||
static getBatchedChunks(chunks: string[]): string[][] {
|
||||
getBatchedChunks(chunks: string[]): string[][] {
|
||||
if (!this.maxBatchSize) {
|
||||
console.warn(
|
||||
`${this.getBatchedChunks.name} should only be called if 'maxBatchSize' is defined`,
|
||||
|
|
@ -62,15 +64,10 @@ abstract class BaseEmbeddingsProvider implements IBaseEmbeddingsProvider {
|
|||
return [chunks];
|
||||
}
|
||||
|
||||
if (chunks.length > this.maxBatchSize) {
|
||||
return [chunks];
|
||||
}
|
||||
|
||||
const batchedChunks = [];
|
||||
|
||||
for (let i = 0; i < chunks.length; i += this.maxBatchSize) {
|
||||
const batchSizedChunk = chunks.slice(i, i + this.maxBatchSize);
|
||||
batchedChunks.push(batchSizedChunk);
|
||||
batchedChunks.push(chunks.slice(i, i + this.maxBatchSize));
|
||||
}
|
||||
|
||||
return batchedChunks;
|
||||
|
|
|
|||
|
|
@ -18,16 +18,7 @@ class CohereEmbeddingsProvider extends BaseEmbeddingsProvider {
|
|||
this.options.apiBase += "/";
|
||||
}
|
||||
|
||||
const batchedChunks = [];
|
||||
for (
|
||||
let i = 0;
|
||||
i < chunks.length;
|
||||
i += CohereEmbeddingsProvider.maxBatchSize
|
||||
) {
|
||||
batchedChunks.push(
|
||||
chunks.slice(i, i + CohereEmbeddingsProvider.maxBatchSize),
|
||||
);
|
||||
}
|
||||
const batchedChunks = this.getBatchedChunks(chunks);
|
||||
return (
|
||||
await Promise.all(
|
||||
batchedChunks.map(async (batch) => {
|
||||
|
|
|
|||
|
|
@ -24,16 +24,7 @@ class FreeTrialEmbeddingsProvider extends BaseEmbeddingsProvider {
|
|||
}
|
||||
|
||||
async embed(chunks: string[]) {
|
||||
const batchedChunks = [];
|
||||
for (
|
||||
let i = 0;
|
||||
i < chunks.length;
|
||||
i += FreeTrialEmbeddingsProvider.maxBatchSize
|
||||
) {
|
||||
batchedChunks.push(
|
||||
chunks.slice(i, i + FreeTrialEmbeddingsProvider.maxBatchSize),
|
||||
);
|
||||
}
|
||||
const batchedChunks = this.getBatchedChunks(chunks);
|
||||
return (
|
||||
await Promise.all(
|
||||
batchedChunks.map(async (batch) => {
|
||||
|
|
|
|||
|
|
@ -55,10 +55,10 @@ class GeminiEmbeddingsProvider extends BaseEmbeddingsProvider {
|
|||
}
|
||||
|
||||
async embed(chunks: string[]) {
|
||||
const batches = GeminiEmbeddingsProvider.getBatchedChunks(chunks);
|
||||
const batchedChunks = this.getBatchedChunks(chunks);
|
||||
|
||||
const results = await Promise.all(
|
||||
batches.map((batch) => this.getSingleBatchEmbedding(batch)),
|
||||
batchedChunks.map((batch) => this.getSingleBatchEmbedding(batch)),
|
||||
);
|
||||
return results.flat();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@ import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js";
|
|||
|
||||
class HuggingFaceTEIEmbeddingsProvider extends BaseEmbeddingsProvider {
|
||||
static providerName: EmbeddingsProviderName = "huggingface-tei";
|
||||
maxBatchSize = 32;
|
||||
private _maxBatchSize?: number;
|
||||
get maxBatchSize(): number | undefined {
|
||||
return this._maxBatchSize ?? super.maxBatchSize;
|
||||
}
|
||||
|
||||
static defaultOptions: Partial<EmbedOptions> | undefined = {
|
||||
apiBase: "http://localhost:8080",
|
||||
|
|
@ -24,18 +27,16 @@ class HuggingFaceTEIEmbeddingsProvider extends BaseEmbeddingsProvider {
|
|||
}
|
||||
this.doInfoRequest().then((response) => {
|
||||
this.options.model = response.model_id;
|
||||
this.maxBatchSize = response.max_client_batch_size;
|
||||
this._maxBatchSize = response.max_client_batch_size;
|
||||
});
|
||||
}
|
||||
|
||||
async embed(chunks: string[]) {
|
||||
const promises = [];
|
||||
for (let i = 0; i < chunks.length; i += this.maxBatchSize) {
|
||||
promises.push(
|
||||
this.doEmbedRequest(chunks.slice(i, i + this.maxBatchSize)),
|
||||
);
|
||||
}
|
||||
const results = await Promise.all(promises);
|
||||
const batchedChunks = this.getBatchedChunks(chunks);
|
||||
|
||||
const results = await Promise.all(
|
||||
batchedChunks.map((batch) => this.doEmbedRequest(batch)),
|
||||
);
|
||||
return results.flat();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,16 +35,7 @@ class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider {
|
|||
}
|
||||
|
||||
async embed(chunks: string[]) {
|
||||
const batchedChunks = [];
|
||||
for (
|
||||
let i = 0;
|
||||
i < chunks.length;
|
||||
i += OpenAIEmbeddingsProvider.maxBatchSize
|
||||
) {
|
||||
batchedChunks.push(
|
||||
chunks.slice(i, i + OpenAIEmbeddingsProvider.maxBatchSize),
|
||||
);
|
||||
}
|
||||
const batchedChunks = this.getBatchedChunks(chunks);
|
||||
return (
|
||||
await Promise.all(
|
||||
batchedChunks.map(async (batch) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue