Skip to content

Commit 12d510d

Browse files
authored
feat(genai): Add new Controlled Generation samples (#13154)
* feat(genai): Add additional Controlled Generation Samples - CtrlGen with Enum Class - CtrlGen with Resp Schema - Added requirements.txt
1 parent 67485b7 commit 12d510d

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_ctrlgen_with_enum_class_schema]
18+
import enum
19+
20+
from google import genai
21+
from google.genai.types import HttpOptions
22+
23+
class InstrumentClass(enum.Enum):
24+
PERCUSSION = "Percussion"
25+
STRING = "String"
26+
WOODWIND = "Woodwind"
27+
BRASS = "Brass"
28+
KEYBOARD = "Keyboard"
29+
30+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
31+
response = client.models.generate_content(
32+
model="gemini-2.0-flash-001",
33+
contents="What type of instrument is a guitar?",
34+
config={
35+
"response_mime_type": "text/x.enum",
36+
"response_schema": InstrumentClass,
37+
},
38+
)
39+
40+
print(response.text)
41+
# Example output:
42+
# String
43+
44+
# [END googlegenaisdk_ctrlgen_with_enum_class_schema]
45+
return response.text
46+
47+
48+
if __name__ == "__main__":
49+
generate_content()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_ctrlgen_with_resp_schema]
18+
from google import genai
19+
from google.genai.types import HttpOptions
20+
21+
response_schema = {
22+
"type": "ARRAY",
23+
"items": {
24+
"type": "OBJECT",
25+
"properties": {
26+
"recipe_name": {"type": "STRING"},
27+
"ingredients": {"type": "ARRAY", "items": {"type": "STRING"}},
28+
},
29+
"required": ["recipe_name", "ingredients"],
30+
},
31+
}
32+
33+
prompt = """
34+
List a few popular cookie recipes.
35+
"""
36+
37+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
38+
response = client.models.generate_content(
39+
model="gemini-2.0-flash-001",
40+
contents=prompt,
41+
config={
42+
"response_mime_type": "application/json",
43+
"response_schema": response_schema,
44+
},
45+
)
46+
47+
print(response.text)
48+
# Example output:
49+
# [
50+
# {
51+
# "ingredients": [
52+
# "2 1/4 cups all-purpose flour",
53+
# "1 teaspoon baking soda",
54+
# "1 teaspoon salt",
55+
# "1 cup (2 sticks) unsalted butter, softened",
56+
# "3/4 cup granulated sugar",
57+
# "3/4 cup packed brown sugar",
58+
# "1 teaspoon vanilla extract",
59+
# "2 large eggs",
60+
# "2 cups chocolate chips",
61+
# ],
62+
# "recipe_name": "Chocolate Chip Cookies",
63+
# }
64+
# ]
65+
# [END googlegenaisdk_ctrlgen_with_resp_schema]
66+
return response.text
67+
68+
69+
if __name__ == "__main__":
70+
generate_content()

genai/controlled_generation/test_controlled_generation_samples.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import os
2020

2121
import ctrlgen_with_class_schema
22+
import ctrlgen_with_enum_class_schema
2223
import ctrlgen_with_enum_schema
2324
import ctrlgen_with_nested_class_schema
2425
import ctrlgen_with_nullable_schema
26+
import ctrlgen_with_resp_schema
2527

2628
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
2729
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
@@ -33,6 +35,10 @@ def test_ctrlgen_with_class_schema() -> None:
3335
assert ctrlgen_with_class_schema.generate_content()
3436

3537

38+
def test_ctrlgen_with_enum_class_schema() -> None:
39+
assert ctrlgen_with_enum_class_schema.generate_content()
40+
41+
3642
def test_ctrlgen_with_enum_schema() -> None:
3743
assert ctrlgen_with_enum_schema.generate_content()
3844

@@ -43,3 +49,7 @@ def test_ctrlgen_with_nested_class_schema() -> None:
4349

4450
def test_ctrlgen_with_nullable_schema() -> None:
4551
assert ctrlgen_with_nullable_schema.generate_content()
52+
53+
54+
def test_ctrlgen_with_resp_schema() -> None:
55+
assert ctrlgen_with_resp_schema.generate_content()

0 commit comments

Comments
 (0)