E-book: OpenAI API & HuggingFace Made Simple
• Introduction
- OpenAI API allows you to use advanced AI models for chatbots, content, and more.
- HuggingFace gives you access to many AI models and tools for developers.
• OpenAI API Basics
- Sign up at OpenAI and get an API key.
- Make requests using Python, JavaScript, or other languages.
- Example Endpoints:
- Chat (GPT)
- Text Completion
- Image generation (DALL-E)
• HuggingFace Overview
- HuggingFace Hub has thousands of AI models ready to use.
- Get API key after signing up.
- Use models for NLP, vision, translation, etc.
• Comparison Table (OpenAI vs HuggingFace)
| Feature | OpenAI API | HuggingFace |
|---|---|---|
| Ease of Use | Very Simple | Simple/Intermediate |
| Variety of Models | Few (GPT, DALL-E) | Many (Thousands) |
| Community Support | Growing | Large Community |
| Pricing (per call) | Paid (in USD) | Some Free, Some Paid |
• API Call Flowchart
• AI API Mind Map
• Q & A Section (Click to Reveal Solution)
Q1: How to call OpenAI API in JavaScript?
// JavaScript Example: OpenAI API (Text Completion)
fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_OPENAI_KEY'
},
body: JSON.stringify({
model: 'text-davinci-003',
prompt: 'Hello, world!',
max_tokens: 50
})
})
.then(res => res.json())
.then(data => console.log(data));
Q2: How to use HuggingFace model in Python?
# Python Example: HuggingFace Transformers
from transformers import pipeline
qa = pipeline('question-answering')
result = qa({'question': 'What is AI?', 'context': 'AI is artificial intelligence.'})
print(result)
• Pricing in Rupees
- OpenAI API average cost: ₹0.20 to ₹4 per call (as per model & usage)
- HuggingFace: Many models are free, paid ones can range from ₹15+ per 1000 calls
