base.d.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type { InputValues, PartialValues, StringWithAutocomplete } from "../utils/types/index.js";
  2. import { type BasePromptValueInterface } from "../prompt_values.js";
  3. import { BaseOutputParser } from "../output_parsers/index.js";
  4. import type { SerializedFields } from "../load/map_keys.js";
  5. import { Runnable } from "../runnables/base.js";
  6. import { BaseCallbackConfig } from "../callbacks/manager.js";
  7. import type { SerializedBasePromptTemplate } from "../prompts/serde.js";
  8. export type TypedPromptInputValues<RunInput> = InputValues<StringWithAutocomplete<Extract<keyof RunInput, string>>>;
  9. export type Example = Record<string, string>;
  10. /**
  11. * Input common to all prompt templates.
  12. */
  13. export interface BasePromptTemplateInput<InputVariables extends InputValues = any, PartialVariableName extends string = any> {
  14. /**
  15. * A list of variable names the prompt template expects
  16. */
  17. inputVariables: Array<Extract<keyof InputVariables, string>>;
  18. /**
  19. * How to parse the output of calling an LLM on this formatted prompt
  20. */
  21. outputParser?: BaseOutputParser;
  22. /** Partial variables */
  23. partialVariables?: PartialValues<PartialVariableName>;
  24. }
  25. /**
  26. * Base class for prompt templates. Exposes a format method that returns a
  27. * string prompt given a set of input values.
  28. */
  29. export declare abstract class BasePromptTemplate<RunInput extends InputValues = any, RunOutput extends BasePromptValueInterface = BasePromptValueInterface, PartialVariableName extends string = any> extends Runnable<RunInput, RunOutput> implements BasePromptTemplateInput {
  30. PromptValueReturnType: RunOutput;
  31. lc_serializable: boolean;
  32. lc_namespace: string[];
  33. get lc_attributes(): SerializedFields | undefined;
  34. inputVariables: Array<Extract<keyof RunInput, string>>;
  35. outputParser?: BaseOutputParser;
  36. partialVariables: PartialValues<PartialVariableName>;
  37. /**
  38. * Metadata to be used for tracing.
  39. */
  40. metadata?: Record<string, unknown>;
  41. /** Tags to be used for tracing. */
  42. tags?: string[];
  43. constructor(input: BasePromptTemplateInput);
  44. abstract partial(values: PartialValues): Promise<BasePromptTemplate<RunInput, RunOutput, PartialVariableName>>;
  45. /**
  46. * Merges partial variables and user variables.
  47. * @param userVariables The user variables to merge with the partial variables.
  48. * @returns A Promise that resolves to an object containing the merged variables.
  49. */
  50. mergePartialAndUserVariables(userVariables: TypedPromptInputValues<RunInput>): Promise<InputValues<Extract<keyof RunInput, string> | PartialVariableName>>;
  51. /**
  52. * Invokes the prompt template with the given input and options.
  53. * @param input The input to invoke the prompt template with.
  54. * @param options Optional configuration for the callback.
  55. * @returns A Promise that resolves to the output of the prompt template.
  56. */
  57. invoke(input: RunInput, options?: BaseCallbackConfig): Promise<RunOutput>;
  58. /**
  59. * Format the prompt given the input values.
  60. *
  61. * @param values - A dictionary of arguments to be passed to the prompt template.
  62. * @returns A formatted prompt string.
  63. *
  64. * @example
  65. * ```ts
  66. * prompt.format({ foo: "bar" });
  67. * ```
  68. */
  69. abstract format(values: TypedPromptInputValues<RunInput>): Promise<string>;
  70. /**
  71. * Format the prompt given the input values and return a formatted prompt value.
  72. * @param values
  73. * @returns A formatted PromptValue.
  74. */
  75. abstract formatPromptValue(values: TypedPromptInputValues<RunInput>): Promise<RunOutput>;
  76. /**
  77. * Return the string type key uniquely identifying this class of prompt template.
  78. */
  79. abstract _getPromptType(): string;
  80. /**
  81. * Return a json-like object representing this prompt template.
  82. * @deprecated
  83. */
  84. serialize(): SerializedBasePromptTemplate;
  85. /**
  86. * @deprecated
  87. * Load a prompt template from a json-like object describing it.
  88. *
  89. * @remarks
  90. * Deserializing needs to be async because templates (e.g. {@link FewShotPromptTemplate}) can
  91. * reference remote resources that we read asynchronously with a web
  92. * request.
  93. */
  94. static deserialize(data: SerializedBasePromptTemplate): Promise<BasePromptTemplate<InputValues, BasePromptValueInterface, string>>;
  95. }