base.cjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.BasePromptTemplate = void 0;
  6. const base_js_1 = require("../runnables/base.cjs");
  7. /**
  8. * Base class for prompt templates. Exposes a format method that returns a
  9. * string prompt given a set of input values.
  10. */
  11. class BasePromptTemplate extends base_js_1.Runnable {
  12. get lc_attributes() {
  13. return {
  14. partialVariables: undefined, // python doesn't support this yet
  15. };
  16. }
  17. constructor(input) {
  18. super(input);
  19. Object.defineProperty(this, "lc_serializable", {
  20. enumerable: true,
  21. configurable: true,
  22. writable: true,
  23. value: true
  24. });
  25. Object.defineProperty(this, "lc_namespace", {
  26. enumerable: true,
  27. configurable: true,
  28. writable: true,
  29. value: ["langchain_core", "prompts", this._getPromptType()]
  30. });
  31. Object.defineProperty(this, "inputVariables", {
  32. enumerable: true,
  33. configurable: true,
  34. writable: true,
  35. value: void 0
  36. });
  37. Object.defineProperty(this, "outputParser", {
  38. enumerable: true,
  39. configurable: true,
  40. writable: true,
  41. value: void 0
  42. });
  43. Object.defineProperty(this, "partialVariables", {
  44. enumerable: true,
  45. configurable: true,
  46. writable: true,
  47. value: void 0
  48. });
  49. /**
  50. * Metadata to be used for tracing.
  51. */
  52. Object.defineProperty(this, "metadata", {
  53. enumerable: true,
  54. configurable: true,
  55. writable: true,
  56. value: void 0
  57. });
  58. /** Tags to be used for tracing. */
  59. Object.defineProperty(this, "tags", {
  60. enumerable: true,
  61. configurable: true,
  62. writable: true,
  63. value: void 0
  64. });
  65. const { inputVariables } = input;
  66. if (inputVariables.includes("stop")) {
  67. throw new Error("Cannot have an input variable named 'stop', as it is used internally, please rename.");
  68. }
  69. Object.assign(this, input);
  70. }
  71. /**
  72. * Merges partial variables and user variables.
  73. * @param userVariables The user variables to merge with the partial variables.
  74. * @returns A Promise that resolves to an object containing the merged variables.
  75. */
  76. async mergePartialAndUserVariables(userVariables) {
  77. const partialVariables = this.partialVariables ?? {};
  78. const partialValues = {};
  79. for (const [key, value] of Object.entries(partialVariables)) {
  80. if (typeof value === "string") {
  81. partialValues[key] = value;
  82. }
  83. else {
  84. partialValues[key] = await value();
  85. }
  86. }
  87. const allKwargs = {
  88. ...partialValues,
  89. ...userVariables,
  90. };
  91. return allKwargs;
  92. }
  93. /**
  94. * Invokes the prompt template with the given input and options.
  95. * @param input The input to invoke the prompt template with.
  96. * @param options Optional configuration for the callback.
  97. * @returns A Promise that resolves to the output of the prompt template.
  98. */
  99. async invoke(input, options) {
  100. const metadata = {
  101. ...this.metadata,
  102. ...options?.metadata,
  103. };
  104. const tags = [...(this.tags ?? []), ...(options?.tags ?? [])];
  105. return this._callWithConfig((input) => this.formatPromptValue(input), input, { ...options, tags, metadata, runType: "prompt" });
  106. }
  107. /**
  108. * Return a json-like object representing this prompt template.
  109. * @deprecated
  110. */
  111. serialize() {
  112. throw new Error("Use .toJSON() instead");
  113. }
  114. /**
  115. * @deprecated
  116. * Load a prompt template from a json-like object describing it.
  117. *
  118. * @remarks
  119. * Deserializing needs to be async because templates (e.g. {@link FewShotPromptTemplate}) can
  120. * reference remote resources that we read asynchronously with a web
  121. * request.
  122. */
  123. static async deserialize(data) {
  124. switch (data._type) {
  125. case "prompt": {
  126. const { PromptTemplate } = await import("./prompt.js");
  127. return PromptTemplate.deserialize(data);
  128. }
  129. case undefined: {
  130. const { PromptTemplate } = await import("./prompt.js");
  131. return PromptTemplate.deserialize({ ...data, _type: "prompt" });
  132. }
  133. case "few_shot": {
  134. const { FewShotPromptTemplate } = await import("./few_shot.js");
  135. return FewShotPromptTemplate.deserialize(data);
  136. }
  137. default:
  138. throw new Error(`Invalid prompt type in config: ${data._type}`);
  139. }
  140. }
  141. }
  142. exports.BasePromptTemplate = BasePromptTemplate;