Skip to content

Commit f0e7ed2

Browse files
Thoughtseize1gcf-owl-bot[bot]
authored andcommitted
refactor: (GenAI) Reorganized Controlled Generation Samples (Group A) (GoogleCloudPlatform#12573)
* Reorganized Controlled Generation folder * Minor comment fixes * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Changed "Example output" -> "Example response" * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3247f92 commit f0e7ed2

File tree

10 files changed

+535
-10
lines changed

10 files changed

+535
-10
lines changed

generative_ai/controlled_generation/controlled_generation_test.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,47 @@
1414

1515
import os
1616

17-
import response_mime_type
18-
import response_schema
19-
17+
import example_01
18+
import example_02
19+
import example_03
20+
import example_04
21+
import example_05
22+
import example_06
23+
import example_07
2024

2125
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2226

2327

2428
def test_config_response_mime_type() -> None:
25-
response = response_mime_type.generate_content(PROJECT_ID)
29+
response = example_05.generate_content()
2630
assert response
2731

2832

2933
def test_config_response_schema() -> None:
30-
response = response_schema.generate_content(PROJECT_ID)
34+
response = example_01.generate_content()
3135
assert response
3236

3337

3438
def test_config_response_schema2() -> None:
35-
response = response_schema.generate_content2(PROJECT_ID)
39+
response = example_02.generate_content()
3640
assert response
3741

3842

3943
def test_config_response_schema3() -> None:
40-
response = response_schema.generate_content3(PROJECT_ID)
44+
response = example_03.generate_content()
4145
assert response
4246

4347

4448
def test_config_response_schema4() -> None:
45-
response = response_schema.generate_content4(PROJECT_ID)
49+
response = example_04.generate_content()
4650
assert response
4751

4852

4953
def test_config_response_schema6() -> None:
50-
response = response_schema.generate_content6(PROJECT_ID)
54+
response = example_06.generate_content()
5155
assert response
5256

5357

5458
def test_config_response_schema7() -> None:
55-
response = response_schema.generate_content7(PROJECT_ID)
59+
response = example_07.generate_content()
5660
assert response
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_content() -> str:
20+
# [START generativeaionvertexai_gemini_controlled_generation_response_schema]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerationConfig, GenerativeModel
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
response_schema = {
30+
"type": "array",
31+
"items": {
32+
"type": "object",
33+
"properties": {
34+
"recipe_name": {
35+
"type": "string",
36+
},
37+
},
38+
"required": ["recipe_name"],
39+
},
40+
}
41+
42+
model = GenerativeModel("gemini-1.5-pro-001")
43+
44+
response = model.generate_content(
45+
"List a few popular cookie recipes",
46+
generation_config=GenerationConfig(
47+
response_mime_type="application/json", response_schema=response_schema
48+
),
49+
)
50+
51+
print(response.text)
52+
# Example response:
53+
# [
54+
# {"recipe_name": "Chocolate Chip Cookies"},
55+
# {"recipe_name": "Peanut Butter Cookies"},
56+
# {"recipe_name": "Snickerdoodles"},
57+
# {"recipe_name": "Oatmeal Raisin Cookies"},
58+
# ]
59+
60+
# [END generativeaionvertexai_gemini_controlled_generation_response_schema]
61+
return response.text
62+
63+
64+
if __name__ == "__main__":
65+
generate_content()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_content() -> str:
20+
# [START generativeaionvertexai_gemini_controlled_generation_response_schema_2]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerationConfig, GenerativeModel
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
response_schema = {
30+
"type": "ARRAY",
31+
"items": {
32+
"type": "ARRAY",
33+
"items": {
34+
"type": "OBJECT",
35+
"properties": {
36+
"rating": {"type": "INTEGER"},
37+
"flavor": {"type": "STRING"},
38+
},
39+
},
40+
},
41+
}
42+
43+
prompt = """
44+
Reviews from our social media:
45+
- "Absolutely loved it! Best ice cream I've ever had." Rating: 4, Flavor: Strawberry Cheesecake
46+
- "Quite good, but a bit too sweet for my taste." Rating: 1, Flavor: Mango Tango
47+
"""
48+
49+
model = GenerativeModel("gemini-1.5-pro-001")
50+
51+
response = model.generate_content(
52+
prompt,
53+
generation_config=GenerationConfig(
54+
response_mime_type="application/json", response_schema=response_schema
55+
),
56+
)
57+
58+
print(response.text)
59+
# Example response:
60+
# [
61+
# [
62+
# {"flavor": "Strawberry Cheesecake", "rating": 4},
63+
# {"flavor": "Mango Tango", "rating": 1},
64+
# ]
65+
# ]
66+
67+
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_2]
68+
return response.text
69+
70+
71+
if __name__ == "__main__":
72+
generate_content()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_content() -> str:
20+
# [START generativeaionvertexai_gemini_controlled_generation_response_schema_3]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerationConfig, GenerativeModel
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
response_schema = {
31+
"type": "OBJECT",
32+
"properties": {
33+
"forecast": {
34+
"type": "ARRAY",
35+
"items": {
36+
"type": "OBJECT",
37+
"properties": {
38+
"Day": {"type": "STRING", "nullable": True},
39+
"Forecast": {"type": "STRING", "nullable": True},
40+
"Temperature": {"type": "INTEGER", "nullable": True},
41+
"Humidity": {"type": "STRING", "nullable": True},
42+
"Wind Speed": {"type": "INTEGER", "nullable": True},
43+
},
44+
"required": ["Day", "Temperature", "Forecast", "Wind Speed"],
45+
},
46+
}
47+
},
48+
}
49+
50+
prompt = """
51+
The week ahead brings a mix of weather conditions.
52+
Sunday is expected to be sunny with a temperature of 77°F and a humidity level of 50%. Winds will be light at around 10 km/h.
53+
Monday will see partly cloudy skies with a slightly cooler temperature of 72°F and the winds will pick up slightly to around 15 km/h.
54+
Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%.
55+
Wednesday may see thunderstorms, with a temperature of 68°F.
56+
Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%.
57+
Friday returns to partly cloudy conditions, with a temperature of 73°F and the Winds will be light at 12 km/h.
58+
Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, and a humidity level of 40%. Winds will be gentle at 8 km/h.
59+
"""
60+
61+
model = GenerativeModel("gemini-1.5-pro-001")
62+
63+
response = model.generate_content(
64+
prompt,
65+
generation_config=GenerationConfig(
66+
response_mime_type="application/json", response_schema=response_schema
67+
),
68+
)
69+
70+
print(response.text)
71+
# Example response:
72+
# {"forecast": [{"Day": "Sunday", "Forecast": "Sunny", "Temperature": 77, "Humidity": "50%", "Wind Speed": 10},
73+
# {"Day": "Monday", "Forecast": "Partly Cloudy", "Temperature": 72, "Wind Speed": 15},
74+
# {"Day": "Tuesday", "Forecast": "Rain Showers", "Temperature": 64, "Humidity": "70%"},
75+
# {"Day": "Wednesday", "Forecast": "Thunderstorms", "Temperature": 68},
76+
# {"Day": "Thursday", "Forecast": "Cloudy", "Temperature": 66, "Humidity": "60%"},
77+
# {"Day": "Friday", "Forecast": "Partly Cloudy", "Temperature": 73, "Wind Speed": 12},
78+
# {"Day": "Saturday", "Forecast": "Sunny", "Temperature": 80, "Humidity": "40%", "Wind Speed": 8}]}
79+
80+
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_3]
81+
return response.text
82+
83+
84+
if __name__ == "__main__":
85+
generate_content()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_content() -> str:
20+
# [START generativeaionvertexai_gemini_controlled_generation_response_schema_4]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerationConfig, GenerativeModel
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
response_schema = {
30+
"type": "ARRAY",
31+
"items": {
32+
"type": "OBJECT",
33+
"properties": {
34+
"to_discard": {"type": "INTEGER"},
35+
"subcategory": {"type": "STRING"},
36+
"safe_handling": {"type": "INTEGER"},
37+
"item_category": {
38+
"type": "STRING",
39+
"enum": [
40+
"clothing",
41+
"winter apparel",
42+
"specialized apparel",
43+
"furniture",
44+
"decor",
45+
"tableware",
46+
"cookware",
47+
"toys",
48+
],
49+
},
50+
"for_resale": {"type": "INTEGER"},
51+
"condition": {
52+
"type": "STRING",
53+
"enum": [
54+
"new in package",
55+
"like new",
56+
"gently used",
57+
"used",
58+
"damaged",
59+
"soiled",
60+
],
61+
},
62+
},
63+
},
64+
}
65+
66+
prompt = """
67+
Item description:
68+
The item is a long winter coat that has many tears all around the seams and is falling apart.
69+
It has large questionable stains on it.
70+
"""
71+
72+
model = GenerativeModel("gemini-1.5-pro-001")
73+
74+
response = model.generate_content(
75+
prompt,
76+
generation_config=GenerationConfig(
77+
response_mime_type="application/json", response_schema=response_schema
78+
),
79+
)
80+
81+
print(response.text)
82+
# Example response:
83+
# [
84+
# {
85+
# "condition": "damaged",
86+
# "item_category": "clothing",
87+
# "subcategory": "winter apparel",
88+
# "to_discard": 123,
89+
# }
90+
# ]
91+
92+
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_4]
93+
return response.text
94+
95+
96+
if __name__ == "__main__":
97+
generate_content()

0 commit comments

Comments
 (0)