router.d.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Runnable, type RunnableBatchOptions } from "./base.js";
  2. import { IterableReadableStream } from "../utils/stream.js";
  3. import { type RunnableConfig } from "./config.js";
  4. export type RouterInput = {
  5. key: string;
  6. input: any;
  7. };
  8. /**
  9. * A runnable that routes to a set of runnables based on Input['key'].
  10. * Returns the output of the selected runnable.
  11. * @example
  12. * ```typescript
  13. * import { RouterRunnable, RunnableLambda } from "@langchain/core/runnables";
  14. *
  15. * const router = new RouterRunnable({
  16. * runnables: {
  17. * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),
  18. * reverseText: RunnableLambda.from((text: string) =>
  19. * text.split("").reverse().join("")
  20. * ),
  21. * },
  22. * });
  23. *
  24. * // Invoke the 'reverseText' runnable
  25. * const result1 = router.invoke({ key: "reverseText", input: "Hello World" });
  26. *
  27. * // "dlroW olleH"
  28. *
  29. * // Invoke the 'toUpperCase' runnable
  30. * const result2 = router.invoke({ key: "toUpperCase", input: "Hello World" });
  31. *
  32. * // "HELLO WORLD"
  33. * ```
  34. */
  35. export declare class RouterRunnable<RunInput extends RouterInput, RunnableInput, RunOutput> extends Runnable<RunInput, RunOutput> {
  36. static lc_name(): string;
  37. lc_namespace: string[];
  38. lc_serializable: boolean;
  39. runnables: Record<string, Runnable<RunnableInput, RunOutput>>;
  40. constructor(fields: {
  41. runnables: Record<string, Runnable<RunnableInput, RunOutput>>;
  42. });
  43. invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
  44. batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions & {
  45. returnExceptions?: false;
  46. }): Promise<RunOutput[]>;
  47. batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions & {
  48. returnExceptions: true;
  49. }): Promise<(RunOutput | Error)[]>;
  50. batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
  51. stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
  52. }