PromptTemplate class final

A prompt template for a language model.

A prompt template consists of a string template. It accepts a set of parameters from the user that can be used to generate a prompt for a language model.

Example:

final promptTemplate = PromptTemplate(
  inputVariables: ['product'],
  template: 'What is a good name for a company that makes {product}?',
);
final prompt = promptTemplate.formatPrompt({'product': 'colorful socks'});
final res = await llm.invoke(prompt);

Note: the default constructor does not validate the template. You can use PromptTemplate.validateTemplate to validate the template.

You can also use the following convenience factory constructors to create a prompt template:

final promptTemplate = PromptTemplate.fromTemplate(
  'What is a good name for a company that makes {product}?',
);
Inheritance
Annotations
  • @immutable

Constructors

PromptTemplate({required Set<String> inputVariables, PartialValues? partialVariables, required String template})
A prompt template for a language model.
const
PromptTemplate.fromExamples({required List<String> examples, required String suffix, required Set<String> inputVariables, String exampleSeparator = '\n\n', String prefix = '', bool validateTemplate = true})
Take examples in list format with prefix and suffix to create a prompt.
factory
PromptTemplate.fromTemplate(String template, {PartialValues? partialVariables, bool validateTemplate = true})
Creates a prompt template from a string template automatically extracting the input variables.
factory

Properties

defaultOptions BaseLangChainOptions
The default options to use when invoking the Runnable.
finalinherited
hashCode int
The hash code for this object.
no setteroverride
inputVariables Set<String>
A set of the names of the variables the prompt template expects.
finalinherited
partialVariables PartialValues?
Partial variables.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
template String
The prompt template.
final
type String
The type of the prompt template.
no setteroverride

Methods

batch(List<InputValues> inputs, {List<BaseLangChainOptions>? options}) Future<List<PromptValue>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(BaseLangChainOptions options) RunnableBinding<InputValues, BaseLangChainOptions, PromptValue>
Binds the Runnable to the given options.
inherited
close() → void
Cleans up any resources associated with it the Runnable.
inherited
copyWith({Set<String>? inputVariables, Map<String, dynamic>? partialVariables, String? template}) PromptTemplate
Copy the prompt template with the given parameters.
override
format([InputValues values = const {}]) String
Format the prompt given the input values and return a formatted string.
override
formatPrompt(InputValues values) PromptValue
Format the prompt given the input values and return a formatted prompt value.
override
getCompatibleOptions(RunnableOptions? options) BaseLangChainOptions?
Returns the given options if they are compatible with the Runnable, otherwise returns null.
inherited
invoke(InputValues input, {BaseLangChainOptions? options}) Future<PromptValue>
Format the prompt given the input values and return a formatted prompt value.
inherited
mergePartialAndUserVariables(Map<String, dynamic> userVariables) Map<String, Object>
Merge the partial variables with the user variables.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
partial(PartialValues values) PromptTemplate
Return a partial of the prompt template.
override
pipe<NewRunOutput extends Object?, NewCallOptions extends RunnableOptions>(Runnable<PromptValue, NewCallOptions, NewRunOutput> next) RunnableSequence<InputValues, NewRunOutput>
Pipes the output of this Runnable into another Runnable using a RunnableSequence.
inherited
stream(InputValues input, {BaseLangChainOptions? options}) Stream<PromptValue>
Streams the output of invoking the Runnable on the given input.
inherited
streamFromInputStream(Stream<InputValues> inputStream, {BaseLangChainOptions? options}) Stream<PromptValue>
Streams the output of invoking the Runnable on the given inputStream.
inherited
toString() String
A string representation of this object.
override
validateTemplate() → void
Validate the integrity of the prompt template, checking that all the variables are present and that the right format is used.
override
withFallbacks(List<Runnable<InputValues, RunnableOptions, PromptValue>> fallbacks) RunnableWithFallback<InputValues, PromptValue>
Adds fallback runnables to be invoked if the primary runnable fails.
inherited
withRetry({int maxRetries = 3, FutureOr<bool> retryIf(Object e)?, List<Duration?>? delayDurations, bool addJitter = false}) RunnableRetry<InputValues, PromptValue>
Adds retry logic to an existing runnable.
inherited

Operators

operator ==(covariant PromptTemplate other) bool
The equality operator.
override

Static Methods

fromFile(String templateFile, {PartialValues? partialVariables, bool validateTemplate = true}) Future<PromptTemplate>
Loads a prompt from a file.