Skip to content

Latest commit

Β 

History

History
407 lines (300 loc) Β· 13.1 KB

File metadata and controls

407 lines (300 loc) Β· 13.1 KB

πŸš€ OpenAI API Playground

A production-focused collection of comprehensive OpenAI API examples built with Python. Learn scalable, maintainable, and production-ready AI systems using modern OpenAI APIs, structured engineering patterns, and real-world architecture best practices.

Python 3.12+ OpenAI API License: MIT PRs Welcome


πŸ‘¨β€πŸ’» About This Repository

This repository is a comprehensive, production-grade reference for OpenAI API engineering. It provides:

  • βœ… 20+ fully implemented examples across all major OpenAI API categories
  • βœ… Production-ready patterns for authentication, error handling, and configuration
  • βœ… Structured examples showing how to build scalable AI systems
  • βœ… Real-world use cases including RAG, agents, realtime systems, and more
  • βœ… Best practices for security, cost optimization, and deployment

Perfect for:

  • πŸŽ“ AI engineering interview preparation
  • πŸ“š Learning OpenAI API fundamentals
  • πŸ—οΈ Production AI architecture reference
  • πŸ€– Building AI SaaS products
  • πŸ”§ Internal AI tooling and automation

πŸ‘€ Author

Chaitanya Dasadiya

  • πŸ”— GitHub: @cdasadiya
  • 🎯 Focus Areas: AI Engineering, Python Development, OpenAI APIs, Production Systems

πŸ“‹ Repository Goals

This repository aims to become a complete professional OpenAI engineering reference covering:

Category Coverage
Core Platform Authentication, API keys, organizations, projects, usage, billing, rate limits
Text & Reasoning Responses API, streaming, structured outputs, function calling, tool calling
Realtime Systems WebSocket, live streaming, voice, transcription, interrupt handling
Audio & Vision Speech-to-text, translation, TTS, image understanding, OCR
AI Agents Multi-agent systems, orchestration, memory, MCP integration
Infrastructure RAG pipelines, embeddings, fine-tuning, deployment, monitoring

πŸ“ Repository Structure

openai-api-playground/
β”œβ”€β”€ 01_core_platform/          # Authentication, billing, rate limits, tokens
β”œβ”€β”€ 02_responses_api/          # Chat, streaming, structured outputs, tool calling
β”œβ”€β”€ 03_realtime_apis/          # WebSocket, voice, low-latency systems
β”œβ”€β”€ 04_audio_apis/             # Transcription, translation, TTS (coming soon)
β”œβ”€β”€ 05_vision_apis/            # Image understanding, OCR (coming soon)
β”œβ”€β”€ utils/                     # Shared OpenAI client and utilities
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ .env.example              # Environment template
└── README.md                 # This file

πŸ“š Folder Guide

Folder Purpose
01_core_platform/ Production foundations: authentication, API keys, organizations, projects, usage tracking, billing, rate limits, models, tokens, pricing optimization
02_responses_api/ Complete text API examples: basic responses, structured output, streaming, function calling, tool calling, multi-turn conversations, reasoning models
03_realtime_apis/ Realtime API examples: WebSocket connections, live streaming, voice systems, transcription, interrupt handling, low-latency architectures
04_audio_apis/ Audio API examples: transcription, translation, text-to-speech, voice workflows (⏳ coming soon)
05_vision_apis/ Vision API examples: image understanding, OCR, multi-image analysis, reasoning (⏳ coming soon)
utils/ Shared configuration, OpenAI client factory, and helper utilities

βœ… Implementation Status

🟒 Core Platform

All Core Platform examples are fully implemented in 01_core_platform/:

βœ“ authentication.py          - Secure authentication validation
βœ“ api_keys.py                - Safe API key management
βœ“ organizations.py           - Multi-organization scoping
βœ“ projects.py                - Project-based isolation
βœ“ usage_tracking.py          - Token usage analysis
βœ“ billing.py                 - Billing concepts and tracking
βœ“ rate_limits.py             - Rate limit handling with exponential backoff
βœ“ models.py                  - Model discovery and metadata
βœ“ tokens.py                  - Token estimation and comparison
βœ“ pricing_optimization.py    - Cost optimization strategies

🟒 Responses API

All Responses API examples are fully implemented in 02_responses_api/:

βœ“ basic_response.py               - Simple text generation
βœ“ structured_json_output.py       - Structured outputs with schema
βœ“ system_prompting.py             - System instructions
βœ“ streaming_responses.py          - Real-time streaming
βœ“ function_calling.py             - Function invocation
βœ“ tool_calling.py                 - Tool orchestration
βœ“ multi_turn_conversation.py      - Conversational AI
βœ“ reasoning_models.py             - Advanced reasoning models

🟒 Realtime APIs

All Realtime API examples are fully implemented in 03_realtime_apis/:

βœ“ websocket_connections.py    - WebSocket fundamentals
βœ“ live_streaming.py           - Audio stream handling
βœ“ realtime_voice.py           - Voice interaction systems
βœ“ realtime_transcription.py   - Real-time speech-to-text
βœ“ interrupt_handling.py       - User interruption patterns
βœ“ low_latency_systems.py      - Optimized latency architecture

🟑 Audio APIs (Coming Soon)

⏳ Speech-to-text
⏳ Transcription
⏳ Translation
⏳ Text-to-speech
⏳ Voice synthesis
⏳ Audio generation

🟑 Vision APIs (Coming Soon)

⏳ Image understanding
⏳ OCR
⏳ Multi-image analysis
⏳ Vision reasoning

πŸš€ Quick Start

Prerequisites

  • Python 3.12 or higher
  • OpenAI API key from platform.openai.com
  • Git and basic terminal knowledge

1. Clone the Repository

git clone https://github.com/cdasadiya/openai-api-playground.git
cd openai-api-playground

2. Choose Your Setup Method

Option A: Local Development (Recommended)

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate          # macOS/Linux
# or
.venv\Scripts\activate             # Windows

# Install dependencies
pip install -r requirements.txt

# Create .env file
echo "OPENAI_API_KEY=sk-proj-your_api_key_here" > .env

# Run an example
python 02_responses_api/basic_response.py

Option B: GitHub Codespaces (No setup required)

  1. Click Code β†’ Codespaces β†’ Create codespace on main
  2. Add GitHub Codespaces secret:
    • Settings β†’ Secrets and variables β†’ Codespaces
    • Name: OPENAI_API_KEY
    • Value: Your OpenAI API key
    • Grant access to openai-api-playground
  3. Run: python 02_responses_api/basic_response.py

3. Run Your First Example

# Run a simple Responses API example
python 02_responses_api/basic_response.py

# Or run a Core Platform example
python 01_core_platform/authentication.py

πŸ” API Key Setup Guide

Local Development with .env

# 1. Create .env file in repository root
OPENAI_API_KEY=sk-proj-your_api_key_here

# Optional: Set organization and project scope
OPENAI_ORG_ID=org-...
OPENAI_PROJECT_ID=proj-...

GitHub Codespaces with Secrets

Settings β†’ Codespaces β†’ Secrets β†’ New repository secret
Name: OPENAI_API_KEY
Value: sk-proj-your_api_key_here
Repository access: openai-api-playground

πŸ”’ Security Best Practices

βœ… DO:

  • βœ”οΈ Store API keys in .env files locally
  • βœ”οΈ Use GitHub Codespaces secrets for cloud development
  • βœ”οΈ Use environment variables in production
  • βœ”οΈ Add .env to .gitignore
  • βœ”οΈ Rotate compromised keys immediately

❌ DON'T:

  • ❌ Commit API keys to GitHub
  • ❌ Hardcode keys in Python files
  • ❌ Push .env files to repositories
  • ❌ Expose keys in screenshots/logs
  • ❌ Store secrets in public repositories

Recommended .gitignore

# Environment
.env
.env.*
!.env.example

# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python

# Virtual Environment
.venv/
venv/
ENV/

# IDE
.vscode/
.idea/
*.swp
*.swo

πŸ“Š Complete API Coverage Checklist

βœ… Implemented

  • Core Platform β€” 10/10 topics covered
  • Responses API β€” 8/8 topics covered
  • Realtime APIs β€” 6/6 topics covered

🟑 Planned

  • Audio APIs β€” 0/6 topics covered
  • Vision APIs β€” 0/4 topics covered
  • Image APIs β€” 0/5 topics covered
  • Embeddings & Search β€” 0/5 topics covered
  • Fine-Tuning β€” 0/5 topics covered
  • Assistants & Agents β€” 0/6 topics covered
  • Files & Data β€” 0/4 topics covered
  • Safety & Moderation β€” 0/4 topics covered
  • Production Engineering β€” 0/9 topics covered
  • AI Architecture β€” 0/6 topics covered
  • Deployment β€” 0/6 topics covered
  • Ecosystem Integrations β€” 0/6 topics covered
  • Advanced Systems β€” 0/9 topics covered

πŸ—οΈ Tech Stack

Layer Technologies
Language Python 3.12+
AI Platform OpenAI API (latest)
Environment Python Venv, Python-dotenv
Patterns Async/await, Streaming, Structured outputs
Focus Production-ready, secure, scalable

πŸ’‘ Engineering Principles

This repository follows production-grade standards:

  • πŸ” Secure Architecture β€” Centralized client, environment-based configuration
  • πŸ“¦ Structured Outputs β€” Type-safe, schema-validated responses
  • πŸ”„ Error Handling β€” Graceful failures, exponential backoff retry logic
  • πŸ“Š Observability β€” Logging, debugging support, usage tracking
  • ⚑ Performance β€” Streaming support, async operations, caching patterns
  • πŸš€ Scalability β€” Reusable utilities, modular design
  • πŸ›‘οΈ Security β€” Secret management, no hardcoded keys
  • 🐳 Compatibility β€” Codespaces-ready, Docker-compatible

πŸ“š Recommended Learning Path

Follow this progression to master OpenAI APIs:

  1. Start Here β†’ 01_core_platform/authentication.py β€” Validate setup
  2. Basics β†’ 02_responses_api/basic_response.py β€” Text generation
  3. Structured β†’ 02_responses_api/structured_json_output.py β€” Typed outputs
  4. Real-time β†’ 02_responses_api/streaming_responses.py β€” Live responses
  5. Tools β†’ 02_responses_api/function_calling.py β€” API integration
  6. Advanced β†’ 02_responses_api/tool_calling.py β€” Tool orchestration
  7. Conversation β†’ 02_responses_api/multi_turn_conversation.py β€” Memory
  8. Reasoning β†’ 02_responses_api/reasoning_models.py β€” Complex tasks
  9. Voice β†’ 03_realtime_apis/realtime_voice.py β€” Voice systems
  10. Production β†’ Scale to production with monitoring and optimization

πŸ’Ό Use Cases

This repository can accelerate development for:

  • πŸŽ“ Interview Prep β€” AI engineering interview preparation and problem solving
  • πŸ“– Learning β€” Understanding OpenAI API capabilities and best practices
  • πŸ›οΈ Architecture β€” Reference implementations for production AI systems
  • πŸš€ SaaS β€” Building AI-powered SaaS products and platforms
  • πŸ”§ Internal Tools β€” Creating internal AI tooling and automation
  • 🎨 Prototyping β€” Rapid AI prototyping and experimentation
  • πŸ€– Agents β€” Multi-agent systems and orchestration
  • 🎬 Multimodal β€” Combining text, voice, vision, and audio

🀝 Contributing

Contributions are welcome and appreciated! We're looking for help in these areas:

  • ✨ New Examples β€” Additional OpenAI API examples
  • πŸ—οΈ Architecture β€” RAG systems, workflow patterns, agent orchestration
  • πŸ”„ Realtime β€” Voice, video, and low-latency applications
  • πŸ€– Agents β€” Multi-agent systems, tool orchestration
  • πŸš€ Deployment β€” CI/CD, Docker, Kubernetes, serverless
  • ⚑ Performance β€” Optimization, benchmarking, caching
  • πŸ›‘οΈ Production β€” Monitoring, logging, observability
  • πŸ“š Documentation β€” Improving guides and examples

See CONTRIBUTING.md for guidelines.


πŸ“„ License

This repository is licensed under the MIT License.

See LICENSE file for complete details.


πŸ”— Quick Links


Made with ❀️ by Chaitanya Dasadiya

⭐ If this helped you, please star this repository! ⭐