{"version":3,"sources":["../../src/metrics/regexp.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 {\n  EvalStatusEnum,\n  type BaseEvalDataPoint,\n  type Score,\n} from 'genkit/evaluator';\n\n/**\n * A simple regexp evaluator -- matches regexp from the reference against the output.\n */\nexport async function regexp(dataPoint: BaseEvalDataPoint): Promise<Score> {\n  if (!dataPoint.output) {\n    throw new Error('Output was not provided');\n  }\n  if (!dataPoint.reference) {\n    throw new Error('Reference was not provided');\n  }\n  if (typeof dataPoint.reference !== 'string') {\n    throw new Error('Reference must be a string (regex)');\n  }\n\n  const re = new RegExp(dataPoint.reference);\n\n  const outputStr =\n    typeof dataPoint.output !== 'string'\n      ? JSON.stringify(dataPoint.output)\n      : dataPoint.output;\n\n  const score = re.test(outputStr);\n  return {\n    score,\n    status: score ? EvalStatusEnum.PASS : EvalStatusEnum.FAIL,\n  };\n}\n"],"mappings":"AAgBA;AAAA,EACE;AAAA,OAGK;AAKP,eAAsB,OAAO,WAA8C;AACzE,MAAI,CAAC,UAAU,QAAQ;AACrB,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AACA,MAAI,CAAC,UAAU,WAAW;AACxB,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC9C;AACA,MAAI,OAAO,UAAU,cAAc,UAAU;AAC3C,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,QAAM,KAAK,IAAI,OAAO,UAAU,SAAS;AAEzC,QAAM,YACJ,OAAO,UAAU,WAAW,WACxB,KAAK,UAAU,UAAU,MAAM,IAC/B,UAAU;AAEhB,QAAM,QAAQ,GAAG,KAAK,SAAS;AAC/B,SAAO;AAAA,IACL;AAAA,IACA,QAAQ,QAAQ,eAAe,OAAO,eAAe;AAAA,EACvD;AACF;","names":[]}