Share via


ChatHistory Class

  • java.lang.Object
    • com.microsoft.semantickernel.services.chatcompletion.ChatHistory

Implements

java.lang.Iterable<ChatMessageContent<?>>

public class ChatHistory
implements java.lang.Iterable<ChatMessageContent<?>>

Provides a history of messages between the User, Assistant and System

Constructor Summary

Constructor Description
ChatHistory()

The default constructor

ChatHistory(String instructions)

Constructor that adds the given system instructions to the chat history.

ChatHistory(List<? extends ChatMessageContent<?>> chatMessageContents)

Constructor that adds the given chat message contents to the chat history.

Method Summary

Modifier and Type Method and Description
void addAll(ChatHistory value)

Add all messages from the given chat history to this chat history

ChatHistory addAll(List<ChatMessageContent<?>> messages)

Add all messages to the chat history

ChatHistory addAssistantMessage(String content)

Add an assistant message to the chat history

ChatHistory addMessage(AuthorRole authorRole, String content)

Add a message to the chat history

ChatHistory addMessage(AuthorRole authorRole, String content, Charset encoding, FunctionResultMetadata<?> metadata)

Add a message to the chat history

ChatHistory addMessage(ChatMessageContent<?> content)

Add a message to the chat history

ChatHistory addSystemMessage(String content)

Add an system message to the chat history

ChatHistory addUserMessage(String content)

Add a user message to the chat history

void forEach(Consumer<? super ChatMessageContent<?>> action)

Perform the given action for each message in the chat history

java.util.Optional<ChatMessageContent<?>> getLastMessage()

Get last message

java.util.List<ChatMessageContent<?>> getMessages()

Get the chat history

java.util.Iterator<ChatMessageContent<?>> iterator()

Create an Iterator from the chat history.

java.util.Spliterator<ChatMessageContent<?>> spliterator()

Create a Spliterator from the chat history

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Constructor Details

ChatHistory

public ChatHistory()

The default constructor

ChatHistory

public ChatHistory(String instructions)

Constructor that adds the given system instructions to the chat history.

Parameters:

instructions - The instructions to add to the chat history

ChatHistory

public ChatHistory(List> chatMessageContents)

Constructor that adds the given chat message contents to the chat history.

Parameters:

chatMessageContents - The chat message contents to add to the chat history

Method Details

addAll

public void addAll(ChatHistory value)

Add all messages from the given chat history to this chat history

Parameters:

value - The chat history to add to this chat history

addAll

public ChatHistory addAll(List> messages)

Add all messages to the chat history

Parameters:

messages - The messages to add to the chat history

Returns:

this ChatHistory

addAssistantMessage

public ChatHistory addAssistantMessage(String content)

Add an assistant message to the chat history

Parameters:

content - The content of the assistant message

Returns:

this ChatHistory

addMessage

public ChatHistory addMessage(AuthorRole authorRole, String content)

Add a message to the chat history

Parameters:

authorRole - The role of the author of the message
content - The content of the message

Returns:

this ChatHistory

addMessage

public ChatHistory addMessage(AuthorRole authorRole, String content, Charset encoding, FunctionResultMetadata metadata)

Add a message to the chat history

Parameters:

authorRole - The role of the author of the message
content - The content of the message
encoding - The encoding of the message
metadata - The metadata of the message

Returns:

this ChatHistory

addMessage

public ChatHistory addMessage(ChatMessageContent content)

Add a message to the chat history

Parameters:

content - The content of the message

Returns:

this ChatHistory

addSystemMessage

public ChatHistory addSystemMessage(String content)

Add an system message to the chat history

Parameters:

content - The content of the system message

Returns:

this ChatHistory

addUserMessage

public ChatHistory addUserMessage(String content)

Add a user message to the chat history

Parameters:

content - The content of the user message

Returns:

this ChatHistory

forEach

public void forEach(Consumer> action)

Perform the given action for each message in the chat history

Parameters:

action - The action to perform for each message in the chat history

getLastMessage

public Optional> getLastMessage()

Get last message

Returns:

The most recent message in chat

getMessages

public List> getMessages()

Get the chat history

Returns:

List of messages in the chat

iterator

public Iterator> iterator()

Create an Iterator from the chat history.

Returns:

An Iterator from the chat history.

spliterator

public Spliterator> spliterator()

Create a Spliterator from the chat history

Returns:

A Spliterator from the chat history

Applies to