branch.d.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Runnable, RunnableLike } from "./base.js";
  2. import { RunnableConfig } from "./config.js";
  3. import { CallbackManagerForChainRun } from "../callbacks/manager.js";
  4. /**
  5. * Type for a branch in the RunnableBranch. It consists of a condition
  6. * runnable and a branch runnable. The condition runnable is used to
  7. * determine whether the branch should be executed, and the branch runnable
  8. * is executed if the condition is true.
  9. */
  10. export type Branch<RunInput, RunOutput> = [
  11. Runnable<RunInput, boolean>,
  12. Runnable<RunInput, RunOutput>
  13. ];
  14. export type BranchLike<RunInput, RunOutput> = [
  15. RunnableLike<RunInput, boolean>,
  16. RunnableLike<RunInput, RunOutput>
  17. ];
  18. /**
  19. * Class that represents a runnable branch. The RunnableBranch is
  20. * initialized with an array of branches and a default branch. When invoked,
  21. * it evaluates the condition of each branch in order and executes the
  22. * corresponding branch if the condition is true. If none of the conditions
  23. * are true, it executes the default branch.
  24. * @example
  25. * ```typescript
  26. * const branch = RunnableBranch.from([
  27. * [
  28. * (x: { topic: string; question: string }) =>
  29. * x.topic.toLowerCase().includes("anthropic"),
  30. * anthropicChain,
  31. * ],
  32. * [
  33. * (x: { topic: string; question: string }) =>
  34. * x.topic.toLowerCase().includes("langchain"),
  35. * langChainChain,
  36. * ],
  37. * generalChain,
  38. * ]);
  39. *
  40. * const fullChain = RunnableSequence.from([
  41. * {
  42. * topic: classificationChain,
  43. * question: (input: { question: string }) => input.question,
  44. * },
  45. * branch,
  46. * ]);
  47. *
  48. * const result = await fullChain.invoke({
  49. * question: "how do I use LangChain?",
  50. * });
  51. * ```
  52. */
  53. export declare class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<RunInput, RunOutput> {
  54. static lc_name(): string;
  55. lc_namespace: string[];
  56. lc_serializable: boolean;
  57. default: Runnable<RunInput, RunOutput>;
  58. branches: Branch<RunInput, RunOutput>[];
  59. constructor(fields: {
  60. branches: Branch<RunInput, RunOutput>[];
  61. default: Runnable<RunInput, RunOutput>;
  62. });
  63. /**
  64. * Convenience method for instantiating a RunnableBranch from
  65. * RunnableLikes (objects, functions, or Runnables).
  66. *
  67. * Each item in the input except for the last one should be a
  68. * tuple with two items. The first is a "condition" RunnableLike that
  69. * returns "true" if the second RunnableLike in the tuple should run.
  70. *
  71. * The final item in the input should be a RunnableLike that acts as a
  72. * default branch if no other branches match.
  73. *
  74. * @example
  75. * ```ts
  76. * import { RunnableBranch } from "@langchain/core/runnables";
  77. *
  78. * const branch = RunnableBranch.from([
  79. * [(x: number) => x > 0, (x: number) => x + 1],
  80. * [(x: number) => x < 0, (x: number) => x - 1],
  81. * (x: number) => x
  82. * ]);
  83. * ```
  84. * @param branches An array where the every item except the last is a tuple of [condition, runnable]
  85. * pairs. The last item is a default runnable which is invoked if no other condition matches.
  86. * @returns A new RunnableBranch.
  87. */
  88. static from<RunInput = any, RunOutput = any>(branches: [
  89. ...BranchLike<RunInput, RunOutput>[],
  90. RunnableLike<RunInput, RunOutput>
  91. ]): RunnableBranch<RunInput, RunOutput>;
  92. _invoke(input: RunInput, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
  93. invoke(input: RunInput, config?: RunnableConfig): Promise<RunOutput>;
  94. _streamIterator(input: RunInput, config?: Partial<RunnableConfig>): AsyncGenerator<Awaited<RunOutput>, void, unknown>;
  95. }