@@ -19,6 +19,13 @@ The following examples indicate how this SDK could eventually be used.
19
19
20
20
#### Generate text using any suitable model from any provider (most basic example)
21
21
22
+ ##### Fluent API
23
+ ``` php
24
+ $text = AiClient::prompt('Write a 2-verse poem about PHP.')
25
+ ->generateText();
26
+ ```
27
+
28
+ ##### Traditional API
22
29
``` php
23
30
$text = AiClient::generateTextResult(
24
31
'Write a 2-verse poem about PHP.'
@@ -27,6 +34,14 @@ $text = AiClient::generateTextResult(
27
34
28
35
#### Generate text using a Google model
29
36
37
+ ##### Fluent API
38
+ ``` php
39
+ $text = AiClient::prompt('Write a 2-verse poem about PHP.')
40
+ ->usingModel('gemini-2.5-flash')
41
+ ->generateText();
42
+ ```
43
+
44
+ ##### Traditional API
30
45
``` php
31
46
$text = AiClient::generateTextResult(
32
47
'Write a 2-verse poem about PHP.',
@@ -36,6 +51,14 @@ $text = AiClient::generateTextResult(
36
51
37
52
#### Generate multiple text candidates using an Anthropic model
38
53
54
+ ##### Fluent API
55
+ ``` php
56
+ $texts = AiClient::prompt('Write a 2-verse poem about PHP.')
57
+ ->usingModel('claude-3.7-sonnet')
58
+ ->generateTexts(4);
59
+ ```
60
+
61
+ ##### Traditional API
39
62
``` php
40
63
$texts = AiClient::generateTextResult(
41
64
'Write a 2-verse poem about PHP.',
@@ -48,6 +71,15 @@ $texts = AiClient::generateTextResult(
48
71
49
72
#### Generate an image using any suitable OpenAI model
50
73
74
+ ##### Fluent API
75
+ ``` php
76
+ $imageFile = AiClient::prompt('Generate an illustration of the PHP elephant in the Carribean sea.')
77
+ ->usingProvider('openai')
78
+ ->usingModelSupportingImages() // Optional.
79
+ ->generateImage();
80
+ ```
81
+
82
+ ##### Traditional API
51
83
``` php
52
84
$modelsMetadata = AiClient::defaultRegistry()->findProviderModelsMetadataForSupport(
53
85
'openai',
@@ -64,6 +96,14 @@ $imageFile = AiClient::generateImageResult(
64
96
65
97
#### Generate an image using any suitable model from any provider
66
98
99
+ ##### Fluent API
100
+ ``` php
101
+ $imageFile = AiClient::prompt('Generate an illustration of the PHP elephant in the Carribean sea.')
102
+ ->usingModelSupportingImages() // Optional.
103
+ ->generateImage();
104
+ ```
105
+
106
+ ##### Traditional API
67
107
``` php
68
108
$providerModelsMetadata = AiClient::defaultRegistry()->findModelsMetadataForSupport(
69
109
new AiModelRequirements([AiCapability::IMAGE_GENERATION])
@@ -81,6 +121,14 @@ $imageFile = AiClient::generateImageResult(
81
121
82
122
_ Note: This does effectively the exact same as [ the first code example] ( #generate-text-using-any-suitable-model-from-any-provider-most-basic-example ) , but more verbosely. In other words, if you omit the model parameter, the SDK will do this internally._
83
123
124
+ ##### Fluent API
125
+ ``` php
126
+ $text = AiClient::prompt('Write a 2-verse poem about PHP.')
127
+ ->usingModelSupportingText() // Optional.
128
+ ->generateText();
129
+ ```
130
+
131
+ ##### Traditional API
84
132
``` php
85
133
$providerModelsMetadata = AiClient::defaultRegistry()->findModelsMetadataForSupport(
86
134
new AiModelRequirements([AiCapability::TEXT_GENERATION])
@@ -98,6 +146,14 @@ $text = AiClient::generateTextResult(
98
146
99
147
_ Note: Since this omits the model parameter, the SDK will automatically determine which models are suitable and use any of them, similar to [ the first code example] ( #generate-text-using-any-suitable-model-from-any-provider-most-basic-example ) . Since it knows the input includes an image, it can internally infer that the model needs to not only support ` AiCapability::TEXT_GENERATION ` , but also ` AiOption::INPUT_MODALITIES => ['text', 'image'] ` ._
100
148
149
+ ##### Fluent API
150
+ ``` php
151
+ $text = AiClient::prompt('Generate alternative text for this image.')
152
+ ->withImage('image/png', $base64blob)
153
+ ->generateText();
154
+ ```
155
+
156
+ ##### Traditional API
101
157
``` php
102
158
$text = AiClient::generateTextResult(
103
159
[
@@ -116,6 +172,17 @@ $text = AiClient::generateTextResult(
116
172
117
173
_ Note: Similarly to the previous example, even without specifying the model here, the SDK will be able to infer required model capabilities because it can detect that multiple chat messages are passed. Therefore it will internally only consider models that support ` AiCapability::TEXT_GENERATION ` as well as ` AiCapability::CHAT_HISTORY ` ._
118
174
175
+ ##### Fluent API
176
+ ``` php
177
+ $text = AiClient::prompt('Can you repeat that please?')
178
+ ->withHistory(
179
+ new UserMessage('Do you spell it WordPress or Wordpress?'),
180
+ new AgentMessage('The correct spelling is WordPress.')
181
+ )
182
+ ->generateText();
183
+ ```
184
+
185
+ ##### Traditional API
119
186
``` php
120
187
$text = AiClient::generateTextResult(
121
188
[
@@ -139,6 +206,21 @@ $text = AiClient::generateTextResult(
139
206
140
207
_ Note: Unlike the previous two examples, to require JSON output it is necessary to go the verbose route, since it is impossible for the SDK to detect whether you require JSON output purely from the prompt input. Therefore this code example contains the logic to manually search for suitable models and then use one of them for the task._
141
208
209
+ ##### Fluent API
210
+ ``` php
211
+ // Verbose.
212
+ $text = AiClient::prompt('Transform the following CSV content into a JSON array of row data.')
213
+ ->asJsonResponse()
214
+ ->usingOutputSchema(['name' => 'string', 'age' => 'integer'])
215
+ ->generateText();
216
+
217
+ // Simple.
218
+ $text = AiClient::prompt('Transform the following CSV content into a JSON array of row data.')
219
+ ->asJsonResponse(['name' => 'string', 'age' => 'integer'])
220
+ ->generateText();
221
+ ```
222
+
223
+ ##### Traditional API
142
224
``` php
143
225
$providerModelsMetadata = AiClient::defaultRegistry()->findModelsMetadataForSupport(
144
226
new AiModelRequirements(
@@ -178,6 +260,13 @@ $jsonString = AiClient::generateTextResult(
178
260
179
261
#### Generate embeddings using any suitable model from any provider
180
262
263
+ ##### Fluent API
264
+ ``` php
265
+ $embeddings = AiClient::prompt('A very long text.', 'Another very long text.', 'More long text.')
266
+ ->generateEmbeddings();
267
+ ```
268
+
269
+ ##### Traditional API
181
270
``` php
182
271
$providerModelsMetadata = AiClient::defaultRegistry()->findModelsMetadataForSupport(
183
272
new AiModelRequirements([AiCapability::EMBEDDING_GENERATION])
@@ -217,19 +306,34 @@ classDiagram
217
306
direction LR
218
307
namespace Ai {
219
308
class AiClient {
220
- +prompt(? string $text) PromptBuilder$
221
- +message(? string $text) MessageBuilder$
309
+ +prompt(... string $text) PromptBuilder$
310
+ +message(... string $text) MessageBuilder$
222
311
}
223
312
224
313
class PromptBuilder {
225
314
+withText(string $text) self
315
+ +withImage(string $mimeType, string $base64Blob) self
226
316
+withImageFile(File $file) self
227
317
+withAudioFile(File $file) self
228
318
+withVideoFile(File $file) self
229
319
+withFunctionResponse(FunctionResponse $functionResponse) self
230
320
+withMessageParts(...MessagePart $part) self
231
321
+withHistory(...Message $messages) self
232
322
+usingModel(AiModel $model) self
323
+ +usingModelSupporting(...AiCapability|AiOption $aiCapabilityOrOption) self
324
+ +usingModelSupportingCapability(...AiCapability $aiCapability) self
325
+ +usingModelSupportingOption(...AiOption $aiOption) self
326
+ +usingModelSupportingAudio() self
327
+ +usingModelSupportingHistory() self
328
+ +usingModelSupportingEmbeddings() self
329
+ +usingModelSupportingImages() self
330
+ +usingModelSupportingJsonOutput() self
331
+ +usingModelSupportingMusic() self
332
+ +usingModelSupportingOutputSchema() self
333
+ +usingModelSupportingSpeech() self
334
+ +usingModelSupportingText() self
335
+ +usingModelSupportingTextToSpeech() self
336
+ +usingModelSupportingVideo() self
233
337
+usingSystemInstruction(string|MessagePart[]|Message $systemInstruction) self
234
338
+usingMaxTokens(int $maxTokens) self
235
339
+usingTemperature(float $temperature) self
@@ -240,24 +344,41 @@ direction LR
240
344
+usingOutputMime(string $mimeType) self
241
345
+usingOutputSchema(array< string, mixed > $schema) self
242
346
+usingOutputModalities(...AiModality $modalities) self
347
+ +asArrayResponse(?array< string, mixed > $schema) self
348
+ +asJsonResponse(?array< string, mixed > $schema) self
243
349
+generateResult() GenerativeAiResult
350
+ +generateResults(int $candidateCount) GenerativeAiResult[]
244
351
+generateOperation() GenerativeAiOperation
352
+ +generateOperations(int $candidateCount) GenerativeAiOperation[]
245
353
+generateTextResult() GenerativeAiResult
354
+ +generateTextResults(int $candidateCount) GenerativeAiResult[]
246
355
+streamGenerateTextResult() Generator< GenerativeAiResult >
247
356
+generateImageResult() GenerativeAiResult
357
+ +generateImageResults(int $candidateCount) GenerativeAiResult[]
248
358
+convertTextToSpeechResult() GenerativeAiResult
359
+ +convertTextToSpeechResults(int $candidateCount) GenerativeAiResult[]
249
360
+generateSpeechResult() GenerativeAiResult
361
+ +generateSpeechResults(int $candidateCount) GenerativeAiResult[]
250
362
+generateEmbeddingsResult() EmbeddingResult
363
+ +generateEmbeddingsResults(int $candidateCount) EmbeddingResult[]
251
364
+generateTextOperation() GenerativeAiOperation
365
+ +generateTextOperations(int $candidateCount) GenerativeAiOperation[]
252
366
+generateImageOperation() GenerativeAiOperation
367
+ +generateImageOperations(int $candidateCount) GenerativeAiOperation[]
253
368
+convertTextToSpeechOperation() GenerativeAiOperation
254
369
+generateSpeechOperation() GenerativeAiOperation
370
+ +generateSpeechOperations(int $candidateCount) GenerativeAiOperation[]
255
371
+generateEmbeddingsOperation() EmbeddingOperation
372
+ +generateEmbeddingsOperations(int $candidateCount) EmbeddingOperation[]
256
373
+generateText() string
374
+ +generateTexts(int $candidateCount) string[]
257
375
+streamGenerateText() Generator< string >
258
376
+generateImage() File
377
+ +generateImages(int $candidateCount) File[]
259
378
+convertTextToSpeech() File
379
+ +convertTextToSpeeches(int $candidateCount) File[]
260
380
+generateSpeech() File
381
+ +generateSpeeches(int $candidateCount) File[]
261
382
+generateEmbeddings() Embedding[]
262
383
+getModelRequirements() AiModelRequirements
263
384
+isSupported() bool
@@ -266,6 +387,7 @@ direction LR
266
387
class MessageBuilder {
267
388
+usingRole(MessageRole $role) self
268
389
+withText(string $text) self
390
+ +withImage(string $mimeType, string $base64Blob) self
269
391
+withImageFile(File $file) self
270
392
+withAudioFile(File $file) self
271
393
+withVideoFile(File $file) self
@@ -356,8 +478,8 @@ classDiagram
356
478
direction LR
357
479
namespace Ai {
358
480
class AiClient {
359
- +prompt(? string $text) PromptBuilder$
360
- +message(? string $text) MessageBuilder$
481
+ +prompt(... string $text) PromptBuilder$
482
+ +message(... string $text) MessageBuilder$
361
483
+defaultRegistry() AiProviderRegistry$
362
484
+isConfigured(AiProviderAvailability $availability) bool$
363
485
+generateResult(string|MessagePart|MessagePart[]|Message|Message[] $prompt, AiModel $model) GenerativeAiResult$
@@ -377,13 +499,28 @@ direction LR
377
499
378
500
class PromptBuilder {
379
501
+withText(string $text) self
502
+ +withImage(string $mimeType, string $base64Blob) self
380
503
+withImageFile(File $file) self
381
504
+withAudioFile(File $file) self
382
505
+withVideoFile(File $file) self
383
506
+withFunctionResponse(FunctionResponse $functionResponse) self
384
507
+withMessageParts(...MessagePart $part) self
385
508
+withHistory(...Message $messages) self
386
509
+usingModel(AiModel $model) self
510
+ +usingModelSupporting(...AiCapability|AiOption $aiCapabilityOrOption) self
511
+ +usingModelSupportingCapability(...AiCapability $aiCapability) self
512
+ +usingModelSupportingOption(...AiOption $aiOption) self
513
+ +usingModelSupportingAudio() self
514
+ +usingModelSupportingHistory() self
515
+ +usingModelSupportingEmbeddings() self
516
+ +usingModelSupportingImages() self
517
+ +usingModelSupportingJsonOutput() self
518
+ +usingModelSupportingMusic() self
519
+ +usingModelSupportingOutputSchema() self
520
+ +usingModelSupportingSpeech() self
521
+ +usingModelSupportingText() self
522
+ +usingModelSupportingTextToSpeech() self
523
+ +usingModelSupportingVideo() self
387
524
+usingSystemInstruction(string|MessagePart[]|Message $systemInstruction) self
388
525
+usingMaxTokens(int $maxTokens) self
389
526
+usingTemperature(float $temperature) self
@@ -394,24 +531,41 @@ direction LR
394
531
+usingOutputMime(string $mimeType) self
395
532
+usingOutputSchema(array< string, mixed > $schema) self
396
533
+usingOutputModalities(...AiModality $modalities) self
534
+ +asArrayResponse(?array< string, mixed > $schema) self
535
+ +asJsonResponse(?array< string, mixed > $schema) self
397
536
+generateResult() GenerativeAiResult
537
+ +generateResults(int $candidateCount) GenerativeAiResult[]
398
538
+generateOperation() GenerativeAiOperation
539
+ +generateOperations(int $candidateCount) GenerativeAiOperation[]
399
540
+generateTextResult() GenerativeAiResult
541
+ +generateTextResults(int $candidateCount) GenerativeAiResult[]
400
542
+streamGenerateTextResult() Generator< GenerativeAiResult >
401
543
+generateImageResult() GenerativeAiResult
544
+ +generateImageResults(int $candidateCount) GenerativeAiResult[]
402
545
+convertTextToSpeechResult() GenerativeAiResult
546
+ +convertTextToSpeechResults(int $candidateCount) GenerativeAiResult[]
403
547
+generateSpeechResult() GenerativeAiResult
548
+ +generateSpeechResults(int $candidateCount) GenerativeAiResult[]
404
549
+generateEmbeddingsResult() EmbeddingResult
550
+ +generateEmbeddingsResults(int $candidateCount) EmbeddingResult[]
405
551
+generateTextOperation() GenerativeAiOperation
552
+ +generateTextOperations(int $candidateCount) GenerativeAiOperation[]
406
553
+generateImageOperation() GenerativeAiOperation
554
+ +generateImageOperations(int $candidateCount) GenerativeAiOperation[]
407
555
+convertTextToSpeechOperation() GenerativeAiOperation
408
556
+generateSpeechOperation() GenerativeAiOperation
557
+ +generateSpeechOperations(int $candidateCount) GenerativeAiOperation[]
409
558
+generateEmbeddingsOperation() EmbeddingOperation
559
+ +generateEmbeddingsOperations(int $candidateCount) EmbeddingOperation[]
410
560
+generateText() string
561
+ +generateTexts(int $candidateCount) string[]
411
562
+streamGenerateText() Generator< string >
412
563
+generateImage() File
564
+ +generateImages(int $candidateCount) File[]
413
565
+convertTextToSpeech() File
566
+ +convertTextToSpeeches(int $candidateCount) File[]
414
567
+generateSpeech() File
568
+ +generateSpeeches(int $candidateCount) File[]
415
569
+generateEmbeddings() Embedding[]
416
570
+getModelRequirements() AiModelRequirements
417
571
+isSupported() bool
@@ -420,6 +574,7 @@ direction LR
420
574
class MessageBuilder {
421
575
+usingRole(MessageRole $role) self
422
576
+withText(string $text) self
577
+ +withImage(string $mimeType, string $base64Blob) self
423
578
+withImageFile(File $file) self
424
579
+withAudioFile(File $file) self
425
580
+withVideoFile(File $file) self
0 commit comments