pipeline.cjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.PipelinePromptTemplate = void 0;
  4. const base_js_1 = require("./base.cjs");
  5. const chat_js_1 = require("./chat.cjs");
  6. /**
  7. * Class that handles a sequence of prompts, each of which may require
  8. * different input variables. Includes methods for formatting these
  9. * prompts, extracting required input values, and handling partial
  10. * prompts.
  11. * @example
  12. * ```typescript
  13. * const composedPrompt = new PipelinePromptTemplate({
  14. * pipelinePrompts: [
  15. * {
  16. * name: "introduction",
  17. * prompt: PromptTemplate.fromTemplate(`You are impersonating {person}.`),
  18. * },
  19. * {
  20. * name: "example",
  21. * prompt: PromptTemplate.fromTemplate(
  22. * `Here's an example of an interaction:
  23. * Q: {example_q}
  24. * A: {example_a}`,
  25. * ),
  26. * },
  27. * {
  28. * name: "start",
  29. * prompt: PromptTemplate.fromTemplate(
  30. * `Now, do this for real!
  31. * Q: {input}
  32. * A:`,
  33. * ),
  34. * },
  35. * ],
  36. * finalPrompt: PromptTemplate.fromTemplate(
  37. * `{introduction}
  38. * {example}
  39. * {start}`,
  40. * ),
  41. * });
  42. *
  43. * const formattedPrompt = await composedPrompt.format({
  44. * person: "Elon Musk",
  45. * example_q: `What's your favorite car?`,
  46. * example_a: "Tesla",
  47. * input: `What's your favorite social media site?`,
  48. * });
  49. * ```
  50. */
  51. class PipelinePromptTemplate extends base_js_1.BasePromptTemplate {
  52. static lc_name() {
  53. return "PipelinePromptTemplate";
  54. }
  55. constructor(input) {
  56. super({ ...input, inputVariables: [] });
  57. Object.defineProperty(this, "pipelinePrompts", {
  58. enumerable: true,
  59. configurable: true,
  60. writable: true,
  61. value: void 0
  62. });
  63. Object.defineProperty(this, "finalPrompt", {
  64. enumerable: true,
  65. configurable: true,
  66. writable: true,
  67. value: void 0
  68. });
  69. this.pipelinePrompts = input.pipelinePrompts;
  70. this.finalPrompt = input.finalPrompt;
  71. this.inputVariables = this.computeInputValues();
  72. }
  73. /**
  74. * Computes the input values required by the pipeline prompts.
  75. * @returns Array of input values required by the pipeline prompts.
  76. */
  77. computeInputValues() {
  78. const intermediateValues = this.pipelinePrompts.map((pipelinePrompt) => pipelinePrompt.name);
  79. const inputValues = this.pipelinePrompts
  80. .map((pipelinePrompt) => pipelinePrompt.prompt.inputVariables.filter((inputValue) => !intermediateValues.includes(inputValue)))
  81. .flat();
  82. return [...new Set(inputValues)];
  83. }
  84. static extractRequiredInputValues(allValues, requiredValueNames) {
  85. return requiredValueNames.reduce((requiredValues, valueName) => {
  86. // eslint-disable-next-line no-param-reassign
  87. requiredValues[valueName] = allValues[valueName];
  88. return requiredValues;
  89. }, {});
  90. }
  91. /**
  92. * Formats the pipeline prompts based on the provided input values.
  93. * @param values Input values to format the pipeline prompts.
  94. * @returns Promise that resolves with the formatted input values.
  95. */
  96. async formatPipelinePrompts(values) {
  97. const allValues = await this.mergePartialAndUserVariables(values);
  98. for (const { name: pipelinePromptName, prompt: pipelinePrompt } of this
  99. .pipelinePrompts) {
  100. const pipelinePromptInputValues = PipelinePromptTemplate.extractRequiredInputValues(allValues, pipelinePrompt.inputVariables);
  101. // eslint-disable-next-line no-instanceof/no-instanceof
  102. if (pipelinePrompt instanceof chat_js_1.ChatPromptTemplate) {
  103. allValues[pipelinePromptName] = await pipelinePrompt.formatMessages(pipelinePromptInputValues);
  104. }
  105. else {
  106. allValues[pipelinePromptName] = await pipelinePrompt.format(pipelinePromptInputValues);
  107. }
  108. }
  109. return PipelinePromptTemplate.extractRequiredInputValues(allValues, this.finalPrompt.inputVariables);
  110. }
  111. /**
  112. * Formats the final prompt value based on the provided input values.
  113. * @param values Input values to format the final prompt value.
  114. * @returns Promise that resolves with the formatted final prompt value.
  115. */
  116. async formatPromptValue(values) {
  117. return this.finalPrompt.formatPromptValue(await this.formatPipelinePrompts(values));
  118. }
  119. async format(values) {
  120. return this.finalPrompt.format(await this.formatPipelinePrompts(values));
  121. }
  122. /**
  123. * Handles partial prompts, which are prompts that have been partially
  124. * filled with input values.
  125. * @param values Partial input values.
  126. * @returns Promise that resolves with a new PipelinePromptTemplate instance with updated input variables.
  127. */
  128. async partial(values) {
  129. const promptDict = { ...this };
  130. promptDict.inputVariables = this.inputVariables.filter((iv) => !(iv in values));
  131. promptDict.partialVariables = {
  132. ...(this.partialVariables ?? {}),
  133. ...values,
  134. };
  135. return new PipelinePromptTemplate(promptDict);
  136. }
  137. serialize() {
  138. throw new Error("Not implemented.");
  139. }
  140. _getPromptType() {
  141. return "pipeline";
  142. }
  143. }
  144. exports.PipelinePromptTemplate = PipelinePromptTemplate;