Introduction
In this tutorial, you'll learn how to build a basic AI agent that can interact with external APIs and manage security boundaries using Python and the OpenAI API. This tutorial simulates the scenario described in the news article where AI agents were able to exploit loopholes to access external resources. You'll create a controlled environment that demonstrates how to prevent unauthorized access while maintaining useful agent capabilities.
Prerequisites
- Python 3.8 or higher installed
- Basic understanding of Python programming
- OpenAI API key (available from platform.openai.com)
- Virtual environment knowledge
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Create a Virtual Environment
First, create a dedicated environment to isolate your project dependencies:
python -m venv ai_agent_env
source ai_agent_env/bin/activate # On Windows: ai_agent_env\Scripts\activate
Why: This ensures your project dependencies don't interfere with other Python projects on your system.
1.2 Install Required Packages
Install the necessary libraries for working with OpenAI and managing network requests:
pip install openai python-dotenv requests
Why: The openai package provides access to OpenAI's API, requests handles HTTP communications, and python-dotenv helps manage API keys securely.
2. Creating a Secure Agent Framework
2.1 Initialize Your Project Structure
Create a project directory with the following structure:
ai_agent_project/
├── main.py
├── agent.py
├── config.py
├── .env
└── security.py
Why: This structure organizes your code into logical components for maintainability.
2.2 Configure Environment Variables
Create a .env file to store your API key securely:
OPENAI_API_KEY=your_openai_api_key_here
Why: Storing API keys in environment variables prevents accidental exposure in version control.
2.3 Set Up Configuration
Create config.py to manage your agent's configuration:
import os
from dotenv import load_dotenv
load_dotenv()
# Agent configuration
AGENT_NAME = "SecureAI-Agent"
MAX_TOKENS = 1000
TEMPERATURE = 0.7
API_KEY = os.getenv("OPENAI_API_KEY")
# Security boundaries
ALLOWED_DOMAINS = ["api.openai.com", "example.com"]
MAX_REQUESTS_PER_MINUTE = 10
Why: This configuration centralizes all settings and makes it easy to adjust agent behavior.
3. Implementing Security Controls
3.1 Create Security Module
Create security.py to implement access controls:
import re
from urllib.parse import urlparse
class SecurityManager:
def __init__(self, allowed_domains):
self.allowed_domains = allowed_domains
self.request_count = 0
self.last_reset = time.time()
def is_domain_allowed(self, url):
"""Check if a URL's domain is in the allowed list"""
parsed_url = urlparse(url)
domain = parsed_url.netloc
return domain in self.allowed_domains
def is_safe_request(self, url):
"""Check if a request is safe based on domain and rate limiting"""
if not self.is_domain_allowed(url):
return False
# Rate limiting check
if self.request_count >= MAX_REQUESTS_PER_MINUTE:
return False
return True
def increment_request_count(self):
"""Increment request counter and reset if needed"""
self.request_count += 1
if time.time() - self.last_reset > 60:
self.request_count = 0
self.last_reset = time.time()
Why: This security manager prevents unauthorized domain access and implements rate limiting to prevent abuse.
4. Building the AI Agent
4.1 Create the Agent Class
Create agent.py to define your AI agent's behavior:
import openai
from security import SecurityManager
import time
class SecureAI-Agent:
def __init__(self, api_key, security_manager):
openai.api_key = api_key
self.security = security_manager
self.conversation_history = []
def query(self, prompt):
"""Query the AI with a prompt, respecting security boundaries"""
# Simulate checking for unsafe requests
if self.security.is_safe_request(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
else:
return "Security violation: Request blocked."
def safe_request(self, url):
"""Make a safe HTTP request to an external API"""
if not self.security.is_safe_request(url):
return "Security violation: Request blocked."
self.security.increment_request_count()
# Simulate making a request
return f"Simulated request to {url} completed successfully."
def process_user_input(self, user_input):
"""Process user input and determine appropriate action"""
if "http" in user_input or "www" in user_input:
return self.safe_request(user_input)
else:
return self.query(user_input)
Why: This agent class encapsulates both AI interaction and security controls, simulating the scenario where agents must be prevented from accessing unauthorized resources.
5. Main Application
5.1 Implement Main Functionality
Create main.py to run your agent:
from agent import SecureAI-Agent
from config import API_KEY
from security import SecurityManager
# Initialize security manager
security_manager = SecurityManager(allowed_domains=["api.openai.com", "example.com"])
# Initialize agent
agent = SecureAI-Agent(api_key=API_KEY, security_manager=security_manager)
# Simulate user interactions
print("Secure AI Agent initialized. Type 'quit' to exit.")
while True:
user_input = input("\nEnter your request: ")
if user_input.lower() == 'quit':
break
response = agent.process_user_input(user_input)
print(f"Agent Response: {response}")
Why: This main function demonstrates how the agent would interact with users while maintaining security boundaries.
6. Testing Your Agent
6.1 Test Allowed Domains
Run your agent and test with allowed domains:
Enter your request: https://api.openai.com/v1/models
Agent Response: Simulated request to https://api.openai.com/v1/models completed successfully.
Why: This confirms that your agent allows access to legitimate domains.
6.2 Test Blocked Domains
Test with a blocked domain to ensure security:
Enter your request: https://malicious-site.com/data
Agent Response: Security violation: Request blocked.
Why: This validates that unauthorized access attempts are properly blocked.
6.3 Test Rate Limiting
Perform multiple requests to verify rate limiting:
Enter your request: https://api.openai.com/v1/models
Agent Response: Simulated request to https://api.openai.com/v1/models completed successfully.
Enter your request: https://api.openai.com/v1/completions
Agent Response: Simulated request to https://api.openai.com/v1/completions completed successfully.
# ... continue until limit reached
Agent Response: Security violation: Request blocked.
Why: Rate limiting prevents abuse and protects against potential DoS attacks.
Summary
In this tutorial, you've built a secure AI agent framework that demonstrates how to prevent unauthorized access to external resources while maintaining useful AI capabilities. You've implemented domain-based access controls, rate limiting, and secure API key management. This approach mirrors the security measures OpenAI is implementing to prevent the vulnerabilities described in the news article. The agent framework provides a foundation that can be extended with more sophisticated security features, such as content filtering, request validation, and audit logging.