ChatSession
Namespace: LLama
The main chat session class.
public class ChatSession
Inheritance Object → ChatSession
Fields
OutputTransform
The output transform used in this session.
public ITextStreamTransform OutputTransform;
MODEL_STATE_FILENAME
The filename for the serialized model state (KV cache, etc).
public static string MODEL_STATE_FILENAME;
EXECUTOR_STATE_FILENAME
The filename for the serialized executor state.
public static string EXECUTOR_STATE_FILENAME;
HISTORY_STATE_FILENAME
The filename for the serialized chat history.
public static string HISTORY_STATE_FILENAME;
INPUT_TRANSFORM_FILENAME
The filename for the serialized input transform pipeline.
public static string INPUT_TRANSFORM_FILENAME;
OUTPUT_TRANSFORM_FILENAME
The filename for the serialized output transform.
public static string OUTPUT_TRANSFORM_FILENAME;
HISTORY_TRANSFORM_FILENAME
The filename for the serialized history transform.
public static string HISTORY_TRANSFORM_FILENAME;
Properties
Executor
The executor for this session.
public ILLamaExecutor Executor { get; private set; }
Property Value
History
The chat history for this session.
public ChatHistory History { get; private set; }
Property Value
HistoryTransform
The history transform used in this session.
public IHistoryTransform HistoryTransform { get; set; }
Property Value
InputTransformPipeline
The input transform pipeline used in this session.
public List<ITextTransform> InputTransformPipeline { get; set; }
Property Value
Constructors
ChatSession(ILLamaExecutor)
Create a new chat session.
public ChatSession(ILLamaExecutor executor)
Parameters
executor ILLamaExecutor
The executor for this session
ChatSession(ILLamaExecutor, ChatHistory)
Create a new chat session with a custom history.
public ChatSession(ILLamaExecutor executor, ChatHistory history)
Parameters
executor ILLamaExecutor
history ChatHistory
Methods
InitializeSessionFromHistoryAsync(ILLamaExecutor, ChatHistory)
Create a new chat session and preprocess history.
public static Task<ChatSession> InitializeSessionFromHistoryAsync(ILLamaExecutor executor, ChatHistory history)
Parameters
executor ILLamaExecutor
The executor for this session
history ChatHistory
History for this session
Returns
WithHistoryTransform(IHistoryTransform)
Use a custom history transform.
public ChatSession WithHistoryTransform(IHistoryTransform transform)
Parameters
transform IHistoryTransform
Returns
AddInputTransform(ITextTransform)
Add a text transform to the input transform pipeline.
public ChatSession AddInputTransform(ITextTransform transform)
Parameters
transform ITextTransform
Returns
WithOutputTransform(ITextStreamTransform)
Use a custom output transform.
public ChatSession WithOutputTransform(ITextStreamTransform transform)
Parameters
transform ITextStreamTransform
Returns
SaveSession(String)
Save a session from a directory.
public void SaveSession(string path)
Parameters
path String
Exceptions
GetSessionState()
Get the session state.
public SessionState GetSessionState()
Returns
SessionState
SessionState object representing session state in-memory
LoadSession(SessionState, Boolean)
Load a session from a session state.
public void LoadSession(SessionState state, bool loadTransforms)
Parameters
state SessionState
loadTransforms Boolean
If true loads transforms saved in the session state.
Exceptions
LoadSession(String, Boolean)
Load a session from a directory.
public void LoadSession(string path, bool loadTransforms)
Parameters
path String
loadTransforms Boolean
If true loads transforms saved in the session state.
Exceptions
AddMessage(Message)
Add a message to the chat history.
public ChatSession AddMessage(Message message)
Parameters
message Message
Returns
AddSystemMessage(String)
Add a system message to the chat history.
public ChatSession AddSystemMessage(string content)
Parameters
content String
Returns
AddAssistantMessage(String)
Add an assistant message to the chat history.
public ChatSession AddAssistantMessage(string content)
Parameters
content String
Returns
AddUserMessage(String)
Add a user message to the chat history.
public ChatSession AddUserMessage(string content)
Parameters
content String
Returns
RemoveLastMessage()
Remove the last message from the chat history.
public ChatSession RemoveLastMessage()
Returns
AddAndProcessMessage(Message)
Compute KV cache for the message and add it to the chat history.
public Task<ChatSession> AddAndProcessMessage(Message message)
Parameters
message Message
Returns
AddAndProcessSystemMessage(String)
Compute KV cache for the system message and add it to the chat history.
public Task<ChatSession> AddAndProcessSystemMessage(string content)
Parameters
content String
Returns
AddAndProcessUserMessage(String)
Compute KV cache for the user message and add it to the chat history.
public Task<ChatSession> AddAndProcessUserMessage(string content)
Parameters
content String
Returns
AddAndProcessAssistantMessage(String)
Compute KV cache for the assistant message and add it to the chat history.
public Task<ChatSession> AddAndProcessAssistantMessage(string content)
Parameters
content String
Returns
ReplaceUserMessage(Message, Message)
Replace a user message with a new message and remove all messages after the new message. This is useful when the user wants to edit a message. And regenerate the response.
public ChatSession ReplaceUserMessage(Message oldMessage, Message newMessage)
Parameters
oldMessage Message
newMessage Message
Returns
ChatAsync(Message, Boolean, IInferenceParams, CancellationToken)
Chat with the model.
public IAsyncEnumerable<string> ChatAsync(Message message, bool applyInputTransformPipeline, IInferenceParams inferenceParams, CancellationToken cancellationToken)
Parameters
message Message
applyInputTransformPipeline Boolean
inferenceParams IInferenceParams
cancellationToken CancellationToken
Returns
Exceptions
ChatAsync(Message, IInferenceParams, CancellationToken)
Chat with the model.
public IAsyncEnumerable<string> ChatAsync(Message message, IInferenceParams inferenceParams, CancellationToken cancellationToken)
Parameters
message Message
inferenceParams IInferenceParams
cancellationToken CancellationToken
Returns
ChatAsync(ChatHistory, Boolean, IInferenceParams, CancellationToken)
Chat with the model.
public IAsyncEnumerable<string> ChatAsync(ChatHistory history, bool applyInputTransformPipeline, IInferenceParams inferenceParams, CancellationToken cancellationToken)
Parameters
history ChatHistory
applyInputTransformPipeline Boolean
inferenceParams IInferenceParams
cancellationToken CancellationToken
Returns
Exceptions
ChatAsync(ChatHistory, IInferenceParams, CancellationToken)
Chat with the model.
public IAsyncEnumerable<string> ChatAsync(ChatHistory history, IInferenceParams inferenceParams, CancellationToken cancellationToken)
Parameters
history ChatHistory
inferenceParams IInferenceParams
cancellationToken CancellationToken
Returns
RegenerateAssistantMessageAsync(InferenceParams, CancellationToken)
Regenerate the last assistant message.
public IAsyncEnumerable<string> RegenerateAssistantMessageAsync(InferenceParams inferenceParams, CancellationToken cancellationToken)
Parameters
inferenceParams InferenceParams
cancellationToken CancellationToken