This package adds the possibility to use GPT to generate fake text. It completely integrates with Laravel and their factories.
Sorry for this bad code, I don't have the time at the moment. What this fork do:
- set the completion api url to
http://localhost:1234/v1for using with my LM Studio local installation - a fast written prompt in french (made for Mistral) that force the LLM to respond in json with the requested fake elements.
Exemple:
Find a title and description for a training in the medical field. Returns only a single JSON object with the title and description keys.
- regex match the json pattern and decode to a php array returned by gpt() function
- usage section updated bellow
Install the package using composer:
composer require dejury/gptfaker --devAdd this to your .env file:
FAKERGPT_OPENAI_API_KEY=<your-api-key>The config file can be published with the following command:
php artisan vendor:publish --provider="Motivo\GptFaker\GptFakerServiceProvider"
Use it in your Laravel Factory:
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Page;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class PageFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Page::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
$fakes = $this->faker->unique()->gpt('Crée un titre et un slug pour une page web de commerce en ligne. Renvoie uniquement un seul objet JSON avec les clefs title et slug.');
return [
'name' => $fakes['title'] ?? null,
'slug' => $fakes['title'] ?? null,
'content' => $this->faker->gpt('Create a short paragraph for the page'),
'visible' => true,
];
}
}A fallback can be supplied in case ChatGPT is unable to create a response, the api key is not set or if FakerGPT should not be executed for the given environment.
By default this value is set to null so it is highly recommended to provide a valid fallback.
$this->faker->gpt(
'Create a short paragraph for the page',
$this->faker->paragraph
),Prompts are accepted in every language ChatGPT understands. The results will be translated to your configured faker locale.
It is possible to give an array with prompts:
$choices = $this->faker->unique()->gpt(
prompt: [
"Maak een functietitel binnen de zorg aan",
"Maak een korte titel over het werken in de zorg",
"Maak een korte quote over het werken in de zorg",
"Maak een korte alinea over het werken in de zorg",
],
returnArray: true
);
return [
'overview_pretitle' => Str::of($choices[0]?->text)->trim(),
];You can configure for which environments FakerGPT should be executed. By default this is only for local environment.
This can be changed by publishing the config file as described above and overwriting the environment config.
ChatGPT has a tendency to start end and sentences with quotes, especially when requesting titles, this is often undesirable. These quotes can be trimmed as follows:
$this->faker->unique()->gpt(
prompt: "Maak een functietitel binnen de zorg aan",
trimQuotes: true
);Performance mode can be enabled to drastically improve the speed of your factories/seeders. This executes the same prompt several times at once instead of making multiple requests and caches the results. Each result will only be used once. When the cache runs empty a new batch will be fetched. This is an opt-in feature, as it may expend tokens to cache results that might never be used, and as a result it becomes more expensive to use.
This feature can be enabled by adding the following line to your .env
FAKERGPT_PERFORMANCE_MODE=true
Often it is enough to generate fake data once rather than for every time the seeders are being executed, both for monetary and execution speed reasons.
For these situations persistent cache can be enabled. This will cache the prompts and their results in .fakergpt_cache.php.
FAKERGPT_PERSISTENT_CACHE=true
Persistent cache will have the side effect (and potential benefit) that generated content is predictable.
Unlike most cache files it's highly recommended to commit the .fakergpt_cache.php when this feature is enabled.