Skip to content

Commit ad14f14

Browse files
authored
Include additional code examples for some simpler and some more complex use-cases, with additional explanations.
1 parent 42c1a83 commit ad14f14

File tree

1 file changed

+77
-9
lines changed

1 file changed

+77
-9
lines changed

docs/ARCHITECTURE.md

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ The main additional aspect that the Vercel AI SDK does not cater for easily is f
1212

1313
The following examples indicate how this SDK could eventually be used.
1414

15+
#### Generate text using any suitable model from any provider (most basic example)
16+
17+
```php
18+
$text = Ai::generateTextResult(
19+
'Write a 2-verse poem about PHP.'
20+
)->toText();
21+
```
22+
1523
#### Generate text using a Google model
1624

1725
```php
@@ -64,27 +72,68 @@ $imageFile = Ai::generateImageResult(
6472
)->toImageFile();
6573
```
6674

67-
#### Generate embeddings using any suitable model from any provider
75+
#### Generate text using any suitable model from any provider
76+
77+
_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._
6878

6979
```php
7080
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
71-
AiFeature::EMBEDDING_GENERATION
81+
AiFeature::TEXT_GENERATION
7282
);
73-
$embeddings = Ai::generateEmbeddingsResult(
74-
[
75-
'A very long text.',
76-
'Another very long text.',
77-
'More long text.',
78-
],
83+
$text = Ai::generateTextResult(
84+
'Write a 2-verse poem about PHP.',
7985
Ai::defaultRegistry()->getProviderModel(
8086
$providerModelsMetadata[0]->getProvider()->getId(),
8187
$providerModelsMetadata[0]->getModels()[0]->getId()
8288
)
83-
)->getEmbeddings();
89+
)->toText();
90+
```
91+
92+
#### Generate text with an image as additional input using any suitable model from any provider
93+
94+
_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 `AiFeature::TEXT_GENERATION`, but also `AiCapability::INPUT_MODALITIES => ['text', 'image']`._
95+
96+
```php
97+
$text = Ai::generateTextResult(
98+
[
99+
[
100+
'text' => 'Generate alternative text for this image.',
101+
],
102+
[
103+
'mimeType' => 'image/png',
104+
'base64Data' => '...', // Base64-encoded data blob.
105+
],
106+
]
107+
)->toText();
108+
```
109+
110+
#### Generate text with chat history using any suitable model from any provider
111+
112+
_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 `AiFeature::TEXT_GENERATION` as well as `AiCapability::CHAT_HISTORY`._
113+
114+
```php
115+
$text = Ai::generateTextResult(
116+
[
117+
[
118+
'role' => MessageRole::USER,
119+
'parts' => ['text' => 'Do you spell it WordPress or Wordpress?'],
120+
],
121+
[
122+
'role' => MessageRole::MODEL,
123+
'parts' => ['text' => 'The correct spelling is WordPress.'],
124+
],
125+
[
126+
'role' => MessageRole::USER,
127+
'parts' => ['text' => 'Can you repeat that please?'],
128+
],
129+
]
130+
)->toText();
84131
```
85132

86133
#### Generate text with JSON output using any suitable model from any provider
87134

135+
_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._
136+
88137
```php
89138
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
90139
AiFeature::TEXT_GENERATION,
@@ -122,6 +171,25 @@ $jsonString = Ai::generateTextResult(
122171
)->toText();
123172
```
124173

174+
#### Generate embeddings using any suitable model from any provider
175+
176+
```php
177+
$providerModelsMetadata = Ai::defaultRegistry()->findModelsMetadataForSupport(
178+
AiFeature::EMBEDDING_GENERATION
179+
);
180+
$embeddings = Ai::generateEmbeddingsResult(
181+
[
182+
'A very long text.',
183+
'Another very long text.',
184+
'More long text.',
185+
],
186+
Ai::defaultRegistry()->getProviderModel(
187+
$providerModelsMetadata[0]->getProvider()->getId(),
188+
$providerModelsMetadata[0]->getModels()[0]->getId()
189+
)
190+
)->getEmbeddings();
191+
```
192+
125193
## Class diagrams
126194

127195
This section shows comprehensive class diagrams for the proposed architecture. For explanation on specific terms, see the [glossary](./GLOSSARY.md).

0 commit comments

Comments
 (0)