Skip to content
Open
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
70 changes: 24 additions & 46 deletions js/plugins/vertexai/src/evaluation/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,15 @@
* limitations under the License.
*/

import type { protos } from '@google-cloud/aiplatform';
import { z, type Action, type Genkit } from 'genkit';
import type { GoogleAuth } from 'google-auth-library';
import { EvaluatorFactory } from './evaluator_factory.js';

/**
* Vertex AI Evaluation metrics. See API documentation for more information.
* https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/evaluation#parameter-list
*/
export enum VertexAIEvaluationMetricType {
// Update genkit/docs/plugins/vertex-ai.md when modifying the list of enums
BLEU = 'BLEU',
ROUGE = 'ROUGE',
FLUENCY = 'FLEUNCY',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, note that this currently has a typo in the value:
FLEUNCY should be
FLUENCY

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
*/
export type VertexAIEvaluationMetricConfig = {
type: VertexAIEvaluationMetricType;
metricSpec: any;
};

export type VertexAIEvaluationMetric =
| VertexAIEvaluationMetricType
| VertexAIEvaluationMetricConfig;
Comment on lines -20 to -50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was duplicated from types.ts

import type { VertexAIEvaluationMetricConfig } from './types';
import {
VertexAIEvaluationMetric,
VertexAIEvaluationMetricType,
} from './types';

function stringify(input: unknown) {
return typeof input === 'string' ? input : JSON.stringify(input);
Expand All @@ -62,8 +37,9 @@ export function vertexEvaluators(
): Action[] {
const factory = new EvaluatorFactory(auth, location, projectId);
return metrics.map((metric) => {
const metricType = isConfig(metric) ? metric.type : metric;
const metricSpec = isConfig(metric) ? metric.metricSpec : {};
const { type: metricType, metricSpec } = isConfig(metric)
? metric
: { type: metric, metricSpec: {} };

switch (metricType) {
case VertexAIEvaluationMetricType.BLEU: {
Expand Down Expand Up @@ -110,7 +86,7 @@ const BleuResponseSchema = z.object({
function createBleuEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.IBleuSpec
): Action {
return factory.create(
ai,
Expand All @@ -128,7 +104,7 @@ function createBleuEvaluator(
instances: [
{
prediction: stringify(datapoint.output),
reference: datapoint.reference,
reference: datapoint.reference as string,
},
],
},
Expand All @@ -152,7 +128,7 @@ const RougeResponseSchema = z.object({
function createRougeEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.IRougeSpec
): Action {
return factory.create(
ai,
Expand All @@ -167,10 +143,12 @@ function createRougeEvaluator(
return {
rougeInput: {
metricSpec,
instances: {
prediction: stringify(datapoint.output),
reference: datapoint.reference,
},
instances: [
{
prediction: stringify(datapoint.output),
reference: datapoint.reference as string,
},
],
},
};
},
Expand All @@ -193,7 +171,7 @@ const FluencyResponseSchema = z.object({
function createFluencyEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.IFluencySpec
): Action {
return factory.create(
ai,
Expand Down Expand Up @@ -235,7 +213,7 @@ const SafetyResponseSchema = z.object({
function createSafetyEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.ISafetySpec
): Action {
return factory.create(
ai,
Expand Down Expand Up @@ -277,7 +255,7 @@ const GroundednessResponseSchema = z.object({
function createGroundednessEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.IGroundednessSpec
): Action {
return factory.create(
ai,
Expand Down Expand Up @@ -321,7 +299,7 @@ const SummarizationQualityResponseSchema = z.object({
function createSummarizationQualityEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationQualitySpec
): Action {
return factory.create(
ai,
Expand Down Expand Up @@ -365,7 +343,7 @@ const SummarizationHelpfulnessResponseSchema = z.object({
function createSummarizationHelpfulnessEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationHelpfulnessSpec
): Action {
return factory.create(
ai,
Expand Down Expand Up @@ -410,7 +388,7 @@ const SummarizationVerbositySchema = z.object({
function createSummarizationVerbosityEvaluator(
ai: Genkit,
factory: EvaluatorFactory,
metricSpec: any
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationVerbositySpec
): Action {
return factory.create(
ai,
Expand Down
16 changes: 10 additions & 6 deletions js/plugins/vertexai/src/evaluation/evaluator_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import type { protos } from '@google-cloud/aiplatform';
import { type Action, type Genkit, type z } from 'genkit';
import type { BaseEvalDataPoint, Score } from 'genkit/evaluator';
import { runInNewSpan } from 'genkit/tracing';
Expand All @@ -36,7 +37,9 @@ export class EvaluatorFactory {
definition: string;
responseSchema: ResponseType;
},
toRequest: (datapoint: BaseEvalDataPoint) => any,
toRequest: (
datapoint: BaseEvalDataPoint
) => protos.google.cloud.aiplatform.v1.IEvaluateInstancesRequest,
responseHandler: (response: z.infer<ResponseType>) => Score
): Action {
return ai.defineEvaluator(
Expand All @@ -63,7 +66,7 @@ export class EvaluatorFactory {

async evaluateInstances<ResponseType extends z.ZodTypeAny>(
ai: Genkit,
partialRequest: any,
partialRequest: protos.google.cloud.aiplatform.v1.IEvaluateInstancesRequest,
responseSchema: ResponseType
): Promise<z.infer<ResponseType>> {
const locationName = `projects/${this.projectId}/locations/${this.location}`;
Expand All @@ -75,10 +78,11 @@ export class EvaluatorFactory {
},
},
async (metadata, _otSpan) => {
const request = {
location: locationName,
...partialRequest,
};
const request: protos.google.cloud.aiplatform.v1.IEvaluateInstancesRequest =
{
location: locationName,
...partialRequest,
};

metadata.input = request;
const client = await this.auth.getClient();
Expand Down
42 changes: 38 additions & 4 deletions js/plugins/vertexai/src/evaluation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
* limitations under the License.
*/

import type { protos } from '@google-cloud/aiplatform';
import type { CommonPluginOptions } from '../common/types.js';

/**
* Vertex AI Evaluation metrics. See API documentation for more information.
* https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/evaluation#parameter-list
*/
export enum VertexAIEvaluationMetricType {
// Update genkit/docs/plugins/vertex-ai.md when modifying the list of enums
BLEU = 'BLEU',
Expand All @@ -34,10 +39,39 @@ export enum VertexAIEvaluationMetricType {
* 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
*/
export type VertexAIEvaluationMetricConfig = {
type: VertexAIEvaluationMetricType;
metricSpec: any;
};
export type VertexAIEvaluationMetricConfig =
| {
type: VertexAIEvaluationMetricType.BLEU;
metricSpec: protos.google.cloud.aiplatform.v1.IBleuSpec;
}
| {
type: VertexAIEvaluationMetricType.ROUGE;
metricSpec: protos.google.cloud.aiplatform.v1.IRougeSpec;
}
| {
type: VertexAIEvaluationMetricType.FLUENCY;
metricSpec: protos.google.cloud.aiplatform.v1.IFluencySpec;
}
| {
type: VertexAIEvaluationMetricType.SAFETY;
metricSpec: protos.google.cloud.aiplatform.v1.ISafetySpec;
}
| {
type: VertexAIEvaluationMetricType.GROUNDEDNESS;
metricSpec: protos.google.cloud.aiplatform.v1.IGroundednessSpec;
}
| {
type: VertexAIEvaluationMetricType.SUMMARIZATION_QUALITY;
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationQualitySpec;
}
| {
type: VertexAIEvaluationMetricType.SUMMARIZATION_HELPFULNESS;
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationHelpfulnessSpec;
}
| {
type: VertexAIEvaluationMetricType.SUMMARIZATION_VERBOSITY;
metricSpec: protos.google.cloud.aiplatform.v1.ISummarizationVerbositySpec;
};

export type VertexAIEvaluationMetric =
| VertexAIEvaluationMetricType
Expand Down