Spaces:
Runtime error
Runtime error
File size: 6,248 Bytes
eeb0f9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
#!/usr/bin/env python3
"""
Automated fine-tuning for all agents
Uploads training data and creates fine-tuning jobs
"""
import os
import json
import time
from pathlib import Path
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY', 'sk--PC8FIAvV01G7aUyZsJD7Q'),
base_url=os.getenv('OPENAI_BASE_URL', 'https://aiportalapi.stu-platform.live/jpe')
)
AGENTS = ['nutrition', 'symptom', 'exercise', 'mental_health']
def upload_training_file(file_path):
"""Upload training file to OpenAI"""
print(f"π€ Uploading {file_path.name}...")
try:
with open(file_path, 'rb') as f:
response = client.files.create(
file=f,
purpose='fine-tune'
)
file_id = response.id
print(f"β
Uploaded: {file_id}")
return file_id
except Exception as e:
print(f"β Upload failed: {e}")
return None
def create_fine_tuning_job(file_id, agent_name):
"""Create fine-tuning job"""
print(f"π Creating fine-tuning job for {agent_name}...")
try:
response = client.fine_tuning.jobs.create(
training_file=file_id,
model='gpt-4o-mini-2024-07-18',
suffix=f'{agent_name}-v1'
)
job_id = response.id
print(f"β
Job created: {job_id}")
return job_id
except Exception as e:
print(f"β Job creation failed: {e}")
return None
def wait_for_job(job_id, agent_name):
"""Wait for fine-tuning job to complete"""
print(f"β³ Waiting for {agent_name} fine-tuning to complete...")
print(f" This may take 10-30 minutes...")
try:
while True:
response = client.fine_tuning.jobs.retrieve(job_id)
status = response.status
if status == 'succeeded':
model_id = response.fine_tuned_model
print(f"β
Fine-tuning completed!")
print(f" Model: {model_id}")
return model_id
elif status in ['failed', 'cancelled']:
print(f"β Fine-tuning {status}")
return None
else:
print(f" Status: {status}...", end='\r')
time.sleep(30) # Check every 30 seconds
except Exception as e:
print(f"β Error checking status: {e}")
return None
def save_model_config(agent_models):
"""Save fine-tuned model IDs to config"""
config_file = Path("fine_tuning/fine_tuned_models.json")
config_file.parent.mkdir(parents=True, exist_ok=True)
with open(config_file, 'w') as f:
json.dumps(agent_models, f, indent=2)
print(f"\nβ
Model config saved to: {config_file}")
def update_agent_configs(agent_models):
"""Update agent files to use fine-tuned models"""
print("\nπ Updating agent configurations...")
# Create a config file that agents can read
config_content = f"""# Fine-tuned Models Configuration
# Generated automatically by auto_finetune.py
FINE_TUNED_MODELS = {{
"""
for agent, model_id in agent_models.items():
if model_id:
config_content += f" '{agent}': '{model_id}',\n"
config_content += "}\n"
# Save to config file
config_file = Path("config/fine_tuned_models.py")
with open(config_file, 'w') as f:
f.write(config_content)
print(f"β
Configuration saved to: {config_file}")
print("\nπ To use fine-tuned models, update config/settings.py:")
print(" from config.fine_tuned_models import FINE_TUNED_MODELS")
print(" MODEL = FINE_TUNED_MODELS.get('nutrition', 'gpt-4o-mini')")
def fine_tune_all_agents():
"""Fine-tune all agents"""
print("π― Starting automated fine-tuning for all agents...")
print()
training_dir = Path("fine_tuning/training_data")
if not training_dir.exists():
print("β Training data not found!")
print(" Run: python scripts/generate_training_data.py")
return
agent_models = {}
for agent in AGENTS:
print(f"\n{'='*60}")
print(f"π€ Processing {agent}_agent")
print(f"{'='*60}\n")
# Find training file
training_file = training_dir / f"{agent}_training.jsonl"
if not training_file.exists():
print(f"β οΈ Training file not found: {training_file}")
continue
# Check file size
file_size = training_file.stat().st_size
print(f"π Training file size: {file_size:,} bytes")
# Count conversations
with open(training_file, 'r') as f:
conv_count = sum(1 for _ in f)
print(f"π Conversations: {conv_count}")
if conv_count < 10:
print(f"β οΈ Too few conversations ({conv_count}), skipping...")
continue
# Upload training file
file_id = upload_training_file(training_file)
if not file_id:
continue
# Create fine-tuning job
job_id = create_fine_tuning_job(file_id, agent)
if not job_id:
continue
# Wait for completion
model_id = wait_for_job(job_id, agent)
if model_id:
agent_models[agent] = model_id
print()
# Save results
if agent_models:
print(f"\n{'='*60}")
print("π Fine-tuning Complete!")
print(f"{'='*60}\n")
print("π Fine-tuned models:")
for agent, model_id in agent_models.items():
print(f" {agent}: {model_id}")
# Update configurations
update_agent_configs(agent_models)
print("\nβ
All done! Your agents are now fine-tuned!")
print("\nπ Next steps:")
print(" 1. Review fine_tuned_models.py")
print(" 2. Update your agent code to use fine-tuned models")
print(" 3. Test the improved agents!")
else:
print("\nβ οΈ No models were fine-tuned")
print(" Check the errors above and try again")
if __name__ == "__main__":
fine_tune_all_agents()
|