UNPKG

@langchain/core

Version:
1 lines 5.66 kB
{"version":3,"file":"passthrough.cjs","names":["Runnable","ensureConfig","concat","RunnableAssign","RunnableMap"],"sources":["../../src/runnables/passthrough.ts"],"sourcesContent":["import { concat } from \"../utils/stream.js\";\nimport {\n Runnable,\n RunnableAssign,\n RunnableMap,\n RunnableMapLike,\n} from \"./base.js\";\nimport { ensureConfig, type RunnableConfig } from \"./config.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype RunnablePassthroughFunc<RunInput = any> =\n | ((input: RunInput) => void)\n | ((input: RunInput, config?: RunnableConfig) => void)\n | ((input: RunInput) => Promise<void>)\n | ((input: RunInput, config?: RunnableConfig) => Promise<void>);\n\n/**\n * A runnable to passthrough inputs unchanged or with additional keys.\n *\n * This runnable behaves almost like the identity function, except that it\n * can be configured to add additional keys to the output, if the input is\n * an object.\n *\n * The example below demonstrates how to use `RunnablePassthrough to\n * passthrough the input from the `.invoke()`\n *\n * @example\n * ```typescript\n * const chain = RunnableSequence.from([\n * {\n * question: new RunnablePassthrough(),\n * context: async () => loadContextFromStore(),\n * },\n * prompt,\n * llm,\n * outputParser,\n * ]);\n * const response = await chain.invoke(\n * \"I can pass a single string instead of an object since I'm using `RunnablePassthrough`.\"\n * );\n * ```\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport class RunnablePassthrough<RunInput = any> extends Runnable<\n RunInput,\n RunInput\n> {\n static lc_name() {\n return \"RunnablePassthrough\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n func?: RunnablePassthroughFunc<RunInput>;\n\n constructor(fields?: { func?: RunnablePassthroughFunc<RunInput> }) {\n super(fields);\n if (fields) {\n this.func = fields.func;\n }\n }\n\n async invoke(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<RunInput> {\n const config = ensureConfig(options);\n if (this.func) {\n await this.func(input, config);\n }\n\n return this._callWithConfig(\n (input: RunInput) => Promise.resolve(input),\n input,\n config\n );\n }\n\n async *transform(\n generator: AsyncGenerator<RunInput>,\n options: Partial<RunnableConfig>\n ): AsyncGenerator<RunInput> {\n const config = ensureConfig(options);\n let finalOutput: RunInput | undefined;\n let finalOutputSupported = true;\n\n for await (const chunk of this._transformStreamWithConfig(\n generator,\n (input: AsyncGenerator<RunInput>) => input,\n config\n )) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n finalOutput = concat(finalOutput, chunk as any);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n\n if (this.func && finalOutput !== undefined) {\n await this.func(finalOutput, config);\n }\n }\n\n /**\n * A runnable that assigns key-value pairs to the input.\n *\n * The example below shows how you could use it with an inline function.\n *\n * @example\n * ```typescript\n * const prompt =\n * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}\n * Question: {question}\n * SQL Query:`);\n *\n * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`\n * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.\n * // In this case, we're passing the schema.\n * const sqlQueryGeneratorChain = RunnableSequence.from([\n * RunnablePassthrough.assign({\n * schema: async () => db.getTableInfo(),\n * }),\n * prompt,\n * new ChatOpenAI({ model: \"gpt-4o-mini\" }).withConfig({ stop: [\"\\nSQLResult:\"] }),\n * new StringOutputParser(),\n * ]);\n * const result = await sqlQueryGeneratorChain.invoke({\n * question: \"How many employees are there?\",\n * });\n * ```\n */\n static assign<\n RunInput extends Record<string, unknown> = Record<string, unknown>,\n RunOutput extends Record<string, unknown> = Record<string, unknown>,\n >(\n mapping: RunnableMapLike<RunInput, RunOutput>\n ): RunnableAssign<RunInput, RunInput & RunOutput> {\n return new RunnableAssign(new RunnableMap({ steps: mapping }));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,sBAAb,cAAyDA,aAAAA,SAGvD;CACA,OAAO,UAAU;EACf,OAAO;CACT;CAEA,eAAe,CAAC,kBAAkB,WAAW;CAE7C,kBAAkB;CAElB;CAEA,YAAY,QAAuD;EACjE,MAAM,MAAM;EACZ,IAAI,QACF,KAAK,OAAO,OAAO;CAEvB;CAEA,MAAM,OACJ,OACA,SACmB;EACnB,MAAM,SAASC,eAAAA,aAAa,OAAO;EACnC,IAAI,KAAK,MACP,MAAM,KAAK,KAAK,OAAO,MAAM;EAG/B,OAAO,KAAK,iBACT,UAAoB,QAAQ,QAAQ,KAAK,GAC1C,OACA,MACF;CACF;CAEA,OAAO,UACL,WACA,SAC0B;EAC1B,MAAM,SAASA,eAAAA,aAAa,OAAO;EACnC,IAAI;EACJ,IAAI,uBAAuB;EAE3B,WAAW,MAAM,SAAS,KAAK,2BAC7B,YACC,UAAoC,OACrC,MACF,GAAG;GACD,MAAM;GACN,IAAI,sBACF,IAAI,gBAAgB,KAAA,GAClB,cAAc;QAEd,IAAI;IAEF,cAAcC,qBAAAA,OAAO,aAAa,KAAY;GAChD,QAAQ;IACN,cAAc,KAAA;IACd,uBAAuB;GACzB;EAGN;EAEA,IAAI,KAAK,QAAQ,gBAAgB,KAAA,GAC/B,MAAM,KAAK,KAAK,aAAa,MAAM;CAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8BA,OAAO,OAIL,SACgD;EAChD,OAAO,IAAIC,aAAAA,eAAe,IAAIC,aAAAA,YAAY,EAAE,OAAO,QAAQ,CAAC,CAAC;CAC/D;AACF"}