evaluatedBy.cjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.wrapEvaluator = wrapEvaluator;
  4. exports.evaluatedBy = evaluatedBy;
  5. const traceable_js_1 = require("../../../traceable.cjs");
  6. const globals_js_1 = require("../globals.cjs");
  7. const uuid_1 = require("uuid");
  8. function isEvaluationResult(x) {
  9. return (x != null &&
  10. typeof x === "object" &&
  11. "key" in x &&
  12. typeof x.key === "string" &&
  13. "score" in x);
  14. }
  15. function wrapEvaluator(evaluator) {
  16. return async (input, config) => {
  17. const context = globals_js_1.testWrapperAsyncLocalStorageInstance.getStore();
  18. if (context === undefined || context.currentExample === undefined) {
  19. throw new Error([
  20. `Could not identify current LangSmith context.`,
  21. `Please ensure you are calling this matcher within "ls.test()"`,
  22. `See this page for more information: https://docs.smith.langchain.com/evaluation/how_to_guides/vitest_jest`,
  23. ].join("\n"));
  24. }
  25. const evalRunId = config?.runId ?? config?.id ?? (0, uuid_1.v4)();
  26. let evalResult;
  27. if ((0, globals_js_1.trackingEnabled)(context)) {
  28. const wrappedEvaluator = (0, traceable_js_1.traceable)(async (_runTree, params) => {
  29. return evaluator(params);
  30. }, {
  31. id: evalRunId,
  32. trace_id: evalRunId,
  33. reference_example_id: context.currentExample.id,
  34. client: context.client,
  35. tracingEnabled: true,
  36. name: evaluator.name ?? "<evaluator>",
  37. project_name: "evaluators",
  38. ...config,
  39. });
  40. evalResult = await wrappedEvaluator(traceable_js_1.ROOT, input);
  41. }
  42. else {
  43. evalResult = await evaluator(input);
  44. }
  45. let normalizedResult;
  46. if (!Array.isArray(evalResult)) {
  47. normalizedResult = [evalResult];
  48. }
  49. else {
  50. normalizedResult = evalResult;
  51. }
  52. for (const result of normalizedResult) {
  53. if (isEvaluationResult(result)) {
  54. (0, globals_js_1._logTestFeedback)({
  55. exampleId: context?.currentExample?.id,
  56. feedback: result,
  57. context,
  58. runTree: context.testRootRunTree,
  59. client: context.client,
  60. sourceRunId: evalRunId,
  61. });
  62. }
  63. }
  64. return evalResult;
  65. };
  66. }
  67. async function evaluatedBy(outputs, evaluator) {
  68. const context = globals_js_1.testWrapperAsyncLocalStorageInstance.getStore();
  69. if (context === undefined || context.currentExample === undefined) {
  70. throw new Error([
  71. `Could not identify current LangSmith context.`,
  72. `Please ensure you are calling this matcher within "ls.test()"`,
  73. `See this page for more information: https://docs.smith.langchain.com/evaluation/how_to_guides/vitest_jest`,
  74. ].join("\n"));
  75. }
  76. const wrappedEvaluator = wrapEvaluator(evaluator);
  77. const evalRunId = (0, uuid_1.v4)();
  78. const evalResult = await wrappedEvaluator({
  79. inputs: context.currentExample?.inputs ?? {},
  80. referenceOutputs: context?.currentExample?.outputs ?? {},
  81. outputs,
  82. }, { runId: evalRunId });
  83. if (Array.isArray(evalResult)) {
  84. return evalResult.map((result) => result.score);
  85. }
  86. return evalResult.score;
  87. }