Skip to main content

SmartSession

Namespace: Wisej.AI

Assembly: Wisej.AI

Represents a session that manages interactions with a smart hub and endpoint, handling prompts, messages, and tools within the session context.

public class SmartSession : SmartObject

The SmartSession class is responsible for managing the lifecycle of a session, including initialization, message handling, and tool usage. It supports asynchronous operations for asking questions and processing responses, and it can handle context overflow through truncation or summarization strategies.

Constructors

SmartSession(hub, systemPrompt)

Initializes a new instance of the SmartSession class with the specified hub and optional system prompt.

NameTypeDescription
hubSmartHubThe smart hub associated with the session.
systemPromptSmartPromptThe optional system prompt for the session. Default is null.

Throws:

SmartSession(endpoint, systemPrompt)

Initializes a new instance of the SmartSession class with the specified endpoint and optional system prompt.

NameTypeDescription
endpointSmartEndpointThe smart endpoint associated with the session.
systemPromptSmartPromptThe optional system prompt for the session. Default is null.

Throws:

Properties

Endpoint

SmartEndpoint: Gets the smart endpoint associated with the session.

HasModelOptions

Boolean: Gets a value indicating whether the session has model options.

Hub

SmartHub: Gets the smart hub associated with the session.

IsDisposed

Boolean: Gets a value indicating whether the session has been disposed.

Messages

MessageCollection: Gets the collection of messages in the session.

ModelOptions

Object: Gets or sets the model options for the session.

Response

Message: Gets the last response message from the assistant.

SystemPrompt

SmartPrompt: Gets the system prompt for the session.

Methods

AskAsync(question, image)

Asynchronously asks a question and returns the response message.

ParameterTypeDescription
questionStringThe question to ask.
image ImageAn optional image associated with the question. Default is null.

Returns: Task<Message>. A task representing the asynchronous operation, with a Message as the result.

Throws:

AskAsync(message)

Asynchronously asks a question using a message and returns the response message.

ParameterTypeDescription
messageMessageThe message containing the question.

Returns: Task<Message>. A task representing the asynchronous operation, with a Message as the result.

Throws:

AskAsyncCore(question)

Asynchronously sends a question to the AI and processes the response.

ParameterTypeDescription
questionMessageThe message containing the question to be sent to the AI.

Returns: Task<Message>. A task representing the asynchronous operation, with a Message as the result containing the AI's response.

This method handles the communication with the AI, including managing the session's message context, handling tool calls, and processing AI responses. It ensures that the session is not disposed and that the question is not null before proceeding. The method also manages the context window size and handles exceptions such as token limit exceedance.


var response = await AskAsyncCore(new Message { Text = "What is the weather today?" });
Console.WriteLine(response.Text);

ConvertParameter(value)

Raises the ConvertParameter event.

ParameterTypeDescription
valueObject

Returns: String.

Dispose()

Releases all resources used by the SmartSession.

GetParameterValue(parameter)

Gets the value of a parameter.

ParameterTypeDescription
parameterParameterThe parameter to get the value for.

Returns: String. The value of the parameter as a string.

IsTerminated()

Returns: Boolean.

PrepareSessionMessages(messages)

Prepares session messages by replacing parameters in the system and user messages.

ParameterTypeDescription
messagesMessageCollectionThe collection of messages to prepare.

RemoveTool(tool)

Removes a tool from the smart session.

ParameterTypeDescription
toolDelegateThe tool to remove.

Returns: SmartSession. The current SmartSession instance.

Throws:

ReplaceParameters(prompt)

Replaces parameters in the given prompt with their values.

ParameterTypeDescription
promptStringThe prompt containing parameters to replace.

Returns: String. The prompt with parameters replaced by their values.

TrimMessagesAsync(messages)

Trims messages in the session based on the specified truncation strategy.

ParameterTypeDescription
messagesMessageCollectionThe collection of messages to trim.

Returns: Task.

UseTool(tool)

Registers a tool using a delegate.

ParameterTypeDescription
toolDelegateThe delegate representing the tool to register.

Returns: SmartSession. The current SmartSession instance.

Throws:

UseTools(target)

Registers the tools declared on the target.

ParameterTypeDescription
targetObjectThe target object containing tools to register.

Returns: SmartSession. The current SmartSession instance.

Inherited By

NameDescription
SmartRealtimeSession