My Journey to AI Agents 🤖: The Backbones

Fermin Blanco
Google Cloud - Community
4 min readJun 25, 2024

AI agents sound so cool 😎 but without the right mindset could be a leisurely walk. And we love action! 🏃

AI Agent

AI agents more often that I want to admit had escaped my understanding. 🧐 The concept is super-attractive 🤩 but building a real one could be a real pain. 🤕

STOP THIS MADNESS AND SHOW ME THE CODE! (Coming)

Abstract

This article opens a door to the world of AI agents and guides you through building a fun weather assistant ☔ to explain their secret powers. We’ll cover the essential steps involved in creating an AI agent, from sensing its surroundings to making decisions, taking action, and learning from feedback 🧠. Whether you’re new to AI or looking to deepen your understanding, this hands-on example will provide valuable insights 🛠️. Let’s dive in!

AI AGENTS

“AI agents are software programs or systems designed to perceive their environment, make decisions, and take actions to achieve specific goals. These agents use artificial intelligence techniques to process information, learn from experiences, and adapt their behaviour” -Claude 3.5 Sonnet

I love the definition from Claude AI but I like to think of them as conventional applications powered by LLMs. In this article we’ll embark on a journey to build a mental model for AI agents.

The Agentic Workflow

The Weather Assistant 🌧️☀️

We are going to build a weather assistant, a simple yet functional AI agent that provides weather-based recommendations.

The Goal

To help users decide whether to bring an umbrella based on the weather forecast. ☂️

def main():
agent = WeatherAssistant()
agent.run()

To make sure we are faithfully implementing an Agent we build upon an agentic workflow as follows.

The Agentic Workflow

  1. Perceiving environment: Simulating getting weather data.
  2. Make Decision: Uses the weather data to decide whether to recommend an umbrella.
  3. Take Action: Printing to I/O the recommendation
  4. Get Feedback: Simulates receiving user feedback
  5. Learn: Updates its performance metrics based on the feedback
def run(self):
# 1. Perceive environment
weather_data = self.perceive_environment()

# 2. Make decision based on weather data
decision = self.make_decision(weather_data)

# 3. Take action based on decision
self.take_action(decision)

# 4. Get feedback from user
feedback = self.get_feedback()

# 5. Learn from feedback
self.learn(feedback)

Let’s explore in detail each step.

Perceiving the environment 🔭

Perceiving the environment

This is the data our Weather Agent uses to properly make recommendations. Although we are faking the process of collecting weather data, we’ll enhance it over time.

def perceive_environment(self):
"""
Simulate weather API
"""
# 1. Get temperature in Celsius
temperature = random.uniform(0, 30)
# 2. Get precipitation change
precipitation_chance = random.uniform(0, 100)
# 3. Get wind speed in km/h
wind_speed = random.uniform(0, 50)

# Return current environment state
return {"temperature": temperature, "precipitation_chance": precipitation_chance, "wind_speed": wind_speed}

Making a decision (Recommendation) 💡

Making a decision

The decision-making process follows a basic rules system based on precipitation chances and wind speed.

def make_decision(self, weather_data):
"""
Make decision based on weather data: precipitation chance must be greater than 50
or wind speed must be greater than 20 and precipitation chance must be greater than 30
for an umbrella to be recommended.
"""
# Make a decision based on precipitation chance and wind speed
if weather_data["precipitation_chance"] > 50 or (weather_data["precipitation_chance"] > 30 and weather_data["wind_speed"] > 20):
return "Bring an umbrella"
else:
return "No need for an umbrella"

Taking action 📣

Taking action

Taking action is where the Goal of our Weather Agent materialises.

def take_action(self, decision):
"""
Take action based on decision
"""
# Taking action based on decision (for now *actions* is all about printing)
print(f"Weather Assistant recommends: {decision}")

Getting feedback 🤔

Getting feedback

Getting feedback is the third step on our agentic workflow where the Agent interacts with their environment. I know, I know! We are faking the feedback using the random library but this step opens the door for further improvements like getting real feedback from users. That would be the focus of an upcoming article.

def get_feedback(self):
"""
Get feedback from user
"""
# Get feedback from user (randomly getting True or False)
return random.choice([True, False])

Learning

Learning

An AI Agent must learn from their experiences, their interaction with the environment it lives in. For the Weather assistant this comes to learn from the user feedback.

def learn(self, feedback):
"""
Learn from feedback
"""
# Increment feedback count and update positive feedback count
self.feedback_count += 1
if feedback:
self.positive_feedback += 1

# Print current accuracy every 10 feedback
if self.feedback_count % 10 == 0:
accuracy = (self.positive_feedback / self.feedback_count) * 100
print(f"Current accuracy: {accuracy:.2f}%")

Resources

--

--