UNPKG

@genkit-ai/checks

Version:

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

1 lines 6.1 kB
{"version":3,"sources":["../src/evaluation.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 { z, type EvaluatorAction, type Genkit } from 'genkit';\nimport type { BaseEvalDataPoint } from 'genkit/evaluator';\nimport { runInNewSpan } from 'genkit/tracing';\nimport type { GoogleAuth } from 'google-auth-library';\nimport {\n isConfig,\n type ChecksEvaluationMetric,\n type ChecksEvaluationMetricConfig,\n} from './metrics';\n\nexport function checksEvaluators(\n ai: Genkit,\n auth: GoogleAuth,\n metrics: ChecksEvaluationMetric[],\n projectId: string\n): EvaluatorAction {\n const policy_configs: ChecksEvaluationMetricConfig[] = metrics.map(\n (metric) => {\n const metricType = isConfig(metric) ? metric.type : metric;\n const threshold = isConfig(metric) ? metric.threshold : undefined;\n\n return {\n type: metricType,\n threshold,\n };\n }\n );\n\n return createPolicyEvaluator(projectId, auth, ai, policy_configs);\n}\n\nconst ResponseSchema = z.object({\n policyResults: z.array(\n z.object({\n policyType: z.string(),\n score: z.number(),\n violationResult: z.string(),\n })\n ),\n});\n\nfunction createPolicyEvaluator(\n projectId: string,\n auth: GoogleAuth,\n ai: Genkit,\n policy_config: ChecksEvaluationMetricConfig[]\n): EvaluatorAction {\n return ai.defineEvaluator(\n {\n name: 'checks/guardrails',\n displayName: 'checks/guardrails',\n definition: `Evaluates input text against the Checks ${policy_config.map((policy) => policy.type)} policies.`,\n },\n async (datapoint: BaseEvalDataPoint) => {\n const partialRequest = {\n input: {\n text_input: {\n content: datapoint.output as string,\n },\n },\n policies: policy_config.map((config) => {\n return {\n policy_type: config.type,\n threshold: config.threshold,\n };\n }),\n };\n\n const response = await checksEvalInstance(\n ai,\n projectId,\n auth,\n partialRequest,\n ResponseSchema\n );\n\n const evaluationResults = response.policyResults.map((result) => {\n return {\n id: result.policyType,\n score: result.score,\n details: {\n reasoning: `Status ${result.violationResult}`,\n },\n };\n });\n\n return {\n evaluation: evaluationResults,\n testCaseId: datapoint.testCaseId,\n };\n }\n );\n}\n\nasync function checksEvalInstance<ResponseType extends z.ZodTypeAny>(\n ai: Genkit,\n projectId: string,\n auth: GoogleAuth,\n partialRequest: any,\n responseSchema: ResponseType\n): Promise<z.infer<ResponseType>> {\n return await runInNewSpan(\n ai,\n {\n metadata: {\n name: 'EvaluationService#evaluateInstances',\n },\n },\n async (metadata, _otSpan) => {\n const request = {\n ...partialRequest,\n };\n\n metadata.input = request;\n const client = await auth.getClient();\n const url =\n 'https://checks.googleapis.com/v1alpha/aisafety:classifyContent';\n\n const response = await client.request({\n url,\n method: 'POST',\n body: JSON.stringify(request),\n headers: {\n 'x-goog-user-project': projectId,\n 'Content-Type': 'application/json',\n },\n });\n metadata.output = response.data;\n\n try {\n return responseSchema.parse(response.data);\n } catch (e) {\n throw new Error(`Error parsing ${url} API response: ${e}`);\n }\n }\n );\n}\n"],"mappings":"AAgBA,SAAS,SAA4C;AAErD,SAAS,oBAAoB;AAE7B;AAAA,EACE;AAAA,OAGK;AAEA,SAAS,iBACd,IACA,MACA,SACA,WACiB;AACjB,QAAM,iBAAiD,QAAQ;AAAA,IAC7D,CAAC,WAAW;AACV,YAAM,aAAa,SAAS,MAAM,IAAI,OAAO,OAAO;AACpD,YAAM,YAAY,SAAS,MAAM,IAAI,OAAO,YAAY;AAExD,aAAO;AAAA,QACL,MAAM;AAAA,QACN;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,sBAAsB,WAAW,MAAM,IAAI,cAAc;AAClE;AAEA,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