UNPKG

@genkit-ai/checks

Version:

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

1 lines 5.45 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Genkit } from 'genkit';\nimport { logger } from 'genkit/logging';\nimport type { ModelMiddleware } from 'genkit/model';\nimport { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';\nimport { GoogleAuth, type GoogleAuthOptions } from 'google-auth-library';\nimport { checksEvaluators } from './evaluation.js';\nimport {\n ChecksEvaluationMetricType,\n type ChecksEvaluationMetric,\n} from './metrics.js';\nimport { checksMiddleware as authorizedMiddleware } from './middleware.js';\n\nexport { ChecksEvaluationMetricType };\n\nexport interface PluginOptions {\n /** The Google Cloud project id to call. Must have quota for the Checks API. */\n projectId?: string;\n /** Provide custom authentication configuration for connecting to Checks API. */\n googleAuthOptions?: GoogleAuthOptions;\n /** Configure Checks evaluators. */\n evaluation?: {\n metrics: ChecksEvaluationMetric[];\n };\n}\n\nconst CLOUD_PLATFROM_OAUTH_SCOPE =\n 'https://www.googleapis.com/auth/cloud-platform';\n\nconst CHECKS_OAUTH_SCOPE = 'https://www.googleapis.com/auth/checks';\n\n/**\n * Add Google Checks evaluators.\n */\nexport function checks(options?: PluginOptions): GenkitPlugin {\n return genkitPlugin('checks', async (ai: Genkit) => {\n const googleAuth = inititializeAuth(options?.googleAuthOptions);\n\n const projectId = options?.projectId || (await googleAuth.getProjectId());\n\n if (!projectId) {\n throw new Error(\n `Checks Plugin is missing the 'projectId' configuration. Please set the 'GCLOUD_PROJECT' environment variable or explicitly pass 'projectId' into genkit config.`\n );\n }\n\n const metrics =\n options?.evaluation && options.evaluation.metrics.length > 0\n ? options.evaluation.metrics\n : [];\n checksEvaluators(ai, googleAuth, metrics, projectId);\n });\n}\n\nexport function checksMiddleware(options: {\n authOptions: GoogleAuthOptions;\n metrics: ChecksEvaluationMetric[];\n}): ModelMiddleware {\n const googleAuth = inititializeAuth(options.authOptions);\n\n return authorizedMiddleware({\n auth: googleAuth,\n metrics: options.metrics,\n projectId: options.authOptions.projectId,\n });\n}\n\n/**\n * Helper function for initializing an instance of GoogleAuth.\n *\n * @param options Options for initializing a GoogleAuth instance.\n * @returns GoogleAuth\n */\nfunction inititializeAuth(options?: GoogleAuthOptions): GoogleAuth {\n let googleAuth: GoogleAuth;\n\n // Allow customers to pass in cloud credentials from environment variables\n // following: https://github.com/googleapis/google-auth-library-nodejs?tab=readme-ov-file#loading-credentials-from-environment-variables\n if (process.env.GCLOUD_SERVICE_ACCOUNT_CREDS) {\n const serviceAccountCreds = JSON.parse(\n process.env.GCLOUD_SERVICE_ACCOUNT_CREDS\n );\n options = {\n credentials: serviceAccountCreds,\n scopes: [CLOUD_PLATFROM_OAUTH_SCOPE, CHECKS_OAUTH_SCOPE],\n };\n googleAuth = new GoogleAuth(options);\n } else {\n googleAuth = new GoogleAuth(\n options ?? {\n scopes: [CLOUD_PLATFROM_OAUTH_SCOPE, CHECKS_OAUTH_SCOPE],\n }\n );\n }\n\n googleAuth.getClient().then((client) => {\n if (client.quotaProjectId && options?.projectId) {\n logger.warn(\n `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}).`\n );\n }\n });\n\n return googleAuth;\n}\n\nexport default checks;\n"],"mappings":"AAiBA,SAAS,cAAc;AAEvB,SAAS,oBAAuC;AAChD,SAAS,kBAA0C;AACnD,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,OAEK;AACP,SAAS,oBAAoB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