Skip to content

Commit

Permalink
feat(js/plugins): added custom dimensions parameter to text-embedding (
Browse files Browse the repository at this point in the history
  • Loading branch information
river0g authored Dec 16, 2024
1 parent 65ef534 commit 67b6151
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 9 additions & 0 deletions js/plugins/googleai/src/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export const GeminiEmbeddingConfigSchema = z.object({
taskType: TaskTypeSchema.optional(),
title: z.string().optional(),
version: z.string().optional(),
/**
* The `outputDimensionality` 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.
**/
outputDimensionality: z.number().min(1).max(768).optional(),
});

export type GeminiEmbeddingConfig = z.infer<typeof GeminiEmbeddingConfigSchema>;
Expand Down Expand Up @@ -122,6 +130,7 @@ export function defineGoogleAIEmbedder(
role: '',
parts: [{ text: doc.text }],
},
outputDimensionality: options?.outputDimensionality,
} as EmbedContentRequest);
const values = response.embedding.values;
return { embedding: values };
Expand Down
11 changes: 10 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 `outputDimensionality` 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.
**/
outputDimensionality: z.number().min(1).max(768).optional(),
});

export type VertexEmbeddingConfig = z.infer<typeof VertexEmbeddingConfigSchema>;
Expand Down Expand Up @@ -145,7 +153,8 @@ export function defineVertexAIEmbedder(
task_type: options?.taskType,
title: options?.title,
};
})
}),
{ outputDimensionality: options?.outputDimensionality }
);
return {
embeddings: response.predictions.map((p) => ({
Expand Down
6 changes: 3 additions & 3 deletions js/plugins/vertexai/src/predict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface PredictionResponse<R> {

export type PredictClient<I = unknown, R = unknown, P = unknown> = (
instances: I[],
parameters?: P
parameters: P
) => Promise<PredictionResponse<R>>;

export function predictModel<I = unknown, R = unknown, P = unknown>(
Expand All @@ -43,14 +43,14 @@ export function predictModel<I = unknown, R = unknown, P = unknown>(
): PredictClient<I, R, P> {
return async (
instances: I[],
parameters?: P
parameters: P
): Promise<PredictionResponse<R>> => {
const fetch = (await import('node-fetch')).default;

const accessToken = await auth.getAccessToken();
const req = {
instances,
parameters: parameters || {},
parameters,
};

const response = await fetch(
Expand Down

0 comments on commit 67b6151

Please sign in to comment.