I have a list of [(InputText, TrainingOutput), ...] data samples for an LLM I downloaded via Ollama that I want to fine tune. Write a script to fine tune this LLM using python
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python script to fine-tune an Ollama model using your list of (input, output) pairs:
python
from ollama import OllamaModel
# Load your model from Ollama
model = OllamaModel.load("your_model_name")
# Prepare training data in required format
training_data = [
{"input": inp, "output": out}
for inp, out in your_data_list # Replace with your actual data
]
# Fine-tune the model
model.fine_tune(
training_data,
epochs=5, # Number of training iterations
learning_rate=0.001, # Learning rate for optimization
batch_size=8, # Adjust based on available memory
shuffle=True # Enable data shuffling
)
# Save the fine-tuned model
model.save("fine_tuned_model_name")
Key components explained:
Data Preparation
Convert your list of tuples to a list of dictionaries with input and output keys. This matches Ollama's expected format for supervised fine-tuning[2][4].
Training Configuration
epochs: Number of complete passes through the dataset
batch_size: Adjust based on available GPU/CPU memory
shuffle: Randomizes data order between epochs
Model Saving
The save() method persists the fine-tuned weights locally or to HuggingFace Hub depending on Ollama's configuration[1][3].
Advanced Options
For larger models or memory constraints, consider parameter-efficient techniques like LoRA adapters[5][7]:
python
# Example LoRA configuration (requires PEFT library)
from peft import LoraConfig
lora_config = LoraConfig(
r=8, # Rank of LoRA matrices
lora_alpha=32, # Scaling factor
target_modules=["q_proj", "v_proj"], # Target attention layers
lora_dropout=0.05, # Dropout for LoRA weights
bias="none" # Don't add bias terms
)
# Apply LoRA to model
model = model.add_adapter(lora_config)
model.train_adapter(lora_config)
Validation
After training, evaluate performance using a validation set:
python
# Evaluate on validation data
validation_data = [...] # Your validation samples
loss = model.evaluate(validation_data)
print(f"Validation Loss: {loss}")
Deployment
To use the fine-tuned model in applications: