מתי להשתמש
"OpenAI API", "GPT-4 API", "Whisper", "DALL-E API", "Assistants API".
הוראות עבודה
1. Setup
from openai import OpenAI
client = OpenAI(api_key="sk-...")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)
2. Models 2026
| Model | Use | Pricing (per 1M) |
|---|---|---|
| gpt-4o | Default, multimodal | $2.50 / $10 |
| gpt-4o-mini | Cheap, fast | $0.15 / $0.60 |
| o1 | Reasoning, slow | $15 / $60 |
| o3-mini | Reasoning, faster | $1.10 / $4.40 |
| gpt-4 turbo | Legacy, still solid | $10 / $30 |
3. Function Calling
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}}
}
}
}]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Weather in Tel Aviv?"}],
tools=tools
)
4. Structured Outputs (JSON Mode)
Old Way
response_format={"type": "json_object"}
New (better) — Strict JSON Schema
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
email: str
response = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[...],
response_format=Person
)
person = response.choices[0].message.parsed
5. Vision
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "https://..."}}
]
}]
)
6. Whisper (Audio → Text)
audio_file = open("interview.mp3", "rb")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(transcript.text)
Supports
- 50+ languages including Hebrew.
- $0.006 per minute.
7. Text-to-Speech (TTS)
response = client.audio.speech.create(
model="tts-1",
voice="alloy", # 6 voices
input="Hello, this is AI speaking"
)
response.stream_to_file("output.mp3")
8. DALL-E (Images)
response = client.images.generate(
model="dall-e-3",
prompt="A serene Tel Aviv sunset",
size="1024x1024",
quality="standard"
)
url = response.data[0].url
9. Embeddings
response = client.embeddings.create(
input="Your text here",
model="text-embedding-3-small" # or large
)
embedding = response.data[0].embedding # 1536-dim vector
Use For
- Semantic search.
- RAG.
- Clustering.
10. Assistants API (Beta)
What
- Persistent assistants with memory + tools.
- Code Interpreter, Retrieval, Function calling.
- Threads (persistent conversations).
When
- Building chatbot with memory.
- Need built-in code execution.
- Want OpenAI-managed RAG.
Caveat
- Less flexible than building yourself.
- Vendor lock-in.
11. Fine-tuning
# Upload training data (JSONL)
file = client.files.create(
file=open("training.jsonl", "rb"),
purpose="fine-tune"
)
# Create job
job = client.fine_tuning.jobs.create(
training_file=file.id,
model="gpt-4o-mini-2024-07-18"
)
When
- Domain-specific tone.
- High-volume specific task.
- 100+ training examples.
12. Best Practices
- Use gpt-4o-mini for 80% of tasks.
- Stream for UX.
- Structured outputs — reliable parsing.
- Retry logic for rate limits.
- API key in env, not code.
13. Cost Optimization
- Cheaper models first.
- Cache common responses (your side).
- Batch API for async (50% off).
- Limit max_tokens.
14. OpenAI vs Anthropic
| OpenAI | Anthropic | |
|---|---|---|
| Strongest in | Multimodal (image gen, voice) | Long context, coding |
| Pricing | Usually cheaper | Premium |
| Hebrew | Good | Excellent |
| Reasoning | o1/o3 best | Opus 4 strong |
| Ecosystem | Larger (DALL-E, Whisper) | Focused (Claude family) |
15. אסיים בהמלצה.
פרומפט לדוגמה
Build voice transcription pipeline with Whisper.
Function calling for sales agent. Code.
Structured outputs vs JSON mode — when each.
© 2026 AI Expert Pro | גרסה 1.0.0