ChatPromptTemplate.fromTemplate constructor

ChatPromptTemplate.fromTemplate(
  1. String template, {
  2. ChatMessageType type = ChatMessageType.human,
  3. String? customRole,
  4. PartialValues? partialVariables,
  5. bool validateTemplate = true,
})

Creates a chat prompt template with a single message from a string template.

Example:

final promptTemplate = ChatPromptTemplate.fromTemplate(
  "Hello {foo}, I'm {bar}. Thanks for the {context}",
  partialVariables: {'foo': 'GPT-4', 'bar': 'Gemini'},
);
final prompt = promptTemplate.formatPrompt({'context': 'competition'});
final res = await chatModel.invoke(prompt);
  • template the template string.
  • type the type of chat message prompt template (HumanChatMessagePromptTemplate by default).
  • customRole the role of the message if type is ChatMessageType.custom.
  • partialVariables the partial variables to use for the template.
  • validateTemplate whether to validate the template.

Implementation

factory ChatPromptTemplate.fromTemplate(
  final String template, {
  final ChatMessageType type = ChatMessageType.human,
  final String? customRole,
  final PartialValues? partialVariables,
  final bool validateTemplate = true,
}) {
  return ChatPromptTemplate.fromTemplates(
    [(type, template)],
    customRole: customRole,
    partialVariables: partialVariables,
    validateTemplate: validateTemplate,
  );
}