Skip to content

Commit

Permalink
add googleai embed dimension and fix vertexai parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
river0g committed Dec 16, 2024
1 parent a4e3892 commit aa46a65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
16 changes: 16 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 `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 GeminiEmbeddingConfig = z.infer<typeof GeminiEmbeddingConfigSchema>;
Expand Down Expand Up @@ -69,6 +77,8 @@ export const SUPPORTED_MODELS = {
'text-embedding-004': textEmbedding004,
};

const customDimensionsSupportedModelNames = [textEmbedding004.name];

export function defineGoogleAIEmbedder(
ai: Genkit,
name: string,
Expand Down Expand Up @@ -106,6 +116,11 @@ export function defineGoogleAIEmbedder(
info: embedder.info!,
},
async (input, options) => {
const dimensions =
customDimensionsSupportedModelNames.includes(embedder.name) &&
options?.dimensions
? options.dimensions
: undefined;
const client = new GoogleGenerativeAI(apiKey!).getGenerativeModel({
model:
options?.version ||
Expand All @@ -122,6 +137,7 @@ export function defineGoogleAIEmbedder(
role: '',
parts: [{ text: doc.text }],
},
outputDimensionality: dimensions,
} as EmbedContentRequest);
const values = response.embedding.values;
return { embedding: values };
Expand Down
2 changes: 1 addition & 1 deletion js/plugins/vertexai/src/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function defineVertexAIEmbedder(
title: options?.title,
};
}),
dimensions ? { outputDimensionality: dimensions } : {}
{ outputDimensionality: dimensions }
);
return {
embeddings: response.predictions.map((p) => ({
Expand Down
4 changes: 2 additions & 2 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,7 +43,7 @@ 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;

Expand Down

0 comments on commit aa46a65

Please sign in to comment.