Store chat history

Completed

The Semantic Kernel SDK supports a ChatHistory object that keeps a record of the messages exchanged in a chat session. It stores messages and metadata from different authors, such as users, assistants, tools, or the system. The ChatHistory object is essential for maintaining context and continuity in a conversation. Here's some sample output of a ChatHistory object:

system: You are a helpful assistant.
user: What's available to order?
assistant: We have pizza, pasta, and salad available to order. What would you like to order?
user: I'd like to have the first option, please.

Creating a chat history object

Before creating a ChatHistory object, you need to import the ChatCompletion package. Under the hood, a chat history object is a list that functions as a container for managing the sequence of messages in a chat session.

using Microsoft.SemanticKernel.ChatCompletion;

// Create a chat history object
ChatHistory chatHistory = [];

// Add role messages to the chat history
chatHistory.AddSystemMessage("You are a helpful assistant.");
chatHistory.AddUserMessage("What's available to order?");
chatHistory.AddAssistantMessage("We have pizza, pasta, and salad available to order. What would you like to order?");
chatHistory.AddUserMessage("I'd like to have the first option, please.");

for (int i = 0; i < chatHistory.Count; i++)
{
    Console.WriteLine($"{chatHistory[i].Role}: {chatHistory[i]}");
}

In this example, a ChatHistory object is created and populated with messages from different authors:

  • System Message: Sets the role or behavior of the assistant, e.g., “You are a helpful assistant.”
  • User Messages: Captures the user's input, such as asking for available items or placing an order.
  • Assistant Messages: Contains responses generated by the AI assistant, offering suggestions or confirming actions.

By using chat history, you can build intelligent, context-aware chat experiences that feel natural and responsive to user needs.

You can also add more details to the chat history by creating a ChatMessage object. The ChatMessage object supports additional information, like user names and image content. Here's an example:

// Add user message with an image
#pragma warning disable SKEXP0001 // AuthorName is subject to change and emits a warning
chatHistory.Add(
    new() {
        Role = AuthorRole.User,
        AuthorName = "Laimonis Dumins",
        Items = [
            new TextContent { Text = "What available on this menu" },
            new ImageContent { Uri = new Uri("https://example.com/menu.jpg") }
        ]
    }
);

Using the chat history in your project can help enhance user interactions. For example, chat history ensures continuity by retaining context across multiple exchanges, allowing the assistant to respond more accurately without users repeating information You can also use the chat history to analyze user interactions for improving AI responses, such as identifying common queries or refining conversational flow.

The ChatHistory object allows you to keep track of messages exchanged during a chat session, ensuring conversations remain contextually aware and natural. By adding system, user, and assistant messages, you can define behaviors and interactions that create a cohesive user experience. The ChatMessage object provides flexibility by supporting details like user names and multimedia content. These tools make it easy to design dynamic and personalized chat applications.