Skip to content

Commit

Permalink
System upgraded.
Browse files Browse the repository at this point in the history
  • Loading branch information
halilcosdu committed Apr 22, 2024
1 parent 4bcd2a9 commit e3e17a4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,54 @@ use HalilCosdu\FineTuner\Facades\FineTuner;
// Generate examples for fine-tuning
$trainingExamples = FineTuner::generateExamples("A versatile AI assistant designed to provide personalized support across all aspects of life, from physical health and mental wellness to emotional well-being, adapting its guidance to the user's unique situation and needs. It offers practical advice, empathetic support, and proactive reminders, all while ensuring privacy and security, and continuously evolving through feedback and research.")

array:2 [▼
"training_data" => array:1 [▼
0 => array:1 [▼
"messages" => array:3 [▼
0 => array:2 [▼
"role" => "system"
"content" => "Given your current situation and needs, how can I assist you today?"
]
1 => array:2 [▼
"role" => "user"
"content" => "Hey AI, I've been feeling really stressed out lately because of work. Any advice on how I can manage this better?"
]
2 => array:2 [▼
"role" => "assistant"
"content" => """
I'm really sorry to hear that you're feeling this way, but please know that it's okay to feel stressed. Here are a few suggestions that might help:



1. Mindfulness and Relaxation: Try techniques like deep breathing, meditation, or yoga. They can help you to stay calm and focused.



2. Time Management: Prioritize your tasks and break them down into manageable chunks. Remember, it's okay to say no to tasks that are not a priority.



3. Physical Activity: Regular exercise can help reduce stress levels. Even a short walk can make a difference.



4. Connect with Others: Talk to someone you trust about your stress. It can often help to share what you're experiencing.



5. Professional Help: If your stress continues or leads to feelings of despair, it might be helpful to speak with a mental health professional.



Remember, everyone experiences stress differently and different techniques work for different people. It's important to find what works best for you.
"""
]
]
]
]
"file_url" => null
]

// If the `FINE_TUNER_USE_STORAGE` environment variable is set to `true`, the `file_url` will be returned for the upload function. Alternatively, you can create your own training `.jsonl` file using the `training_data`.

// Upload the training data
Expand All @@ -57,6 +105,34 @@ $fileId = FineTuner::upload($trainingExamples['file_url'])
FineTuner::fineTune($fileId, 'gpt-3.5-turbo')
```

The `php artisan laravel-finetuner` command is used to interact with the Laravel Finetuner package. This command initiates a process that generates examples.

Here's a detailed explanation of how to use this command:

1. Open your terminal.

2. Navigate to your Laravel project directory.

3. Run the command `php artisan laravel-finetuner`.

4. The command will first ask for a `Prompt`. This is a string that will be used to generate examples for fine-tuning. If you don't provide a prompt, it will use a default one.

5. Next, it will ask for the `Temperature`. This is a float value that controls the randomness of the examples generated. A higher value will result in more random examples. If you don't provide a temperature, it will use a default value of `.4`.

6. Then, it will ask for the `Number of examples`. This is an integer that specifies how many examples to generate. If you don't provide a number, it will use a default value of `1`.

7. After you've provided these inputs, the command will ask for your confirmation to continue. If you confirm, it will start generating examples, which may take a while.

8. Once the examples are generated, they will be uploaded. If there's an error during this process, the command will display an error message.

9. If everything goes well, the command will display a URL. This URL points to the location where the generated examples were uploaded.

Remember, this command is part of the Laravel Finetuner package, which is designed to automate the fine-tuning of OpenAI models in Laravel applications.

```php
php artisan laravel-finetuner
```

## Testing

```bash
Expand Down
30 changes: 26 additions & 4 deletions src/Commands/FineTunerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@

namespace HalilCosdu\FineTuner\Commands;

use HalilCosdu\FineTuner\Facades\FineTuner;
use Illuminate\Console\Command;

class FineTunerCommand extends Command
{
public $signature = 'laravel-finetuner';

public $description = 'My command';
public $description = 'Command to generate examples, upload them, and start a fine-tuning job with Laravel Finetuner';

public function handle(): int
private const DEFAULT_PROMPT = "A versatile AI assistant designed to provide personalized support across all aspects of life, from physical health and mental wellness to emotional well-being, adapting its guidance to the user's unique situation and needs. It offers practical advice, empathetic support, and proactive reminders, all while ensuring privacy and security, and continuously evolving through feedback and research.";

public function handle(): void
{
$this->comment('All done');
$prompt = $this->ask('Prompt?', self::DEFAULT_PROMPT);
$temperature = (float) $this->ask('Temperature', '.4');
$numberOfExamples = (int) $this->ask('Number of examples?', '1');

if ($this->confirm('Do you wish to continue?', true)) {
$this->info('Generating examples...');
$this->warn('This may take a while. Please be patient.');

try {
$response = FineTuner::generateExamples(
$prompt,
$temperature,
$numberOfExamples
);
} catch (\Exception $e) {
$this->error($e->getMessage());

return;
}

return self::SUCCESS;
$this->info("\n{$response['url']}");
}
}
}
23 changes: 19 additions & 4 deletions src/FineTuner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Sleep;
use Illuminate\Support\Str;
use OpenAI as OpenAIFactory;
use OpenAI\Client;

Expand All @@ -30,7 +31,8 @@ private function example($prompt, $prevExamples, $temperature = .5): ?string

if (count($prevExamples) > 0) {
if (count($prevExamples) > 8) {
$prevExamples = array_rand($prevExamples, 8);
$keys = array_rand($prevExamples, 8);
$prevExamples = array_intersect_key($prevExamples, array_flip($keys));
}
foreach ($prevExamples as $example) {
$messages[] = [
Expand All @@ -49,7 +51,7 @@ private function example($prompt, $prevExamples, $temperature = .5): ?string
return $response->choices[0]->message->content;
}

public function generateExamples($prompt, $temperature = .4, $numberOfExamples = 2): array
public function generateExamples($prompt, $temperature = .4, $numberOfExamples = 1): array
{
$prevExamples = [];
for ($i = 0; $i < $numberOfExamples; $i++) {
Expand All @@ -61,6 +63,13 @@ public function generateExamples($prompt, $temperature = .4, $numberOfExamples =
return $this->saveTrainingExamples($prevExamples, $this->generateSystemMessage($prompt, $temperature));
}

public function save(array $data, string $fileName): string
{
Storage::disk(config('finetuner.storage.disk'))->put($fileName, json_encode($data));

return Storage::disk(config('finetuner.storage.disk'))->url($fileName);
}

private function generateSystemMessage($prompt, $temperature = .5): ?string
{
$response = $this->client->chat()->create([
Expand Down Expand Up @@ -97,12 +106,18 @@ private function saveTrainingExamples($prevExamples, $systemMessage): array
$trainingExamples[] = $trainingExample;
}
}
$fileName = Str::random(40).'.jsonl';

if (app()->runningInConsole()) {
return ['url' => $this->save($trainingExamples, $fileName)];
}

$url = null;

if (config('finetuner.use_storage')) {
Storage::disk(config('finetuner.storage.disk'))->put('training_data.jsonl', json_encode($trainingExamples));
$url = Storage::disk(config('finetuner.storage.disk'))->url('training_data.jsonl');
$fileName = Str::random(40).'.jsonl';
Storage::disk(config('finetuner.storage.disk'))->put($fileName, json_encode($trainingExamples));
$url = Storage::disk(config('finetuner.storage.disk'))->url($fileName);
}

return ['training_data' => $trainingExamples, 'file_url' => $url];
Expand Down

0 comments on commit e3e17a4

Please sign in to comment.