SmartSession

Wisej.AI.SmartSession

Namespace: Wisej.AI

Assembly: Wisej.AI (3.5.0.0)

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.

Name
Type
Description

hub

The smart hub associated with the session.

systemPrompt

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

Name
Type
Description

endpoint

The smart endpoint associated with the session.

systemPrompt

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

Parameter
Type
Description

question

The question to ask.

image

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

Parameter
Type
Description

message

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

Parameter
Type
Description

question

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

Parameter
Type
Description

value

Returns: String.

Dispose()

Releases all resources used by the SmartSession.

GetParameterValue(parameter)

Gets the value of a parameter.

Parameter
Type
Description

parameter

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

Parameter
Type
Description

messages

The collection of messages to prepare.

RemoveTool(tool)

Removes a tool from the smart session.

Parameter
Type
Description

tool

The tool to remove.

Returns: SmartSession. The current SmartSession instance.

Throws:

ReplaceParameters(prompt)

Replaces parameters in the given prompt with their values.

Parameter
Type
Description

prompt

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

Parameter
Type
Description

messages

The collection of messages to trim.

Returns: Task.

UseTool(tool)

Registers a tool using a delegate.

Parameter
Type
Description

tool

The delegate representing the tool to register.

Returns: SmartSession. The current SmartSession instance.

Throws:

UseTools(target)

Registers the tools declared on the target.

Parameter
Type
Description

target

The target object containing tools to register.

Returns: SmartSession. The current SmartSession instance.

Inherited By

Name
Description

Last updated