passthrough.cjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RunnablePassthrough = void 0;
  4. const stream_js_1 = require("../utils/stream.cjs");
  5. const base_js_1 = require("./base.cjs");
  6. const config_js_1 = require("./config.cjs");
  7. /**
  8. * A runnable to passthrough inputs unchanged or with additional keys.
  9. *
  10. * This runnable behaves almost like the identity function, except that it
  11. * can be configured to add additional keys to the output, if the input is
  12. * an object.
  13. *
  14. * The example below demonstrates how to use `RunnablePassthrough to
  15. * passthrough the input from the `.invoke()`
  16. *
  17. * @example
  18. * ```typescript
  19. * const chain = RunnableSequence.from([
  20. * {
  21. * question: new RunnablePassthrough(),
  22. * context: async () => loadContextFromStore(),
  23. * },
  24. * prompt,
  25. * llm,
  26. * outputParser,
  27. * ]);
  28. * const response = await chain.invoke(
  29. * "I can pass a single string instead of an object since I'm using `RunnablePassthrough`."
  30. * );
  31. * ```
  32. */
  33. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  34. class RunnablePassthrough extends base_js_1.Runnable {
  35. static lc_name() {
  36. return "RunnablePassthrough";
  37. }
  38. constructor(fields) {
  39. super(fields);
  40. Object.defineProperty(this, "lc_namespace", {
  41. enumerable: true,
  42. configurable: true,
  43. writable: true,
  44. value: ["langchain_core", "runnables"]
  45. });
  46. Object.defineProperty(this, "lc_serializable", {
  47. enumerable: true,
  48. configurable: true,
  49. writable: true,
  50. value: true
  51. });
  52. Object.defineProperty(this, "func", {
  53. enumerable: true,
  54. configurable: true,
  55. writable: true,
  56. value: void 0
  57. });
  58. if (fields) {
  59. this.func = fields.func;
  60. }
  61. }
  62. async invoke(input, options) {
  63. const config = (0, config_js_1.ensureConfig)(options);
  64. if (this.func) {
  65. await this.func(input, config);
  66. }
  67. return this._callWithConfig((input) => Promise.resolve(input), input, config);
  68. }
  69. async *transform(generator, options) {
  70. const config = (0, config_js_1.ensureConfig)(options);
  71. let finalOutput;
  72. let finalOutputSupported = true;
  73. for await (const chunk of this._transformStreamWithConfig(generator, (input) => input, config)) {
  74. yield chunk;
  75. if (finalOutputSupported) {
  76. if (finalOutput === undefined) {
  77. finalOutput = chunk;
  78. }
  79. else {
  80. try {
  81. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  82. finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
  83. }
  84. catch {
  85. finalOutput = undefined;
  86. finalOutputSupported = false;
  87. }
  88. }
  89. }
  90. }
  91. if (this.func && finalOutput !== undefined) {
  92. await this.func(finalOutput, config);
  93. }
  94. }
  95. /**
  96. * A runnable that assigns key-value pairs to the input.
  97. *
  98. * The example below shows how you could use it with an inline function.
  99. *
  100. * @example
  101. * ```typescript
  102. * const prompt =
  103. * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}
  104. * Question: {question}
  105. * SQL Query:`);
  106. *
  107. * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`
  108. * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.
  109. * // In this case, we're passing the schema.
  110. * const sqlQueryGeneratorChain = RunnableSequence.from([
  111. * RunnablePassthrough.assign({
  112. * schema: async () => db.getTableInfo(),
  113. * }),
  114. * prompt,
  115. * new ChatOpenAI({}).withConfig({ stop: ["\nSQLResult:"] }),
  116. * new StringOutputParser(),
  117. * ]);
  118. * const result = await sqlQueryGeneratorChain.invoke({
  119. * question: "How many employees are there?",
  120. * });
  121. * ```
  122. */
  123. static assign(mapping) {
  124. return new base_js_1.RunnableAssign(new base_js_1.RunnableMap({ steps: mapping }));
  125. }
  126. }
  127. exports.RunnablePassthrough = RunnablePassthrough;