@@ -15,25 +15,22 @@ The following examples indicate how this SDK could eventually be used.
15
15
#### Generate text using a Google model
16
16
17
17
``` php
18
- $text = Ai::generateText (
18
+ $text = Ai::generateTextResult (
19
19
'Write a 2-verse poem about PHP.',
20
20
Google::model('gemini-2.5-flash')
21
- );
21
+ )->toText() ;
22
22
```
23
23
24
24
#### Generate multiple text candidates using an Anthropic model
25
25
26
26
``` php
27
- $result = Ai::generateTextResult(
27
+ $texts = Ai::generateTextResult(
28
28
'Write a 2-verse poem about PHP.',
29
29
Anthropic::model(
30
30
'claude-3.7-sonnet',
31
31
[TextGenerationConfig::CANDIDATE_COUNT => 4]
32
32
)
33
- );
34
- $texts = CandidatesUtil::toTexts(
35
- $result->getCandidates()
36
- );
33
+ )->toTexts();
37
34
```
38
35
39
36
#### Generate an image using any suitable OpenAI model
@@ -43,13 +40,13 @@ $modelsMetadata = Ai::defaultRegistry()->findProviderModelsMetadataForSupport(
43
40
'openai',
44
41
AiFeature::IMAGE_GENERATION
45
42
);
46
- $imageFile = Ai::generateImage (
43
+ $imageFile = Ai::generateImageResult (
47
44
'Generate an illustration of the PHP elephant in the Carribean sea.',
48
45
Ai::defaultRegistry()->getProviderModel(
49
46
'openai',
50
47
$modelsMetadata[0]->getId()
51
48
)
52
- );
49
+ )->toImageFile() ;
53
50
```
54
51
55
52
#### Generate an image using any suitable model from any provider
@@ -58,13 +55,13 @@ $imageFile = Ai::generateImage(
58
55
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
59
56
AiFeature::IMAGE_GENERATION
60
57
);
61
- $imageFile = Ai::generateImage (
58
+ $imageFile = Ai::generateImageResult (
62
59
'Generate an illustration of the PHP elephant in the Carribean sea.',
63
60
Ai::defaultRegistry()->getProviderModel(
64
61
$providerModelsMetadata[0]->getProvider()->getId(),
65
62
$providerModelsMetadata[0]->getModels()[0]->getId()
66
63
)
67
- );
64
+ )->toImageFile() ;
68
65
```
69
66
70
67
#### Generate embeddings using any suitable model from any provider
@@ -73,7 +70,7 @@ $imageFile = Ai::generateImage(
73
70
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
74
71
AiFeature::EMBEDDING_GENERATION
75
72
);
76
- $embeddings = Ai::generateEmbeddings (
73
+ $embeddings = Ai::generateEmbeddingsResult (
77
74
[
78
75
'A very long text.',
79
76
'Another very long text.',
@@ -83,7 +80,7 @@ $embeddings = Ai::generateEmbeddings(
83
80
$providerModelsMetadata[0]->getProvider()->getId(),
84
81
$providerModelsMetadata[0]->getModels()[0]->getId()
85
82
)
86
- );
83
+ )->getEmbeddings() ;
87
84
```
88
85
89
86
#### Generate text with JSON output using any suitable model from any provider
@@ -97,7 +94,7 @@ $providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
97
94
TextGenerationConfig::OUTPUT_SCHEMA => true,
98
95
]
99
96
);
100
- $imageFile = Ai::generateText (
97
+ $jsonString = Ai::generateTextResult (
101
98
'Transform the following CSV content into a JSON array of row data.',
102
99
Ai::defaultRegistry()->getProviderModel(
103
100
$providerModelsMetadata[0]->getProvider()->getId(),
@@ -122,7 +119,7 @@ $imageFile = Ai::generateText(
122
119
],
123
120
]
124
121
)
125
- );
122
+ )->toText() ;
126
123
```
127
124
128
125
## Class diagrams
@@ -156,12 +153,6 @@ direction LR
156
153
class AiEntrypoint {
157
154
+defaultRegistry() AiProviderRegistry
158
155
+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[]$
165
156
+generateResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
166
157
+generateOperation(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiOperation$
167
158
+generateTextResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
@@ -206,12 +197,6 @@ direction LR
206
197
class AiEntrypoint {
207
198
+defaultRegistry() AiProviderRegistry
208
199
+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[]$
215
200
+generateResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
216
201
+generateOperation(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiOperation$
217
202
+generateTextResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
@@ -304,6 +289,17 @@ direction LR
304
289
+getUsage() TokenUsage
305
290
+getProviderMetadata() array< string, mixed >
306
291
+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[]
307
303
}
308
304
class EmbeddingResult {
309
305
+getId() string
0 commit comments