🚀 OpenAI API Basics
अगर आप AI tools या automation बनाना चाहते हैं तो OpenAI API आपके लिए सबसे powerful option है। इस ब्लॉग में हम जानेंगे API basics, API keys, endpoints और Python code examples ताकि आप आसानी से OpenAI models (जैसे GPT, DALL·E, Whisper) को अपनी applications में use कर सकें।
🔹 API क्या है?
API (Application Programming Interface) एक तरीका है जिससे अलग-अलग software systems आपस में बात कर पाते हैं। OpenAI API आपको AI models तक पहुँच देती है ताकि आप उन्हें अपने प्रोजेक्ट्स में use कर सकें।
🔹 API Key क्या होती है?
OpenAI API use करने के लिए आपको एक API Key चाहिए। यह key आपके account से जुड़ी होती है और authentication करती है। इसे कभी भी public code या GitHub पर share न करें।
✅ Steps to Get API Key:
- OpenAI account बनाएं।
- Dashboard → View API Keys पर जाएँ।
- एक नई secret key generate करें।
- इस key को अपने environment variables में सुरक्षित रखें।
🔹 Basic API Request कैसे करें?
OpenAI API को आप HTTP requests (POST method) के जरिए call करते हैं। Example:
POST https://api.openai.com/v1/completions Headers: Authorization: Bearer YOUR_API_KEY Content-Type: application/json Body: { "model": "text-davinci-003", "prompt": "Explain AI in simple terms", "max_tokens": 100 }
🔹 Python Example: Text Completion
import openai openai.api_key = "YOUR_API_KEY" response = openai.Completion.create( model="text-davinci-003", prompt="Explain Artificial Intelligence in 3 points.", max_tokens=100, temperature=0.7 ) print(response.choices[0].text.strip())
🔹 OpenAI API Endpoints
- Completions: Text generation (GPT models)
- Chat: Chat-based conversations (ChatGPT)
- Images: DALL·E से image generation
- Audio: Whisper से speech-to-text
- Embeddings: Semantic search और vector similarity
🔹 Error Handling
API call करते समय errors आ सकती हैं। Common errors:
- 401 Unauthorized: API key गलत या missing है।
- 429 Rate Limit: बहुत ज़्यादा requests भेजी गईं।
- 500 Server Error: OpenAI servers की issue।
🔹 Best Practices
- API key को हमेशा environment variables में store करें।
- Prompts को test और optimize करें।
- Rate limits का ध्यान रखें।
- Cost बचाने के लिए छोटे models use करें (जैसे gpt-3.5)।
📘 Students के लिए Practice
- एक API key बनाएं और Python में simple completion call चलाएँ।
- एक prompt लिखें और उसका output compare करें different models से।
- Image generation API से एक unique image बनाएं।
💡 Final Thoughts
OpenAI API basics समझना बहुत जरूरी है क्योंकि यही foundation है advanced automation और AI applications बनाने की। अगर आप इसे अच्छे से master कर लेते हैं तो आप chatbots, content generators, image apps और बहुत कुछ बना सकते हैं।