websocket_platform#

class besser.agent.platforms.websocket.websocket_platform.WebSocketPlatform(agent, use_ui=True)[source]#

Bases: Platform

The WebSocket Platform allows an agent to communicate with the users using the WebSocket bidirectional communications protocol.

This platform implements the WebSocket server, and it can establish connection with a client, allowing the bidirectional communication between server and client (i.e. sending and receiving messages).

Note

We provide different interfaces implementing a WebSocket client to communicate with the agent, though you can use or create your own UI as long as it has a WebSocket client that connects to the agent’s WebSocket server.

Parameters:
  • agent (Agent) – the agent the platform belongs to

  • use_ui (bool) – whether to use the built-in UI or not

_agent#

The agent the platform belongs to

Type:

Agent

_host#

The WebSocket host address (e.g. localhost)

Type:

str

_port#

The WebSocket port (e.g. 8765)

Type:

int

_use_ui#

Whether to use the built-in UI or not

Type:

bool

_connections#

The list of active connections (i.e. users connected to the agent)

Type:

dict[str, ServerConnection]

_websocket_server#

The WebSocket server instance

Type:

WebSocketServer or None

_message_handler#

The function that handles the user connections (sessions) and incoming messages

Type:

Callable[[ServerConnection], None]

_abc_impl = <_abc._abc_data object>#
_send(session_id, payload)[source]#

Send a payload message to a specific user.

Parameters:
  • session_id (str) – the user to send the response to

  • payload (Payload) – the payload message to send to the user

initialize()[source]#

Initialize the platform. This function is called right before starting the platform.

reply(session, message)[source]#

Send an agent reply, i.e. a text message, to a specific user.

Parameters:
  • session (Session) – the user session

  • message (str) – the message to send to the user

reply_dataframe(session, df)[source]#

Send a DataFrame agent reply, i.e. a table, to a specific user.

Parameters:
reply_file(session, file)[source]#

Send a file reply to a specific user

Parameters:
  • session (Session) – the user session

  • file (File) – the file to send

reply_html(session, message)[source]#

Send an agent reply to a specific user, containing text in HTML format.

Parameters:
  • session (Session) – the user session

  • message (str) – the message in HTML format to send to the user

reply_image(session, img)[source]#

Send an image reply to a specific user.

Before being sent, the image is encoded as jpg and then as a base64 string. This must be known before dedocing the image on the client side.

Parameters:
  • session (Session) – the user session

  • img (np.ndarray) – the image to send

reply_location(session, latitude, longitude)[source]#

Send a location reply to a specific user.

Parameters:
  • session (Session) – the user session

  • latitude (str) – the latitude of the location

  • longitude (str) – the longitude of the location

reply_markdown(session, message)[source]#

Send an agent reply to a specific user, containing text in Markdown format.

Parameters:
  • session (Session) – the user session

  • message (str) – the message in Markdown format to send to the user

reply_options(session, options)[source]#

Send a list of options as a reply. They can be used to let the user choose one of them

Parameters:
  • session (Session) – the user session

  • options (list[str]) – the list of options to send to the user

reply_plotly(session, plot)[source]#

Send a Plotly figure as an agent reply, to a specific user.

Parameters:
  • session (Session) – the user session

  • plot (plotly.graph_objs.Figure) – the message to send to the user

reply_rag(session, rag_message)[source]#

Send a rag reply to a specific user.

Parameters:
  • session (Session) – the user session

  • rag_message (RAGMessage) – the rag message to send to the user

start()[source]#

Start the platform.

stop()[source]#

Terminate the platform execution.