PromptTemplate.fromTemplate constructor

PromptTemplate.fromTemplate(
  1. String template, {
  2. PartialValues? partialVariables,
  3. bool validateTemplate = true,
})

Creates a prompt template from a string template automatically extracting the input variables.

Example:

final promptTemplate = PromptTemplate.fromTemplate(
  'What is a good name for a company that makes {product}?',
);
final prompt = promptTemplate.formatPrompt({'product': 'colorful socks'});
final res = await llm.invoke(prompt);
  • template the template string.
  • partialVariables the partial variables to use for the template.
  • validateTemplate whether to validate the template.

Implementation

factory PromptTemplate.fromTemplate(
  final String template, {
  final PartialValues? partialVariables,
  final bool validateTemplate = true,
}) {
  final t = PromptTemplate(
    inputVariables: parseFStringTemplate(template)
        .whereType<ParsedFStringVariableNode>()
        .map((final node) => node.name)
        .toSet()
        .difference(partialVariables?.keys.toSet() ?? {}),
    partialVariables: partialVariables,
    template: template,
  );
  if (validateTemplate) {
    t.validateTemplate();
  }
  return t;
}