Skip to content

Commit 42c1a83

Browse files
committed
Use utility methods on result object instead of declarative methods on entry point.
1 parent d8f932b commit 42c1a83

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

docs/ARCHITECTURE.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@ The following examples indicate how this SDK could eventually be used.
1515
#### Generate text using a Google model
1616

1717
```php
18-
$text = Ai::generateText(
18+
$text = Ai::generateTextResult(
1919
'Write a 2-verse poem about PHP.',
2020
Google::model('gemini-2.5-flash')
21-
);
21+
)->toText();
2222
```
2323

2424
#### Generate multiple text candidates using an Anthropic model
2525

2626
```php
27-
$result = Ai::generateTextResult(
27+
$texts = Ai::generateTextResult(
2828
'Write a 2-verse poem about PHP.',
2929
Anthropic::model(
3030
'claude-3.7-sonnet',
3131
[TextGenerationConfig::CANDIDATE_COUNT => 4]
3232
)
33-
);
34-
$texts = CandidatesUtil::toTexts(
35-
$result->getCandidates()
36-
);
33+
)->toTexts();
3734
```
3835

3936
#### Generate an image using any suitable OpenAI model
@@ -43,13 +40,13 @@ $modelsMetadata = Ai::defaultRegistry()->findProviderModelsMetadataForSupport(
4340
'openai',
4441
AiFeature::IMAGE_GENERATION
4542
);
46-
$imageFile = Ai::generateImage(
43+
$imageFile = Ai::generateImageResult(
4744
'Generate an illustration of the PHP elephant in the Carribean sea.',
4845
Ai::defaultRegistry()->getProviderModel(
4946
'openai',
5047
$modelsMetadata[0]->getId()
5148
)
52-
);
49+
)->toImageFile();
5350
```
5451

5552
#### Generate an image using any suitable model from any provider
@@ -58,13 +55,13 @@ $imageFile = Ai::generateImage(
5855
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
5956
AiFeature::IMAGE_GENERATION
6057
);
61-
$imageFile = Ai::generateImage(
58+
$imageFile = Ai::generateImageResult(
6259
'Generate an illustration of the PHP elephant in the Carribean sea.',
6360
Ai::defaultRegistry()->getProviderModel(
6461
$providerModelsMetadata[0]->getProvider()->getId(),
6562
$providerModelsMetadata[0]->getModels()[0]->getId()
6663
)
67-
);
64+
)->toImageFile();
6865
```
6966

7067
#### Generate embeddings using any suitable model from any provider
@@ -73,7 +70,7 @@ $imageFile = Ai::generateImage(
7370
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
7471
AiFeature::EMBEDDING_GENERATION
7572
);
76-
$embeddings = Ai::generateEmbeddings(
73+
$embeddings = Ai::generateEmbeddingsResult(
7774
[
7875
'A very long text.',
7976
'Another very long text.',
@@ -83,7 +80,7 @@ $embeddings = Ai::generateEmbeddings(
8380
$providerModelsMetadata[0]->getProvider()->getId(),
8481
$providerModelsMetadata[0]->getModels()[0]->getId()
8582
)
86-
);
83+
)->getEmbeddings();
8784
```
8885

8986
#### Generate text with JSON output using any suitable model from any provider
@@ -97,7 +94,7 @@ $providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
9794
TextGenerationConfig::OUTPUT_SCHEMA => true,
9895
]
9996
);
100-
$imageFile = Ai::generateText(
97+
$jsonString = Ai::generateTextResult(
10198
'Transform the following CSV content into a JSON array of row data.',
10299
Ai::defaultRegistry()->getProviderModel(
103100
$providerModelsMetadata[0]->getProvider()->getId(),
@@ -122,7 +119,7 @@ $imageFile = Ai::generateText(
122119
],
123120
]
124121
)
125-
);
122+
)->toText();
126123
```
127124

128125
## Class diagrams
@@ -156,12 +153,6 @@ direction LR
156153
class AiEntrypoint {
157154
+defaultRegistry() AiProviderRegistry
158155
+isConfigured(AiProviderAvailability $availability) bool$
159-
+generateText(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) string$
160-
+streamGenerateText(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) Generator< string >$
161-
+generateImage(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
162-
+textToSpeech(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
163-
+generateSpeech(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
164-
+generateEmbeddings(Message[] $input, AiModel $model) Embedding[]$
165156
+generateResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
166157
+generateOperation(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiOperation$
167158
+generateTextResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
@@ -206,12 +197,6 @@ direction LR
206197
class AiEntrypoint {
207198
+defaultRegistry() AiProviderRegistry
208199
+isConfigured(AiProviderAvailability $availability) bool$
209-
+generateText(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) string$
210-
+streamGenerateText(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) Generator< string >$
211-
+generateImage(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
212-
+textToSpeech(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
213-
+generateSpeech(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) File$
214-
+generateEmbeddings(Message[] $input, AiModel $model) Embedding[]$
215200
+generateResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
216201
+generateOperation(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiOperation$
217202
+generateTextResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
@@ -304,6 +289,17 @@ direction LR
304289
+getUsage() TokenUsage
305290
+getProviderMetadata() array< string, mixed >
306291
+getJsonSchema() array< string, mixed >$
292+
%% The following utility methods transform the result candidates into a specific shape.
293+
+toText() string
294+
+toImageFile() File
295+
+toAudioFile() File
296+
+toVideoFile() File
297+
+toMessage() Message
298+
+toTexts() string[]
299+
+toImageFiles() File[]
300+
+toAudioFiles() File[]
301+
+toVideoFiles() File[]
302+
+toMessages() Message[]
307303
}
308304
class EmbeddingResult {
309305
+getId() string

0 commit comments

Comments
 (0)