SYS://VISION.ACTIVE
VIEWPORT.01
LAT 28.0222° N
SIGNAL.NOMINAL
VISION Loading
Back to Blog

Fine-Tuning AI Models for Your Domain: A Laravel Developer's Guide

Shane Barron

Shane Barron

Laravel Developer & AI Integration Specialist

When Fine-Tuning Makes Sense

General models work well for general tasks. But when you need consistent output formats, domain-specific language, or behavior that prompting alone can't achieve, fine-tuning creates a model tailored to your needs.

Preparing Training Data

class TrainingDataExporter
{
    public function export(): string
    {
        $examples = TrainingExample::all()->map(function ($example) {
            return [
                'messages' => [
                    ['role' => 'system', 'content' => $this->systemPrompt],
                    ['role' => 'user', 'content' => $example->input],
                    ['role' => 'assistant', 'content' => $example->expected_output],
                ],
            ];
        });

        $path = storage_path('training_data.jsonl');
        $file = fopen($path, 'w');

        foreach ($examples as $example) {
            fwrite($file, json_encode($example) . "\n");
        }

        fclose($file);
        return $path;
    }
}

Uploading and Training

class FineTuningService
{
    public function startTraining(string $dataPath): string
    {
        // Upload training file
        $fileResponse = $this->client->post('https://api.openai.com/v1/files', [
            'multipart' => [
                ['name' => 'purpose', 'contents' => 'fine-tune'],
                ['name' => 'file', 'contents' => fopen($dataPath, 'r')],
            ],
        ]);

        $fileId = json_decode($fileResponse->getBody(), true)['id'];

        // Start fine-tuning job
        $jobResponse = $this->client->post('https://api.openai.com/v1/fine_tuning/jobs', [
            'json' => [
                'training_file' => $fileId,
                'model' => 'gpt-4o-mini-2024-07-18',
                'hyperparameters' => [
                    'n_epochs' => 3,
                ],
            ],
        ]);

        return json_decode($jobResponse->getBody(), true)['id'];
    }
}

Collecting Training Examples

class TrainingDataCollector
{
    public function recordExample(string $input, string $output, bool $approved): void
    {
        TrainingExample::create([
            'input' => $input,
            'expected_output' => $output,
            'approved' => $approved,
            'collected_at' => now(),
        ]);
    }

    public function fromUserCorrections(): void
    {
        // Collect from user feedback
        UserCorrection::where('used_for_training', false)
            ->each(function ($correction) {
                $this->recordExample(
                    $correction->original_prompt,
                    $correction->corrected_output,
                    true
                );
                $correction->update(['used_for_training' => true]);
            });
    }
}

Using Your Fine-Tuned Model

class FineTunedAIService
{
    public function __construct(
        private string $fineTunedModelId = 'ft:gpt-4o-mini:your-org::abc123'
    ) {}

    public function generate(string $prompt): string
    {
        return $this->ai->generate($prompt, [
            'model' => $this->fineTunedModelId,
            // Fine-tuned models often work better with lower temperature
            'temperature' => 0.3,
        ]);
    }
}

Evaluating Model Performance

class ModelEvaluator
{
    public function evaluate(string $modelId, array $testSet): array
    {
        $results = [];

        foreach ($testSet as $test) {
            $output = $this->ai->generate($test['input'], ['model' => $modelId]);
            $results[] = [
                'input' => $test['input'],
                'expected' => $test['expected'],
                'actual' => $output,
                'score' => $this->calculateSimilarity($test['expected'], $output),
            ];
        }

        return [
            'results' => $results,
            'average_score' => collect($results)->avg('score'),
        ];
    }
}

Conclusion

Fine-tuning is powerful but requires quality data and careful evaluation. Start by collecting good examples from production use, maintain a test set for evaluation, and iterate based on real-world performance.

Share this article
Shane Barron

Shane Barron

Strategic Technology Architect with 40 years of experience building production systems. Specializing in Laravel, AI integration, and enterprise architecture.

Need Help With Your Project?

I respond to all inquiries within 24 hours. Let's discuss how I can help build your production-ready system.

Get In Touch