UNPKG

@langchain/core

Version:
1 lines 5.45 kB
{"version":3,"file":"dict.cjs","names":["Runnable","renderTemplate"],"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;EAAO;CAEpD,kBAAkB;CAElB;CAEA;CAEA;CAEA,OAAO,UAAU;AACf,SAAO;;CAGT,YAAY,QAGT;EACD,MAAM,iBAAiB,OAAO,kBAAkB;EAChD,MAAM,iBAAiB,mBACrB,OAAO,UACP,eACD;AACD,QAAM;GAAE;GAAgB,GAAG;GAAQ,CAAC;AACpC,OAAK,WAAW,OAAO;AACvB,OAAK,iBAAiB;AACtB,OAAK,iBAAiB;;CAGxB,MAAM,OAAO,QAA8D;AACzE,SAAO,sBACL,KAAK,UACL,QACA,KAAK,eACN;;CAGH,MAAM,OACJ,QACoB;AACpB,SAAO,MAAM,KAAK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