Introduction
In this tutorial, you'll learn how to create a basic personal AI assistant similar to Meta's Muse using Python and the OpenAI API. Muse is designed to handle everyday tasks like booking travel, managing schedules, and even selling items online. While Muse is a sophisticated commercial product, we'll build a simplified version that demonstrates the core concepts behind personal AI agents.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- Python 3.7 or higher installed
- An OpenAI API key (free to get at platform.openai.com)
- Basic understanding of Python programming concepts
Step-by-Step Instructions
Step 1: Set Up Your Development Environment
Install Required Packages
First, create a new directory for your project and set up a virtual environment to keep your dependencies organized:
mkdir personal_ai_assistant
cd personal_ai_assistant
python -m venv ai_env
source ai_env/bin/activate # On Windows: ai_env\Scripts\activate
pip install openai python-dotenv
Why: Using a virtual environment ensures your project dependencies don't interfere with other Python projects on your system. The openai package provides the interface to OpenAI's API, while python-dotenv helps manage your API key securely.
Step 2: Create Your API Key Configuration
Set Up Environment Variables
Create a file called .env in your project directory:
OPENAI_API_KEY=your_actual_api_key_here
Replace your_actual_api_key_here with your actual OpenAI API key from your account.
Why: Storing your API key in a separate file prevents accidentally sharing it in public repositories. The python-dotenv package will load this file automatically.
Step 3: Create the Main AI Assistant Class
Build the Foundation
Create a file called ai_assistant.py with the following code:
import openai
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize the OpenAI client
client = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
class PersonalAIAssistant:
def __init__(self):
self.conversation_history = []
def get_response(self, user_input):
# Add user message to conversation history
self.conversation_history.append({'role': 'user', 'content': user_input})
# Call OpenAI API
response = client.chat.completions.create(
model='gpt-4-turbo',
messages=self.conversation_history,
max_tokens=150,
temperature=0.7
)
# Get AI response
ai_response = response.choices[0].message.content
# Add AI response to conversation history
self.conversation_history.append({'role': 'assistant', 'content': ai_response})
return ai_response
Why: This class creates a conversation context that remembers previous interactions, making the AI more helpful. The gpt-4-turbo model is chosen for its balance of intelligence and speed.
Step 4: Add Task-Specific Capabilities
Enhance with Specialized Functions
Extend your ai_assistant.py file with these methods:
def book_flight(self, destination, date):
prompt = f"You are helping a user book a flight to {destination} on {date}. Suggest a few options and explain the booking process."
return self.get_response(prompt)
def sell_item(self, item_name, price):
prompt = f"You are helping a user sell {item_name} for ${price}. Provide advice on the best platforms to list it and tips for successful sales."
return self.get_response(prompt)
def schedule_meeting(self, topic, time):
prompt = f"You are helping a user schedule a meeting about {topic} at {time}. Provide a professional email template for the invite."
return self.get_response(prompt)
Why: These specialized methods demonstrate how personal AI assistants can handle specific tasks. Each method creates a focused prompt that guides the AI toward relevant responses.
Step 5: Create a User Interface
Build an Interactive Experience
Create a new file called main.py:
from ai_assistant import PersonalAIAssistant
# Initialize the assistant
assistant = PersonalAIAssistant()
print("Personal AI Assistant Ready!")
print("Type 'quit' to exit, 'help' for commands")
while True:
user_input = input("\nYou: ")
if user_input.lower() in ['quit', 'exit']:
print("Assistant: Goodbye!")
break
elif user_input.lower() == 'help':
print("Assistant: I can help with booking flights, selling items, or scheduling meetings.")
print("Try saying: 'Book a flight to Paris on Friday', 'Sell my old laptop', or 'Schedule a meeting about project status'")
continue
# Check for task-specific commands
if 'book flight' in user_input.lower():
destination = user_input.split('to ')[1].split(' on')[0] if 'to ' in user_input else 'destination'
date = user_input.split(' on ')[1] if ' on ' in user_input else 'date'
response = assistant.book_flight(destination, date)
elif 'sell' in user_input.lower() and 'item' in user_input.lower():
item = user_input.split('sell ')[1].split(' for')[0] if 'sell ' in user_input else 'item'
price = user_input.split(' for $')[1] if ' for $' in user_input else 'price'
response = assistant.sell_item(item, price)
elif 'schedule' in user_input.lower() and 'meeting' in user_input.lower():
topic = user_input.split('about ')[1].split(' at')[0] if 'about ' in user_input else 'topic'
time = user_input.split(' at ')[1] if ' at ' in user_input else 'time'
response = assistant.schedule_meeting(topic, time)
else:
# General conversation
response = assistant.get_response(user_input)
print(f"Assistant: {response}")
Why: This interactive interface allows you to test different capabilities of your AI assistant. It demonstrates how a real assistant might parse natural language commands and route them to appropriate functions.
Step 6: Run Your AI Assistant
Test Your Creation
Run your assistant with the following command:
python main.py
Why: This command starts your interactive AI assistant. You can now test various commands like booking flights, selling items, or scheduling meetings to see how your AI responds.
Step 7: Experiment and Improve
Enhance Your Assistant
Try these experiments:
- Add more specialized functions like grocery shopping or restaurant recommendations
- Implement a database to store user preferences and past interactions
- Use different OpenAI models for different types of tasks
- Add voice input/output capabilities using libraries like
speech_recognitionandpyttsx3
Why: Experimenting with different features helps you understand how personal AI assistants work and how to expand their capabilities.
Summary
In this tutorial, you've built a basic personal AI assistant similar to Meta's Muse that can handle tasks like booking flights, selling items, and scheduling meetings. You learned how to set up an OpenAI API connection, create conversation context, and implement task-specific functions. While this is a simplified version, it demonstrates the core concepts behind personal AI agents that can assist with daily activities. As you continue developing, you'll discover how these systems require careful consideration of user trust, privacy, and helpfulness to be truly useful.

