Agent ExampleΒΆ

As a simple B-UML agent model example, we modeled the Greetings Agent from the BAF documentation.

import datetime
from besser.BUML.metamodel.state_machine.state_machine import Body, ConfigProperty
from besser.BUML.metamodel.state_machine.agent import Agent, AgentSession
import operator

agent = Agent('Generated_Agent')

agent.add_property(ConfigProperty('websocket_platform', 'websocket.host', 'localhost'))
agent.add_property(ConfigProperty('websocket_platform', 'websocket.port', 8765))
agent.add_property(ConfigProperty('websocket_platform', 'streamlit.host', 'localhost'))
agent.add_property(ConfigProperty('websocket_platform', 'streamlit.port', 5000))
agent.add_property(ConfigProperty('nlp', 'nlp.language', 'en'))
agent.add_property(ConfigProperty('nlp', 'nlp.region', 'US'))
agent.add_property(ConfigProperty('nlp', 'nlp.timezone', 'Europe/Madrid'))
agent.add_property(ConfigProperty('nlp', 'nlp.pre_processing', True))
agent.add_property(ConfigProperty('nlp', 'nlp.intent_threshold', 0.4))

# INTENTS
Greeting = agent.new_intent('Greeting', [
    'Hi',
    'Hello',
    'Howdy',
])
Good = agent.new_intent('Good', [
    'Good',
    'Fine',
    'I m alright',
])
Bad = agent.new_intent('Bad', [
    'Bad',
    'Not so good',
    'Could be better',
])


# STATES
initial = agent.new_state('initial', initial=True)
greeting = agent.new_state('greeting')
bad = agent.new_state('bad')
good = agent.new_state('good')

# initial state
# greeting state
def greeting_body(session: AgentSession):
    session.reply('Hi!')
    session.reply('How are you?')

greeting.set_body(Body('greeting_body', greeting_body))
greeting.when_intent_matched(Good).go_to(good)
greeting.when_intent_matched(Bad).go_to(bad)

# bad state
def bad_body(session: AgentSession):
    session.reply('I m sorry to hear that...')

bad.set_body(Body('bad_body', bad_body))
bad.go_to(initial)

# good state
def good_body(session: AgentSession):
    session.reply('I am glad to hear that!')

good.set_body(Body('good_body', good_body))
good.go_to(initial)