123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { Runnable } from "../runnables/base.js";
- import { parseTemplate, renderTemplate } from "./template.js";
- export class DictPromptTemplate extends Runnable {
- static lc_name() {
- return "DictPromptTemplate";
- }
- constructor(fields) {
- const templateFormat = fields.templateFormat ?? "f-string";
- const inputVariables = _getInputVariables(fields.template, templateFormat);
- super({ inputVariables, ...fields });
- Object.defineProperty(this, "lc_namespace", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: ["langchain_core", "prompts", "dict"]
- });
- Object.defineProperty(this, "lc_serializable", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: true
- });
- Object.defineProperty(this, "template", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "templateFormat", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "inputVariables", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- this.template = fields.template;
- this.templateFormat = templateFormat;
- this.inputVariables = inputVariables;
- }
- async format(values) {
- return _insertInputVariables(this.template, values, this.templateFormat);
- }
- async invoke(values) {
- return await this._callWithConfig(this.format.bind(this), values, {
- runType: "prompt",
- });
- }
- }
- function _getInputVariables(template, templateFormat) {
- const inputVariables = [];
- for (const v of Object.values(template)) {
- if (typeof v === "string") {
- parseTemplate(v, templateFormat).forEach((t) => {
- if (t.type === "variable") {
- inputVariables.push(t.name);
- }
- });
- }
- else if (Array.isArray(v)) {
- for (const x of v) {
- if (typeof x === "string") {
- parseTemplate(x, templateFormat).forEach((t) => {
- if (t.type === "variable") {
- inputVariables.push(t.name);
- }
- });
- }
- else if (typeof x === "object") {
- inputVariables.push(..._getInputVariables(x, templateFormat));
- }
- }
- }
- else if (typeof v === "object" && v !== null) {
- inputVariables.push(..._getInputVariables(v, templateFormat));
- }
- }
- return Array.from(new Set(inputVariables));
- }
- function _insertInputVariables(template, inputs, templateFormat) {
- const formatted = {};
- for (const [k, v] of Object.entries(template)) {
- if (typeof v === "string") {
- formatted[k] = renderTemplate(v, templateFormat, inputs);
- }
- else if (Array.isArray(v)) {
- const formattedV = [];
- for (const x of v) {
- if (typeof x === "string") {
- formattedV.push(renderTemplate(x, templateFormat, inputs));
- }
- else if (typeof x === "object") {
- formattedV.push(_insertInputVariables(x, inputs, templateFormat));
- }
- }
- formatted[k] = formattedV;
- }
- else if (typeof v === "object" && v !== null) {
- formatted[k] = _insertInputVariables(v, inputs, templateFormat);
- }
- else {
- formatted[k] = v;
- }
- }
- return formatted;
- }
|