Greetings agent#
1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
2# import sys
3# sys.path.append("/Path/to/directory/agentic-framework") # Replace with your directory path
4
5import logging
6
7from besser.agent.core.agent import Agent
8from besser.agent.core.session import Session
9from besser.agent.exceptions.logger import logger
10
11# Configure the logging module (optional)
12logger.setLevel(logging.INFO)
13
14# Create the agent
15agent = Agent('greetings_agent')
16# Load agent properties stored in a dedicated file
17agent.load_properties('config.ini')
18# Define the platform your agent will use
19websocket_platform = agent.use_websocket_platform(use_ui=True)
20
21# STATES
22
23initial_state = agent.new_state('initial_state', initial=True)
24hello_state = agent.new_state('hello_state')
25good_state = agent.new_state('good_state')
26bad_state = agent.new_state('bad_state')
27
28# INTENTS
29
30hello_intent = agent.new_intent('hello_intent', [
31 'hello',
32 'hi',
33])
34
35good_intent = agent.new_intent('good_intent', [
36 'good',
37 'fine',
38])
39
40bad_intent = agent.new_intent('bad_intent', [
41 'bad',
42 'awful',
43])
44
45
46# STATES BODIES' DEFINITION + TRANSITIONS
47
48
49initial_state.when_intent_matched(hello_intent).go_to(hello_state)
50
51
52def hello_body(session: Session):
53 session.reply('Hi! How are you?')
54
55
56hello_state.set_body(hello_body)
57hello_state.when_intent_matched(good_intent).go_to(good_state)
58hello_state.when_intent_matched(bad_intent).go_to(bad_state)
59
60
61def good_body(session: Session):
62 session.reply('I am glad to hear that!')
63
64
65good_state.set_body(good_body)
66good_state.go_to(initial_state)
67
68
69def bad_body(session: Session):
70 session.reply('I am sorry to hear that...')
71
72
73bad_state.set_body(bad_body)
74bad_state.go_to(initial_state)
75
76
77# RUN APPLICATION
78
79if __name__ == '__main__':
80 agent.run()