Skip to content

Commit

Permalink
add custom dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
river0g committed Dec 16, 2024
1 parent 2238a15 commit a4e3892
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion js/plugins/vertexai/src/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export const VertexEmbeddingConfigSchema = z.object({
title: z.string().optional(),
location: z.string().optional(),
version: z.string().optional(),
/**
* The `dimensions` parameter allows you to specify the dimensionality of the embedding output.
* By default, the model generates embeddings with 768 dimensions. Models such as
* `text-embedding-004`, `text-embedding-005`, and `text-multilingual-embedding-002`
* allow the output dimensionality to be adjusted between 1 and 768.
* By selecting a smaller output dimensionality, users can save memory and storage space, leading to more efficient computations.
**/
dimensions: z.number().min(1).max(768).optional(),
});

export type VertexEmbeddingConfig = z.infer<typeof VertexEmbeddingConfigSchema>;
Expand Down Expand Up @@ -83,6 +91,12 @@ export const SUPPORTED_EMBEDDER_MODELS: Record<string, EmbedderReference> = {
// ]),
};

const customDimensionsSupportedModelNames = [
textEmbedding004.name,
textEmbedding005.name,
textMultilingualEmbedding002.name,
];

interface EmbeddingInstance {
task_type?: TaskType;
content: string;
Expand Down Expand Up @@ -137,6 +151,11 @@ export function defineVertexAIEmbedder(
info: embedder.info!,
},
async (input, options) => {
const dimensions =
customDimensionsSupportedModelNames.includes(embedder.name) &&
options?.dimensions
? options.dimensions
: undefined;
const predictClient = predictClientFactory(options);
const response = await predictClient(
input.map((i) => {
Expand All @@ -145,7 +164,8 @@ export function defineVertexAIEmbedder(
task_type: options?.taskType,
title: options?.title,
};
})
}),
dimensions ? { outputDimensionality: dimensions } : {}
);
return {
embeddings: response.predictions.map((p) => ({
Expand Down

0 comments on commit a4e3892

Please sign in to comment.