UNPKG

@langchain/core

Version:
1 lines 7.13 kB
{"version":3,"file":"image.cjs","names":["BasePromptTemplate","renderTemplate","ImagePromptValue"],"sources":["../../src/prompts/image.ts"],"sourcesContent":["import { MessageContent, ContentBlock } from \"../messages/index.js\";\nimport { ImagePromptValue, ImageContent } from \"../prompt_values.js\";\nimport type { InputValues, PartialValues } from \"../utils/types/index.js\";\nimport {\n BasePromptTemplate,\n BasePromptTemplateInput,\n TypedPromptInputValues,\n} from \"./base.js\";\nimport {\n TemplateFormat,\n checkValidTemplate,\n renderTemplate,\n} from \"./template.js\";\n\n/**\n * Inputs to create a {@link ImagePromptTemplate}\n * @augments BasePromptTemplateInput\n */\nexport interface ImagePromptTemplateInput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any,\n> extends BasePromptTemplateInput<RunInput, PartialVariableName> {\n /**\n * The prompt template\n */\n template: Record<string, unknown>;\n\n /**\n * The format of the prompt template. Options are 'f-string'\n *\n * @defaultValue 'f-string'\n */\n templateFormat?: TemplateFormat;\n\n /**\n * Whether or not to try validating the template on initialization\n *\n * @defaultValue `true`\n */\n validateTemplate?: boolean;\n\n /**\n * Additional fields which should be included inside\n * the message content array if using a complex message\n * content.\n */\n additionalContentFields?: ContentBlock;\n}\n\n/**\n * An image prompt template for a multimodal model.\n */\nexport class ImagePromptTemplate<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any,\n> extends BasePromptTemplate<RunInput, ImagePromptValue, PartialVariableName> {\n static lc_name() {\n return \"ImagePromptTemplate\";\n }\n\n lc_namespace = [\"langchain_core\", \"prompts\", \"image\"];\n\n template: Record<string, unknown>;\n\n templateFormat: TemplateFormat = \"f-string\";\n\n validateTemplate = true;\n\n /**\n * Additional fields which should be included inside\n * the message content array if using a complex message\n * content.\n */\n additionalContentFields?: ContentBlock;\n\n constructor(input: ImagePromptTemplateInput<RunInput, PartialVariableName>) {\n super(input);\n this.template = input.template;\n this.templateFormat = input.templateFormat ?? this.templateFormat;\n this.validateTemplate = input.validateTemplate ?? this.validateTemplate;\n this.additionalContentFields = input.additionalContentFields;\n\n if (this.validateTemplate) {\n let totalInputVariables: string[] = this.inputVariables;\n if (this.partialVariables) {\n totalInputVariables = totalInputVariables.concat(\n Object.keys(this.partialVariables)\n );\n }\n checkValidTemplate(\n [\n { type: \"image_url\", image_url: this.template },\n ] as unknown as MessageContent,\n this.templateFormat,\n totalInputVariables\n );\n }\n }\n\n _getPromptType(): \"prompt\" {\n return \"prompt\";\n }\n\n /**\n * Partially applies values to the prompt template.\n * @param values The values to be partially applied to the prompt template.\n * @returns A new instance of ImagePromptTemplate with the partially applied values.\n */\n async partial<NewPartialVariableName extends string>(\n values: PartialValues<NewPartialVariableName>\n ) {\n const newInputVariables = this.inputVariables.filter(\n (iv) => !(iv in values)\n ) as Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>[];\n const newPartialVariables = {\n ...(this.partialVariables ?? {}),\n ...values,\n } as PartialValues<PartialVariableName | NewPartialVariableName>;\n const promptDict = {\n ...this,\n inputVariables: newInputVariables,\n partialVariables: newPartialVariables,\n };\n return new ImagePromptTemplate<\n InputValues<\n Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>\n >\n >(promptDict);\n }\n\n /**\n * Formats the prompt template with the provided values.\n * @param values The values to be used to format the prompt template.\n * @returns A promise that resolves to a string which is the formatted prompt.\n */\n async format<FormatOutput = ImageContent>(\n values: TypedPromptInputValues<RunInput>\n ): Promise<FormatOutput> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formatted: Record<string, any> = {};\n for (const [key, value] of Object.entries(this.template)) {\n if (typeof value === \"string\") {\n formatted[key] = renderTemplate(value, this.templateFormat, values);\n } else {\n formatted[key] = value;\n }\n }\n const url = values.url || formatted.url;\n const detail = values.detail || formatted.detail;\n if (!url) {\n throw new Error(\"Must provide either an image URL.\");\n }\n if (typeof url !== \"string\") {\n throw new Error(\"url must be a string.\");\n }\n const output: ImageContent = { url };\n if (detail) {\n output.detail = detail;\n }\n return output as FormatOutput;\n }\n\n /**\n * Formats the prompt given the input values and returns a formatted\n * prompt value.\n * @param values The input values to format the prompt.\n * @returns A Promise that resolves to a formatted prompt value.\n */\n async formatPromptValue(\n values: TypedPromptInputValues<RunInput>\n ): Promise<ImagePromptValue> {\n const formattedPrompt = await this.format(values);\n return new ImagePromptValue(formattedPrompt);\n }\n}\n"],"mappings":";;;;;;;;AAsDA,IAAa,sBAAb,MAAa,4BAKHA,gCAAoE;CAC5E,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe;EAAC;EAAkB;EAAW;EAAQ;CAErD;CAEA,iBAAiC;CAEjC,mBAAmB;;;;;;CAOnB;CAEA,YAAY,OAAgE;AAC1E,QAAM,MAAM;AACZ,OAAK,WAAW,MAAM;AACtB,OAAK,iBAAiB,MAAM,kBAAkB,KAAK;AACnD,OAAK,mBAAmB,MAAM,oBAAoB,KAAK;AACvD,OAAK,0BAA0B,MAAM;AAErC,MAAI,KAAK,kBAAkB;GACzB,IAAI,sBAAgC,KAAK;AACzC,OAAI,KAAK,iBACP,uBAAsB,oBAAoB,OACxC,OAAO,KAAK,KAAK,iBAAiB,CACnC;AAEH,uCACE,CACE;IAAE,MAAM;IAAa,WAAW,KAAK;IAAU,CAChD,EACD,KAAK,gBACL,oBACD;;;CAIL,iBAA2B;AACzB,SAAO;;;;;;;CAQT,MAAM,QACJ,QACA;EACA,MAAM,oBAAoB,KAAK,eAAe,QAC3C,OAAO,EAAE,MAAM,QACjB;EACD,MAAM,sBAAsB;GAC1B,GAAI,KAAK,oBAAoB,EAAE;GAC/B,GAAG;GACJ;AAMD,SAAO,IAAI,oBALQ;GACjB,GAAG;GACH,gBAAgB;GAChB,kBAAkB;GACnB,CAKY;;;;;;;CAQf,MAAM,OACJ,QACuB;EAEvB,MA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