image.js 4.4 KB

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