router.cjs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RouterRunnable = void 0;
  4. const base_js_1 = require("./base.cjs");
  5. const config_js_1 = require("./config.cjs");
  6. /**
  7. * A runnable that routes to a set of runnables based on Input['key'].
  8. * Returns the output of the selected runnable.
  9. * @example
  10. * ```typescript
  11. * import { RouterRunnable, RunnableLambda } from "@langchain/core/runnables";
  12. *
  13. * const router = new RouterRunnable({
  14. * runnables: {
  15. * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),
  16. * reverseText: RunnableLambda.from((text: string) =>
  17. * text.split("").reverse().join("")
  18. * ),
  19. * },
  20. * });
  21. *
  22. * // Invoke the 'reverseText' runnable
  23. * const result1 = router.invoke({ key: "reverseText", input: "Hello World" });
  24. *
  25. * // "dlroW olleH"
  26. *
  27. * // Invoke the 'toUpperCase' runnable
  28. * const result2 = router.invoke({ key: "toUpperCase", input: "Hello World" });
  29. *
  30. * // "HELLO WORLD"
  31. * ```
  32. */
  33. class RouterRunnable extends base_js_1.Runnable {
  34. static lc_name() {
  35. return "RouterRunnable";
  36. }
  37. constructor(fields) {
  38. super(fields);
  39. Object.defineProperty(this, "lc_namespace", {
  40. enumerable: true,
  41. configurable: true,
  42. writable: true,
  43. value: ["langchain_core", "runnables"]
  44. });
  45. Object.defineProperty(this, "lc_serializable", {
  46. enumerable: true,
  47. configurable: true,
  48. writable: true,
  49. value: true
  50. });
  51. Object.defineProperty(this, "runnables", {
  52. enumerable: true,
  53. configurable: true,
  54. writable: true,
  55. value: void 0
  56. });
  57. this.runnables = fields.runnables;
  58. }
  59. async invoke(input, options) {
  60. const { key, input: actualInput } = input;
  61. const runnable = this.runnables[key];
  62. if (runnable === undefined) {
  63. throw new Error(`No runnable associated with key "${key}".`);
  64. }
  65. return runnable.invoke(actualInput, (0, config_js_1.ensureConfig)(options));
  66. }
  67. async batch(inputs, options, batchOptions) {
  68. const keys = inputs.map((input) => input.key);
  69. const actualInputs = inputs.map((input) => input.input);
  70. const missingKey = keys.find((key) => this.runnables[key] === undefined);
  71. if (missingKey !== undefined) {
  72. throw new Error(`One or more keys do not have a corresponding runnable.`);
  73. }
  74. const runnables = keys.map((key) => this.runnables[key]);
  75. const optionsList = this._getOptionsList(options ?? {}, inputs.length);
  76. const maxConcurrency = optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
  77. const batchSize = maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;
  78. const batchResults = [];
  79. for (let i = 0; i < actualInputs.length; i += batchSize) {
  80. const batchPromises = actualInputs
  81. .slice(i, i + batchSize)
  82. .map((actualInput, i) => runnables[i].invoke(actualInput, optionsList[i]));
  83. const batchResult = await Promise.all(batchPromises);
  84. batchResults.push(batchResult);
  85. }
  86. return batchResults.flat();
  87. }
  88. async stream(input, options) {
  89. const { key, input: actualInput } = input;
  90. const runnable = this.runnables[key];
  91. if (runnable === undefined) {
  92. throw new Error(`No runnable associated with key "${key}".`);
  93. }
  94. return runnable.stream(actualInput, options);
  95. }
  96. }
  97. exports.RouterRunnable = RouterRunnable;