ChatPromptTemplate

Models & Types · AiDotNet.PromptEngineering.Templates

Prompt template for chat-based interactions with role-based messages.

Inheritance: PromptTemplateBase → ChatPromptTemplate

For Beginners

A template for building conversations with different roles.

Example:

var template = new ChatPromptTemplate();

// Set system message (instructions for the AI)
template.AddSystemMessage("You are a helpful math tutor for elementary students.");

// Add conversation history if needed
template.AddUserMessage("What is 5 + 3?");
template.AddAssistantMessage("5 + 3 equals 8!");

// Add current query
template.AddUserMessage("What about 12 + 7?");

var prompt = template.Format(new Dictionary<string, string>());

// Result:
// System: You are a helpful math tutor for elementary students.
// User: What is 5 + 3?
// Assistant: 5 + 3 equals 8!
// User: What about 12 + 7?

How It Works

This template structures prompts as conversations with different roles (system, user, assistant). It’s designed for chat-based language models that understand conversational context.

Constructors

Constructor Summary
ChatPromptTemplate(string messageFormat) Initializes a new instance of the ChatPromptTemplate class.

Properties

Property Summary
IReadOnlyList<ChatMessage> Messages { get; } Gets all messages in the conversation.

Methods

Method Summary
ChatPromptTemplate AddAssistantMessage(string content) Adds an assistant message to the conversation.
ChatPromptTemplate AddMessage(string role, string content) Adds a message with a custom role.
ChatPromptTemplate AddSystemMessage(string content) Adds a system message to the conversation.
ChatPromptTemplate AddUserMessage(string content) Adds a user message to the conversation.