123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.ImagePromptTemplate = void 0;
- const prompt_values_js_1 = require("../prompt_values.cjs");
- const base_js_1 = require("./base.cjs");
- const template_js_1 = require("./template.cjs");
- /**
- * An image prompt template for a multimodal model.
- */
- class ImagePromptTemplate extends base_js_1.BasePromptTemplate {
- static lc_name() {
- return "ImagePromptTemplate";
- }
- constructor(input) {
- super(input);
- Object.defineProperty(this, "lc_namespace", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: ["langchain_core", "prompts", "image"]
- });
- Object.defineProperty(this, "template", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "templateFormat", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: "f-string"
- });
- Object.defineProperty(this, "validateTemplate", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: true
- });
- /**
- * Additional fields which should be included inside
- * the message content array if using a complex message
- * content.
- */
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- Object.defineProperty(this, "additionalContentFields", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- this.template = input.template;
- this.templateFormat = input.templateFormat ?? this.templateFormat;
- this.validateTemplate = input.validateTemplate ?? this.validateTemplate;
- this.additionalContentFields = input.additionalContentFields;
- if (this.validateTemplate) {
- let totalInputVariables = this.inputVariables;
- if (this.partialVariables) {
- totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
- }
- (0, template_js_1.checkValidTemplate)([
- { type: "image_url", image_url: this.template },
- ], this.templateFormat, totalInputVariables);
- }
- }
- _getPromptType() {
- return "prompt";
- }
- /**
- * Partially applies values to the prompt template.
- * @param values The values to be partially applied to the prompt template.
- * @returns A new instance of ImagePromptTemplate with the partially applied values.
- */
- async partial(values) {
- const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
- const newPartialVariables = {
- ...(this.partialVariables ?? {}),
- ...values,
- };
- const promptDict = {
- ...this,
- inputVariables: newInputVariables,
- partialVariables: newPartialVariables,
- };
- return new ImagePromptTemplate(promptDict);
- }
- /**
- * Formats the prompt template with the provided values.
- * @param values The values to be used to format the prompt template.
- * @returns A promise that resolves to a string which is the formatted prompt.
- */
- async format(values) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- const formatted = {};
- for (const [key, value] of Object.entries(this.template)) {
- if (typeof value === "string") {
- formatted[key] = (0, template_js_1.renderTemplate)(value, this.templateFormat, values);
- }
- else {
- formatted[key] = value;
- }
- }
- const url = values.url || formatted.url;
- const detail = values.detail || formatted.detail;
- if (!url) {
- throw new Error("Must provide either an image URL.");
- }
- if (typeof url !== "string") {
- throw new Error("url must be a string.");
- }
- const output = { url };
- if (detail) {
- output.detail = detail;
- }
- return output;
- }
- /**
- * Formats the prompt given the input values and returns a formatted
- * prompt value.
- * @param values The input values to format the prompt.
- * @returns A Promise that resolves to a formatted prompt value.
- */
- async formatPromptValue(values) {
- const formattedPrompt = await this.format(values);
- return new prompt_values_js_1.ImagePromptValue(formattedPrompt);
- }
- }
- exports.ImagePromptTemplate = ImagePromptTemplate;
|