@genkit-ai/checks
Version:
Google Checks AI Safety plugins for classifying the safety of text against Checks AI safety policies.
1 lines • 3.87 kB
Source Map (JSON)
{"version":3,"sources":["../src/guardrails.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 } from 'genkit';\nimport type { GoogleAuth } from 'google-auth-library';\nimport { isConfig, type ChecksEvaluationMetric } from './metrics';\n\nconst GUARDRAILS_URL =\n 'https://checks.googleapis.com/v1alpha/aisafety:classifyContent';\n\n/**\n * Request to the Checks Guardrails ClisifyContent endpoint.\n */\ntype GuardrailsRequest = {\n input: {\n text_input: {\n content: string;\n };\n };\n policies: {\n policy_type: string;\n threshold?: number;\n }[];\n};\n\n/**\n * Response type returned by the classifyContent endpoint.\n */\nconst ResponseSchema = z.object({\n policyResults: z.array(\n z.object({\n policyType: z.string(),\n score: z.number().optional(),\n violationResult: z.string(),\n })\n ),\n});\n\ntype ResponseType = z.infer<typeof ResponseSchema>;\n\n/**\n * API implementation for making requests to the guardrails api.\n */\nexport class Guardrails {\n private auth: GoogleAuth;\n private projectId: string | undefined;\n\n constructor(auth: GoogleAuth, projectId?: string) {\n this.auth = auth;\n this.projectId = projectId;\n }\n\n async classifyContent(\n content: string,\n policies: ChecksEvaluationMetric[]\n ): Promise<ResponseType> {\n const body: GuardrailsRequest = {\n input: {\n text_input: {\n content: content,\n },\n },\n policies: policies.map((policy) => {\n const policyType = isConfig(policy) ? policy.type : policy;\n const threshold = isConfig(policy) ? policy.threshold : undefined;\n\n return {\n policy_type: policyType,\n threshold: threshold,\n };\n }),\n };\n\n const client = await this.auth.getClient();\n const response = await client.request({\n url: GUARDRAILS_URL,\n method: 'POST',\n body: JSON.stringify(body),\n headers: {\n 'x-goog-user-project': this.projectId,\n 'Content-Type': 'application/json',\n },\n });\n\n try {\n return ResponseSchema.parse(response.data);\n } catch (e) {\n throw new Error(`Error parsing ${GUARDRAILS_URL} API response: ${e}`);\n }\n }\n}\n"],"mappings":"AAgBA,SAAS,SAAS;AAElB,SAAS,gBAA6C;AAEtD,MAAM,iBACJ;AAoBF,MAAM,iBAAiB,EAAE,OAAO;AAAA,EAC9B,eAAe,EAAE;AAAA,IACf,EAAE,OAAO;AAAA,MACP,YAAY,EAAE,OAAO;AAAA,MACrB,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,MAC3B,iBAAiB,EAAE,OAAO;AAAA,IAC5B,CAAC;AAAA,EACH;AACF,CAAC;AAOM,MAAM,WAAW;AAAA,EACd;AAAA,EACA;AAAA,EAER,YAAY,MAAkB,WAAoB;AAChD,SAAK,OAAO;AACZ,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,gBACJ,SACA,UACuB;AACvB,UAAM,OAA0B;AAAA,MAC9B,OAAO;AAAA,QACL,YAAY;AAAA,UACV;AAAA,QACF;AAAA,MACF;AAAA,MACA,UAAU,SAAS,IAAI,CAAC,WAAW;AACjC,cAAM,aAAa,SAAS,MAAM,IAAI,OAAO,OAAO;AACpD,cAAM,YAAY,SAAS,MAAM,IAAI,OAAO,YAAY;AAExD,eAAO;AAAA,UACL,aAAa;AAAA,UACb;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAEA,UAAM,SAAS,MAAM,KAAK,KAAK,UAAU;AACzC,UAAM,WAAW,MAAM,OAAO,QAAQ;AAAA,MACpC,KAAK;AAAA,MACL,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,IAAI;AAAA,MACzB,SAAS;AAAA,QACP,uBAAuB,KAAK;AAAA,QAC5B,gBAAgB;AAAA,MAClB;AAAA,IACF,CAAC;AAED,QAAI;AACF,aAAO,eAAe,MAAM,SAAS,IAAI;AAAA,IAC3C,SAAS,GAAG;AACV,YAAM,IAAI,MAAM,iBAAiB,cAAc,kBAAkB,CAAC,EAAE;AAAA,IACtE;AAAA,EACF;AACF;","names":[]}