Weather 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
 6import random
 7
 8from besser.agent.core.agent import Agent
 9from besser.agent.core.session import Session
10from besser.agent.exceptions.logger import logger
11from besser.agent.nlp.intent_classifier.intent_classifier_prediction import IntentClassifierPrediction
12
13# Configure the logging module (optional)
14logger.setLevel(logging.INFO)
15
16agent = Agent('weather_agent')
17# Load agent properties stored in a dedicated file
18agent.load_properties('config.ini')
19# Define the platform your agent will use
20websocket_platform = agent.use_websocket_platform(use_ui=True)
21
22# STATES
23
24s0 = agent.new_state('s0', initial=True)
25weather_state = agent.new_state('weather_state')
26
27# ENTITIES
28
29city_entity = agent.new_entity('city_entity', entries={
30    'Barcelona': ['BCN', 'barna'],
31    'Madrid': [],
32    'Luxembourg': ['LUX']
33})
34
35# INTENTS
36
37weather_intent = agent.new_intent('weather_intent', [
38    'what is the weather in CITY?',
39    'weather in CITY',
40])
41weather_intent.parameter('city1', 'CITY', city_entity)
42
43# STATES BODIES' DEFINITION + TRANSITIONS
44
45
46def s0_body(session: Session):
47    session.reply('Waiting...')
48
49
50s0.set_body(s0_body)
51s0.when_intent_matched(weather_intent).go_to(weather_state)
52
53
54def weather_body(session: Session):
55    predicted_intent: IntentClassifierPrediction = session.event.predicted_intent
56    city = predicted_intent.get_parameter('city1')
57    temperature = round(random.uniform(0, 30), 2)
58    if city.value is None:
59        session.reply("Sorry, I didn't get the city")
60    else:
61        session.reply(f"The weather in {city.value} is {temperature}°C")
62        if temperature < 15:
63            session.reply('🥶')
64        else:
65            session.reply('🥵')
66
67
68weather_state.set_body(weather_body)
69weather_state.go_to(s0)
70
71# RUN APPLICATION
72
73if __name__ == '__main__':
74    agent.run()