d5b87be988f4ddf445c4664044b81c86405798b3258ceafc6c95a13e43da6f9f.json 15 KB

1
  1. {"ast":null,"code":"import _asyncToGenerator from \"F:/workspace/202226701027/huinongbao-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { BasePromptTemplate } from \"./base.js\";\nimport { ChatPromptTemplate } from \"./chat.js\";\n/**\n * Class that handles a sequence of prompts, each of which may require\n * different input variables. Includes methods for formatting these\n * prompts, extracting required input values, and handling partial\n * prompts.\n * @example\n * ```typescript\n * const composedPrompt = new PipelinePromptTemplate({\n * pipelinePrompts: [\n * {\n * name: \"introduction\",\n * prompt: PromptTemplate.fromTemplate(`You are impersonating {person}.`),\n * },\n * {\n * name: \"example\",\n * prompt: PromptTemplate.fromTemplate(\n * `Here's an example of an interaction:\n * Q: {example_q}\n * A: {example_a}`,\n * ),\n * },\n * {\n * name: \"start\",\n * prompt: PromptTemplate.fromTemplate(\n * `Now, do this for real!\n * Q: {input}\n * A:`,\n * ),\n * },\n * ],\n * finalPrompt: PromptTemplate.fromTemplate(\n * `{introduction}\n * {example}\n * {start}`,\n * ),\n * });\n *\n * const formattedPrompt = await composedPrompt.format({\n * person: \"Elon Musk\",\n * example_q: `What's your favorite car?`,\n * example_a: \"Tesla\",\n * input: `What's your favorite social media site?`,\n * });\n * ```\n */\nexport class PipelinePromptTemplate extends BasePromptTemplate {\n static lc_name() {\n return \"PipelinePromptTemplate\";\n }\n constructor(input) {\n super({\n ...input,\n inputVariables: []\n });\n Object.defineProperty(this, \"pipelinePrompts\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"finalPrompt\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n this.pipelinePrompts = input.pipelinePrompts;\n this.finalPrompt = input.finalPrompt;\n this.inputVariables = this.computeInputValues();\n }\n /**\n * Computes the input values required by the pipeline prompts.\n * @returns Array of input values required by the pipeline prompts.\n */\n computeInputValues() {\n const intermediateValues = this.pipelinePrompts.map(pipelinePrompt => pipelinePrompt.name);\n const inputValues = this.pipelinePrompts.map(pipelinePrompt => pipelinePrompt.prompt.inputVariables.filter(inputValue => !intermediateValues.includes(inputValue))).flat();\n return [...new Set(inputValues)];\n }\n static extractRequiredInputValues(allValues, requiredValueNames) {\n return requiredValueNames.reduce((requiredValues, valueName) => {\n // eslint-disable-next-line no-param-reassign\n requiredValues[valueName] = allValues[valueName];\n return requiredValues;\n }, {});\n }\n /**\n * Formats the pipeline prompts based on the provided input values.\n * @param values Input values to format the pipeline prompts.\n * @returns Promise that resolves with the formatted input values.\n */\n formatPipelinePrompts(values) {\n var _this = this;\n return _asyncToGenerator(function* () {\n const allValues = yield _this.mergePartialAndUserVariables(values);\n for (const {\n name: pipelinePromptName,\n prompt: pipelinePrompt\n } of _this.pipelinePrompts) {\n const pipelinePromptInputValues = PipelinePromptTemplate.extractRequiredInputValues(allValues, pipelinePrompt.inputVariables);\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (pipelinePrompt instanceof ChatPromptTemplate) {\n allValues[pipelinePromptName] = yield pipelinePrompt.formatMessages(pipelinePromptInputValues);\n } else {\n allValues[pipelinePromptName] = yield pipelinePrompt.format(pipelinePromptInputValues);\n }\n }\n return PipelinePromptTemplate.extractRequiredInputValues(allValues, _this.finalPrompt.inputVariables);\n })();\n }\n /**\n * Formats the final prompt value based on the provided input values.\n * @param values Input values to format the final prompt value.\n * @returns Promise that resolves with the formatted final prompt value.\n */\n formatPromptValue(values) {\n var _this2 = this;\n return _asyncToGenerator(function* () {\n return _this2.finalPrompt.formatPromptValue(yield _this2.formatPipelinePrompts(values));\n })();\n }\n format(values) {\n var _this3 = this;\n return _asyncToGenerator(function* () {\n return _this3.finalPrompt.format(yield _this3.formatPipelinePrompts(values));\n })();\n }\n /**\n * Handles partial prompts, which are prompts that have been partially\n * filled with input values.\n * @param values Partial input values.\n * @returns Promise that resolves with a new PipelinePromptTemplate instance with updated input variables.\n */\n partial(values) {\n var _this4 = this;\n return _asyncToGenerator(function* () {\n var _this4$partialVariabl;\n const promptDict = {\n ..._this4\n };\n promptDict.inputVariables = _this4.inputVariables.filter(iv => !(iv in values));\n promptDict.partialVariables = {\n ...((_this4$partialVariabl = _this4.partialVariables) !== null && _this4$partialVariabl !== void 0 ? _this4$partialVariabl : {}),\n ...values\n };\n return new PipelinePromptTemplate(promptDict);\n })();\n }\n serialize() {\n throw new Error(\"Not implemented.\");\n }\n _getPromptType() {\n return \"pipeline\";\n }\n}","map":{"version":3,"names":["BasePromptTemplate","ChatPromptTemplate","PipelinePromptTemplate","lc_name","constructor","input","inputVariables","Object","defineProperty","enumerable","configurable","writable","value","pipelinePrompts","finalPrompt","computeInputValues","intermediateValues","map","pipelinePrompt","name","inputValues","prompt","filter","inputValue","includes","flat","Set","extractRequiredInputValues","allValues","requiredValueNames","reduce","requiredValues","valueName","formatPipelinePrompts","values","_this","_asyncToGenerator","mergePartialAndUserVariables","pipelinePromptName","pipelinePromptInputValues","formatMessages","format","formatPromptValue","_this2","_this3","partial","_this4","_this4$partialVariabl","promptDict","iv","partialVariables","serialize","Error","_getPromptType"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/@langchain/core/dist/prompts/pipeline.js"],"sourcesContent":["import { BasePromptTemplate } from \"./base.js\";\nimport { ChatPromptTemplate } from \"./chat.js\";\n/**\n * Class that handles a sequence of prompts, each of which may require\n * different input variables. Includes methods for formatting these\n * prompts, extracting required input values, and handling partial\n * prompts.\n * @example\n * ```typescript\n * const composedPrompt = new PipelinePromptTemplate({\n * pipelinePrompts: [\n * {\n * name: \"introduction\",\n * prompt: PromptTemplate.fromTemplate(`You are impersonating {person}.`),\n * },\n * {\n * name: \"example\",\n * prompt: PromptTemplate.fromTemplate(\n * `Here's an example of an interaction:\n * Q: {example_q}\n * A: {example_a}`,\n * ),\n * },\n * {\n * name: \"start\",\n * prompt: PromptTemplate.fromTemplate(\n * `Now, do this for real!\n * Q: {input}\n * A:`,\n * ),\n * },\n * ],\n * finalPrompt: PromptTemplate.fromTemplate(\n * `{introduction}\n * {example}\n * {start}`,\n * ),\n * });\n *\n * const formattedPrompt = await composedPrompt.format({\n * person: \"Elon Musk\",\n * example_q: `What's your favorite car?`,\n * example_a: \"Tesla\",\n * input: `What's your favorite social media site?`,\n * });\n * ```\n */\nexport class PipelinePromptTemplate extends BasePromptTemplate {\n static lc_name() {\n return \"PipelinePromptTemplate\";\n }\n constructor(input) {\n super({ ...input, inputVariables: [] });\n Object.defineProperty(this, \"pipelinePrompts\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"finalPrompt\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n this.pipelinePrompts = input.pipelinePrompts;\n this.finalPrompt = input.finalPrompt;\n this.inputVariables = this.computeInputValues();\n }\n /**\n * Computes the input values required by the pipeline prompts.\n * @returns Array of input values required by the pipeline prompts.\n */\n computeInputValues() {\n const intermediateValues = this.pipelinePrompts.map((pipelinePrompt) => pipelinePrompt.name);\n const inputValues = this.pipelinePrompts\n .map((pipelinePrompt) => pipelinePrompt.prompt.inputVariables.filter((inputValue) => !intermediateValues.includes(inputValue)))\n .flat();\n return [...new Set(inputValues)];\n }\n static extractRequiredInputValues(allValues, requiredValueNames) {\n return requiredValueNames.reduce((requiredValues, valueName) => {\n // eslint-disable-next-line no-param-reassign\n requiredValues[valueName] = allValues[valueName];\n return requiredValues;\n }, {});\n }\n /**\n * Formats the pipeline prompts based on the provided input values.\n * @param values Input values to format the pipeline prompts.\n * @returns Promise that resolves with the formatted input values.\n */\n async formatPipelinePrompts(values) {\n const allValues = await this.mergePartialAndUserVariables(values);\n for (const { name: pipelinePromptName, prompt: pipelinePrompt } of this\n .pipelinePrompts) {\n const pipelinePromptInputValues = PipelinePromptTemplate.extractRequiredInputValues(allValues, pipelinePrompt.inputVariables);\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (pipelinePrompt instanceof ChatPromptTemplate) {\n allValues[pipelinePromptName] = await pipelinePrompt.formatMessages(pipelinePromptInputValues);\n }\n else {\n allValues[pipelinePromptName] = await pipelinePrompt.format(pipelinePromptInputValues);\n }\n }\n return PipelinePromptTemplate.extractRequiredInputValues(allValues, this.finalPrompt.inputVariables);\n }\n /**\n * Formats the final prompt value based on the provided input values.\n * @param values Input values to format the final prompt value.\n * @returns Promise that resolves with the formatted final prompt value.\n */\n async formatPromptValue(values) {\n return this.finalPrompt.formatPromptValue(await this.formatPipelinePrompts(values));\n }\n async format(values) {\n return this.finalPrompt.format(await this.formatPipelinePrompts(values));\n }\n /**\n * Handles partial prompts, which are prompts that have been partially\n * filled with input values.\n * @param values Partial input values.\n * @returns Promise that resolves with a new PipelinePromptTemplate instance with updated input variables.\n */\n async partial(values) {\n const promptDict = { ...this };\n promptDict.inputVariables = this.inputVariables.filter((iv) => !(iv in values));\n promptDict.partialVariables = {\n ...(this.partialVariables ?? {}),\n ...values,\n };\n return new PipelinePromptTemplate(promptDict);\n }\n serialize() {\n throw new Error(\"Not implemented.\");\n }\n _getPromptType() {\n return \"pipeline\";\n }\n}\n"],"mappings":";AAAA,SAASA,kBAAkB,QAAQ,WAAW;AAC9C,SAASC,kBAAkB,QAAQ,WAAW;AAC9C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,SAASF,kBAAkB,CAAC;EAC3D,OAAOG,OAAOA,CAAA,EAAG;IACb,OAAO,wBAAwB;EACnC;EACAC,WAAWA,CAACC,KAAK,EAAE;IACf,KAAK,CAAC;MAAE,GAAGA,KAAK;MAAEC,cAAc,EAAE;IAAG,CAAC,CAAC;IACvCC,MAAM,CAACC,cAAc,CAAC,IAAI,EAAE,iBAAiB,EA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