Module 5: Getting Started with OpenAI API
What is the OpenAI API?
The OpenAI API allows you to access powerful large language models like GPT-3.5 and GPT-4 via a simple HTTP-based interface.
You can use it for:
- Chatbots
- Content creation
- Text summarization
- Translation
- Code generation
- Audio transcription
- Image generation
- Embedding generation
Create an OpenAI Account
- Go to https://platform.openai.com
- Sign up using your email, Google, or GitHub.
- Navigate to https://platform.openai.com/account/api-keys
- Click “Create new secret key”
- Copy and store your key securely.
Warning
Your secret key starts with sk- and should never be shared publicly.
Install the OpenAI Python Package
Set Up Your API Key in Python
You can use your API key in two ways:
Option 1: Set directly in code (for testing only)
Option 2: Use environment variables (recommended)
Available Functionalities in openai>=1.0.0
1. Chat Completion
2. Image Generation
3. Embeddings
4. Audio Transcription (Whisper)
Summary
- Use
OpenAI()to create a client with your API key. - Modern usage recommends
client.chat.completions.create()for chat interactions. - You can also generate images, extract embeddings, and transcribe audio using a consistent client interface.