UNPKG

@genkit-ai/checks

Version:

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

80 lines 2.59 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 guardrails_exports = {}; __export(guardrails_exports, { Guardrails: () => Guardrails }); module.exports = __toCommonJS(guardrails_exports); var import_genkit = require("genkit"); var import_metrics = require("./metrics"); const GUARDRAILS_URL = "https://checks.googleapis.com/v1alpha/aisafety:classifyContent"; const ResponseSchema = import_genkit.z.object({ policyResults: import_genkit.z.array( import_genkit.z.object({ policyType: import_genkit.z.string(), score: import_genkit.z.number().optional(), violationResult: import_genkit.z.string() }) ) }); class Guardrails { auth; projectId; constructor(auth, projectId) { this.auth = auth; this.projectId = projectId; } async classifyContent(content, policies) { const body = { input: { text_input: { content } }, policies: policies.map((policy) => { const policyType = (0, import_metrics.isConfig)(policy) ? policy.type : policy; const threshold = (0, import_metrics.isConfig)(policy) ? policy.threshold : void 0; return { policy_type: policyType, threshold }; }) }; const client = await this.auth.getClient(); const response = await client.request({ url: GUARDRAILS_URL, method: "POST", body: JSON.stringify(body), headers: { "x-goog-user-project": this.projectId, "Content-Type": "application/json" } }); try { return ResponseSchema.parse(response.data); } catch (e) { throw new Error(`Error parsing ${GUARDRAILS_URL} API response: ${e}`); } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Guardrails }); //# sourceMappingURL=guardrails.js.map