UNPKG

@genkit-ai/checks

Version:

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

90 lines 3.65 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var index_exports = {}; __export(index_exports, { ChecksEvaluationMetricType: () => import_metrics.ChecksEvaluationMetricType, checks: () => checks, checksMiddleware: () => checksMiddleware, default: () => index_default }); module.exports = __toCommonJS(index_exports); var import_logging = require("genkit/logging"); var import_plugin = require("genkit/plugin"); var import_google_auth_library = require("google-auth-library"); var import_evaluation = require("./evaluation.js"); var import_metrics = require("./metrics.js"); var import_middleware = require("./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 (0, import_plugin.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 : []; (0, import_evaluation.checksEvaluators)(ai, googleAuth, metrics, projectId); }); } function checksMiddleware(options) { const googleAuth = inititializeAuth(options.authOptions); return (0, import_middleware.checksMiddleware)({ 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 import_google_auth_library.GoogleAuth(options); } else { googleAuth = new import_google_auth_library.GoogleAuth( options ?? { scopes: [CLOUD_PLATFROM_OAUTH_SCOPE, CHECKS_OAUTH_SCOPE] } ); } googleAuth.getClient().then((client) => { if (client.quotaProjectId && options?.projectId) { import_logging.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; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ChecksEvaluationMetricType, checks, checksMiddleware }); //# sourceMappingURL=index.js.map