Slide 1

Slide 1 text

Building Ai agents in Python: A Deep Dive

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Slides

Slide 5

Slide 5 text

Building Ai agents in Python: A Deep Dive

Slide 6

Slide 6 text

Building agents from real scratch

Slide 7

Slide 7 text

Task: Control a drone with your mind

Slide 8

Slide 8 text

Task: Control a drone with your mind

Slide 9

Slide 9 text

Task: Control a drone with your mind Use openai or Gemini API to take off or land a drone

Slide 10

Slide 10 text

Task: Control a drone with your mind Use openai or Gemini API to take off or land a drone

Slide 11

Slide 11 text

Task: Control a drone with your mind Use openai or Gemini API to take off or land a drone

Slide 12

Slide 12 text

Task: Control a drone with your mind Use openai or Gemini API to take off or land a drone

Slide 13

Slide 13 text

Task: Control a drone with your mind Function Calling (old) def get_headset_value(): … def move_drone_up(): … def move_drone_down(): …

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Raw function calling is powerful

Slide 19

Slide 19 text

Task: Create a chatGPT plugin system

Slide 20

Slide 20 text

Task: Create a chatGPT plugin system

Slide 21

Slide 21 text

Task: Create a chatGPT plugin system

Slide 22

Slide 22 text

Task: Create a chatGPT plugin system

Slide 23

Slide 23 text

Task: Create a chatGPT plugin system

Slide 24

Slide 24 text

Task: Create a chatGPT plugin system

Slide 25

Slide 25 text

Raw function calling is powerful

Slide 26

Slide 26 text

Raw function calling is powerful Even when dumb

Slide 27

Slide 27 text

The concept of agents

Slide 28

Slide 28 text

Function calling is the meat of Ai agents

Slide 29

Slide 29 text

Function calling is the meat of Ai agents

Slide 30

Slide 30 text

Overview of an agent

Slide 31

Slide 31 text

ReAct

Slide 32

Slide 32 text

Generally speaking

Slide 33

Slide 33 text

Flows

Slide 34

Slide 34 text

Chaining of actions

Slide 35

Slide 35 text

Gating

Slide 36

Slide 36 text

Process results

Slide 37

Slide 37 text

Feedback

Slide 38

Slide 38 text

Building agents

Slide 39

Slide 39 text

Smolagent

Slide 40

Slide 40 text

Smolagent from typing import Optional import requests from smolagents import CodeAgent, LiteLLMModel, tool model = LiteLLMModel(model_id="gpt-4o")

Slide 41

Slide 41 text

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"

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Smolagent agent = CodeAgent( tools=[ get_joke, ], model=model, ) agent.run("Tell me a joke")

Slide 44

Slide 44 text

Agents working together

Slide 45

Slide 45 text

Agents for tasks

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Graph Agents

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Graph Agents State as in state machine

Slide 55

Slide 55 text

Graph Agents State as in state machine

Slide 56

Slide 56 text

Graph Agents State as in state machine

Slide 57

Slide 57 text

Graph Agents State as in state machine, but with a twist

Slide 58

Slide 58 text

Graph Agents Unit of work are called nodes

Slide 59

Slide 59 text

Graph Agents Unit of work are called nodes Nodes can have edges The indicate the control flow and order of execution

Slide 60

Slide 60 text

Graph Agents Unit of work are called nodes Nodes can have edges Conditional edges indicate branching

Slide 61

Slide 61 text

Graph Agents Unit of work are called nodes They can be agents

Slide 62

Slide 62 text

Graph Agents Unit of work are called nodes Nodes modify state/s

Slide 63

Slide 63 text

Graph Agents Unit of work are called nodes Nodes modify state/s

Slide 64

Slide 64 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 65

Slide 65 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 66

Slide 66 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 67

Slide 67 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 68

Slide 68 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 69

Slide 69 text

Graph Agents Unit of work are called nodes Nodes modify state/s The state is the input to any node whenever it is invoked

Slide 70

Slide 70 text

Graph Agents langgraph is powerful enough It can be used to build pretty much any agent scenario or reasoning-focused workflows

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

MCP

Slide 73

Slide 73 text

What if we could make agents talk to each other?

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

That’s where MCP comes in

Slide 76

Slide 76 text

Model Context Protocol

Slide 77

Slide 77 text

Model Context Protocol

Slide 78

Slide 78 text

Model Context Protocol

Slide 79

Slide 79 text

Model Context Protocol

Slide 80

Slide 80 text

Model Context Protocol

Slide 81

Slide 81 text

Model Context Protocol

Slide 82

Slide 82 text

Through JSON-RPC

Slide 83

Slide 83 text

Python $ pip install mcp

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

Python (Claude desktop) $ mcp install server.py

Slide 86

Slide 86 text

MCP: A Game Changer

Slide 87

Slide 87 text

Marketplace? Just like people sell APIs on marketplaces

Slide 88

Slide 88 text

Enabling agents Search registry for tools

Slide 89

Slide 89 text

Enabling agents Search registry for tools

Slide 90

Slide 90 text

Watch out It’s the web so …

Slide 91

Slide 91 text

Watch out It’s the web so … Think about security for example

Slide 92

Slide 92 text

Papers

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Interesting reads Abhinav Upaday - chatGPT plugin Anthropic - Effective agents https://github.com/mannaandpoem/OpenManus See how to use Python with n8n

Slide 95

Slide 95 text

That’s all folks! Join the Pymug on WA