agent#
- class baf.core.agent.Agent(name, persist_sessions=False, user_profiles_path=None, agent_configurations_path=None)[source]#
Bases:
objectThe agent class.
- Parameters:
- _platforms_threads#
The threads where the platforms are run
- Type:
- _event_loop#
The event loop managing external events
- _event_thread#
The thread where the event loop is run
- Type:
- _default_ic_config#
the intent classifier configuration used by default for the agent states
- _trained#
Whether the agent has been trained or not. It must be trained before it starts its execution.
- Type:
- _monitoring_db#
The monitoring component of the agent that communicates with a database to store usage information for later visualization or analysis
- Type:
- global_initial_states#
List of tuples of initial global states and their triggering intent
- global_state_component#
Dictionary of global state components, where key is initial global state and values is set of states in corresponding global component
- _init_global_states()[source]#
Initialise the global states and add the necessary transitions.
Go through all the global states and add transitions to every state to jump to the global states. Also add the transition to jump back to the previous state once the global state component has been completed.
- _monitoring_db_delete_session(session)[source]#
Delete a session record from the monitoring database.
- Parameters:
session (Session) – the session of the current user
- _monitoring_db_get_last_state_of_session(session_id, platform)[source]#
Get the last state of a session from the monitoring database.
- _monitoring_db_insert_chat(session, message)[source]#
Insert a message record into the monitoring database.
- Parameters:
session (Session) – the session of the current user
- _monitoring_db_insert_event(event)[source]#
Insert an event record into the monitoring database.
- Parameters:
event (Event) – the event to insert into the database
- _monitoring_db_insert_intent_prediction(session, predicted_intent)[source]#
Insert an intent prediction record into the monitoring database.
- Parameters:
session (Session) – the session of the current user
predicted_intent (IntentClassifierPrediction) – the intent prediction
- _monitoring_db_insert_session(session)[source]#
Insert a session record into the monitoring database.
- Parameters:
session (Session) – the session of the current user
- _monitoring_db_insert_transition(session, transition)[source]#
Insert a transition record into the monitoring database.
- Parameters:
session (Session) – the session of the current user
- _monitoring_db_load_session_variables(session)[source]#
Load the session variables (private data dictionary) from the monitoring database.
- Parameters:
session (Session) – The session to load the variables for.
- _monitoring_db_session_exists(session_id, platform)[source]#
Check if a session with the given session_id exists in the monitoring database.
- _monitoring_db_store_session_variables(session)[source]#
Store the session variables (private data dictionary) in the monitoring database.
- Parameters:
session (Session) – The session to store the variables for.
- property agent_configurations#
Return loaded agent configurations mapped by profile/user key.
- close_session(session_id)[source]#
Delete an existing agent session.
- Parameters:
session_id (str) – the session id
- delete_session(session_id)[source]#
Delete an existing agent session.
- Parameters:
session_id (str) – the session id
- get_property(prop)[source]#
Get an agent property’s value
- Parameters:
prop (Property) – the property to get its value
- Returns:
the property value, or None
- Return type:
Any
- initial_state()[source]#
Get the agent’s initial state. It can be None if it has not been set.
- Returns:
the initial state of the agent, if exists
- Return type:
State or None
- load_agent_configurations(path)[source]#
Load agent configurations from a JSON file.
Expected format: a mapping where keys represent profile/user identifiers and values are the corresponding agent configuration objects.
- load_properties(path)[source]#
Read a properties file and store its properties in the agent configuration.
Supported formats are YAML (
.yaml,.yml).Example YAML properties file,
config.yaml:- Parameters:
path (str) – the path to the properties file
- new_entity(name, base_entity=False, entries=None, description=None)[source]#
Create a new entity in the agent.
- Parameters:
- Returns:
the entity
- Return type:
- new_intent(name, training_sentences=None, parameters=None, description=None)[source]#
Create a new intent in the agent.
- Parameters:
- Returns:
the intent
- Return type:
- new_state(name, initial=False, ic_config=None)[source]#
Create a new state in the agent.
- Parameters:
name (str) – the state name. It must be unique in the agent.
initial (bool) – whether the state is initial or not. An agent must have 1 initial state.
ic_config (IntentClassifierConfiguration or None) – the intent classifier configuration for the state. If None is provided, the agent’s default one will be assigned to the state.
- Returns:
the state
- Return type:
- process(session, message, is_user_message)[source]#
Runs the agent processors in a message.
Only processors that process messages of the same type as the given message will be run. If the message to process is a user message, only processors that process user messages will be run. If the message to process is an agent message, only processors that process agent messages will be run.
- receive_event(event)[source]#
Receive an external event from a platform.
Receiving an event and send it to all the applicable sessions of the agent
- Parameters:
event (Event) – the received event
- reset(session_id)[source]#
Reset the agent current state and memory for the specified session. Then, restart the agent again for this session.
- set_default_ic_config(ic_config)[source]#
Set the default intent classifier configuration.
- Parameters:
ic_config (IntentClassifierConfiguration) – the intent classifier configuration
- set_global_fallback_body(body)[source]#
Set the fallback body for all agent states.
The fallback body is a state’s callable function that will be run whenever necessary to handle unexpected scenarios (e.g. when no intent is matched, the current state’s fallback is run). This method simply sets the same fallback body to all agent states.
See also
- Parameters:
body (Callable[[Session], None]) – the fallback body
- set_property(prop, value)[source]#
Set an agent property.
- Parameters:
prop (Property) – the property to set
value (Any) – the property value
- use_a2a_platform()[source]#
Use the :class: ~baf.platforms.a2a.a2a_platform.A2APlatform on this agent.
- Returns:
the A2A platform
- Return type:
- use_db_handler()[source]#
Use the
DBHandleron this agent.DB connections are established lazily, when the first query is executed.
- Returns:
the database handler
- Return type:
- use_github_platform()[source]#
Use the
GitHubPlatformon this agent.- Returns:
the GitHub platform
- Return type:
- use_gitlab_platform()[source]#
Use the
GitLabPlatformon this agent.- Returns:
the GitLab platform
- Return type:
- use_telegram_platform()[source]#
Use the
TelegramPlatformon this agent.- Returns:
the telegram platform
- Return type:
- use_websocket_platform(use_ui=True, authenticate_users=False)[source]#
Use the
WebSocketPlatformon this agent.- Parameters:
- Returns:
the websocket platform
- Return type:
- property user_profiles#
Return loaded user profiles data, or None if not set.