@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 6.65 kB
Source Map (JSON)
{"version":3,"file":"base.cjs","names":["Runnable","input: BasePromptTemplateInput","userVariables: TypedPromptInputValues<RunInput>","partialValues: Record<string, string>","input: RunInput","options?: BaseCallbackConfig","input"],"sources":["../../src/prompts/base.ts"],"sourcesContent":["// Default generic \"any\" values are for backwards compatibility.\n// Replace with \"string\" when we are comfortable with a breaking change.\n\nimport type {\n InputValues,\n PartialValues,\n StringWithAutocomplete,\n} from \"../utils/types/index.js\";\nimport { type BasePromptValueInterface } from \"../prompt_values.js\";\nimport { BaseOutputParser } from \"../output_parsers/index.js\";\nimport type { SerializedFields } from \"../load/map_keys.js\";\nimport { Runnable } from \"../runnables/base.js\";\nimport { BaseCallbackConfig } from \"../callbacks/manager.js\";\n\nexport type TypedPromptInputValues<RunInput> = InputValues<\n StringWithAutocomplete<Extract<keyof RunInput, string>>\n>;\n\nexport type Example = Record<string, string>;\n\n/**\n * Input common to all prompt templates.\n */\nexport interface BasePromptTemplateInput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n InputVariables extends InputValues = any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any\n> {\n /**\n * A list of variable names the prompt template expects\n */\n inputVariables: Array<Extract<keyof InputVariables, string>>;\n\n /**\n * How to parse the output of calling an LLM on this formatted prompt\n */\n outputParser?: BaseOutputParser;\n\n /** Partial variables */\n partialVariables?: PartialValues<PartialVariableName>;\n}\n\n/**\n * Base class for prompt templates. Exposes a format method that returns a\n * string prompt given a set of input values.\n */\nexport abstract class BasePromptTemplate<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n RunOutput extends BasePromptValueInterface = BasePromptValueInterface,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any\n >\n extends Runnable<RunInput, RunOutput>\n implements BasePromptTemplateInput\n{\n declare PromptValueReturnType: RunOutput;\n\n lc_serializable = true;\n\n lc_namespace = [\"langchain_core\", \"prompts\", this._getPromptType()];\n\n get lc_attributes(): SerializedFields | undefined {\n return {\n partialVariables: undefined, // python doesn't support this yet\n };\n }\n\n inputVariables: Array<Extract<keyof RunInput, string>>;\n\n outputParser?: BaseOutputParser;\n\n partialVariables: PartialValues<PartialVariableName>;\n\n /**\n * Metadata to be used for tracing.\n */\n metadata?: Record<string, unknown>;\n\n /** Tags to be used for tracing. */\n tags?: string[];\n\n constructor(input: BasePromptTemplateInput) {\n super(input);\n const { inputVariables } = input;\n if (inputVariables.includes(\"stop\")) {\n throw new Error(\n \"Cannot have an input variable named 'stop', as it is used internally, please rename.\"\n );\n }\n Object.assign(this, input);\n }\n\n abstract partial(\n values: PartialValues\n ): Promise<BasePromptTemplate<RunInput, RunOutput, PartialVariableName>>;\n\n /**\n * Merges partial variables and user variables.\n * @param userVariables The user variables to merge with the partial variables.\n * @returns A Promise that resolves to an object containing the merged variables.\n */\n async mergePartialAndUserVariables(\n userVariables: TypedPromptInputValues<RunInput>\n ): Promise<\n InputValues<Extract<keyof RunInput, string> | PartialVariableName>\n > {\n const partialVariables = this.partialVariables ?? {};\n const partialValues: Record<string, string> = {};\n\n for (const [key, value] of Object.entries(partialVariables)) {\n if (typeof value === \"string\") {\n partialValues[key] = value;\n } else {\n partialValues[key] = await (value as () => Promise<string>)();\n }\n }\n\n const allKwargs = {\n ...(partialValues as Record<PartialVariableName, string>),\n ...userVariables,\n };\n return allKwargs;\n }\n\n /**\n * Invokes the prompt template with the given input and options.\n * @param input The input to invoke the prompt template with.\n * @param options Optional configuration for the callback.\n * @returns A Promise that resolves to the output of the prompt template.\n */\n async invoke(\n input: RunInput,\n options?: BaseCallbackConfig\n ): Promise<RunOutput> {\n const metadata = {\n ...this.metadata,\n ...options?.metadata,\n };\n const tags = [...(this.tags ?? []), ...(options?.tags ?? [])];\n return this._callWithConfig(\n (input: RunInput) => this.formatPromptValue(input),\n input,\n { ...options, tags, metadata, runType: \"prompt\" }\n );\n }\n\n /**\n * Format the prompt given the input values.\n *\n * @param values - A dictionary of arguments to be passed to the prompt template.\n * @returns A formatted prompt string.\n *\n * @example\n * ```ts\n * prompt.format({ foo: \"bar\" });\n * ```\n */\n abstract format(values: TypedPromptInputValues<RunInput>): Promise<string>;\n\n /**\n * Format the prompt given the input values and return a formatted prompt value.\n * @param values\n * @returns A formatted PromptValue.\n */\n abstract formatPromptValue(\n values: TypedPromptInputValues<RunInput>\n ): Promise<RunOutput>;\n\n /**\n * Return the string type key uniquely identifying this class of prompt template.\n */\n abstract _getPromptType(): string;\n}\n"],"mappings":";;;;;;;AA+CA,IAAsB,qBAAtB,cAOUA,sBAEV;CAGE,kBAAkB;CAElB,eAAe;EAAC;EAAkB;EAAW,KAAK,gBAAgB;CAAC;CAEnE,IAAI,gBAA8C;AAChD,SAAO,EACL,kBAAkB,OACnB;CACF;CAED;CAEA;CAEA;;;;CAKA;;CAGA;CAEA,YAAYC,OAAgC;EAC1C,MAAM,MAAM;EACZ,MAAM,EAAE,gBAAgB,GAAG;AAC3B,MAAI,eAAe,SAAS,OAAO,CACjC,OAAM,IAAI,MACR;EAGJ,OAAO,OAAO,MAAM,MAAM;CAC3B;;;;;;CAWD,MAAM,6BACJC,eAGA;EACA,MAAM,mBAAmB,KAAK,oBAAoB,CAAE;EACpD,MAAMC,gBAAwC,CAAE;AAEhD,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,iBAAiB,CACzD,KAAI,OAAO,UAAU,UACnB,cAAc,OAAO;OAErB,cAAc,OAAO,MAAO,OAAiC;EAIjE,MAAM,YAAY;GAChB,GAAI;GACJ,GAAG;EACJ;AACD,SAAO;CACR;;;;;;;CAQD,MAAM,OACJC,OACAC,SACoB;EACpB,MAAM,WAAW;GACf,GAAG,KAAK;GACR,GAAG,SAAS;EACb;EACD,MAAM,OAAO,CAAC,GAAI,KAAK,QAAQ,CAAE,GAAG,GAAI,SAAS,QAAQ,CAAE,CAAE;AAC7D,SAAO,KAAK,gBACV,CAACD,YAAoB,KAAK,kBAAkBE,QAAM,EAClD,OACA;GAAE,GAAG;GAAS;GAAM;GAAU,SAAS;EAAU,EAClD;CACF;AA4BF"}