passthrough.d.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Runnable, RunnableAssign, RunnableMapLike } from "./base.js";
  2. import { type RunnableConfig } from "./config.js";
  3. type RunnablePassthroughFunc<RunInput = any> = ((input: RunInput) => void) | ((input: RunInput, config?: RunnableConfig) => void) | ((input: RunInput) => Promise<void>) | ((input: RunInput, config?: RunnableConfig) => Promise<void>);
  4. /**
  5. * A runnable to passthrough inputs unchanged or with additional keys.
  6. *
  7. * This runnable behaves almost like the identity function, except that it
  8. * can be configured to add additional keys to the output, if the input is
  9. * an object.
  10. *
  11. * The example below demonstrates how to use `RunnablePassthrough to
  12. * passthrough the input from the `.invoke()`
  13. *
  14. * @example
  15. * ```typescript
  16. * const chain = RunnableSequence.from([
  17. * {
  18. * question: new RunnablePassthrough(),
  19. * context: async () => loadContextFromStore(),
  20. * },
  21. * prompt,
  22. * llm,
  23. * outputParser,
  24. * ]);
  25. * const response = await chain.invoke(
  26. * "I can pass a single string instead of an object since I'm using `RunnablePassthrough`."
  27. * );
  28. * ```
  29. */
  30. export declare class RunnablePassthrough<RunInput = any> extends Runnable<RunInput, RunInput> {
  31. static lc_name(): string;
  32. lc_namespace: string[];
  33. lc_serializable: boolean;
  34. func?: RunnablePassthroughFunc<RunInput>;
  35. constructor(fields?: {
  36. func?: RunnablePassthroughFunc<RunInput>;
  37. });
  38. invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunInput>;
  39. transform(generator: AsyncGenerator<RunInput>, options: Partial<RunnableConfig>): AsyncGenerator<RunInput>;
  40. /**
  41. * A runnable that assigns key-value pairs to the input.
  42. *
  43. * The example below shows how you could use it with an inline function.
  44. *
  45. * @example
  46. * ```typescript
  47. * const prompt =
  48. * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}
  49. * Question: {question}
  50. * SQL Query:`);
  51. *
  52. * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`
  53. * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.
  54. * // In this case, we're passing the schema.
  55. * const sqlQueryGeneratorChain = RunnableSequence.from([
  56. * RunnablePassthrough.assign({
  57. * schema: async () => db.getTableInfo(),
  58. * }),
  59. * prompt,
  60. * new ChatOpenAI({}).withConfig({ stop: ["\nSQLResult:"] }),
  61. * new StringOutputParser(),
  62. * ]);
  63. * const result = await sqlQueryGeneratorChain.invoke({
  64. * question: "How many employees are there?",
  65. * });
  66. * ```
  67. */
  68. static assign<RunInput extends Record<string, unknown> = Record<string, unknown>, RunOutput extends Record<string, unknown> = Record<string, unknown>>(mapping: RunnableMapLike<RunInput, RunOutput>): RunnableAssign<RunInput, RunInput & RunOutput>;
  69. }
  70. export {};