prompt.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { BaseStringPromptTemplate } from "./string.js";
  2. import type { BasePromptTemplateInput, TypedPromptInputValues } from "./base.js";
  3. import { type TemplateFormat } from "./template.js";
  4. import type { SerializedPromptTemplate } from "./serde.js";
  5. import type { InputValues, PartialValues } from "../utils/types/index.js";
  6. import { MessageContent, MessageContentComplex } from "../messages/index.js";
  7. /**
  8. * Inputs to create a {@link PromptTemplate}
  9. * @augments BasePromptTemplateInput
  10. */
  11. export interface PromptTemplateInput<RunInput extends InputValues = any, PartialVariableName extends string = any, Format extends TemplateFormat = TemplateFormat> extends BasePromptTemplateInput<RunInput, PartialVariableName> {
  12. /**
  13. * The prompt template
  14. */
  15. template: MessageContent;
  16. /**
  17. * The format of the prompt template. Options are "f-string" and "mustache"
  18. */
  19. templateFormat?: Format;
  20. /**
  21. * Whether or not to try validating the template on initialization
  22. *
  23. * @defaultValue `true`
  24. */
  25. validateTemplate?: boolean;
  26. /**
  27. * Additional fields which should be included inside
  28. * the message content array if using a complex message
  29. * content.
  30. */
  31. additionalContentFields?: MessageContentComplex;
  32. }
  33. type NonAlphanumeric = " " | "\t" | "\n" | "\r" | '"' | "'" | "{" | "[" | "(" | "`" | ":" | ";";
  34. /**
  35. * Recursive type to extract template parameters from a string.
  36. * @template T - The input string.
  37. * @template Result - The resulting array of extracted template parameters.
  38. */
  39. type ExtractTemplateParamsRecursive<T extends string, Result extends string[] = []> = T extends `${string}{${infer Param}}${infer Rest}` ? Param extends `${NonAlphanumeric}${string}` ? ExtractTemplateParamsRecursive<Rest, Result> : ExtractTemplateParamsRecursive<Rest, [...Result, Param]> : Result;
  40. export type ParamsFromFString<T extends string> = {
  41. [Key in ExtractTemplateParamsRecursive<T>[number] | (string & Record<never, never>)]: any;
  42. };
  43. export type ExtractedFStringParams<T extends string, RunInput extends InputValues = Symbol> = RunInput extends Symbol ? ParamsFromFString<T> : RunInput;
  44. /**
  45. * Schema to represent a basic prompt for an LLM.
  46. * @augments BasePromptTemplate
  47. * @augments PromptTemplateInput
  48. *
  49. * @example
  50. * ```ts
  51. * import { PromptTemplate } from "langchain/prompts";
  52. *
  53. * const prompt = new PromptTemplate({
  54. * inputVariables: ["foo"],
  55. * template: "Say {foo}",
  56. * });
  57. * ```
  58. */
  59. export declare class PromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BaseStringPromptTemplate<RunInput, PartialVariableName> implements PromptTemplateInput<RunInput, PartialVariableName> {
  60. static lc_name(): string;
  61. template: MessageContent;
  62. templateFormat: TemplateFormat;
  63. validateTemplate: boolean;
  64. /**
  65. * Additional fields which should be included inside
  66. * the message content array if using a complex message
  67. * content.
  68. */
  69. additionalContentFields?: MessageContentComplex;
  70. constructor(input: PromptTemplateInput<RunInput, PartialVariableName>);
  71. _getPromptType(): "prompt";
  72. /**
  73. * Formats the prompt template with the provided values.
  74. * @param values The values to be used to format the prompt template.
  75. * @returns A promise that resolves to a string which is the formatted prompt.
  76. */
  77. format(values: TypedPromptInputValues<RunInput>): Promise<string>;
  78. /**
  79. * Take examples in list format with prefix and suffix to create a prompt.
  80. *
  81. * Intended to be used a a way to dynamically create a prompt from examples.
  82. *
  83. * @param examples - List of examples to use in the prompt.
  84. * @param suffix - String to go after the list of examples. Should generally set up the user's input.
  85. * @param inputVariables - A list of variable names the final prompt template will expect
  86. * @param exampleSeparator - The separator to use in between examples
  87. * @param prefix - String that should go before any examples. Generally includes examples.
  88. *
  89. * @returns The final prompt template generated.
  90. */
  91. static fromExamples(examples: string[], suffix: string, inputVariables: string[], exampleSeparator?: string, prefix?: string): PromptTemplate<any, any>;
  92. /**
  93. * Load prompt template from a template f-string
  94. */
  95. static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "f-string">, "template" | "inputVariables">): PromptTemplate<ExtractedFStringParams<T, RunInput>>;
  96. static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): PromptTemplate<ExtractedFStringParams<T, RunInput>>;
  97. static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "mustache">, "template" | "inputVariables">): PromptTemplate<InputValues>;
  98. /**
  99. * Partially applies values to the prompt template.
  100. * @param values The values to be partially applied to the prompt template.
  101. * @returns A new instance of PromptTemplate with the partially applied values.
  102. */
  103. partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<PromptTemplate<InputValues<Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>>, any>>;
  104. serialize(): SerializedPromptTemplate;
  105. static deserialize(data: SerializedPromptTemplate): Promise<PromptTemplate>;
  106. }
  107. export {};