{"version":3,"file":"base.cjs","names":["Runnable"],"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  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  InputVariables extends InputValues = any,\n  // oxlint-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  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  RunInput extends InputValues = any,\n  RunOutput extends BasePromptValueInterface = BasePromptValueInterface,\n  // oxlint-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,aAAAA,SAEV;CAGE,kBAAkB;CAElB,eAAe;EAAC;EAAkB;EAAW,KAAK,gBAAgB;EAAC;CAEnE,IAAI,gBAA8C;AAChD,SAAO,EACL,kBAAkB,KAAA,GACnB;;CAGH;CAEA;CAEA;;;;CAKA;;CAGA;CAEA,YAAY,OAAgC;AAC1C,QAAM,MAAM;EACZ,MAAM,EAAE,mBAAmB;AAC3B,MAAI,eAAe,SAAS,OAAO,CACjC,OAAM,IAAI,MACR,uFACD;AAEH,SAAO,OAAO,MAAM,MAAM;;;;;;;CAY5B,MAAM,6BACJ,eAGA;EACA,MAAM,mBAAmB,KAAK,oBAAoB,EAAE;EACpD,MAAM,gBAAwC,EAAE;AAEhD,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,iBAAiB,CACzD,KAAI,OAAO,UAAU,SACnB,eAAc,OAAO;MAErB,eAAc,OAAO,MAAO,OAAiC;AAQjE,SAJkB;GAChB,GAAI;GACJ,GAAG;GACJ;;;;;;;;CAUH,MAAM,OACJ,OACA,SACoB;EACpB,MAAM,WAAW;GACf,GAAG,KAAK;GACR,GAAG,SAAS;GACb;EACD,MAAM,OAAO,CAAC,GAAI,KAAK,QAAQ,EAAE,EAAG,GAAI,SAAS,QAAQ,EAAE,CAAE;AAC7D,SAAO,KAAK,iBACT,UAAoB,KAAK,kBAAkB,MAAM,EAClD,OACA;GAAE,GAAG;GAAS;GAAM;GAAU,SAAS;GAAU,CAClD"}