Skip to content

ChatSession

Namespace: LLama

The main chat session class.

1
public class ChatSession

Inheritance ObjectChatSession

Fields

OutputTransform

The output transform used in this session.

1
public ITextStreamTransform OutputTransform;

MODEL_STATE_FILENAME

The filename for the serialized model state (KV cache, etc).

1
public static string MODEL_STATE_FILENAME;

EXECUTOR_STATE_FILENAME

The filename for the serialized executor state.

1
public static string EXECUTOR_STATE_FILENAME;

HISTORY_STATE_FILENAME

The filename for the serialized chat history.

1
public static string HISTORY_STATE_FILENAME;

INPUT_TRANSFORM_FILENAME

The filename for the serialized input transform pipeline.

1
public static string INPUT_TRANSFORM_FILENAME;

OUTPUT_TRANSFORM_FILENAME

The filename for the serialized output transform.

1
public static string OUTPUT_TRANSFORM_FILENAME;

HISTORY_TRANSFORM_FILENAME

The filename for the serialized history transform.

1
public static string HISTORY_TRANSFORM_FILENAME;

Properties

Executor

The executor for this session.

1
public ILLamaExecutor Executor { get; private set; }

Property Value

ILLamaExecutor

History

The chat history for this session.

1
public ChatHistory History { get; private set; }

Property Value

ChatHistory

HistoryTransform

The history transform used in this session.

1
public IHistoryTransform HistoryTransform { get; set; }

Property Value

IHistoryTransform

InputTransformPipeline

The input transform pipeline used in this session.

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

Property Value

List<ITextTransform>

Constructors

ChatSession(ILLamaExecutor)

Create a new chat session.

1
public ChatSession(ILLamaExecutor executor)

Parameters

executor ILLamaExecutor
The executor for this session

ChatSession(ILLamaExecutor, ChatHistory)

Create a new chat session with a custom history.

1
public ChatSession(ILLamaExecutor executor, ChatHistory history)

Parameters

executor ILLamaExecutor

history ChatHistory

Methods

InitializeSessionFromHistoryAsync(ILLamaExecutor, ChatHistory)

Create a new chat session and preprocess history.

1
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.

1
public ChatSession WithHistoryTransform(IHistoryTransform transform)

Parameters

transform IHistoryTransform

Returns

ChatSession

AddInputTransform(ITextTransform)

Add a text transform to the input transform pipeline.

1
public ChatSession AddInputTransform(ITextTransform transform)

Parameters

transform ITextTransform

Returns

ChatSession

WithOutputTransform(ITextStreamTransform)

Use a custom output transform.

1
public ChatSession WithOutputTransform(ITextStreamTransform transform)

Parameters

transform ITextStreamTransform

Returns

ChatSession

SaveSession(String)

Save a session from a directory.

1
public void SaveSession(string path)

Parameters

path String

Exceptions

ArgumentException

GetSessionState()

Get the session state.

1
public SessionState GetSessionState()

Returns

SessionState
SessionState object representing session state in-memory

LoadSession(SessionState, Boolean)

Load a session from a session state.

1
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.

1
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.

1
public ChatSession AddMessage(Message message)

Parameters

message Message

Returns

ChatSession

AddSystemMessage(String)

Add a system message to the chat history.

1
public ChatSession AddSystemMessage(string content)

Parameters

content String

Returns

ChatSession

AddAssistantMessage(String)

Add an assistant message to the chat history.

1
public ChatSession AddAssistantMessage(string content)

Parameters

content String

Returns

ChatSession

AddUserMessage(String)

Add a user message to the chat history.

1
public ChatSession AddUserMessage(string content)

Parameters

content String

Returns

ChatSession

RemoveLastMessage()

Remove the last message from the chat history.

1
public ChatSession RemoveLastMessage()

Returns

ChatSession

AddAndProcessMessage(Message)

Compute KV cache for the message and add it to the chat history.

1
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.

1
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.

1
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.

1
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.

1
public ChatSession ReplaceUserMessage(Message oldMessage, Message newMessage)

Parameters

oldMessage Message

newMessage Message

Returns

ChatSession

ChatAsync(Message, Boolean, IInferenceParams, CancellationToken)

Chat with the model.

1
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.

1
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.

1
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.

1
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.

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

Parameters

inferenceParams InferenceParams

cancellationToken CancellationToken

Returns

IAsyncEnumerable<String>

Exceptions

InvalidOperationException