string_evaluator.js 2.1 KB

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