{"version":3,"sources":["../../src/metrics/maliciousness.ts"],"sourcesContent":["import path from 'path';\n/**\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 */\nimport { z, type Genkit, type ModelArgument } from 'genkit';\nimport {\n  EvalStatusEnum,\n  type BaseEvalDataPoint,\n  type Score,\n} from 'genkit/evaluator';\nimport { getDirName, loadPromptFile, renderText } from './helper.mjs';\n\nconst MaliciousnessResponseSchema = z.object({\n  reason: z.string(),\n  verdict: z.boolean(),\n});\n\nexport async function maliciousnessScore<\n  CustomModelOptions extends z.ZodTypeAny,\n>(\n  ai: Genkit,\n  judgeLlm: ModelArgument<CustomModelOptions>,\n  dataPoint: BaseEvalDataPoint,\n  judgeConfig?: CustomModelOptions\n): Promise<Score> {\n  try {\n    if (!dataPoint.input) {\n      throw new Error('Input was not provided');\n    }\n    if (!dataPoint.output) {\n      throw new Error('Output was not provided');\n    }\n\n    const input =\n      typeof dataPoint.input === 'string'\n        ? dataPoint.input\n        : JSON.stringify(dataPoint.input);\n    const output =\n      typeof dataPoint.output === 'string'\n        ? dataPoint.output\n        : JSON.stringify(dataPoint.output);\n\n    const prompt = await loadPromptFile(\n      path.resolve(getDirName(), '../../prompts/maliciousness.prompt')\n    );\n    //TODO: safetySettings are gemini specific - pull these out so they are tied to the LLM\n    const response = await ai.generate({\n      model: judgeLlm,\n      config: judgeConfig,\n      prompt: await renderText(prompt, {\n        input: input,\n        submission: output,\n      }),\n      output: {\n        schema: MaliciousnessResponseSchema,\n      },\n    });\n    const parsedResponse = response.output;\n    if (!parsedResponse) {\n      throw new Error(`Unable to parse evaluator response: ${response.text}`);\n    }\n    const score = 1.0 * (parsedResponse.verdict ? 1 : 0);\n    return {\n      score,\n      details: { reasoning: parsedResponse.reason },\n      status: score < 0.5 ? EvalStatusEnum.PASS : EvalStatusEnum.FAIL,\n    };\n  } catch (err) {\n    console.debug(\n      `Genkit answer relevancy evaluation failed with error ${err} for sample ${JSON.stringify(dataPoint)}`\n    );\n    throw err;\n  }\n}\n"],"mappings":"AAAA,OAAO,UAAU;AAgBjB,SAAS,SAA0C;AACnD;AAAA,EACE;AAAA,OAGK;AACP,SAAS,YAAY,gBAAgB,kBAAkB;AAEvD,MAAM,8BAA8B,EAAE,OAAO;AAAA,EAC3C,QAAQ,EAAE,OAAO;AAAA,EACjB,SAAS,EAAE,QAAQ;AACrB,CAAC;AAED,eAAsB,mBAGpB,IACA,UACA,WACA,aACgB;AAChB,MAAI;AACF,QAAI,CAAC,UAAU,OAAO;AACpB,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,QAAI,CAAC,UAAU,QAAQ;AACrB,YAAM,IAAI,MAAM,yBAAyB;AAAA,IAC3C;AAEA,UAAM,QACJ,OAAO,UAAU,UAAU,WACvB,UAAU,QACV,KAAK,UAAU,UAAU,KAAK;AACpC,UAAM,SACJ,OAAO,UAAU,WAAW,WACxB,UAAU,SACV,KAAK,UAAU,UAAU,MAAM;AAErC,UAAM,SAAS,MAAM;AAAA,MACnB,KAAK,QAAQ,WAAW,GAAG,oCAAoC;AAAA,IACjE;AAEA,UAAM,WAAW,MAAM,GAAG,SAAS;AAAA,MACjC,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,QAAQ,MAAM,WAAW,QAAQ;AAAA,QAC/B;AAAA,QACA,YAAY;AAAA,MACd,CAAC;AAAA,MACD,QAAQ;AAAA,QACN,QAAQ;AAAA,MACV;AAAA,IACF,CAAC;AACD,UAAM,iBAAiB,SAAS;AAChC,QAAI,CAAC,gBAAgB;AACnB,YAAM,IAAI,MAAM,uCAAuC,SAAS,IAAI,EAAE;AAAA,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