Skip to content

ChatSession

Namespace: LLama

The main chat session class.

public class ChatSession

Inheritance ObjectChatSession

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

ILLamaExecutor

History

The chat history for this session.

public ChatHistory History { get; private set; }

Property Value

ChatHistory

HistoryTransform

The history transform used in this session.

public IHistoryTransform HistoryTransform { get; set; }

Property Value

IHistoryTransform

InputTransformPipeline

The input transform pipeline used in this session.

public List<ITextTransform> InputTransformPipeline { get; set; }

Property Value

List<ITextTransform>

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

Task<ChatSession>

WithHistoryTransform(IHistoryTransform)

Use a custom history transform.

public ChatSession WithHistoryTransform(IHistoryTransform transform)

Parameters

transform IHistoryTransform

Returns

ChatSession

AddInputTransform(ITextTransform)

Add a text transform to the input transform pipeline.

public ChatSession AddInputTransform(ITextTransform transform)

Parameters

transform ITextTransform

Returns

ChatSession

WithOutputTransform(ITextStreamTransform)

Use a custom output transform.

public ChatSession WithOutputTransform(ITextStreamTransform transform)

Parameters

transform ITextStreamTransform

Returns

ChatSession

SaveSession(String)

Save a session from a directory.

public void SaveSession(string path)

Parameters

path String

Exceptions

ArgumentException

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

ArgumentException

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

ArgumentException

AddMessage(Message)

Add a message to the chat history.

public ChatSession AddMessage(Message message)

Parameters

message Message

Returns

ChatSession

AddSystemMessage(String)

Add a system message to the chat history.

public ChatSession AddSystemMessage(string content)

Parameters

content String

Returns

ChatSession

AddAssistantMessage(String)

Add an assistant message to the chat history.

public ChatSession AddAssistantMessage(string content)

Parameters

content String

Returns

ChatSession

AddUserMessage(String)

Add a user message to the chat history.

public ChatSession AddUserMessage(string content)

Parameters

content String

Returns

ChatSession

RemoveLastMessage()

Remove the last message from the chat history.

public ChatSession RemoveLastMessage()

Returns

ChatSession

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

Task<ChatSession>

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

Task<ChatSession>

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

Task<ChatSession>

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

Task<ChatSession>

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

ChatSession

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

IAsyncEnumerable<String>

Exceptions

ArgumentException

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

IAsyncEnumerable<String>

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

IAsyncEnumerable<String>

Exceptions

ArgumentException

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

IAsyncEnumerable<String>

RegenerateAssistantMessageAsync(InferenceParams, CancellationToken)

Regenerate the last assistant message.

public IAsyncEnumerable<string> RegenerateAssistantMessageAsync(InferenceParams inferenceParams, CancellationToken cancellationToken)

Parameters

inferenceParams InferenceParams

cancellationToken CancellationToken

Returns

IAsyncEnumerable<String>

Exceptions

InvalidOperationException