Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
17 changes: 8 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 @@ -25,11 +26,10 @@ import {
} from './metrics';

export function checksEvaluators(
ai: Genkit,
auth: GoogleAuth,
metrics: ChecksEvaluationMetric[],
projectId: string
): EvaluatorAction {
): EvaluatorAction[] {
const policy_configs: ChecksEvaluationMetricConfig[] = metrics.map(
(metric) => {
const metricType = isConfig(metric) ? metric.type : metric;
Expand All @@ -42,7 +42,10 @@ export function checksEvaluators(
}
);

return createPolicyEvaluator(projectId, auth, ai, policy_configs);
if (metrics.length === 0) {
return [];
}
return [createPolicyEvaluator(projectId, auth, policy_configs)];
}

const ResponseSchema = z.object({
Expand All @@ -58,10 +61,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 +85,6 @@ function createPolicyEvaluator(
};

const response = await checksEvalInstance(
ai,
projectId,
auth,
partialRequest,
Expand All @@ -109,14 +110,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
38 changes: 20 additions & 18 deletions js/plugins/checks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* 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 {
Expand Down Expand Up @@ -47,23 +46,26 @@ 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);

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

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.`
);
}
export function checks(options?: PluginOptions): GenkitPluginV2 {
return genkitPluginV2({
name: 'checks',
init: async () => {
const googleAuth = inititializeAuth(options?.googleAuthOptions);

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

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 checksEvaluators(googleAuth, metrics, projectId);
},
});
}

Expand Down