Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions js/plugins/vertexai/src/evaluation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ import { vertexEvaluators } from './evaluation.js';
import { PluginOptions } from './types.js';
export { VertexAIEvaluationMetricType } from './types.js';
export { PluginOptions };

/**
* Add Google Cloud Vertex AI Rerankers API to Genkit.
*/
export function vertexAIEvaluation(options: PluginOptions): GenkitPlugin {
return genkitPlugin('vertexAIEvaluation', async (ai: Genkit) => {
const { projectId, location, authClient } = await getDerivedParams(options);

const metrics =
options?.evaluation && options.evaluation.metrics.length > 0
? options.evaluation.metrics
: [];

vertexEvaluators(ai, authClient, metrics, projectId, location);
vertexEvaluators(ai, authClient, options.metrics, projectId, location);
});
}
5 changes: 1 addition & 4 deletions js/plugins/vertexai/src/evaluation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ export type VertexAIEvaluationMetric =

/** Options specific to evaluation configuration */
export interface EvaluationOptions {
/** Configure Vertex AI evaluators */
evaluation?: {
metrics: VertexAIEvaluationMetric[];
};
metrics: VertexAIEvaluationMetric[];
}

export interface PluginOptions extends CommonPluginOptions, EvaluationOptions {}
6 changes: 3 additions & 3 deletions js/plugins/vertexai/src/modelgarden/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import {
modelGardenOpenaiCompatibleModel,
} from './model_garden.js';
import { PluginOptions } from './types.js';

/**
* Add Google Cloud Vertex AI Rerankers API to Genkit.
*/
export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
return genkitPlugin('vertexAIModelGarden', async (ai: Genkit) => {
const { projectId, location, authClient } = await getDerivedParams(options);

const mgModels = options?.modelGardenModels || options?.modelGarden?.models;
mgModels!.forEach((m) => {
options.models.forEach((m) => {
const anthropicEntry = Object.entries(SUPPORTED_ANTHROPIC_MODELS).find(
([_, value]) => value.name === m.name
);
Expand All @@ -49,7 +49,7 @@ export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
projectId,
location,
authClient,
options.modelGarden?.openAiBaseUrlTemplate
options.openAiBaseUrlTemplate
);
return;
}
Expand Down
23 changes: 3 additions & 20 deletions js/plugins/vertexai/src/modelgarden/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,17 @@
import { ModelReference } from 'genkit';
import { CommonPluginOptions } from '../common/types.js';

export enum VertexAIEvaluationMetricType {
// Update genkit/docs/plugins/vertex-ai.md when modifying the list of enums
BLEU = 'BLEU',
ROUGE = 'ROUGE',
FLUENCY = 'FLEUNCY',
SAFETY = 'SAFETY',
GROUNDEDNESS = 'GROUNDEDNESS',
SUMMARIZATION_QUALITY = 'SUMMARIZATION_QUALITY',
SUMMARIZATION_HELPFULNESS = 'SUMMARIZATION_HELPFULNESS',
SUMMARIZATION_VERBOSITY = 'SUMMARIZATION_VERBOSITY',
}

/**
* Evaluation metric config. Use `metricSpec` to define the behavior of the metric.
* The value of `metricSpec` will be included in the request to the API. See the API documentation
* for details on the possible values of `metricSpec` for each metric.
* https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/evaluation#parameter-list
*/

/** Options specific to Model Garden configuration */
export interface ModelGardenOptions {
/**
* @deprecated use `modelGarden.models`
*/
modelGardenModels?: ModelReference<any>[];
modelGarden?: {
models: ModelReference<any>[];
openAiBaseUrlTemplate?: string;
};
models: ModelReference<any>[];
openAiBaseUrlTemplate?: string;
}

export interface PluginOptions
Expand Down
4 changes: 3 additions & 1 deletion js/plugins/vertexai/src/rerankers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function vertexAIRerankers(options: PluginOptions): GenkitPlugin {
projectId,
location,
authClient,
rerankOptions: options.rerankOptions,
rerankOptions: options.rerankers.map((o) =>
typeof o === 'string' ? { model: o } : o
),
});
});
}
5 changes: 4 additions & 1 deletion js/plugins/vertexai/src/rerankers/reranker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ export async function vertexAiRerankers(
const rerankOptions = options.rerankOptions;

if (rerankOptions.length === 0) {
return;
throw new Error('Provide at least one reranker configuration.');
}

const auth = options.authClient;
const client = await auth.getClient();
const projectId = options.projectId;

for (const rerankOption of rerankOptions) {
if (!rerankOption.name && !rerankOption.model) {
throw new Error('At least one of name or model must be provided.');
}
ai.defineReranker(
{
name: `vertexai/${rerankOption.name || rerankOption.model}`,
Expand Down
15 changes: 7 additions & 8 deletions js/plugins/vertexai/src/rerankers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ export type VertexAIRerankerOptions = z.infer<
typeof VertexAIRerankerOptionsSchema
>;

// Define the structure for each individual reranker configuration
export const VertexRerankerConfigSchema = z.object({
name: z.string().optional().describe('Name of the reranker'), // Optional: Name of the reranker
model: z.string().optional().describe('Model name for reranking'), // Optional: Model name, defaults to a pre-defined model
});

export type VertexRerankerConfig = z.infer<typeof VertexRerankerConfigSchema>;
export interface VertexRerankerConfig {
// Optional: Name of the reranker
name?: string;
// Optional: Model name, defaults to a pre-defined model
model?: string;
}

export interface VertexRerankOptions {
authClient: GoogleAuth;
Expand All @@ -50,7 +49,7 @@ export interface VertexRerankOptions {

export interface RerankerOptions {
/** Configure reranker options */
rerankOptions: VertexRerankerConfig[];
rerankers: (string | VertexRerankerConfig)[];
}

export interface PluginOptions extends CommonPluginOptions, RerankerOptions {}
4 changes: 1 addition & 3 deletions js/testapps/anthropic-models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const ai = genkit({
}),
vertexAIModelGarden({
location: 'europe-west1',
modelGarden: {
models: [claude35Sonnet],
},
models: [claude35Sonnet],
}),
],
});
Expand Down
30 changes: 13 additions & 17 deletions js/testapps/dev-ui-gallery/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,23 @@ export const ai = genkit({
}),
vertexAIModelGarden({
location: 'us-central1',
modelGarden: {
models: [claude35Sonnet],
},
models: [claude35Sonnet],
}),
vertexAIEvaluation({
location: 'us-central1',
evaluation: {
metrics: [
VertexAIEvaluationMetricType.BLEU,
VertexAIEvaluationMetricType.GROUNDEDNESS,
VertexAIEvaluationMetricType.SAFETY,
{
type: VertexAIEvaluationMetricType.ROUGE,
metricSpec: {
rougeType: 'rougeLsum',
useStemmer: true,
splitSummaries: 'true',
},
metrics: [
VertexAIEvaluationMetricType.BLEU,
VertexAIEvaluationMetricType.GROUNDEDNESS,
VertexAIEvaluationMetricType.SAFETY,
{
type: VertexAIEvaluationMetricType.ROUGE,
metricSpec: {
rougeType: 'rougeLsum',
useStemmer: true,
splitSummaries: 'true',
},
],
},
},
],
}),

// vector stores
Expand Down
2 changes: 1 addition & 1 deletion js/testapps/format-tester/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ai = genkit({
}),
vertexAIModelGarden({
location: 'us-east5',
modelGarden: { models: [claude35Sonnet, claude35SonnetV2] },
models: [claude35Sonnet, claude35SonnetV2],
}),
],
});
Expand Down
4 changes: 1 addition & 3 deletions js/testapps/model-tester/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export const ai = genkit({
}),
vertexAIModelGarden({
location: 'us-central1',
modelGarden: {
models: [claude3Sonnet, llama31],
},
models: [claude3Sonnet, llama31],
}),
ollama({
models: [
Expand Down
4 changes: 1 addition & 3 deletions js/testapps/rag/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export const ai = genkit({
}),
vertexAIModelGarden({
location: 'us-central1',
modelGarden: {
models: [claude3Sonnet, llama31],
},
models: [claude3Sonnet, llama31],
}),
pinecone([
{
Expand Down
6 changes: 1 addition & 5 deletions js/testapps/vertexai-reranker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ const ai = genkit({
vertexAIRerankers({
projectId: PROJECT_ID,
location: LOCATION,
rerankOptions: [
{
model: 'vertexai/reranker',
},
],
rerankers: ['vertexai/reranker'],
}),
],
});
Expand Down
Loading