AI / Core OpenAI Codex Application Fundamentals Interview Questions
What is fine-tuning in the OpenAI API and when should you use it?
Fine-tuning creates a customised version of an OpenAI model trained on your own examples. It is used when prompting or few-shot examples are insufficient to achieve the desired style, format, or domain-specific accuracy.
| Method | Description | Best for |
|---|---|---|
| Supervised fine-tuning (SFT) | Train on (prompt, ideal completion) pairs | Style, format, domain-specific knowledge |
| Direct Preference Optimisation (DPO) | Train on (prompt, preferred, rejected) triplets | Aligning outputs with human preferences |
| Reinforcement fine-tuning (RFT) | Train with a reward signal (verifiable tasks) | Math, coding tasks with deterministic correct answers |
from openai import OpenAI client = OpenAI() # 1. Prepare training data (JSONL format) # Each line: {"messages": [{"role": "system", "content": "..."}, ...]} # Save as training.jsonl # 2. Upload training file training_file = client.files.create( file=open("training.jsonl", "rb"), purpose="fine-tune" ) # 3. Create fine-tuning job job = client.fine_tuning.jobs.create( training_file=training_file.id, model="gpt-4.1-mini-2025-04-14", # supported base models # model="gpt-4.1-2025-04-14", hyperparameters={ "n_epochs": 3, } ) # 4. Monitor job job_status = client.fine_tuning.jobs.retrieve(job.id) print(f"Status: {job_status.status}") # 5. Use fine-tuned model response = client.chat.completions.create( model=job_status.fine_tuned_model, # e.g. ft:gpt-4.1-mini:my-org::abc123 messages=[{"role": "user", "content": "..."}], )
When NOT to fine-tune first: prompt engineering, few-shot examples, and RAG (retrieval-augmented generation) should be tried before fine-tuning. Fine-tuning requires training data, incurs training costs, and has longer iteration cycles. Reserve it for cases where the base model consistently fails despite good prompting.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
