Introduction
In a recent Wired article, we learned about Joi AI hiring people to explore AI companions for personal wellness. While the specific study involved intimate use of AI, we can learn valuable lessons about AI interaction and personal technology through this story. This tutorial will teach you how to create a basic AI companion chatbot using Python and the OpenAI API - a safe, educational approach to understanding AI interaction without the intimate aspects.
This tutorial will guide you through building a simple AI chatbot that can engage in conversation, helping you understand how AI companions work while maintaining appropriate boundaries.
Prerequisites
Before beginning this tutorial, you'll need:
- A computer with internet access
- Python 3.6 or higher installed
- An OpenAI API key (free to get at platform.openai.com)
- A text editor or IDE (like VS Code or PyCharm)
Step-by-Step Instructions
1. Set Up Your Python Environment
First, we need to create a virtual environment to keep our project organized. Open your terminal or command prompt and run:
python -m venv ai_companion_env
source ai_companion_env/bin/activate # On Windows use: ai_companion_env\Scripts\activate
This creates an isolated environment where we can install our project dependencies without affecting your system Python installation.
2. Install Required Libraries
With your virtual environment activated, install the OpenAI Python library:
pip install openai
This library provides an easy way to interact with OpenAI's API from Python.
3. Get Your OpenAI API Key
Visit platform.openai.com and create an account if you don't have one. Navigate to the API keys section and create a new secret key. Copy this key - you'll need it in the next step.
4. Create Your Main Python Script
Create a new file called ai_companion.py in your project directory. Add the following code:
import openai
# Set up your API key
openai.api_key = "your-api-key-here"
def get_ai_response(user_input):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful AI companion. Be friendly, supportive, and engaging. Keep responses concise but warm."},
{"role": "user", "content": user_input}
],
max_tokens=150,
temperature=0.7
)
return response.choices[0].message.content.strip()
print("AI Companion: Hello! I'm here to chat with you. Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("AI Companion: Goodbye! Take care.")
break
response = get_ai_response(user_input)
print(f"AI Companion: {response}")
This code sets up the basic structure of our AI companion. The system prompt tells the AI to be friendly and supportive, which is appropriate for a wellness-focused chatbot.
5. Replace Your API Key
Replace "your-api-key-here" with the actual API key you copied earlier:
openai.api_key = "sk-...your_actual_key_here..."
Never share your API key publicly - it's like a password for accessing OpenAI's services.
6. Run Your AI Companion
In your terminal, run:
python ai_companion.py
You should see a welcome message and be able to have a conversation with your AI companion. Try asking questions like:
- "How are you feeling today?"
- "What's your favorite hobby?"
- "Tell me about your day."
This demonstrates how AI can provide conversation and support in a safe, educational context.
7. Enhance Your Companion (Optional)
Let's make it more interesting by adding a few more features. Modify your script to include:
import openai
import random
# Set up your API key
openai.api_key = "your-api-key-here"
# Predefined responses for common phrases
common_responses = [
"That sounds interesting! Tell me more.",
"I'm glad you shared that with me.",
"Thanks for talking to me about that.",
"That's a thoughtful point.",
"I appreciate you sharing your thoughts."
]
def get_ai_response(user_input):
# Check for common phrases first
if any(word in user_input.lower() for word in ['hello', 'hi', 'hey']):
return random.choice(["Hello there!", "Hi! How are you doing today?", "Greetings!"])
if any(word in user_input.lower() for word in ['thank', 'thanks']):
return random.choice(["You're welcome!", "Happy to help!", "No problem at all!"])
# For other inputs, use AI
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful AI companion. Be friendly, supportive, and engaging. Keep responses concise but warm. If the user asks about wellbeing or mental health, be particularly empathetic."},
{"role": "user", "content": user_input}
],
max_tokens=150,
temperature=0.7
)
return response.choices[0].message.content.strip()
print("AI Companion: Hello! I'm here to chat with you. Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("AI Companion: Goodbye! Take care.")
break
response = get_ai_response(user_input)
print(f"AI Companion: {response}")
This enhanced version includes some predefined responses for common greetings and thanks, making the conversation feel more natural and human-like.
Summary
In this tutorial, we've built a basic AI chatbot using Python and OpenAI's API. We learned how to:
- Set up a Python virtual environment
- Install the OpenAI Python library
- Connect to the OpenAI API using an API key
- Create a conversation loop that interacts with the AI
- Enhance the AI's responses with basic pre-programmed reactions
This project demonstrates how AI companions work at a fundamental level, focusing on conversation and support rather than intimate interactions. The skills you've learned here can be applied to building more sophisticated AI applications for wellness, education, or customer service.
Remember that AI companions are tools for communication and support. They're designed to be helpful, friendly, and respectful - qualities that are important whether you're exploring AI technology or using it for personal wellness.



