Skip to content

Commit 4f01927

Browse files
committed
Improved all bridges
1 parent cb4970f commit 4f01927

File tree

3 files changed

+97
-54
lines changed

3 files changed

+97
-54
lines changed

src/Bridges/ChatBridge.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ final class ChatBridge implements Bridge
1515
use HasProvider, HasModel, HasNew;
1616

1717
/**
18-
* @var string $externalId The external id of the chat, returned by the provider
18+
* @var string|null $externalId The external id of the chat, returned by the provider
1919
*/
20-
private string $externalId;
20+
private ?string $externalId;
2121

2222
/**
2323
* @var array $messages The messages sent and received in the chat
2424
*/
25-
private array $messages;
25+
private array $messages = [];
2626

2727
/**
28-
* @var Chat $chat The corresponding chat model
28+
* @var Chat|null $chat The corresponding chat model
2929
*/
30-
private Chat $chat;
30+
private ?Chat $chat;
3131

3232
/**
3333
* Setter for the external id
3434
*/
35-
public function withExternalId(string $externalId): self
35+
public function withExternalId(string $externalId = null): self
3636
{
3737
$this->externalId = $externalId;
3838
return $this;
@@ -49,9 +49,9 @@ public function externalId(): string
4949
/**
5050
* Setter for the messages
5151
*/
52-
public function withMessages(array $messages): self
52+
public function withMessages(array $messages = null): self
5353
{
54-
$this->messages = $messages;
54+
$this->messages = $messages ?? [];
5555
return $this;
5656
}
5757

@@ -70,8 +70,9 @@ public function withChat(Chat $chat): self
7070
{
7171
$this->chat = $chat;
7272

73-
$this->withExternalId($chat->external_id);
74-
$this->withMessages($chat->messages);
73+
$this->withModel($chat->model)
74+
->withExternalId($chat->external_id)
75+
->withMessages($chat->messages);
7576

7677
return $this;
7778
}
@@ -90,28 +91,31 @@ public function chat(): Chat
9091
public function toArray(): array
9192
{
9293
return [
94+
'model_id' => $this->model?->id,
9395
'external_id' => $this->externalId,
9496
'messages' => $this->messages,
9597
];
9698
}
9799

98100
/**
99-
* The import method is not implemented for the chat bridge
100-
*
101-
* @throws Exception
101+
* Import the chat data into a Model
102102
*/
103103
public function import(): Model
104104
{
105-
throw new Exception('Not implemented');
105+
$this->chat = $this->chat ?? ( new Chat );
106+
$this->chat->forceFill($this->toArray())->save();
107+
108+
return $this->chat;
106109
}
107110

108111
/**
109112
* Send a message to the chat
110-
*
111-
* @throws Exception
112113
*/
113114
public function send($message): string
114115
{
116+
/**
117+
* Append the message to the messages array
118+
*/
115119
$this->messages[] = [
116120
'role' => 'user',
117121
'content' => $message
@@ -123,20 +127,15 @@ public function send($message): string
123127
$response = $this->provider->getConnector()->chat($this->model->external_id, $this->messages);
124128

125129
/**
126-
* Update or create the chat model
130+
* Populate local data
127131
*/
128-
$this->chat = $this->chat ?? ( new Chat );
129-
$this->chat->forceFill([
130-
'model_id' => $this->model->id,
131-
'external_id' => $response->externalId(),
132-
'messages' => array_merge($this->messages, [$response->message()->toArray()])
133-
])->save();
132+
$this->externalId = $response->externalId();
133+
$this->messages = array_merge($this->messages, [$response->message()->toArray()]);
134134

135135
/**
136-
* Refresh the bridge with the new chat.
137-
* This will update the messages and external id
136+
* Import into a model
138137
*/
139-
$this->withChat($this->chat);
138+
$this->import();
140139

141140
/**
142141
* Return the content of the response

src/Bridges/CompletionBridge.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class CompletionBridge implements Bridge
3030
private string $answer;
3131

3232
/**
33-
* @var Completion $completion The corresponding completion model
33+
* @var Completion|null $completion The corresponding completion model
3434
*/
35-
private Completion $completion;
35+
private ?Completion $completion;
3636

3737
/**
3838
* Setter for the external id
@@ -92,9 +92,10 @@ public function withCompletion(Completion $completion): self
9292
{
9393
$this->completion = $completion;
9494

95-
$this->withExternalId($completion->external_id);
96-
$this->withPrompt($completion->prompt);
97-
$this->withAnswer($completion->answer);
95+
$this->withModel($completion->model)
96+
->withExternalId($completion->external_id)
97+
->withPrompt($completion->prompt)
98+
->withAnswer($completion->answer);
9899

99100
return $this;
100101
}
@@ -112,36 +113,50 @@ public function completion(): Completion
112113
*/
113114
public function toArray(): array
114115
{
115-
return [];
116+
return [
117+
'model_id' => $this->model?->id,
118+
'external_id' => $this->externalId(),
119+
'prompt' => $this->prompt(),
120+
'answer' => $this->answer(),
121+
];
116122
}
117123

118124
/**
119-
* @throws Exception
125+
* Import the bridge into a model
120126
*/
121127
public function import(): Model
122128
{
123-
throw new Exception('Not implemented');
129+
$this->completion = $this->completion ?? ( new Completion );
130+
$this->completion->forceFill($this->toArray())->save();
131+
132+
return $this->completion;
124133
}
125134

126135
/**
127136
* Ask the provider to complete the given text
128-
*
129-
* @throws Exception
130137
*/
131138
public function complete(string $text): string
132139
{
140+
/**
141+
* Get the response from the provider, in the TextResponse format
142+
*/
133143
$response = $this->provider()->getConnector()->complete($this->model->external_id, $text);
134144

135-
$this->completion = $this->completion ?? ( new Completion );
136-
$this->completion->forceFill([
137-
'model_id' => $this->model->id,
138-
'external_id' => $response->externalId(),
139-
'prompt' => $text,
140-
'answer' => $response->message()->content(),
141-
])->save();
142-
143-
$this->withCompletion($this->completion);
144-
145+
/**
146+
* Populate local data
147+
*/
148+
$this->externalId = $response->externalId();
149+
$this->prompt = $text;
150+
$this->answer = $response->message()->content();
151+
152+
/**
153+
* Import into a model
154+
*/
155+
$this->import();
156+
157+
/**
158+
* Return the content of the response
159+
*/
145160
return $response->message()->content();
146161
}
147162
}

src/Bridges/ImageBridge.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,35 @@ class ImageBridge implements Bridge
1414
{
1515
use HasProvider, HasModel, HasNew;
1616

17+
/**
18+
* @var string|null $externalId The external id of the image, returned by the provider
19+
*/
1720
private ?string $externalId;
21+
22+
/**
23+
* @var string|null $prompt The prompt of the image, provided by the user
24+
*/
1825
private ?string $prompt;
19-
private ?int $width;
20-
private ?int $height;
26+
27+
/**
28+
* @var int|null $width The width of the image, provided by the user
29+
*/
30+
private ?int $width;
31+
32+
/**
33+
* @var int|null $height The height of the image, provided by the user
34+
*/
35+
private ?int $height;
36+
37+
/**
38+
* @var string|null $url The url of the image, returned by the provider
39+
*/
2140
private ?string $url;
22-
private ?Image $image;
41+
42+
/**
43+
* @var Image|null $image The corresponding image model
44+
*/
45+
private ?Image $image;
2346

2447
/**
2548
* Setter for the external id
@@ -113,11 +136,11 @@ public function withImage(Image $image): self
113136
{
114137
$this->image = $image;
115138

116-
$this->withExternalId($image->external_id);
117-
$this->withPrompt($image->prompt);
118-
$this->withWidth($image->width);
119-
$this->withHeight($image->height);
120-
$this->withUrl($image->url);
139+
$this->withExternalId($image->external_id)
140+
->withPrompt($image->prompt)
141+
->withWidth($image->width)
142+
->withHeight($image->height)
143+
->withUrl($image->url);
121144

122145
return $this;
123146
}
@@ -159,6 +182,9 @@ public function import(): Model
159182
*/
160183
public function generate(string $prompt, int $width, int $height): string
161184
{
185+
/**
186+
* Get the response from the provider, in the ImageResponse format
187+
*/
162188
$response = $this->provider()->getConnector()->imageGenerate($prompt, $width, $height);
163189

164190
/**
@@ -174,6 +200,9 @@ public function generate(string $prompt, int $width, int $height): string
174200
*/
175201
$this->import();
176202

203+
/**
204+
* Return the url
205+
*/
177206
return $this->url;
178207
}
179208
}

0 commit comments

Comments
 (0)