Skip to content

Commit e3e17a4

Browse files
committed
System upgraded.
1 parent 4bcd2a9 commit e3e17a4

File tree

3 files changed

+121
-8
lines changed

3 files changed

+121
-8
lines changed

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,54 @@ use HalilCosdu\FineTuner\Facades\FineTuner;
4848
// Generate examples for fine-tuning
4949
$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.")
5050

51+
array:2 [▼
52+
"training_data" => array:1 [▼
53+
0 => array:1 [▼
54+
"messages" => array:3 [▼
55+
0 => array:2 [▼
56+
"role" => "system"
57+
"content" => "Given your current situation and needs, how can I assist you today?"
58+
]
59+
1 => array:2 [▼
60+
"role" => "user"
61+
"content" => "Hey AI, I've been feeling really stressed out lately because of work. Any advice on how I can manage this better?"
62+
]
63+
2 => array:2 [▼
64+
"role" => "assistant"
65+
"content" => """
66+
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:
67+
68+
69+
70+
1. Mindfulness and Relaxation: Try techniques like deep breathing, meditation, or yoga. They can help you to stay calm and focused.
71+
72+
73+
74+
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.
75+
76+
77+
78+
3. Physical Activity: Regular exercise can help reduce stress levels. Even a short walk can make a difference.
79+
80+
81+
82+
4. Connect with Others: Talk to someone you trust about your stress. It can often help to share what you're experiencing.
83+
84+
85+
86+
5. Professional Help: If your stress continues or leads to feelings of despair, it might be helpful to speak with a mental health professional.
87+
88+
89+
90+
Remember, everyone experiences stress differently and different techniques work for different people. It's important to find what works best for you.
91+
"""
92+
]
93+
]
94+
]
95+
]
96+
"file_url" => null
97+
]
98+
5199
// 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`.
52100

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

108+
The `php artisan laravel-finetuner` command is used to interact with the Laravel Finetuner package. This command initiates a process that generates examples.
109+
110+
Here's a detailed explanation of how to use this command:
111+
112+
1. Open your terminal.
113+
114+
2. Navigate to your Laravel project directory.
115+
116+
3. Run the command `php artisan laravel-finetuner`.
117+
118+
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.
119+
120+
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`.
121+
122+
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`.
123+
124+
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.
125+
126+
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.
127+
128+
9. If everything goes well, the command will display a URL. This URL points to the location where the generated examples were uploaded.
129+
130+
Remember, this command is part of the Laravel Finetuner package, which is designed to automate the fine-tuning of OpenAI models in Laravel applications.
131+
132+
```php
133+
php artisan laravel-finetuner
134+
```
135+
60136
## Testing
61137

62138
```bash

src/Commands/FineTunerCommand.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,40 @@
22

33
namespace HalilCosdu\FineTuner\Commands;
44

5+
use HalilCosdu\FineTuner\Facades\FineTuner;
56
use Illuminate\Console\Command;
67

78
class FineTunerCommand extends Command
89
{
910
public $signature = 'laravel-finetuner';
1011

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

13-
public function handle(): int
14+
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.";
15+
16+
public function handle(): void
1417
{
15-
$this->comment('All done');
18+
$prompt = $this->ask('Prompt?', self::DEFAULT_PROMPT);
19+
$temperature = (float) $this->ask('Temperature', '.4');
20+
$numberOfExamples = (int) $this->ask('Number of examples?', '1');
21+
22+
if ($this->confirm('Do you wish to continue?', true)) {
23+
$this->info('Generating examples...');
24+
$this->warn('This may take a while. Please be patient.');
25+
26+
try {
27+
$response = FineTuner::generateExamples(
28+
$prompt,
29+
$temperature,
30+
$numberOfExamples
31+
);
32+
} catch (\Exception $e) {
33+
$this->error($e->getMessage());
34+
35+
return;
36+
}
1637

17-
return self::SUCCESS;
38+
$this->info("\n{$response['url']}");
39+
}
1840
}
1941
}

src/FineTuner.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Storage;
66
use Illuminate\Support\Sleep;
7+
use Illuminate\Support\Str;
78
use OpenAI as OpenAIFactory;
89
use OpenAI\Client;
910

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

3132
if (count($prevExamples) > 0) {
3233
if (count($prevExamples) > 8) {
33-
$prevExamples = array_rand($prevExamples, 8);
34+
$keys = array_rand($prevExamples, 8);
35+
$prevExamples = array_intersect_key($prevExamples, array_flip($keys));
3436
}
3537
foreach ($prevExamples as $example) {
3638
$messages[] = [
@@ -49,7 +51,7 @@ private function example($prompt, $prevExamples, $temperature = .5): ?string
4951
return $response->choices[0]->message->content;
5052
}
5153

52-
public function generateExamples($prompt, $temperature = .4, $numberOfExamples = 2): array
54+
public function generateExamples($prompt, $temperature = .4, $numberOfExamples = 1): array
5355
{
5456
$prevExamples = [];
5557
for ($i = 0; $i < $numberOfExamples; $i++) {
@@ -61,6 +63,13 @@ public function generateExamples($prompt, $temperature = .4, $numberOfExamples =
6163
return $this->saveTrainingExamples($prevExamples, $this->generateSystemMessage($prompt, $temperature));
6264
}
6365

66+
public function save(array $data, string $fileName): string
67+
{
68+
Storage::disk(config('finetuner.storage.disk'))->put($fileName, json_encode($data));
69+
70+
return Storage::disk(config('finetuner.storage.disk'))->url($fileName);
71+
}
72+
6473
private function generateSystemMessage($prompt, $temperature = .5): ?string
6574
{
6675
$response = $this->client->chat()->create([
@@ -97,12 +106,18 @@ private function saveTrainingExamples($prevExamples, $systemMessage): array
97106
$trainingExamples[] = $trainingExample;
98107
}
99108
}
109+
$fileName = Str::random(40).'.jsonl';
110+
111+
if (app()->runningInConsole()) {
112+
return ['url' => $this->save($trainingExamples, $fileName)];
113+
}
100114

101115
$url = null;
102116

103117
if (config('finetuner.use_storage')) {
104-
Storage::disk(config('finetuner.storage.disk'))->put('training_data.jsonl', json_encode($trainingExamples));
105-
$url = Storage::disk(config('finetuner.storage.disk'))->url('training_data.jsonl');
118+
$fileName = Str::random(40).'.jsonl';
119+
Storage::disk(config('finetuner.storage.disk'))->put($fileName, json_encode($trainingExamples));
120+
$url = Storage::disk(config('finetuner.storage.disk'))->url($fileName);
106121
}
107122

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

0 commit comments

Comments
 (0)