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 baf.core.agent import Agent
8from baf.core.session import Session
9from baf.exceptions.logger import logger
10
11# Configure the logging module (optional)
12logger.setLevel(logging.INFO)
13
14# Create the agent
15agent = Agent('greetings_agent') # set persist_sessions=True to enable session persistence across restarts
16# Load agent properties stored in a dedicated file
17agent.load_properties('config.yaml')
18# Define the platform your agent will use
19# set authenticate_users=True to enable user authentication and previous history loading in the UI
20websocket_platform = agent.use_websocket_platform(use_ui=True)
21
22# STATES
23
24initial_state = agent.new_state('initial_state', initial=True)
25hello_state = agent.new_state('hello_state')
26good_state = agent.new_state('good_state')
27bad_state = agent.new_state('bad_state')
28
29# INTENTS
30
31hello_intent = agent.new_intent('hello_intent', [
32 'hello',
33 'hi',
34])
35
36good_intent = agent.new_intent('good_intent', [
37 'good',
38 'fine',
39])
40
41bad_intent = agent.new_intent('bad_intent', [
42 'bad',
43 'awful',
44])
45
46
47# STATES BODIES' DEFINITION + TRANSITIONS
48
49
50initial_state.when_intent_matched(hello_intent).go_to(hello_state)
51
52
53def hello_body(session: Session):
54 session.reply('Hi! How are you?')
55
56
57hello_state.set_body(hello_body)
58hello_state.when_intent_matched(good_intent).go_to(good_state)
59hello_state.when_intent_matched(bad_intent).go_to(bad_state)
60
61
62def good_body(session: Session):
63 session.reply('I am glad to hear that!')
64
65
66good_state.set_body(good_body)
67good_state.go_to(initial_state)
68
69
70def bad_body(session: Session):
71 session.reply('I am sorry to hear that...')
72
73
74bad_state.set_body(bad_body)
75bad_state.go_to(initial_state)
76
77
78# RUN APPLICATION
79
80if __name__ == '__main__':
81 agent.run()