1 of 95

Building Ai agents in Python:

A Deep Dive

2 of 95

3 of 95

About me

  • Abdur-Rahmaan Janhangeer

  • Been working with Python professionally since a long time

  • Worked for companies in all continents (except Antartica and South America)

  • Pymug organizing member

compileralchemy.com

4 of 95

Slides

5 of 95

Building Ai agents in Python:

A Deep Dive

6 of 95

Building agents from real scratch

7 of 95

Task: Control a drone with your mind

8 of 95

Task: Control a drone with your mind

9 of 95

Task: Control a drone with your mind

Use openai or Gemini API to take off or land a drone

10 of 95

Task: Control a drone with your mind

Use openai or Gemini API to take off or land a drone

11 of 95

Task: Control a drone with your mind

Use openai or Gemini API to take off or land a drone

12 of 95

Task: Control a drone with your mind

Use openai or Gemini API to take off or land a drone

13 of 95

Task: Control a drone with your mind

Function Calling (old)

def get_headset_value():�

def move_drone_up():

def move_drone_down():

14 of 95

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")

15 of 95

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": {}

}

]

16 of 95

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")

17 of 95

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

18 of 95

Raw function calling is powerful

19 of 95

Task: Create a chatGPT plugin system

20 of 95

Task: Create a chatGPT plugin system

21 of 95

Task: Create a chatGPT plugin system

22 of 95

Task: Create a chatGPT plugin system

23 of 95

Task: Create a chatGPT plugin system

24 of 95

Task: Create a chatGPT plugin system

25 of 95

Raw function calling is powerful

26 of 95

Raw function calling is powerful

Even when dumb

27 of 95

The concept of agents

28 of 95

Function calling is the meat of Ai agents

29 of 95

Function calling is the meat of Ai agents

30 of 95

Overview of an agent

31 of 95

ReAct

32 of 95

Generally speaking

33 of 95

Flows

34 of 95

Chaining of actions

35 of 95

Gating

36 of 95

Process results

37 of 95

Feedback

38 of 95

Building agents

39 of 95

Smolagent

40 of 95

Smolagent

from typing import Optional

import requests

from smolagents import CodeAgent, LiteLLMModel, tool

model = LiteLLMModel(model_id="gpt-4o")

41 of 95

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"

42 of 95

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)}"

43 of 95

Smolagent

agent = CodeAgent(

tools=[

get_joke,

],

model=model,

)

agent.run("Tell me a joke")

44 of 95

Agents working together

45 of 95

Agents for tasks

46 of 95

Let’s say we need to find the phone numbers of hardware shops

47 of 95

Let’s say we need to find the phone numbers of hardware shops

48 of 95

Let’s say we need to find the phone numbers of hardware shops

49 of 95

Let’s say we need to find the phone numbers of hardware shops

50 of 95

Let’s say we need to find the phone numbers of hardware shops

51 of 95

Let’s say we need to find the phone numbers of hardware shops

52 of 95

Graph Agents

53 of 95

Graph Agents

  • Using langgraph - https://langchain-ai.github.io/langgraph/
  • State
  • Node

54 of 95

Graph Agents

State as in state machine

55 of 95

Graph Agents

State as in state machine

56 of 95

Graph Agents

State as in state machine

57 of 95

Graph Agents

State as in state machine,

but with a twist

58 of 95

Graph Agents

Unit of work are called nodes

59 of 95

Graph Agents

Unit of work are called nodes

Nodes can have edges

The indicate the control flow and order of execution

60 of 95

Graph Agents

Unit of work are called nodes

Nodes can have edges

Conditional edges indicate branching

61 of 95

Graph Agents

Unit of work are called nodes

They can be agents

62 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

63 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

64 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

65 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

66 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

67 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

68 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

69 of 95

Graph Agents

Unit of work are called nodes

Nodes modify state/s

The state is the input to any node whenever it is invoked

70 of 95

Graph Agents

langgraph is powerful enough

It can be used to build pretty much any agent scenario or reasoning-focused workflows

71 of 95

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

72 of 95

MCP

73 of 95

What if we could make agents talk to each other?

74 of 95

What if there was a standardised approach to making agents talk to each other?

75 of 95

That’s where MCP comes in

76 of 95

Model Context Protocol

77 of 95

Model Context Protocol

78 of 95

Model Context Protocol

79 of 95

Model Context Protocol

80 of 95

Model Context Protocol

81 of 95

Model Context Protocol

82 of 95

Through JSON-RPC

83 of 95

Python

$ pip install mcp

84 of 95

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}!"

85 of 95

Python (Claude desktop)

$ mcp install server.py

86 of 95

MCP: A Game Changer

87 of 95

Marketplace?

Just like people sell APIs on marketplaces

88 of 95

Enabling agents

Search registry for tools

89 of 95

Enabling agents

Search registry for tools

90 of 95

Watch out

It’s the web so …

91 of 95

Watch out

It’s the web so …

Think about security for example

92 of 95

Papers

93 of 95

Papers

  • ReAct: ReAct: Synergizing Reasoning and Acting in Language Models
  • ReWOO: ReWOO: Decoupling Reasoning from Observations for Efficient Augmented Language Models
  • LLM Compiler: An LLM Compiler for Parallel Function Calling

94 of 95

Interesting reads

Abhinav Upaday - chatGPT plugin

Anthropic - Effective agents

https://github.com/mannaandpoem/OpenManus

See how to use Python with n8n

95 of 95

That’s all folks! Join the Pymug

on WA