Introduction
In this tutorial, you'll learn how to create a simple AI agent using Python that can perform basic tasks like searching the web and answering questions. This mimics the concept of the new ChatGPT agent app that OpenAI is building, where agents work autonomously to complete tasks rather than just having conversations.
While we won't be building the full ChatGPT superapp, we'll create a basic foundation that demonstrates how AI agents work - making decisions, gathering information, and completing tasks on their own.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- Python 3.7 or higher installed
- Basic understanding of Python programming concepts
- Some familiarity with APIs and web requests
No prior experience with AI or machine learning is required - we'll explain everything as we go.
Step-by-Step Instructions
1. Set Up Your Python Environment
First, we need to create a new Python project folder and install the required packages. Open your terminal or command prompt and run these commands:
mkdir ai_agent_project
cd ai_agent_project
python -m venv agent_env
source agent_env/bin/activate # On Windows: agent_env\Scripts\activate
This creates a new project folder and sets up a virtual environment to keep our project dependencies isolated.
2. Install Required Python Packages
Now we'll install the packages we'll need for our AI agent:
pip install requests openai python-dotenv
We're installing three packages:
requests- For making web requests to APIsopenai- For using OpenAI's API (we'll use it for basic text completion)python-dotenv- For managing API keys securely
3. Create a Basic AI Agent Class
Let's create the main agent class that will handle our tasks:
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
class SimpleAIAssistant:
def __init__(self):
self.name = "SimpleAgent"
self.knowledge_base = []
def search_web(self, query):
# This simulates searching the web
# In a real implementation, this would connect to a search API
return f"Search results for '{query}' would appear here"
def answer_question(self, question):
# This simulates answering a question
# In a real implementation, this would use OpenAI's API
return f"Based on my knowledge, {question} is an interesting question!"
def perform_task(self, task):
# This method will determine how to complete a task
if "search" in task.lower():
return self.search_web(task)
elif "answer" in task.lower():
return self.answer_question(task)
else:
return "I'm not sure how to handle that task yet."
# Test our basic agent
agent = SimpleAIAssistant()
print(agent.perform_task("search for Python tutorials"))
This creates a basic AI agent with three methods:
search_web()- Simulates searching the webanswer_question()- Simulates answering questionsperform_task()- Determines which method to use based on the task
4. Add Real API Integration
Now let's make our agent more powerful by connecting it to real APIs. First, create a file called .env in your project folder:
OPENAI_API_KEY=your_openai_api_key_here
Replace your_openai_api_key_here with your actual OpenAI API key (you can get one from OpenAI's website).
5. Enhance Your Agent with OpenAI Integration
Update your agent code to use OpenAI's API for more intelligent responses:
import requests
import os
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables
load_dotenv()
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
class EnhancedAIAssistant:
def __init__(self):
self.name = "EnhancedAgent"
self.conversation_history = []
def get_ai_response(self, prompt):
# Use OpenAI's API to get a response
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=150
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"Error getting response: {str(e)}"
def search_web(self, query):
# Simulate web search with AI
prompt = f"Search the web for information about: {query}. Provide a brief summary."
return self.get_ai_response(prompt)
def answer_question(self, question):
# Answer questions using AI
prompt = f"Answer this question clearly and concisely: {question}"
return self.get_ai_response(prompt)
def perform_task(self, task):
# Determine how to complete a task
if "search" in task.lower():
return self.search_web(task)
elif "answer" in task.lower():
return self.answer_question(task)
else:
return self.get_ai_response(f"How would you complete this task: {task}?")
# Test our enhanced agent
agent = EnhancedAIAssistant()
print(agent.perform_task("search for the latest AI trends"))
This enhanced agent can now:
- Make real API calls to OpenAI
- Get more intelligent responses
- Handle multiple types of tasks
6. Create a Simple Command Line Interface
Let's make our agent interactive by creating a simple command line interface:
import time
# Add this to your existing code
def run_agent_interface():
print("Hello! I'm your AI assistant. Type 'quit' to exit.")
print("I can help you search the web and answer questions.")
while True:
user_input = input("\nWhat can I help you with? ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("Goodbye!")
break
response = agent.perform_task(user_input)
print(f"\n{response}")
time.sleep(1) # Pause briefly for readability
# Run the interface
if __name__ == "__main__":
run_agent_interface()
This creates a simple chat interface where you can interact with your AI agent.
7. Test Your Agent
Save your complete code in a file called ai_agent.py and run it:
python ai_agent.py
You should see a command-line interface where you can ask questions like:
- "Search for Python programming tips"")
- "Answer: What is artificial intelligence?"")
- "How to learn machine learning?"")
Your agent will respond with AI-generated answers based on the tasks you give it.
Summary
In this tutorial, you've learned how to build a basic AI agent that can perform tasks like searching the web and answering questions. While this is a simplified version of what OpenAI is building with ChatGPT, it demonstrates the core concept of AI agents working autonomously to complete tasks.
The key concepts covered include:
- Creating a Python class to represent an AI agent
- Using API calls to connect to AI services
- Building a task-based decision system
- Creating an interactive command-line interface
This foundation can be expanded with more sophisticated features like:
- Connecting to more APIs (like weather, news, or translation services)
- Adding memory to remember previous conversations
- Implementing more complex decision-making logic
- Adding GUI interfaces
Remember, the future of AI is moving toward autonomous agents that can work independently to solve problems - just like the new ChatGPT vision from OpenAI.



