prompt.cjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. // Default generic "any" values are for backwards compatibility.
  3. // Replace with "string" when we are comfortable with a breaking change.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.PromptTemplate = void 0;
  6. const string_js_1 = require("./string.cjs");
  7. const template_js_1 = require("./template.cjs");
  8. /**
  9. * Schema to represent a basic prompt for an LLM.
  10. * @augments BasePromptTemplate
  11. * @augments PromptTemplateInput
  12. *
  13. * @example
  14. * ```ts
  15. * import { PromptTemplate } from "langchain/prompts";
  16. *
  17. * const prompt = new PromptTemplate({
  18. * inputVariables: ["foo"],
  19. * template: "Say {foo}",
  20. * });
  21. * ```
  22. */
  23. class PromptTemplate extends string_js_1.BaseStringPromptTemplate {
  24. static lc_name() {
  25. return "PromptTemplate";
  26. }
  27. constructor(input) {
  28. super(input);
  29. Object.defineProperty(this, "template", {
  30. enumerable: true,
  31. configurable: true,
  32. writable: true,
  33. value: void 0
  34. });
  35. Object.defineProperty(this, "templateFormat", {
  36. enumerable: true,
  37. configurable: true,
  38. writable: true,
  39. value: "f-string"
  40. });
  41. Object.defineProperty(this, "validateTemplate", {
  42. enumerable: true,
  43. configurable: true,
  44. writable: true,
  45. value: true
  46. });
  47. /**
  48. * Additional fields which should be included inside
  49. * the message content array if using a complex message
  50. * content.
  51. */
  52. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  53. Object.defineProperty(this, "additionalContentFields", {
  54. enumerable: true,
  55. configurable: true,
  56. writable: true,
  57. value: void 0
  58. });
  59. // If input is mustache and validateTemplate is not defined, set it to false
  60. if (input.templateFormat === "mustache" &&
  61. input.validateTemplate === undefined) {
  62. this.validateTemplate = false;
  63. }
  64. Object.assign(this, input);
  65. if (this.validateTemplate) {
  66. if (this.templateFormat === "mustache") {
  67. throw new Error("Mustache templates cannot be validated.");
  68. }
  69. let totalInputVariables = this.inputVariables;
  70. if (this.partialVariables) {
  71. totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
  72. }
  73. (0, template_js_1.checkValidTemplate)(this.template, this.templateFormat, totalInputVariables);
  74. }
  75. }
  76. _getPromptType() {
  77. return "prompt";
  78. }
  79. /**
  80. * Formats the prompt template with the provided values.
  81. * @param values The values to be used to format the prompt template.
  82. * @returns A promise that resolves to a string which is the formatted prompt.
  83. */
  84. async format(values) {
  85. const allValues = await this.mergePartialAndUserVariables(values);
  86. return (0, template_js_1.renderTemplate)(this.template, this.templateFormat, allValues);
  87. }
  88. /**
  89. * Take examples in list format with prefix and suffix to create a prompt.
  90. *
  91. * Intended to be used a a way to dynamically create a prompt from examples.
  92. *
  93. * @param examples - List of examples to use in the prompt.
  94. * @param suffix - String to go after the list of examples. Should generally set up the user's input.
  95. * @param inputVariables - A list of variable names the final prompt template will expect
  96. * @param exampleSeparator - The separator to use in between examples
  97. * @param prefix - String that should go before any examples. Generally includes examples.
  98. *
  99. * @returns The final prompt template generated.
  100. */
  101. static fromExamples(examples, suffix, inputVariables, exampleSeparator = "\n\n", prefix = "") {
  102. const template = [prefix, ...examples, suffix].join(exampleSeparator);
  103. return new PromptTemplate({
  104. inputVariables,
  105. template,
  106. });
  107. }
  108. static fromTemplate(template, options) {
  109. const { templateFormat = "f-string", ...rest } = options ?? {};
  110. const names = new Set();
  111. (0, template_js_1.parseTemplate)(template, templateFormat).forEach((node) => {
  112. if (node.type === "variable") {
  113. names.add(node.name);
  114. }
  115. });
  116. return new PromptTemplate({
  117. // Rely on extracted types
  118. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  119. inputVariables: [...names],
  120. templateFormat,
  121. template,
  122. ...rest,
  123. });
  124. }
  125. /**
  126. * Partially applies values to the prompt template.
  127. * @param values The values to be partially applied to the prompt template.
  128. * @returns A new instance of PromptTemplate with the partially applied values.
  129. */
  130. async partial(values) {
  131. const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
  132. const newPartialVariables = {
  133. ...(this.partialVariables ?? {}),
  134. ...values,
  135. };
  136. const promptDict = {
  137. ...this,
  138. inputVariables: newInputVariables,
  139. partialVariables: newPartialVariables,
  140. };
  141. return new PromptTemplate(promptDict);
  142. }
  143. serialize() {
  144. if (this.outputParser !== undefined) {
  145. throw new Error("Cannot serialize a prompt template with an output parser");
  146. }
  147. return {
  148. _type: this._getPromptType(),
  149. input_variables: this.inputVariables,
  150. template: this.template,
  151. template_format: this.templateFormat,
  152. };
  153. }
  154. static async deserialize(data) {
  155. if (!data.template) {
  156. throw new Error("Prompt template must have a template");
  157. }
  158. const res = new PromptTemplate({
  159. inputVariables: data.input_variables,
  160. template: data.template,
  161. templateFormat: data.template_format,
  162. });
  163. return res;
  164. }
  165. }
  166. exports.PromptTemplate = PromptTemplate;