image.cjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ImagePromptTemplate = void 0;
  4. const prompt_values_js_1 = require("../prompt_values.cjs");
  5. const base_js_1 = require("./base.cjs");
  6. const template_js_1 = require("./template.cjs");
  7. /**
  8. * An image prompt template for a multimodal model.
  9. */
  10. class ImagePromptTemplate extends base_js_1.BasePromptTemplate {
  11. static lc_name() {
  12. return "ImagePromptTemplate";
  13. }
  14. constructor(input) {
  15. super(input);
  16. Object.defineProperty(this, "lc_namespace", {
  17. enumerable: true,
  18. configurable: true,
  19. writable: true,
  20. value: ["langchain_core", "prompts", "image"]
  21. });
  22. Object.defineProperty(this, "template", {
  23. enumerable: true,
  24. configurable: true,
  25. writable: true,
  26. value: void 0
  27. });
  28. Object.defineProperty(this, "templateFormat", {
  29. enumerable: true,
  30. configurable: true,
  31. writable: true,
  32. value: "f-string"
  33. });
  34. Object.defineProperty(this, "validateTemplate", {
  35. enumerable: true,
  36. configurable: true,
  37. writable: true,
  38. value: true
  39. });
  40. /**
  41. * Additional fields which should be included inside
  42. * the message content array if using a complex message
  43. * content.
  44. */
  45. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  46. Object.defineProperty(this, "additionalContentFields", {
  47. enumerable: true,
  48. configurable: true,
  49. writable: true,
  50. value: void 0
  51. });
  52. this.template = input.template;
  53. this.templateFormat = input.templateFormat ?? this.templateFormat;
  54. this.validateTemplate = input.validateTemplate ?? this.validateTemplate;
  55. this.additionalContentFields = input.additionalContentFields;
  56. if (this.validateTemplate) {
  57. let totalInputVariables = this.inputVariables;
  58. if (this.partialVariables) {
  59. totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
  60. }
  61. (0, template_js_1.checkValidTemplate)([
  62. { type: "image_url", image_url: this.template },
  63. ], this.templateFormat, totalInputVariables);
  64. }
  65. }
  66. _getPromptType() {
  67. return "prompt";
  68. }
  69. /**
  70. * Partially applies values to the prompt template.
  71. * @param values The values to be partially applied to the prompt template.
  72. * @returns A new instance of ImagePromptTemplate with the partially applied values.
  73. */
  74. async partial(values) {
  75. const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
  76. const newPartialVariables = {
  77. ...(this.partialVariables ?? {}),
  78. ...values,
  79. };
  80. const promptDict = {
  81. ...this,
  82. inputVariables: newInputVariables,
  83. partialVariables: newPartialVariables,
  84. };
  85. return new ImagePromptTemplate(promptDict);
  86. }
  87. /**
  88. * Formats the prompt template with the provided values.
  89. * @param values The values to be used to format the prompt template.
  90. * @returns A promise that resolves to a string which is the formatted prompt.
  91. */
  92. async format(values) {
  93. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  94. const formatted = {};
  95. for (const [key, value] of Object.entries(this.template)) {
  96. if (typeof value === "string") {
  97. formatted[key] = (0, template_js_1.renderTemplate)(value, this.templateFormat, values);
  98. }
  99. else {
  100. formatted[key] = value;
  101. }
  102. }
  103. const url = values.url || formatted.url;
  104. const detail = values.detail || formatted.detail;
  105. if (!url) {
  106. throw new Error("Must provide either an image URL.");
  107. }
  108. if (typeof url !== "string") {
  109. throw new Error("url must be a string.");
  110. }
  111. const output = { url };
  112. if (detail) {
  113. output.detail = detail;
  114. }
  115. return output;
  116. }
  117. /**
  118. * Formats the prompt given the input values and returns a formatted
  119. * prompt value.
  120. * @param values The input values to format the prompt.
  121. * @returns A Promise that resolves to a formatted prompt value.
  122. */
  123. async formatPromptValue(values) {
  124. const formattedPrompt = await this.format(values);
  125. return new prompt_values_js_1.ImagePromptValue(formattedPrompt);
  126. }
  127. }
  128. exports.ImagePromptTemplate = ImagePromptTemplate;