Class ChatSession
Represents a single chat session, including its metadata, settings, messages, and serialization logic.
Provides support for saving, loading, summarizing, and auto-saving the session.
*Note:
- I've tried to make this 'ScriptableObject' but since you need to be able to create instances of this class at runtime, it is not possible.
- So remember this and don't try it again.
public class ChatSession : IChatConversation<ChatMessage>
- Inheritance
-
objectChatSession
- Implements
Properties
Count
public int Count { get; }
Property Value
- int
CreatedAt
Creation timestamp.
public UnixTime CreatedAt { get; }
Property Value
- UnixTime
Id
Unique identifier of the chat session.
public string Id { get; }
Property Value
- string
IsInitialized
public bool IsInitialized { get; }
Property Value
- bool
LastException
public Exception LastException { get; }
Property Value
- Exception
LastMessage
The last message sent by the user or received from the assistant.
public string LastMessage { get; }
Property Value
- string
Messages
List of all chat messages in the session. Known as 'ConversationBufferMemory' in LangChain.
public List<ChatMessage> Messages { get; }
Property Value
- List<ChatMessage>
Name
Name of this data.
public string Name { get; }
Property Value
- string
RecentMessages
List of recent chat messages used for summarization.
public List<ChatMessage> RecentMessages { get; }
Property Value
- List<ChatMessage>
Summary
The current summary of the session.
public ChatSummary Summary { get; }
Property Value
Title
public string Title { get; set; }
Property Value
- string
TotalCostInUSD
Total cost of the session in USD. This is calculated based on the total usage and the cost per token.
public float TotalCostInUSD { get; }
Property Value
- float
TotalUsage
Total usage statistics for the session. This includes token usage, cost, and other metrics.
public Usage TotalUsage { get; }
Property Value
UpdatedAt
Last update timestamp.
public UnixTime UpdatedAt { get; }
Property Value
- UnixTime
Methods
AddTotalUsage(Usage, Currency)
public void AddTotalUsage(Usage usage, Currency cost)
Parameters
usageUsagecostCurrency
ClearMessages()
public void ClearMessages()
CreateSession()
Creates a new chat session. The unique Id will be generated automatically based on the current date and time.
public static ChatSession CreateSession()
Returns
DeleteSession()
public bool DeleteSession()
Returns
- bool
DeleteSession(string)
Permanantly deletes a chat session by session Id. This action cannot be undone.
public static bool DeleteSession(string sessionId)
Parameters
sessionIdstring
Returns
- bool
Equals(ChatSession)
public bool Equals(ChatSession other)
Parameters
otherChatSession
Returns
- bool
GENChat(ChatMessage)
Generates a new chat task for the session.
This task can be used to send messages to the AI model and receive responses.
Example:
string responseMessage = await chatSession.GENChat(chatMessage).ExecuteAsync();
public GENChatTask GENChat(ChatMessage chatMessage)
Parameters
chatMessageChatMessage
Returns
GENChatEdit(int, string)
Edits a user message at the specified index in the chat session. This will update the message content and timestamp, and remove all messages after the edited message.
public GENChatTask GENChatEdit(int index, string editedContent)
Parameters
indexinteditedContentstring
Returns
Exceptions
- ArgumentOutOfRangeException
- InvalidOperationException
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Initialize(ILocalChatbotProfile)
public void Initialize(ILocalChatbotProfile profile)
Parameters
profileILocalChatbotProfile
LoadSession(string)
Loads a chat session by session Id.
public static ChatSession LoadSession(string sessionId)
Parameters
sessionIdstring
Returns
ReCalcTotalCost()
public void ReCalcTotalCost()
ReCalcTotalUsage()
public void ReCalcTotalUsage()
Rewind()
public (ChatMessage lastSent, ChatMessage lastReceived) Rewind()
Returns
SaveSession()
public void SaveSession()
SaveSessionAsync()
public UniTask SaveSessionAsync()
Returns
- UniTask
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
Events
OnTitleChanged
public event Action<string> OnTitleChanged
Event Type
- Action<string>