๐Ÿค–

Autonomous AI Agent

๐Ÿ‘จโ€๐Ÿณ๐Ÿ‘‘ Master Chefโฑ๏ธ 60 minutes

๐Ÿ“‹ Suggested prerequisites

  • โ€ขPython
  • โ€ขLLM API (Gemini)

What you'll build

An autonomous AI agent that reasons, decides which tools to use, and completes tasks by itself.

You give it a task like "research how much a flight to Tokyo costs and save it to a file", and the agent thinks step by step: first searches the web, then calculates dates, finally writes the file. Without you telling it how to do it.

When finished, you'll have an agent in Python using the ReAct pattern (Reasoning + Acting) with Gemini. You can give it tools (web search, calculator, files) and ask it complex tasks that it solves autonomously.


The prompt to start

Create an AI agent in Python with:

  1. ReAct pattern (Reasoning + Acting)
  2. Tools: web search, calculator, read files
  3. Loop that executes until task completion
  4. Uses Gemini as LLM

What the AI will create

import google.generativeai as genai
import os
import json

genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-flash")

# Define tools
def search_web(query: str) -> str:
    """Simulates web search"""
    return f"Results for '{query}': Relevant info found..."

def calculate(expression: str) -> str:
    """Evaluates math expressions"""
    try:
        return str(eval(expression))
    except:
        return "Calculation error"

def read_file(path: str) -> str:
    """Reads file content"""
    try:
        with open(path) as f:
            return f.read()[:1000]
    except:
        return f"Could not read {path}"

TOOLS = {
    "search_web": search_web,
    "calculate": calculate,
    "read_file": read_file,
}

SYSTEM_PROMPT = """You are an agent that solves tasks step by step.

For each step, respond in JSON format:
{
  "thought": "Your reasoning",
  "action": "tool_name",
  "action_input": "parameter"
}

Or if finished:
{
  "thought": "Final reasoning",
  "final_answer": "Your answer"
}

Available tools:
- search_web(query): Search the internet
- calculate(expression): Calculate expressions
- read_file(path): Read files
"""

def run_agent(task: str, max_steps: int = 10):
    """Run agent until task completion"""
    messages = [
        {"role": "user", "parts": [SYSTEM_PROMPT + f"\n\nTask: {task}"]}
    ]

    for step in range(max_steps):
        response = model.generate_content(messages)
        text = response.text

        # Parse JSON response
        try:
            data = json.loads(text.strip().strip("```json").strip("```"))
        except:
            print(f"Error parsing: {text}")
            continue

        print(f"\n--- Step {step + 1} ---")
        print(f"Thought: {data.get('thought')}")

        # Finished?
        if "final_answer" in data:
            print(f"\nโœ… Answer: {data['final_answer']}")
            return data["final_answer"]

        # Execute tool
        action = data.get("action")
        action_input = data.get("action_input")

        if action in TOOLS:
            result = TOOLS[action](action_input)
            print(f"Action: {action}({action_input})")
            print(f"Result: {result}")

            messages.append({"role": "model", "parts": [text]})
            messages.append({"role": "user", "parts": [f"Observation: {result}"]})

    return "Max steps reached"

# Usage
run_agent("What is the result of 45 * 23 + 100?")

Next level

โ†’ Image Classifier