Table of Contents

Class ChatHistory

Namespace
LMKit.TextGeneration.Chat
Assembly
LM-Kit.NET.dll

This class represents the chat history between a user and an assistant within a chat session.

public sealed class ChatHistory : ISerializableData
Inheritance
ChatHistory
Implements
Inherited Members

Examples

Example: Create and populate a chat history

using LMKit.Model;
using LMKit.TextGeneration.Chat;
using System;

// Load the model
LM model = LM.LoadFromModelID("llama-3.2-1b");

// Create chat history
ChatHistory history = new ChatHistory(model);

// Add system message (optional, must be first)
history.AddMessage(AuthorRole.System, "You are a helpful assistant.");

// Add conversation turns
history.AddMessage(AuthorRole.User, "What is the capital of France?");
history.AddMessage(AuthorRole.Assistant, "The capital of France is Paris.");
history.AddMessage(AuthorRole.User, "What about Germany?");
history.AddMessage(AuthorRole.Assistant, "The capital of Germany is Berlin.");

// Display the conversation
Console.WriteLine($"Message count: {history.MessageCount}");
foreach (var message in history.Messages)
{
    Console.WriteLine($"[{message.AuthorRole}]: {message.Text}");
}

Example: Use chat history for fine-tuning

using LMKit.Model;
using LMKit.TextGeneration.Chat;
using LMKit.Finetuning;
using System;

LM model = LM.LoadFromModelID("llama-3.2-1b");

// Create training data as chat history
ChatHistory trainingData = new ChatHistory(model);

// Add training examples
trainingData.AddMessage(AuthorRole.User, "Translate 'hello' to Spanish");
trainingData.AddMessage(AuthorRole.Assistant, "Hola");

trainingData.StartNewConversation(); // Separator between examples

trainingData.AddMessage(AuthorRole.User, "Translate 'goodbye' to Spanish");
trainingData.AddMessage(AuthorRole.Assistant, "Adios");

// Use for fine-tuning
using var finetuning = new LoraFinetuning(model);
int samples = finetuning.LoadTrainingDataFromChatHistory(trainingData);
Console.WriteLine($"Loaded {samples} training samples");

Example: Chat history with attachments

using LMKit.Model;
using LMKit.TextGeneration.Chat;
using LMKit.Data;
using System;

// Load a vision-enabled model
LM model = LM.LoadFromModelID("gemma-3-4b");

ChatHistory history = new ChatHistory(model);

// Create a message with an image attachment
var userMessage = new ChatHistory.Message(AuthorRole.User, "What do you see in this image?");
userMessage.AddAttachment(new Attachment("photo.jpg"));

history.AddMessage(userMessage);
history.AddMessage(AuthorRole.Assistant, "I see a beautiful sunset over the ocean.");

Console.WriteLine($"Messages: {history.MessageCount}");

Remarks

The ChatHistory class manages the sequence of messages exchanged between users and assistants. It is used internally by conversation classes like MultiTurnConversation and can also be used directly for fine-tuning or custom conversation management.

Key Features

  • Store and manage conversation messages with different author roles
  • Support for system, user, assistant, and developer messages
  • Attachment support for multimodal conversations
  • Serialization support for saving and loading chat history
  • Clone support for creating conversation branches
  • Automatic chat template formatting based on model

Author Roles

Common Use Cases

  • Building training data for fine-tuning
  • Custom conversation management outside of MultiTurnConversation
  • Serializing conversations for later continuation
  • Creating conversation templates

Constructors

ChatHistory(LM)

Create a new instance of the ChatHistory class.

Properties

AssistantMessageSuffix

Gets or sets the suffix to append after each assistant message.

AssistantPrefix

Specifies the assistant message prefix used to prompt the language model.

ConversationId

Gets the unique identifier for this conversation session.

MessageCount

Returns the number of messages in the history.

Messages

Specifies a list of all messages exchanged in the chat.

Model

Gets or sets the LM (Language Model) instance associated with this object. The model instance can be changed unless the object is marked as internal/sealed. Throws exceptions when an invalid model is provided or the instance is sealed.

PromptPrefix

Defines a text string that will be added at the start of the chat prompt.

SystemMessageSuffix

Gets or sets the suffix to append after each system message.

SystemPrefix

Gets or sets the system message prefix used to prompt the language model.

TokenCount

Indicates the total count of tokens needed by the associated language model to tokenize the content of this instance.

UserMessageCount

Returns the number of user messages in the history.

UserMessageSuffix

Gets or sets the suffix to append after each user message.

UserPrefix

Specifies the user message prefix used to prompt the language model.

Methods

AddMessage(AuthorRole, string)

Adds a message to the history.

AddMessage(Message)

Adds a message to the history.

ClearHistory()

Clears the history.

Clone()

Creates a deep copy of the current ChatHistory instance.

Deserialize(byte[], LM)

Deserializes the given binary data into a ChatHistory instance using the specified model for context.
This method reconstructs a ChatHistory object from its binary form, applying the model to interpret or configure the data appropriately.

Deserialize(Stream, LM)

Deserializes a ChatHistory from a stream using the provided model for context.
This method reconstructs a ChatHistory object from its binary form, applying the model to interpret or configure the data appropriately.

Deserialize(string, LM)

Deserializes the binary data from the specified file path into a ChatHistory instance using the provided model for context. This method reconstructs a ChatHistory object from its binary form, applying the model to interpret or configure the data appropriately.

Serialize()

Serializes this instance into a binary format and returns the resulting data as a byte array.

Serialize(Stream)

Serializes this instance into a binary format and writes it to the specified stream.

Serialize(string)

Serializes this instance into a binary format and writes it to the specified file path. This method converts the current instance into its binary representation and writes the data to a file at the given path.

StartNewConversation()

Initiates the beginning of a new conversation.

ToString()

Overrides ToString() by directly invoking the ToText() method.

ToText()

Converts the complete chat history, including the formatting provided by the chat template, into a single string.

ToTokens()

Returns the complete history in the form of model tokens.