string_evaluator.cjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StringEvaluator = void 0;
  4. class StringEvaluator {
  5. constructor(params) {
  6. Object.defineProperty(this, "evaluationName", {
  7. enumerable: true,
  8. configurable: true,
  9. writable: true,
  10. value: void 0
  11. });
  12. Object.defineProperty(this, "inputKey", {
  13. enumerable: true,
  14. configurable: true,
  15. writable: true,
  16. value: void 0
  17. });
  18. Object.defineProperty(this, "predictionKey", {
  19. enumerable: true,
  20. configurable: true,
  21. writable: true,
  22. value: void 0
  23. });
  24. Object.defineProperty(this, "answerKey", {
  25. enumerable: true,
  26. configurable: true,
  27. writable: true,
  28. value: void 0
  29. });
  30. Object.defineProperty(this, "gradingFunction", {
  31. enumerable: true,
  32. configurable: true,
  33. writable: true,
  34. value: void 0
  35. });
  36. this.evaluationName = params.evaluationName;
  37. this.inputKey = params.inputKey ?? "input";
  38. this.predictionKey = params.predictionKey ?? "output";
  39. this.answerKey =
  40. params.answerKey !== undefined ? params.answerKey : "output";
  41. this.gradingFunction = params.gradingFunction;
  42. }
  43. async evaluateRun(run, example) {
  44. if (!run.outputs) {
  45. throw new Error("Run outputs cannot be undefined.");
  46. }
  47. const functionInputs = {
  48. input: run.inputs[this.inputKey],
  49. prediction: run.outputs[this.predictionKey],
  50. answer: this.answerKey ? example?.outputs?.[this.answerKey] : null,
  51. };
  52. const gradingResults = await this.gradingFunction(functionInputs);
  53. const key = gradingResults.key || this.evaluationName;
  54. if (!key) {
  55. throw new Error("Evaluation name cannot be undefined.");
  56. }
  57. return {
  58. key,
  59. score: gradingResults.score,
  60. value: gradingResults.value,
  61. comment: gradingResults.comment,
  62. correction: gradingResults.correction,
  63. };
  64. }
  65. }
  66. exports.StringEvaluator = StringEvaluator;