Building Ai agents in Python:
A Deep Dive
About me
Slides
Building Ai agents in Python:
A Deep Dive
Building agents from real scratch
Task: Control a drone with your mind
Task: Control a drone with your mind
Task: Control a drone with your mind
Use openai or Gemini API to take off or land a drone
Task: Control a drone with your mind
Use openai or Gemini API to take off or land a drone
Task: Control a drone with your mind
Use openai or Gemini API to take off or land a drone
Task: Control a drone with your mind
Use openai or Gemini API to take off or land a drone
Task: Control a drone with your mind
Function Calling (old)
def get_headset_value():� …
def move_drone_up():
…
def move_drone_down():
…
Task: Control a drone with your mind
import openai
import time
import random
def get_brainwave_value():
"""Simulate getting a brainwave focus value from the NeuroSky headset."""
return random.randint(0, 100)
def move_drone_up():
print("Drone moving up")
def move_drone_down():
print("Drone moving down")
Task: Control a drone with your mind
functions = [
{
"name": "get_brainwave_value",
"description": "Get the current brainwave focus value.",
"parameters": {}
},
{
"name": "move_drone_up",
"description": "Move the drone up on high focus.",
"parameters": {}
},
{
"name": "move_drone_down",
"description": "Move the drone down on low focus.",
"parameters": {}
}
]
Task: Control a drone with your mind
def control_drone():
"""Continuously monitor brainwave focus values and move the drone accordingly."""
while True:
value = get_brainwave_value()
print(f"Brainwave focus value: {value}")
response = openai.ChatCompletion.create(
model="gpt-4-turbo",
messages=[
{"role": "system", "content": "You control a drone based on brainwave focus levels."},
{"role": "user", "content": value}
],
functions=functions, # here
function_call="auto"
)
function_name = response["choices"][0]["message"].get("function_call", {}).get("name")
Task: Control a drone with your mind
if function_name == "move_drone_up":
move_drone_up()
elif function_name == "move_drone_down":
move_drone_down()
time.sleep(1)
if __name__ == "__main__":
control_drone()
Even the base version has enough meat
Raw function calling is powerful
Task: Create a chatGPT plugin system
Task: Create a chatGPT plugin system
Task: Create a chatGPT plugin system
Task: Create a chatGPT plugin system
Task: Create a chatGPT plugin system
Task: Create a chatGPT plugin system
Raw function calling is powerful
Raw function calling is powerful
Even when dumb
The concept of agents
Function calling is the meat of Ai agents
Function calling is the meat of Ai agents
Overview of an agent
ReAct
Generally speaking
Flows
Chaining of actions
Gating
Process results
Feedback
Building agents
Smolagent
Smolagent
from typing import Optional
import requests
from smolagents import CodeAgent, LiteLLMModel, tool
model = LiteLLMModel(model_id="gpt-4o")
Smolagent
@tool
def get_joke() -> str:
"""
Fetches a random joke from the JokeAPI.
This function sends a GET request to the JokeAPI to retrieve a random joke.
It handles both single jokes and two-part jokes (setup and delivery).
If the request fails or the response does not contain a joke, an error message is returned.
Returns:
str: The joke as a string, or an error message if the joke could not be fetched.
"""
url = "https://v2.jokeapi.dev/joke/Any?type=single"
Smolagent
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
if "joke" in data:
return data["joke"]
elif "setup" in data and "delivery" in data:
return f"{data['setup']} - {data['delivery']}"
else:
return "Error: Unable to fetch joke."
except requests.exceptions.RequestException as e:
return f"Error fetching joke: {str(e)}"
Smolagent
agent = CodeAgent(
tools=[
get_joke,
],
model=model,
)
agent.run("Tell me a joke")
Agents working together
Agents for tasks
Let’s say we need to find the phone numbers of hardware shops
Let’s say we need to find the phone numbers of hardware shops
Let’s say we need to find the phone numbers of hardware shops
Let’s say we need to find the phone numbers of hardware shops
Let’s say we need to find the phone numbers of hardware shops
Let’s say we need to find the phone numbers of hardware shops
Graph Agents
Graph Agents
Graph Agents
State as in state machine
Graph Agents
State as in state machine
Graph Agents
State as in state machine
Graph Agents
State as in state machine,
but with a twist
Graph Agents
Unit of work are called nodes
Graph Agents
Unit of work are called nodes
Nodes can have edges
The indicate the control flow and order of execution
Graph Agents
Unit of work are called nodes
Nodes can have edges
Conditional edges indicate branching
Graph Agents
Unit of work are called nodes
They can be agents
Graph Agents
Unit of work are called nodes
Nodes modify state/s
Graph Agents
Unit of work are called nodes
Nodes modify state/s
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
Unit of work are called nodes
Nodes modify state/s
The state is the input to any node whenever it is invoked
Graph Agents
langgraph is powerful enough
It can be used to build pretty much any agent scenario or reasoning-focused workflows
Graph Agents
langgraph is powerful enough
It can be used to build pretty much any agent scenario or reasoning-focused workflows
BUT SINCE ITS FROM LANGCHAIN, THE API IS AS BAD AS IT CAN BE
MCP
What if we could make agents talk to each other?
What if there was a standardised approach to making agents talk to each other?
That’s where MCP comes in
Model Context Protocol
Model Context Protocol
Model Context Protocol
Model Context Protocol
Model Context Protocol
Model Context Protocol
Through JSON-RPC
Python
$ pip install mcp
Python
# server.py
from mcp.server.fastmcp import FastMCP
# Create an MCP server
mcp = FastMCP("Demo")
# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}!"
Python (Claude desktop)
$ mcp install server.py
MCP: A Game Changer
Marketplace?
Just like people sell APIs on marketplaces
Enabling agents
Search registry for tools
Enabling agents
Search registry for tools
Watch out
It’s the web so …
Watch out
It’s the web so …
Think about security for example
Papers
Papers
Interesting reads
Abhinav Upaday - chatGPT plugin
Anthropic - Effective agents
https://github.com/mannaandpoem/OpenManus
See how to use Python with n8n
That’s all folks! Join the Pymug
on WA