AI Development Lesson 9: AI Agents

🤖 AI Development CourseLesson 9 of 10 · 90% complete

AI agents can take actions — search the web, run code, call APIs, read files — autonomously to complete complex tasks. This is the frontier of AI development.

What is an AI Agent?

// Traditional LLM: user asks → LLM answers → done

// AI Agent:
// 1. User gives a goal
// 2. Agent PLANS: what steps to take?
// 3. Agent ACTS: call tools, search web, run code
// 4. Agent OBSERVES: what happened?
// 5. Agent ADJUSTS: is goal achieved? if not, repeat
// This "ReAct" loop is the basis of most agents

Function Calling (Tool Use)

from openai import OpenAI
import json

client = OpenAI()

# Define tools the AI can use
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"}
                },
                "required": ["city"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role":"user","content":"What's the weather in Tokyo?"}],
    tools=tools,
    tool_choice="auto"
)

# If AI wants to call a tool:
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    city = json.loads(tool_call.function.arguments)['city']
    weather = get_weather(city)  # YOUR function!
    # Send result back to AI for final response

Pre-built Agent Frameworks

# LangChain Agents — built on top of tools
# CrewAI — multi-agent teams
# AutoGen (Microsoft) — conversational agents
# Claude Computer Use — controls browser/desktop
# Cursor AI — coding agent in your IDE

🏋️ Practice Task

Build a research agent that: takes a topic as input, searches Wikipedia (use wikipedia-api package), summarizes 3 articles, identifies key themes, outputs a structured report. Use function calling with Groq API.

💡 Hint: Define tools: search_wikipedia(query), get_article_summary(title). Agent loop: call LLM → if tool_call, execute tool, send back → repeat until no more tool calls.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *