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
14 changes: 5 additions & 9 deletions js/plugins/checks/src/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { z, type EvaluatorAction, type Genkit } from 'genkit';
import { z, type EvaluatorAction } from 'genkit';
import type { BaseEvalDataPoint } from 'genkit/evaluator';
import { evaluator } from 'genkit/plugin';
import { runInNewSpan } from 'genkit/tracing';
import type { GoogleAuth } from 'google-auth-library';
import {
Expand All @@ -24,8 +25,7 @@ import {
type ChecksEvaluationMetricConfig,
} from './metrics';

export function checksEvaluators(
ai: Genkit,
export function checksEvaluator(
auth: GoogleAuth,
metrics: ChecksEvaluationMetric[],
projectId: string
Expand All @@ -42,7 +42,7 @@ export function checksEvaluators(
}
);

return createPolicyEvaluator(projectId, auth, ai, policy_configs);
return createPolicyEvaluator(projectId, auth, policy_configs);
}

const ResponseSchema = z.object({
Expand All @@ -58,10 +58,9 @@ const ResponseSchema = z.object({
function createPolicyEvaluator(
projectId: string,
auth: GoogleAuth,
ai: Genkit,
policy_config: ChecksEvaluationMetricConfig[]
): EvaluatorAction {
return ai.defineEvaluator(
return evaluator(
{
name: 'checks/guardrails',
displayName: 'checks/guardrails',
Expand All @@ -83,7 +82,6 @@ function createPolicyEvaluator(
};

const response = await checksEvalInstance(
ai,
projectId,
auth,
partialRequest,
Expand All @@ -109,14 +107,12 @@ function createPolicyEvaluator(
}

async function checksEvalInstance<ResponseType extends z.ZodTypeAny>(
ai: Genkit,
projectId: string,
auth: GoogleAuth,
partialRequest: any,
responseSchema: ResponseType
): Promise<z.infer<ResponseType>> {
return await runInNewSpan(
ai,
{
metadata: {
name: 'EvaluationService#evaluateInstances',
Expand Down
40 changes: 23 additions & 17 deletions js/plugins/checks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
* limitations under the License.
*/

import type { Genkit } from 'genkit';
import { logger } from 'genkit/logging';
import type { ModelMiddleware } from 'genkit/model';
import { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';
import { genkitPluginV2, type GenkitPluginV2 } from 'genkit/plugin';
import { GoogleAuth, type GoogleAuthOptions } from 'google-auth-library';
import { checksEvaluators } from './evaluation.js';
import { checksEvaluator } from './evaluation.js';
import {
ChecksEvaluationMetricType,
type ChecksEvaluationMetric,
Expand Down Expand Up @@ -47,23 +46,30 @@ const CHECKS_OAUTH_SCOPE = 'https://www.googleapis.com/auth/checks';
/**
* Add Google Checks evaluators.
*/
export function checks(options?: PluginOptions): GenkitPlugin {
return genkitPlugin('checks', async (ai: Genkit) => {
const googleAuth = inititializeAuth(options?.googleAuthOptions);
export function checks(options?: PluginOptions): GenkitPluginV2 {
const googleAuth = inititializeAuth(options?.googleAuthOptions);

const projectId = options?.projectId || (await googleAuth.getProjectId());
const projectId = options?.projectId || googleAuth.getProjectId();
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@CorieW CorieW Sep 30, 2025

Choose a reason for hiding this comment

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

It still is awaited on line 69, 72, and 75

Edit: now 68 and 71


if (!projectId) {
throw new Error(
`Checks Plugin is missing the 'projectId' configuration. Please set the 'GCLOUD_PROJECT' environment variable or explicitly pass 'projectId' into genkit config.`
);
}
if (!projectId) {
throw new Error(
`Checks Plugin is missing the 'projectId' configuration. Please set the 'GCLOUD_PROJECT' environment variable or explicitly pass 'projectId' into Genkit config.`
);
}

const metrics =
options?.evaluation && options.evaluation.metrics.length > 0
? options.evaluation.metrics
: [];
checksEvaluators(ai, googleAuth, metrics, projectId);
const metrics =
options?.evaluation && options.evaluation.metrics.length > 0
? options.evaluation.metrics
: [];

return genkitPluginV2({
name: 'checks',
init: async () => {
return [checksEvaluator(googleAuth, metrics, await projectId)];
},
list: async () => {
return [checksEvaluator(googleAuth, metrics, await projectId).__action];
},
});
}

Expand Down