Introduction
In this tutorial, you'll learn how to set up and work with OpenAI's API using Python - the technology that powers many of the AI applications and systems that companies like OpenAI are developing. While the recent news about OpenAI's leadership changes focuses on organizational restructuring, this tutorial will help you get hands-on experience with the core technology that enables AI applications. You'll learn to create a simple Python script that interacts with OpenAI's API to generate text responses, which is fundamental to understanding how AI systems work.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- Python 3.6 or higher installed on your system
- An OpenAI API key (free to get from openai.com)
- A code editor or IDE (like VS Code, PyCharm, or even a simple text editor)
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Install Python (if not already installed)
First, ensure Python is installed on your computer. Open your terminal or command prompt and type:
python --version
If Python isn't installed, download it from python.org and follow the installation instructions.
1.2 Create a New Python Project Directory
Create a new folder on your computer called openai_project and navigate into it:
mkdir openai_project
cd openai_project
1.3 Install the OpenAI Python Library
Open your terminal in the project directory and run:
pip install openai
This installs the official OpenAI Python library that makes it easy to interact with their API.
2. Getting Your OpenAI API Key
2.1 Create an OpenAI Account
Visit platform.openai.com and sign up for a free account. If you already have an account, log in.
2.2 Generate Your API Key
After logging in, go to your Account Settings and click on API Keys. Click Create new secret key and copy the key that appears. Keep this key secure - it's like a password for accessing OpenAI's services.
3. Creating Your First OpenAI Script
3.1 Create a Python File
In your project directory, create a new file called openai_demo.py using your code editor.
3.2 Add Your API Key to the Script
Open the file and add the following code:
import os
from openai import OpenAI
# Set your API key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
Why this step? The API key is required for authentication with OpenAI's servers. We're using environment variables to keep your key secure rather than hardcoding it in the script.
3.3 Create a Simple Function to Query the API
Add this function to your script:
def get_ai_response(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
max_tokens=150
)
return response.choices[0].message.content
Why this step? This function demonstrates how to send a message to OpenAI's GPT model and receive a response. The model parameter specifies which AI model to use, and max_tokens limits the response length.
3.4 Add a Main Function to Test It
At the bottom of your script, add:
if __name__ == "__main__":
user_input = input("Ask me anything: ")
ai_response = get_ai_response(user_input)
print("AI Response:", ai_response)
Why this step? This makes your script executable and allows you to test it interactively by typing questions.
4. Setting Up Environment Variables
4.1 Set Your API Key as an Environment Variable
On Windows (Command Prompt):
set OPENAI_API_KEY=your_actual_api_key_here
On Mac/Linux (Terminal):
export OPENAI_API_KEY=your_actual_api_key_here
Why this step? Storing your API key in environment variables keeps it secure and prevents accidental exposure in your code.
5. Running Your Script
5.1 Execute the Script
In your terminal, run:
python openai_demo.py
When prompted, type a question like "What is artificial intelligence?" and observe the AI's response.
5.2 Experiment with Different Prompts
Try asking different questions to see how the AI responds. You can also modify the function to include more context or adjust parameters like max_tokens.
Summary
In this tutorial, you've learned how to set up a Python environment, install the OpenAI library, and create a simple script that communicates with OpenAI's API. You've experienced firsthand how AI systems work by sending prompts and receiving generated responses. This foundational knowledge is crucial for understanding how modern AI applications are built and deployed. While the recent news about OpenAI's leadership changes may affect company operations, the underlying technology you've learned about remains fundamental to the AI industry. This skill will help you build upon more complex AI applications in the future.



