Skip to content

Commit 4123010

Browse files
author
Fatih Aydın
committed
feat: Add beta branch
1 parent e27b63f commit 4123010

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ For more information, take a look at the [google-gemini-php/client](https://gith
2525
- [Text-and-image Input](#text-and-image-input)
2626
- [Multi-turn Conversations (Chat)](#multi-turn-conversations-chat)
2727
- [Stream Generate Content](#stream-generate-content)
28+
- [Structured Output](#structured-output)
2829
- [Count tokens](#count-tokens)
2930
- [Configuration](#configuration)
3031
- [Embedding Resource](#embedding-resource)
@@ -146,6 +147,57 @@ foreach ($stream as $response) {
146147
}
147148
```
148149

150+
151+
#### Structured Output
152+
Gemini generates unstructured text by default, but some applications require structured text. For these use cases, you can constrain Gemini to respond with JSON, a structured data format suitable for automated processing. You can also constrain the model to respond with one of the options specified in an enum.
153+
154+
```php
155+
$result = Gemini::geminiFlash()
156+
->withGenerationConfig(
157+
generationConfig: new GenerationConfig(
158+
responseMimeType: ResponseMimeType::APPLICATION_JSON,
159+
responseSchema: new Schema(
160+
type: DataType::ARRAY,
161+
items: new Schema(
162+
type: DataType::OBJECT,
163+
properties: [
164+
"recipe_name" => new Schema(type: DataType::STRING),
165+
"cooking_time_in_minutes" => new Schema(type: DataType::INTEGER)
166+
]
167+
)
168+
)
169+
)
170+
)
171+
->generateContent("List 5 popular cookie recipes with cooking time");
172+
173+
174+
$result->json();
175+
176+
//[
177+
// {
178+
// +"cooking_time_in_minutes": 10,
179+
// +"recipe_name": "Chocolate Chip Cookies",
180+
// },
181+
// {
182+
// +"cooking_time_in_minutes": 12,
183+
// +"recipe_name": "Oatmeal Raisin Cookies",
184+
// },
185+
// {
186+
// +"cooking_time_in_minutes": 10,
187+
// +"recipe_name": "Peanut Butter Cookies",
188+
// },
189+
// {
190+
// +"cooking_time_in_minutes": 10,
191+
// +"recipe_name": "Snickerdoodles",
192+
// },
193+
// {
194+
// +"cooking_time_in_minutes": 12,
195+
// +"recipe_name": "Sugar Cookies",
196+
// },
197+
// ]
198+
199+
```
200+
149201
#### Count tokens
150202
When using long prompts, it might be useful to count tokens before sending any content to the model.
151203

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^8.1.0",
14-
"google-gemini-php/client": "^1.0",
14+
"google-gemini-php/client": "^1.0.0-beta",
1515
"laravel/framework": "^9.0|^10.0|^11.0"
1616
},
1717
"require-dev": {

src/Testing/GeminiFake.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use Gemini\Testing\ClientFake;
88

9-
class GeminiFake extends ClientFake
10-
{
11-
}
9+
class GeminiFake extends ClientFake {}

tests/Arch.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'Gemini\Contracts\ResponseContract',
1212
'Gemini\Laravel\Testing\GeminiFake',
1313
'Gemini\Responses\StreamResponse',
14+
'Gemini\Enums\ModelType',
1415
]);
1516

1617
test('service providers')

0 commit comments

Comments
 (0)