UNPKG

@genkit-ai/checks

Version:

Google Checks AI Safety plugins for classifying the safety of text against Checks AI safety policies.

66 lines 2.41 kB
import { logger } from "genkit/logging"; import { genkitPlugin } from "genkit/plugin"; import { GoogleAuth } from "google-auth-library"; import { checksEvaluators } from "./evaluation.js"; import { ChecksEvaluationMetricType } from "./metrics.js"; import { checksMiddleware as authorizedMiddleware } from "./middleware.js"; const CLOUD_PLATFROM_OAUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platform"; const CHECKS_OAUTH_SCOPE = "https://www.googleapis.com/auth/checks"; function checks(options) { return genkitPlugin("checks", async (ai) => { 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); }); } function checksMiddleware(options) { const googleAuth = inititializeAuth(options.authOptions); return authorizedMiddleware({ auth: googleAuth, metrics: options.metrics, projectId: options.authOptions.projectId }); } function inititializeAuth(options) { let googleAuth; if (process.env.GCLOUD_SERVICE_ACCOUNT_CREDS) { const serviceAccountCreds = JSON.parse( process.env.GCLOUD_SERVICE_ACCOUNT_CREDS ); options = { credentials: serviceAccountCreds, scopes: [CLOUD_PLATFROM_OAUTH_SCOPE, CHECKS_OAUTH_SCOPE] }; googleAuth = new GoogleAuth(options); } else { googleAuth = new GoogleAuth( options ?? { scopes: [CLOUD_PLATFROM_OAUTH_SCOPE, CHECKS_OAUTH_SCOPE] } ); } googleAuth.getClient().then((client) => { if (client.quotaProjectId && options?.projectId) { logger.warn( `Checks Evaluator: Your Google cloud authentication has a default quota project(${client.quotaProjectId}) associated with it which will overrid the projectId in your Checks plugin config(${options?.projectId}).` ); } }); return googleAuth; } var index_default = checks; export { ChecksEvaluationMetricType, checks, checksMiddleware, index_default as default }; //# sourceMappingURL=index.mjs.map