@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.96 kB
Source Map (JSON)
{"version":3,"file":"dict.cjs","names":["Runnable","fields: {\n template: Record<string, unknown>;\n templateFormat?: TemplateFormat;\n }","values: TypedPromptInputValues<RunInput>","values: TypedPromptInputValues<InputValues>","template: Record<string, unknown>","templateFormat: TemplateFormat","inputVariables: Array<Extract<keyof InputValues, string>>","parseTemplate","inputs: TypedPromptInputValues<InputValues>","formatted: Record<string, unknown>","renderTemplate","formattedV: Array<unknown>"],"sources":["../../src/prompts/dict.ts"],"sourcesContent":["import { Runnable } from \"../runnables/base.js\";\nimport type { InputValues } from \"../utils/types/index.js\";\nimport { TypedPromptInputValues } from \"./base.js\";\nimport { parseTemplate, renderTemplate, TemplateFormat } from \"./template.js\";\n\nexport class DictPromptTemplate<\n RunInput extends InputValues = InputValues,\n RunOutput extends Record<string, unknown> = Record<string, unknown>\n> extends Runnable<TypedPromptInputValues<RunInput>, RunOutput> {\n lc_namespace = [\"langchain_core\", \"prompts\", \"dict\"];\n\n lc_serializable = true;\n\n template: Record<string, unknown>;\n\n templateFormat: TemplateFormat;\n\n inputVariables: Array<Extract<keyof RunInput, string>>;\n\n static lc_name() {\n return \"DictPromptTemplate\";\n }\n\n constructor(fields: {\n template: Record<string, unknown>;\n templateFormat?: TemplateFormat;\n }) {\n const templateFormat = fields.templateFormat ?? \"f-string\";\n const inputVariables = _getInputVariables(\n fields.template,\n templateFormat\n ) as Array<Extract<keyof RunInput, string>>;\n super({ inputVariables, ...fields });\n this.template = fields.template;\n this.templateFormat = templateFormat;\n this.inputVariables = inputVariables;\n }\n\n async format(values: TypedPromptInputValues<RunInput>): Promise<RunOutput> {\n return _insertInputVariables(\n this.template,\n values,\n this.templateFormat\n ) as RunOutput;\n }\n\n async invoke(\n values: TypedPromptInputValues<InputValues>\n ): Promise<RunOutput> {\n return await this._callWithConfig(this.format.bind(this), values, {\n runType: \"prompt\",\n });\n }\n}\n\nfunction _getInputVariables(\n template: Record<string, unknown>,\n templateFormat: TemplateFormat\n): Array<Extract<keyof InputValues, string>> {\n const inputVariables: Array<Extract<keyof InputValues, string>> = [];\n for (const v of Object.values(template)) {\n if (typeof v === \"string\") {\n parseTemplate(v, templateFormat).forEach((t) => {\n if (t.type === \"variable\") {\n inputVariables.push(t.name);\n }\n });\n } else if (Array.isArray(v)) {\n for (const x of v) {\n if (typeof x === \"string\") {\n parseTemplate(x, templateFormat).forEach((t) => {\n if (t.type === \"variable\") {\n inputVariables.push(t.name);\n }\n });\n } else if (typeof x === \"object\") {\n inputVariables.push(..._getInputVariables(x, templateFormat));\n }\n }\n } else if (typeof v === \"object\" && v !== null) {\n inputVariables.push(\n ..._getInputVariables(v as Record<string, unknown>, templateFormat)\n );\n }\n }\n return Array.from(new Set(inputVariables));\n}\n\nfunction _insertInputVariables(\n template: Record<string, unknown>,\n inputs: TypedPromptInputValues<InputValues>,\n templateFormat: TemplateFormat\n): Record<string, unknown> {\n const formatted: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(template)) {\n if (typeof v === \"string\") {\n formatted[k] = renderTemplate(v, templateFormat, inputs);\n } else if (Array.isArray(v)) {\n const formattedV: Array<unknown> = [];\n for (const x of v) {\n if (typeof x === \"string\") {\n formattedV.push(renderTemplate(x, templateFormat, inputs));\n } else if (typeof x === \"object\") {\n formattedV.push(_insertInputVariables(x, inputs, templateFormat));\n }\n }\n formatted[k] = formattedV;\n } else if (typeof v === \"object\" && v !== null) {\n formatted[k] = _insertInputVariables(\n v as Record<string, unknown>,\n inputs,\n templateFormat\n );\n } else {\n formatted[k] = v;\n }\n }\n return formatted;\n}\n"],"mappings":";;;;AAKA,IAAa,qBAAb,cAGUA,sBAAsD;CAC9D,eAAe;EAAC;EAAkB;EAAW;CAAO;CAEpD,kBAAkB;CAElB;CAEA;CAEA;CAEA,OAAO,UAAU;AACf,SAAO;CACR;CAED,YAAYC,QAGT;EACD,MAAM,iBAAiB,OAAO,kBAAkB;EAChD,MAAM,iBAAiB,mB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