UNPKG

@langchain/core

Version:
176 lines (175 loc) 6.57 kB
"use strict"; // Default generic "any" values are for backwards compatibility. // Replace with "string" when we are comfortable with a breaking change. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BasePromptTemplate = void 0; const base_js_1 = require("../runnables/base.cjs"); /** * Base class for prompt templates. Exposes a format method that returns a * string prompt given a set of input values. */ class BasePromptTemplate extends base_js_1.Runnable { get lc_attributes() { return { partialVariables: undefined, // python doesn't support this yet }; } constructor(input) { super(input); Object.defineProperty(this, "lc_serializable", { enumerable: true, configurable: true, writable: true, value: true }); Object.defineProperty(this, "lc_namespace", { enumerable: true, configurable: true, writable: true, value: ["langchain_core", "prompts", this._getPromptType()] }); Object.defineProperty(this, "inputVariables", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "outputParser", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "partialVariables", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * Metadata to be used for tracing. */ Object.defineProperty(this, "metadata", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** Tags to be used for tracing. */ Object.defineProperty(this, "tags", { enumerable: true, configurable: true, writable: true, value: void 0 }); const { inputVariables } = input; if (inputVariables.includes("stop")) { throw new Error("Cannot have an input variable named 'stop', as it is used internally, please rename."); } Object.assign(this, input); } /** * Merges partial variables and user variables. * @param userVariables The user variables to merge with the partial variables. * @returns A Promise that resolves to an object containing the merged variables. */ async mergePartialAndUserVariables(userVariables) { const partialVariables = this.partialVariables ?? {}; const partialValues = {}; for (const [key, value] of Object.entries(partialVariables)) { if (typeof value === "string") { partialValues[key] = value; } else { partialValues[key] = await value(); } } const allKwargs = { ...partialValues, ...userVariables, }; return allKwargs; } /** * Invokes the prompt template with the given input and options. * @param input The input to invoke the prompt template with. * @param options Optional configuration for the callback. * @returns A Promise that resolves to the output of the prompt template. */ async invoke(input, options) { const metadata = { ...this.metadata, ...options?.metadata, }; const tags = [...(this.tags ?? []), ...(options?.tags ?? [])]; return this._callWithConfig((input) => this.formatPromptValue(input), input, { ...options, tags, metadata, runType: "prompt" }); } /** * Return a json-like object representing this prompt template. * @deprecated */ serialize() { throw new Error("Use .toJSON() instead"); } /** * @deprecated * Load a prompt template from a json-like object describing it. * * @remarks * Deserializing needs to be async because templates (e.g. {@link FewShotPromptTemplate}) can * reference remote resources that we read asynchronously with a web * request. */ static async deserialize(data) { switch (data._type) { case "prompt": { const { PromptTemplate } = await Promise.resolve().then(() => __importStar(require("./prompt.cjs"))); return PromptTemplate.deserialize(data); } case undefined: { const { PromptTemplate } = await Promise.resolve().then(() => __importStar(require("./prompt.cjs"))); return PromptTemplate.deserialize({ ...data, _type: "prompt" }); } case "few_shot": { const { FewShotPromptTemplate } = await Promise.resolve().then(() => __importStar(require("./few_shot.cjs"))); return FewShotPromptTemplate.deserialize(data); } default: throw new Error(`Invalid prompt type in config: ${data._type}`); } } } exports.BasePromptTemplate = BasePromptTemplate;