Skip to content

Commit 6c627f9

Browse files
authored
Merge pull request #3 from illegalstudio/ephemeral
- Added ephemeral options to bridges. When the ephemeral flag is on, the bridge will not store data in the database. Only usages will be stored in the `requests` table.
2 parents ac8fcc1 + 311772e commit 6c627f9

File tree

12 files changed

+149
-33
lines changed

12 files changed

+149
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Unreleased
22

3+
# v0.1.4
4+
5+
**Release date**: 2023-05-01
6+
7+
- Added ephemeral options to bridges. When the ephemeral flag is on, the bridge will not store data in the database. Only usages will be stored in the `requests` table.
8+
39
# v0.1.3
410

511
**Release date**: 2023-04-30

src/Bridges/ChatBridge.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illegal\LaravelAI\Bridges;
44

55
use Illegal\LaravelAI\Contracts\Bridge;
6+
use Illegal\LaravelAI\Contracts\HasEphemeral;
67
use Illegal\LaravelAI\Contracts\HasModel;
78
use Illegal\LaravelAI\Contracts\HasProvider;
89
use Illegal\LaravelAI\Models\ApiRequest;
@@ -13,7 +14,7 @@
1314

1415
final class ChatBridge implements Bridge
1516
{
16-
use HasProvider, HasModel, HasNew;
17+
use HasProvider, HasEphemeral, HasModel, HasNew;
1718

1819
/**
1920
* @var string|null $externalId The external id of the chat, returned by the provider
@@ -117,7 +118,7 @@ public function saveRequest(TokenUsageResponse $tokenUsage): ApiRequest
117118
$apiRequest = ApiRequest::new()->fill($tokenUsage->toArray());
118119
$apiRequest->external_id = $this->externalId();
119120

120-
if($this->chat()) {
121+
if ($this->chat()) {
121122
$apiRequest->requestable()->associate($this->chat());
122123
}
123124

@@ -127,8 +128,10 @@ public function saveRequest(TokenUsageResponse $tokenUsage): ApiRequest
127128

128129
/**
129130
* Send a message to the chat
131+
*
132+
* @param string $message The message to send
130133
*/
131-
public function send($message): string
134+
public function send(string $message): string
132135
{
133136
/**
134137
* Append the message to the messages array
@@ -150,10 +153,12 @@ public function send($message): string
150153
$this->messages = array_merge($this->messages, [$response->message()->toArray()]);
151154

152155
/**
153-
* 1. Import into a model
156+
* 1. Import into a model, only if the request is not ephemeral
154157
* 2. Save the request
155158
*/
156-
$this->import();
159+
if (!$this->isEphemeral()) {
160+
$this->import();
161+
}
157162
$this->saveRequest($response->tokenUsage());
158163

159164
/**

src/Bridges/CompletionBridge.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illegal\LaravelAI\Bridges;
44

55
use Illegal\LaravelAI\Contracts\Bridge;
6+
use Illegal\LaravelAI\Contracts\HasEphemeral;
67
use Illegal\LaravelAI\Contracts\HasModel;
78
use Illegal\LaravelAI\Contracts\HasProvider;
89
use Illegal\LaravelAI\Models\ApiRequest;
@@ -13,7 +14,7 @@
1314

1415
class CompletionBridge implements Bridge
1516
{
16-
use HasProvider, HasModel, HasNew;
17+
use HasProvider, HasEphemeral, HasModel, HasNew;
1718

1819
/**
1920
* @var string|null $externalId The external id of the completion, returned by the provider
@@ -152,8 +153,11 @@ public function saveRequest(TokenUsageResponse $tokenUsage): ApiRequest
152153
/**
153154
* Ask the provider to complete the given text
154155
*/
155-
public function complete(string $text, int $maxTokens = null, float $temperature = null): string
156-
{
156+
public function complete(
157+
string $text,
158+
int $maxTokens = null,
159+
float $temperature = null
160+
): string {
157161
/**
158162
* Get the response from the provider, in the TextResponse format
159163
*/
@@ -168,10 +172,12 @@ public function complete(string $text, int $maxTokens = null, float $temperature
168172
$this->answer = $response->message()->content();
169173

170174
/**
171-
* 1. Import into a model
175+
* 1. Import into a model, if not ephemeral
172176
* 2. Save the request
173177
*/
174-
$this->import();
178+
if (!$this->isEphemeral()) {
179+
$this->import();
180+
}
175181
$this->saveRequest($response->tokenUsage());
176182

177183
/**

src/Bridges/ImageBridge.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illegal\LaravelAI\Bridges;
44

55
use Illegal\LaravelAI\Contracts\Bridge;
6+
use Illegal\LaravelAI\Contracts\HasEphemeral;
67
use Illegal\LaravelAI\Contracts\HasModel;
78
use Illegal\LaravelAI\Contracts\HasProvider;
89
use Illegal\LaravelAI\Models\ApiRequest;
@@ -13,7 +14,7 @@
1314

1415
class ImageBridge implements Bridge
1516
{
16-
use HasProvider, HasModel, HasNew;
17+
use HasProvider, HasEphemeral, HasModel, HasNew;
1718

1819
/**
1920
* @var string|null $externalId The external id of the image, returned by the provider
@@ -172,7 +173,7 @@ public function toArray(): array
172173
*/
173174
public function import(): Model
174175
{
175-
$this->image = $this->image ?? ( new Image );
176+
$this->image = $this->image ?? (new Image);
176177
$this->image->forceFill($this->toArray())->save();
177178

178179
return $this->image;
@@ -183,10 +184,10 @@ public function import(): Model
183184
*/
184185
public function saveRequest(TokenUsageResponse $tokenUsage): ApiRequest
185186
{
186-
$apiRequest = ApiRequest::new()->fill($tokenUsage->toArray());
187+
$apiRequest = ApiRequest::new()->fill($tokenUsage->toArray());
187188
$apiRequest->external_id = $this->externalId();
188189

189-
if($this->image()) {
190+
if ($this->image()) {
190191
$apiRequest->requestable()->associate($this->image());
191192
}
192193

@@ -213,10 +214,12 @@ public function generate(string $prompt, int $width, int $height): string
213214
$this->url = $response->url();
214215

215216
/**
216-
* 1. Import into a model
217+
* 1. Import into a model, if not ephemeral
217218
* 2. Save the request
218219
*/
219-
$this->import();
220+
if (!$this->isEphemeral()) {
221+
$this->import();
222+
}
220223
$this->saveRequest($response->tokenUsage());
221224

222225
/**

src/Bridges/ModelBridge.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Illegal\LaravelAI\Bridges;
44

55
use Illegal\LaravelAI\Contracts\Bridge;
6+
use Illegal\LaravelAI\Contracts\HasEphemeral;
67
use Illegal\LaravelAI\Contracts\HasModel;
78
use Illegal\LaravelAI\Contracts\HasProvider;
89
use Illegal\LaravelAI\Models\Model;
910
use Illegal\LaravelUtils\Contracts\HasNew;
1011

1112
final class ModelBridge implements Bridge
1213
{
13-
use HasProvider, HasModel, HasNew;
14+
use HasProvider, HasEphemeral, HasModel, HasNew;
1415

1516
/**
1617
* @var string $externalId The external id of the model, returned by the provider

src/Commands/Chat.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Chat extends Command
1111
{
1212
use ConsoleProviderDependent;
1313

14-
protected $signature = 'ai:chat';
14+
protected $signature = 'ai:chat {--E|ephemeral}';
1515

1616
protected $description = 'Chat with AI';
1717

@@ -20,16 +20,36 @@ class Chat extends Command
2020
*/
2121
public function handle(): void
2222
{
23+
/**
24+
* Gather the ephemeral option. If true, the data of the request will not be stored in the database
25+
*/
26+
$isEphemeral = $this->option('ephemeral');
27+
28+
/**
29+
* Ask for provider
30+
*/
2331
$provider = $this->askForProvider();
2432

25-
$chat = ChatBridge::new()->withProvider($provider)->withModel('gpt-3.5-turbo');
26-
27-
while(1) {
33+
/**
34+
* Build the bridge
35+
*/
36+
$chat = ChatBridge::new()
37+
->withProvider($provider)
38+
->withModel('gpt-3.5-turbo')
39+
->withIsEphemeral($isEphemeral);
40+
41+
/**
42+
* Start the chat loop
43+
*/
44+
while (1) {
45+
/**
46+
* Ask for prompt
47+
*/
2848
$message = $this->ask('You');
2949
if ($message === 'exit') {
3050
break;
3151
}
32-
$this->info('AI: ' . $chat->send($message));
52+
$this->info('AI: '.$chat->send($message));
3353
}
3454
}
3555
}

src/Commands/Complete.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Complete extends Command
1111
{
1212
use ConsoleProviderDependent;
1313

14-
protected $signature = 'ai:complete';
14+
protected $signature = 'ai:complete {--E|ephemeral}';
1515

1616
protected $description = 'Use the AI to complete your prompt';
1717

@@ -20,25 +20,40 @@ class Complete extends Command
2020
*/
2121
public function handle(): void
2222
{
23+
/**
24+
* Gather the ephemeral option. If true, the data of the request will not be stored in the database
25+
*/
26+
$isEphemeral = $this->option('ephemeral');
27+
28+
/**
29+
* Ask for provider
30+
*/
2331
$provider = $this->askForProvider();
2432

33+
/**
34+
* Start the complete loop
35+
*/
2536
while (1) {
26-
$message = $this->ask('You');
27-
if ($message === 'exit') {
28-
break;
29-
}
30-
3137
/**
3238
* Ask for max tokens and temperaturek
3339
*/
3440
$maxTokens = $this->ask('Max tokens (leave empty to use defaults)', null);
3541
$temperature = $this->ask('Temperature (leave empty to use defaults)', null);
3642

43+
/**
44+
* Ask for prompt
45+
*/
46+
$message = $this->ask('You');
47+
if ($message === 'exit') {
48+
break;
49+
}
50+
3751
$this->info(
3852
'AI: '.
3953
CompletionBridge::new()
4054
->withProvider($provider)
4155
->withModel('text-davinci-003')
56+
->withIsEphemeral($isEphemeral)
4257
->complete($message, $maxTokens, $temperature)
4358
);
4459
}

src/Commands/ImageGenerate.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ class ImageGenerate extends Command
1010
{
1111
use ConsoleProviderDependent;
1212

13-
protected $signature = 'ai:image:generate';
13+
protected $signature = 'ai:image:generate {--E|ephemeral}';
1414

1515
protected $description = 'Command description';
1616

1717
public function handle(): void
1818
{
19+
/**
20+
* Gather the ephemeral option. If true, the data of the request will not be stored in the database
21+
*/
22+
$isEphemeral = $this->option('ephemeral');
23+
24+
/**
25+
* Ask for provider
26+
*/
1927
$provider = $this->askForProvider();
2028

2129
/**
@@ -49,6 +57,7 @@ public function handle(): void
4957
'AI: '.
5058
ImageBridge::new()
5159
->withProvider($provider)
60+
->withIsEphemeral($isEphemeral)
5261
->generate($prompt, $width, $height)
5362
);
5463
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Illegal\LaravelAI\Models\Model;
88
use Illuminate\Console\Command;
99

10-
class ImportModels extends Command
10+
class ModelsImport extends Command
1111
{
1212
use ConsoleProviderDependent;
1313

1414
/**
1515
* @var string The signature of the console command.
1616
*/
17-
protected $signature = 'ai:import-models';
17+
protected $signature = 'ai:models:import';
1818

1919
/**
2020
* @var string The description of the console command.

src/Contracts/Bridge.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ public static function new(): self;
2323
*/
2424
public function withProvider(Provider $provider): self;
2525

26+
/**
27+
* Get the provider for the bridge.
28+
* This is implemented in the HasProvider trait.
29+
* Bridges should implement this interface and use the trait.
30+
*/
31+
public function provider(): Provider;
32+
33+
/**
34+
* Set the isEphemeral flag for the bridge.
35+
* If true, the bridge will not save the model in the database.
36+
* This is implemented in the HasEpemeral trait.
37+
* Bridges should implement this interface and use the trait.
38+
*/
39+
public function withIsEphemeral(bool $isEphemeral): self;
40+
41+
/**
42+
* Get the isEphemeral flag for the bridge.
43+
* If true, the bridge will not save the model in the database.
44+
* This is implemented in the HasEpemeral trait.
45+
* Bridges should implement this interface and use the trait.
46+
*/
47+
public function isEphemeral(): bool;
48+
2649
/**
2750
* This function should return an array representation of the current bridge.
2851
*/

0 commit comments

Comments
 (0)