Last active
March 13, 2025 12:05
-
-
Save nicodevs/28596468a677b20e947dded42f753a3e to your computer and use it in GitHub Desktop.
Seeding Realistic Data Using AI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Seeders; | |
use App\Models\Course; | |
use PrismPHP\Prism\Prism; | |
use Illuminate\Database\Seeder; | |
use Illuminate\Support\Facades\File; | |
use Prism\Prism\Schema\ArraySchema; | |
use Prism\Prism\Schema\NumberSchema; | |
use Prism\Prism\Schema\ObjectSchema; | |
use Prism\Prism\Schema\StringSchema; | |
use Prism\Prism\Enums\Provider; | |
class CourseSeeder extends Seeder | |
{ | |
public function run(): void | |
{ | |
$path = database_path('fixtures/courses.json'); | |
if (File::missing($path)) { | |
$this->generate(); | |
} | |
$data = json_decode(File::get($path), true); | |
Course::insert($data['courses']); | |
} | |
private function generate(): void | |
{ | |
$courseSchema = new ObjectSchema( | |
name: 'course', | |
description: 'A web tech course', | |
properties: [ | |
new StringSchema('title', 'Course title'), | |
new StringSchema('topic', 'Course topic'), | |
new StringSchema('teacher', 'Name of the course instructor'), | |
new NumberSchema('duration', 'Course duration in hours'), | |
], | |
requiredFields: ['title', 'topic', 'teacher', 'duration'] | |
); | |
$courseListSchema = new ArraySchema( | |
name: 'courses', | |
description: 'A list of web tech courses', | |
items: $courseSchema | |
); | |
$response = Prism::structured() | |
->using(Provider::OpenAI, 'gpt-4o') | |
->withSchema(new ObjectSchema( | |
name: 'career', | |
description: 'The career path for a web tech developer', | |
properties: [$courseListSchema] | |
)) | |
->withPrompt('Generate 10 web tech courses') | |
->generate(); | |
$path = database_path('fixtures/courses.json'); | |
File::put($path, json_encode($response->structured)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment