@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.3 kB
Source Map (JSON)
{"version":3,"file":"branch.d.ts","names":["Runnable","RunnableLike","RunnableConfig","CallbackManagerForChainRun","Branch","RunInput","RunOutput","BranchLike","RunnableBranch","Partial","Promise","Awaited","AsyncGenerator"],"sources":["../../src/runnables/branch.d.ts"],"sourcesContent":["import { Runnable, RunnableLike } from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { CallbackManagerForChainRun } from \"../callbacks/manager.js\";\n/**\n * Type for a branch in the RunnableBranch. It consists of a condition\n * runnable and a branch runnable. The condition runnable is used to\n * determine whether the branch should be executed, and the branch runnable\n * is executed if the condition is true.\n */\nexport type Branch<RunInput, RunOutput> = [\n Runnable<RunInput, boolean>,\n Runnable<RunInput, RunOutput>\n];\nexport type BranchLike<RunInput, RunOutput> = [\n RunnableLike<RunInput, boolean>,\n RunnableLike<RunInput, RunOutput>\n];\n/**\n * Class that represents a runnable branch. The RunnableBranch is\n * initialized with an array of branches and a default branch. When invoked,\n * it evaluates the condition of each branch in order and executes the\n * corresponding branch if the condition is true. If none of the conditions\n * are true, it executes the default branch.\n * @example\n * ```typescript\n * const branch = RunnableBranch.from([\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"anthropic\"),\n * anthropicChain,\n * ],\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"langchain\"),\n * langChainChain,\n * ],\n * generalChain,\n * ]);\n *\n * const fullChain = RunnableSequence.from([\n * {\n * topic: classificationChain,\n * question: (input: { question: string }) => input.question,\n * },\n * branch,\n * ]);\n *\n * const result = await fullChain.invoke({\n * question: \"how do I use LangChain?\",\n * });\n * ```\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport declare class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<RunInput, RunOutput> {\n static lc_name(): string;\n lc_namespace: string[];\n lc_serializable: boolean;\n default: Runnable<RunInput, RunOutput>;\n branches: Branch<RunInput, RunOutput>[];\n constructor(fields: {\n branches: Branch<RunInput, RunOutput>[];\n default: Runnable<RunInput, RunOutput>;\n });\n /**\n * Convenience method for instantiating a RunnableBranch from\n * RunnableLikes (objects, functions, or Runnables).\n *\n * Each item in the input except for the last one should be a\n * tuple with two items. The first is a \"condition\" RunnableLike that\n * returns \"true\" if the second RunnableLike in the tuple should run.\n *\n * The final item in the input should be a RunnableLike that acts as a\n * default branch if no other branches match.\n *\n * @example\n * ```ts\n * import { RunnableBranch } from \"@langchain/core/runnables\";\n *\n * const branch = RunnableBranch.from([\n * [(x: number) => x > 0, (x: number) => x + 1],\n * [(x: number) => x < 0, (x: number) => x - 1],\n * (x: number) => x\n * ]);\n * ```\n * @param branches An array where the every item except the last is a tuple of [condition, runnable]\n * pairs. The last item is a default runnable which is invoked if no other condition matches.\n * @returns A new RunnableBranch.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n static from<RunInput = any, RunOutput = any>(branches: [\n ...BranchLike<RunInput, RunOutput>[],\n RunnableLike<RunInput, RunOutput>\n ]): RunnableBranch<RunInput, RunOutput>;\n _invoke(input: RunInput, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;\n invoke(input: RunInput, config?: RunnableConfig): Promise<RunOutput>;\n _streamIterator(input: RunInput, config?: Partial<RunnableConfig>): AsyncGenerator<Awaited<RunOutput>, void, unknown>;\n}\n"],"mappings":";;;;;;;;AASA;;;;AAEaK,KAFDD,MAECC,CAAAA,QAAAA,EAAAA,SAAAA,CAAAA,GAAAA,CADTL,QACmBM,CADVD,QACUC,EAAAA,OAAAA,CAAAA,EAAnBN,QAAAA,CAASK,QAATL,EAAmBM,SAAnBN,CAAAA,CAAQ;AAEAO,KAAAA,UAAU,CAAA,QAAA,EAAA,SAAA,CAAA,GAAA,CAClBN,YADkB,CACLI,QADK,EAAA,OAAA,CAAA,EAElBJ,YADaI,CACAA,QADAA,EACUC,SADVD,CAAAA,CAAQ;;;;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