session#

class besser.agent.core.session.Session(session_id, agent, platform)[source]#

Bases: object

A user session in an agent execution.

When a user starts interacting with an agent, a session is assigned to him/her to store user related information, such as the current state of the agent, the conversation history, the detected intent with its parameters or any user’s private data. A session can be accessed from the body of the states to read/write user information.

Parameters:
  • session_id (str) – The session id, which must unique among all agent sessions

  • agent (Agent) – The agent the session belongs to

  • platform (Platform) – The platform where the session has been created

_id#

The session id, which must unique among all agent sessions

Type:

str

_agent#

The agent the session belongs to

Type:

str

_platform#

The platform where the session has been created

Type:

str

_current_state#

The current state in the agent for this session

Type:

str

_dictionary#

Storage of private data for this session

Type:

str

_event#

The last event to trigger a transition.

Type:

Any or None

_events#

The queue of received external events to process

Type:

deque[Any]

_event_loop#

The loop in charge of managing incoming events

Type:

asyncio.AbstractEventLoop

_event_thread#

The thread where the event loop is running

Type:

threading.Thread

_timer_handle#

Handler of scheduled calls on the event loop

Type:

TimerHandle

_agent_connections#

WebSocket client connections to other agent’s WebSocket platforms. These connections enable an agent to send messages to other agents.

Type:

dict[str, WebSocketApp]

_run_event_thread()[source]#

Start the thread managing external events

_stop_event_thread()[source]#

Stop the thread managing external events

call_manage_transition()[source]#

Schedule the next call to manage_transition as soon as possible (cancelling the previously scheduled call).

create_agent_connection(url)[source]#

Create a WebSocket connection to a specific WebSocket URL.

Parameters:

url (str) – the WebSocket server’s URL

property current_state#

The current agent state of the session.

Type:

State

delete(key)[source]#

Delete an entry of the session private data storage.

Parameters:

key (str) – the entry key

property event#

The last event matched by the agent.

Type:

Event

property events#

The queue of pending events for this session

Type:

dequeue[Event]

get(key)[source]#

Get an entry of the session private data storage.

Parameters:

key (str) – the entry key

Returns:

the entry value, or None if the key does not exist

Return type:

Any

get_chat_history(n=None)[source]#

Get the history of messages between this session and its agent.

Parameters:

n (int or None) – the number of messages to get (from the most recents). If none is provided, gets all the messages

Returns:

the conversation history

Return type:

list[Message]

property id#

The session id.

Type:

str

manage_transition()[source]#

Evaluate the session’s current state transitions, where one could be satisfied and triggered.

move(transition)[source]#

Move to another agent state.

Parameters:

transition (Transition) – the transition that points to the agent state to move

property platform#

The session platform.

Type:

Platform

reply(message)[source]#

An agent message (usually a reply to a user message) is sent to the session platform to show it to the user.

Parameters:

message (str) – the agent reply

run_rag(message, llm_prompt=None, llm_name=None, k=None, num_previous_messages=None)[source]#

Run the RAG engine.

Parameters:
  • message (str) – the input query for the RAG engine

  • llm_prompt (str) – the prompt containing the instructions for the LLM to generate an answer from the retrieved content. If none is provided, the RAG’s default value will be used

  • llm_name (str) – the name of the LLM to use. If none is provided, the RAG’s default value will be used

  • k (int) – number of chunks to retrieve from the vector store. If none is provided, the RAG’s default value will be used

  • num_previous_messages (int) – number of previous messages of the conversation to add to the prompt context. If none is provided, the RAG’s default value will be used. Necessary a connection to MonitoringDB

Returns:

the answer generated by the RAG engine

Return type:

RAGMessage

save_message(message)[source]#

Save a message in the dedicated chat DB

Parameters:

message (Message) – the message to save

send_message_to_websocket(url, message)[source]#

Send a message to a WebSocket Server, generally used to send a message to an agent through the WebSocket platform.

Parameters:
  • url (str) – the WebSocket URL (i.e., the target agent’s WebSocket platform URL)

  • message (Any) – the message to send to the WebSocket server

set(key, value)[source]#

Set an entry to the session private data storage.

Parameters:
  • key (str) – the entry key

  • value (Any) – the entry value