-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathweather_agent.py
74 lines (53 loc) · 1.99 KB
/
weather_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
# import sys
# sys.path.append("/Path/to/directory/agentic-framework") # Replace with your directory path
import logging
import random
from besser.agent.core.agent import Agent
from besser.agent.core.session import Session
from besser.agent.exceptions.logger import logger
from besser.agent.nlp.intent_classifier.intent_classifier_prediction import IntentClassifierPrediction
# Configure the logging module (optional)
logger.setLevel(logging.INFO)
agent = Agent('weather_agent')
# Load agent properties stored in a dedicated file
agent.load_properties('config.ini')
# Define the platform your agent will use
websocket_platform = agent.use_websocket_platform(use_ui=True)
# STATES
s0 = agent.new_state('s0', initial=True)
weather_state = agent.new_state('weather_state')
# ENTITIES
city_entity = agent.new_entity('city_entity', entries={
'Barcelona': ['BCN', 'barna'],
'Madrid': [],
'Luxembourg': ['LUX']
})
# INTENTS
weather_intent = agent.new_intent('weather_intent', [
'what is the weather in CITY?',
'weather in CITY',
])
weather_intent.parameter('city1', 'CITY', city_entity)
# STATES BODIES' DEFINITION + TRANSITIONS
def s0_body(session: Session):
session.reply('Waiting...')
s0.set_body(s0_body)
s0.when_intent_matched(weather_intent).go_to(weather_state)
def weather_body(session: Session):
predicted_intent: IntentClassifierPrediction = session.event.predicted_intent
city = predicted_intent.get_parameter('city1')
temperature = round(random.uniform(0, 30), 2)
if city.value is None:
session.reply("Sorry, I didn't get the city")
else:
session.reply(f"The weather in {city.value} is {temperature}°C")
if temperature < 15:
session.reply('🥶')
else:
session.reply('🥵')
weather_state.set_body(weather_body)
weather_state.go_to(s0)
# RUN APPLICATION
if __name__ == '__main__':
agent.run()