UNPKG

@langchain/core

Version:
1 lines 7.46 kB
{"version":3,"file":"image.cjs","names":["BasePromptTemplate","input: ImagePromptTemplateInput<RunInput, PartialVariableName>","totalInputVariables: string[]","checkValidTemplate","values: PartialValues<NewPartialVariableName>","values: TypedPromptInputValues<RunInput>","formatted: Record<string, any>","renderTemplate","output: ImageContent","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;CACR;CAED,eAAe;EAAC;EAAkB;EAAW;CAAQ;CAErD;CAEA,iBAAiC;CAEjC,mBAAmB;;;;;;CAOnB;CAEA,YAAYC,OAAgE;EAC1E,MAAM,MAAM;EACZ,KAAK,WAAW,MAAM;EACtB,KAAK,iBAAiB,MAAM,kBAAkB,KAAK;EACnD,KAAK,mBAAmB,MAAM,oBAAoB,KAAK;EACvD,KAAK,0BAA0B,MAAM;AAErC,MAAI,KAAK,kBAAkB;GACzB,IAAIC,sBAAgC,KAAK;AACzC,OAAI,KAAK,kBACP,sBAAsB,oBAAoB,OACxC,OAAO,KAAK,KAAK,iBAAiB,CACnC;GAEHC,oCACE,CACE;IAAE,MAAM;IAAa,WAAW,KAAK;GAAU,CAChD,GACD,KAAK,gBACL,oBACD;EACF;CACF;CAED,iBAA2B;AACzB,SAAO;CACR;;;;;;CAOD,MAAM,QACJC,QACA;EACA,MAAM,oBAAoB,KAAK,eAAe,OAC5C,CAAC,OAAO,EAAE,MAAM,QACjB;EACD,MAAM,sBAAsB;GAC1B,GAAI,KAAK,oB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