Skip to content

Commit 31ca8fb

Browse files
feat(generativeai): update controlled generation (GoogleCloudPlatform#12456)
* update controlled generation * update generate 3 * modifying generate3 and adding generate 7 * adding additonal lines
1 parent dbbfe4a commit 31ca8fb

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

generative_ai/controlled_generation/controlled_generation_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ def test_config_response_schema4() -> None:
4949
def test_config_response_schema6() -> None:
5050
response = response_schema.generate_content6(PROJECT_ID)
5151
assert response
52+
53+
54+
def test_config_response_schema7() -> None:
55+
response = response_schema.generate_content7(PROJECT_ID)
56+
assert response

generative_ai/controlled_generation/response_schema.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def generate_content3(project_id: str) -> str:
105105

106106
# TODO(developer): Update and un-comment below line
107107
# project_id = "PROJECT_ID"
108+
108109
vertexai.init(project=project_id, location="us-central1")
109110

110111
response_schema = {
@@ -115,11 +116,11 @@ def generate_content3(project_id: str) -> str:
115116
"items": {
116117
"type": "OBJECT",
117118
"properties": {
118-
"Day": {"type": "STRING"},
119+
"Day": {"type": "STRING", },
119120
"Forecast": {"type": "STRING"},
120-
"Humidity": {"type": "STRING"},
121121
"Temperature": {"type": "INTEGER"},
122-
"Wind Speed": {"type": "INTEGER"},
122+
"Humidity": {"type": "STRING", "nullable": True},
123+
"Wind Speed": {"type": "INTEGER", "nullable": True},
123124
},
124125
"required": ["Day", "Temperature", "Forecast"],
125126
},
@@ -130,11 +131,11 @@ def generate_content3(project_id: str) -> str:
130131
prompt = """
131132
The week ahead brings a mix of weather conditions.
132133
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.
133-
Monday will see partly cloudy skies with a slightly cooler temperature of 72°F and humidity increasing to 55%. Winds will pick up slightly to around 15 km/h.
134-
Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%. Expect stronger winds at 20 km/h.
135-
Wednesday may see thunderstorms, with a temperature of 68°F and high humidity of 75%. Winds will be gusty at 25 km/h.
136-
Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. Winds will ease slightly to 18 km/h.
137-
Friday returns to partly cloudy conditions, with a temperature of 73°F and lower humidity at 45%. Winds will be light at 12 km/h.
134+
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.
135+
Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%.
136+
Wednesday may see thunderstorms, with a temperature of 68°F.
137+
Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%.
138+
Friday returns to partly cloudy conditions, with a temperature of 73°F and the Winds will be light at 12 km/h.
138139
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.
139140
"""
140141

@@ -148,9 +149,16 @@ def generate_content3(project_id: str) -> str:
148149
)
149150

150151
print(response.text)
152+
# Example reponse:
153+
# {"forecast": [{"Day": "Sunday", "Forecast": "Sunny", "Temperature": 77, "Humidity": "50%", "Wind Speed": 10},
154+
# {"Day": "Monday", "Forecast": "Partly Cloudy", "Temperature": 72, "Wind Speed": 15},
155+
# {"Day": "Tuesday", "Forecast": "Rain Showers", "Temperature": 64, "Humidity": "70%"},
156+
# {"Day": "Wednesday", "Forecast": "Thunderstorms", "Temperature": 68},
157+
# {"Day": "Thursday", "Forecast": "Cloudy", "Temperature": 66, "Humidity": "60%"},
158+
# {"Day": "Friday", "Forecast": "Partly Cloudy", "Temperature": 73, "Wind Speed": 12},
159+
# {"Day": "Saturday", "Forecast": "Sunny", "Temperature": 80, "Humidity": "40%", "Wind Speed": 8}]}
151160
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_3]
152-
153-
return response.text
161+
return response
154162

155163

156164
def generate_content4(project_id: str) -> str:
@@ -267,3 +275,42 @@ def generate_content6(project_id: str) -> str:
267275
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_6]
268276

269277
return response.text
278+
279+
280+
def generate_content7(project_id: str) -> str:
281+
# [START generativeaionvertexai_gemini_controlled_generation_response_schema_7]
282+
import vertexai
283+
284+
from vertexai.generative_models import GenerationConfig, GenerativeModel
285+
286+
# TODO(developer): Update and un-comment below line
287+
# project_id = "PROJECT_ID"
288+
289+
vertexai.init(project=project_id, location="us-central1")
290+
291+
model = GenerativeModel("gemini-1.5-pro")
292+
293+
response_schema = {
294+
"type": "STRING",
295+
"enum": ["drama", "comedy", "documentary"]
296+
}
297+
298+
prompt = "Movie name: The uncovered (2007)Movie context: The film's title refers not only to the un-recovered bodies at ground zero, "
299+
"but also to the state of the nation at large. Set in the hallucinatory period of time between September 11 and Halloween of 2001, "
300+
"The Unrecovered examines the effect of terror on the average mind, the way a state of heightened anxiety and/or alertness can cause "
301+
"the average person to make the sort of imaginative connections that are normally made only by artists and conspiracy theorists-both "
302+
"of whom figure prominently in this film. The Unrecovered explores the way in which irony, empathy, and paranoia relate to one another "
303+
"in the wake of 9/11.Given the movie name and context, please classify the movie type."
304+
305+
response = model.generate_content(
306+
prompt,
307+
generation_config=GenerationConfig(
308+
response_mime_type="text/x.enum", response_schema=response_schema
309+
),
310+
)
311+
312+
print(response.text)
313+
# Example reponse:
314+
# documentary
315+
# [END generativeaionvertexai_gemini_controlled_generation_response_schema_7]
316+
return response

0 commit comments

Comments
 (0)