evaluator.cjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DynamicRunEvaluator = void 0;
  4. exports.runEvaluator = runEvaluator;
  5. const uuid_1 = require("uuid");
  6. const traceable_js_1 = require("../traceable.cjs");
  7. /**
  8. * Wraps an evaluator function + implements the RunEvaluator interface.
  9. */
  10. class DynamicRunEvaluator {
  11. constructor(evaluator) {
  12. Object.defineProperty(this, "func", {
  13. enumerable: true,
  14. configurable: true,
  15. writable: true,
  16. value: void 0
  17. });
  18. this.func = ((input) => {
  19. const { run, example } = input.langSmithRunAndExample;
  20. return evaluator({
  21. ...run,
  22. run,
  23. example,
  24. inputs: example?.inputs,
  25. outputs: run?.outputs,
  26. referenceOutputs: example?.outputs,
  27. attachments: example?.attachments,
  28. }, example);
  29. });
  30. }
  31. isEvaluationResults(x) {
  32. return (typeof x === "object" &&
  33. x != null &&
  34. "results" in x &&
  35. Array.isArray(x.results) &&
  36. x.results.length > 0);
  37. }
  38. coerceEvaluationResults(results, sourceRunId) {
  39. if (this.isEvaluationResults(results)) {
  40. return {
  41. results: results.results.map((r) => this.coerceEvaluationResult(r, sourceRunId, false)),
  42. };
  43. }
  44. return this.coerceEvaluationResult(results, sourceRunId, true);
  45. }
  46. coerceEvaluationResult(result, sourceRunId, allowNoKey = false) {
  47. if ("key" in result) {
  48. if (!result.sourceRunId) {
  49. result.sourceRunId = sourceRunId;
  50. }
  51. return result;
  52. }
  53. if (!("key" in result)) {
  54. if (allowNoKey) {
  55. result["key"] = this.func.name;
  56. }
  57. }
  58. return {
  59. sourceRunId,
  60. ...result,
  61. };
  62. }
  63. /**
  64. * Evaluates a run with an optional example and returns the evaluation result.
  65. * @param run The run to evaluate.
  66. * @param example The optional example to use for evaluation.
  67. * @returns A promise that extracts to the evaluation result.
  68. */
  69. async evaluateRun(run, example, options) {
  70. const sourceRunId = (0, uuid_1.v4)();
  71. const metadata = {
  72. targetRunId: run.id,
  73. };
  74. if ("session_id" in run) {
  75. metadata["experiment"] = run.session_id;
  76. }
  77. if (typeof this.func !== "function") {
  78. throw new Error("Target must be runnable function");
  79. }
  80. const wrappedTraceableFunc = (0, traceable_js_1.traceable)(this.func, {
  81. project_name: "evaluators",
  82. name: "evaluator",
  83. id: sourceRunId,
  84. ...options,
  85. });
  86. const result = await wrappedTraceableFunc(
  87. // Pass data via `langSmithRunAndExample` key to avoid conflicts with other
  88. // inputs. This key is extracted in the wrapped function, with `run` and
  89. // `example` passed to evaluator function as arguments.
  90. { langSmithRunAndExample: { run, example } }, { metadata });
  91. // Check the one required property of EvaluationResult since 'instanceof' is not possible
  92. if ("key" in result) {
  93. if (!result.sourceRunId) {
  94. result.sourceRunId = sourceRunId;
  95. }
  96. return result;
  97. }
  98. if (Array.isArray(result)) {
  99. return {
  100. results: result.map((r) => this.coerceEvaluationResult(r, sourceRunId, false)),
  101. };
  102. }
  103. if (typeof result !== "object") {
  104. throw new Error("Evaluator function must return an object.");
  105. }
  106. return this.coerceEvaluationResults(result, sourceRunId);
  107. }
  108. }
  109. exports.DynamicRunEvaluator = DynamicRunEvaluator;
  110. function runEvaluator(func) {
  111. return new DynamicRunEvaluator(func);
  112. }