Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support model validation #1868

Merged
merged 1 commit into from
Feb 5, 2025
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
31 changes: 1 addition & 30 deletions genkit-tools/common/src/eval/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ import {
EvalKeyAugments,
EvalRun,
EvalRunKey,
GenerateRequest,
GenerateRequestSchema,
GenerateResponseSchema,
MessageData,
RunNewEvaluationRequest,
SpanData,
} from '../types';
import {
evaluatorName,
generateTestCaseId,
getEvalExtractors,
getModelInput,
hasAction,
isEvaluator,
logger,
Expand Down Expand Up @@ -440,30 +438,3 @@ function isSupportedActionRef(actionRef: string) {
actionRef.startsWith(`/${supportedType}`)
);
}

function getModelInput(data: any, modelConfig: any): GenerateRequest {
let message: MessageData;
if (typeof data === 'string') {
message = {
role: 'user',
content: [
{
text: data,
},
],
} as MessageData;
return {
messages: message ? [message] : [],
config: modelConfig,
};
} else {
const maybeRequest = GenerateRequestSchema.safeParse(data);
if (maybeRequest.success) {
return maybeRequest.data;
} else {
throw new Error(
`Unable to parse model input as MessageSchema as input. Details: ${maybeRequest.error}`
);
}
}
}
14 changes: 10 additions & 4 deletions genkit-tools/common/src/eval/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ValidateDataRequest,
ValidateDataResponse,
} from '../types';
import { getModelInput } from '../utils';

// Setup for AJV
type JSONSchema = JSONSchemaType<any> | any;
Expand Down Expand Up @@ -61,8 +62,8 @@ export async function validateSchema(
if (dataset.length === 0) {
return { valid: true };
}
dataset.forEach((sample, index) => {
const response = validate(targetSchema, sample.input);
dataset.forEach((sample) => {
const response = validate(actionRef, targetSchema, sample.input);
if (!response.valid) {
errorsMap[sample.testCaseId] = response.errors ?? [];
}
Expand All @@ -74,7 +75,7 @@ export async function validateSchema(
} else {
const dataset = InferenceDatasetSchema.parse(data);
dataset.forEach((sample, index) => {
const response = validate(targetSchema, sample.input);
const response = validate(actionRef, targetSchema, sample.input);
if (!response.valid) {
errorsMap[index.toString()] = response.errors ?? [];
}
Expand All @@ -86,11 +87,16 @@ export async function validateSchema(
}

function validate(
actionRef: string,
jsonSchema: JSONSchema,
data: unknown
): { valid: boolean; errors?: ErrorDetail[] } {
const isModelAction = actionRef.startsWith('/model');
let input = isModelAction
? getModelInput(data, /* modelConfig= */ undefined)
: data;
const validator = ajv.compile(jsonSchema);
const valid = validator(data) as boolean;
const valid = validator(input) as boolean;
const errors = validator.errors?.map((e) => e);
return { valid, errors: errors?.map(toErrorDetail) };
}
Expand Down
31 changes: 31 additions & 0 deletions genkit-tools/common/src/utils/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ import {
EvaluationDatasetSchema,
EvaluationSample,
EvaluationSampleSchema,
GenerateRequest,
GenerateRequestSchema,
InferenceDatasetSchema,
InferenceSample,
InferenceSampleSchema,
MessageData,
} from '../types';
import { Action } from '../types/action';
import { DocumentData, RetrieverResponse } from '../types/retrievers';
Expand Down Expand Up @@ -330,3 +333,31 @@ export async function hasAction(params: {

return actionsRecord.hasOwnProperty(actionRef);
}

/** Helper function that maps string data to GenerateRequest */
export function getModelInput(data: any, modelConfig: any): GenerateRequest {
let message: MessageData;
if (typeof data === 'string') {
message = {
role: 'user',
content: [
{
text: data,
},
],
} as MessageData;
return {
messages: message ? [message] : [],
config: modelConfig,
};
} else {
const maybeRequest = GenerateRequestSchema.safeParse(data);
if (maybeRequest.success) {
return maybeRequest.data;
} else {
throw new Error(
`Unable to parse model input as MessageSchema as input. Details: ${maybeRequest.error}`
);
}
}
}
Loading