python --version
pip install openai
export OPENAI_API_KEY=sk-...
Fine-tuning requires a funded account
Create a JSONL file with at least 10 chat examples in the required messages format, then upload it to the OpenAI files API.
1 import json 2 from openai import OpenAI 3 4 client = OpenAI() 5 6 # Create training examples 7 examples = [ 8 { 9 "messages": [ 10 {"role": "system", "content": "You are a concise technical assistant."}, 11 {"role": "user", "content": "What is a REST API?"}, 12 {"role": "assistant", "content": "A REST API is an interface that uses HTTP methods (GET, POST, PUT, DELETE) to perform CRUD operations on resources identified by URLs."}, 13 ] 14 }, 15 # Add 9+ more examples... 16 ] 17 18 with open("training.jsonl", "w") as f: 19 for example in examples: 20 f.write(json.dumps(example) + "\n") 21 22 # Upload to OpenAI 23 with open("training.jsonl", "rb") as f: 24 response = client.files.create(file=f, purpose="fine-tune") 25 26 print(f"File ID: {response.id}") 27
Sign in to share your feedback and join the discussion.