Skip to content

Commit b4fe61e

Browse files
committed
Refactoring first sample create_prompts of prompts to migrate to genai/prompts.
1 parent 24d6117 commit b4fe61e

17 files changed

Lines changed: 718 additions & 0 deletions

genai/prompts/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["3.8", "3.9", "3.11", "3.12", "3.13"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}

genai/prompts/prompt_create.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
15+
16+
# [START aiplatform_genai_prompt_template_create]
17+
18+
import os
19+
20+
import agentplatform
21+
22+
from google import genai
23+
24+
25+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
26+
LOCATION_ID = os.getenv("LOCATION_ID", "us-central1")
27+
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-flash")
28+
29+
30+
def prompt_create_2() -> None:
31+
agent_client = agentplatform.Client(project=PROJECT_ID, location=LOCATION_ID)
32+
33+
system_part = genai.types.Part(text="You are a knowledgeable local tour guide.")
34+
35+
user_part = genai.types.Part(
36+
text="Write a short, exciting welcome message for a tourist named {name} visiting {city}. Highlight one famous local food."
37+
)
38+
39+
system_content = genai.types.Content(
40+
role="system",
41+
parts=[system_part]
42+
)
43+
44+
user_content = genai.types.Content(
45+
role="user",
46+
parts=[user_part]
47+
)
48+
49+
variables = {
50+
"name": genai.types.Part(text="Roberto"),
51+
"city": genai.types.Part(text="Tijuana")
52+
}
53+
54+
prompt_data_payload = agentplatform.types.PromptData(
55+
model=GEMINI_MODEL,
56+
contents=[user_content],
57+
system_instruction=system_content,
58+
variables=[variables]
59+
)
60+
61+
prompt_config = agentplatform.types.Prompt(
62+
prompt_data=prompt_data_payload
63+
)
64+
65+
managed_prompt = agent_client.prompts.create(
66+
prompt=prompt_config
67+
)
68+
69+
genai_client = genai.Client(
70+
enterprise=True,
71+
project=PROJECT_ID,
72+
location=LOCATION_ID
73+
)
74+
75+
assembled_contents = managed_prompt.assemble_contents()
76+
77+
response = genai_client.models.generate_content(
78+
model=managed_prompt.prompt_data.model,
79+
contents=assembled_contents
80+
)
81+
82+
print(response.text)
83+
"""
84+
def prompt_create() -> genai.types.Prompt:
85+
86+
client = agentplatform.Client(project=PROJECT_ID, location=LOCATION_ID)
87+
88+
# Create local Prompt
89+
agentplatform.types.Prompt
90+
local_prompt = client.prompts.create(
91+
prompt_name="movie-critic",
92+
prompt_data="Compare the movies {movie1} and {movie2}.",
93+
variables=[
94+
{"movie1": "The Lion King", "movie2": "Frozen"},
95+
{"movie1": "Inception", "movie2": "Interstellar"},
96+
],
97+
model_name=GEMINI_MODEL,
98+
system_instruction="You are a movie critic. Answer in a short sentence.",
99+
# generation_config=GenerationConfig, # Optional,
100+
# safety_settings=SafetySetting, # Optional,
101+
)
102+
103+
# Generate content using the assembled prompt for each variable set.
104+
for i in range(len(local_prompt.variables)):
105+
response = local_prompt.generate_content(
106+
contents=local_prompt.assemble_contents(**local_prompt.variables[i])
107+
)
108+
print(response)
109+
110+
# Save a version
111+
prompt1 = prompts.create_version(prompt=local_prompt)
112+
113+
print(prompt1)
114+
115+
# Example response
116+
# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2
117+
# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2
118+
# Created prompt resource with id 12345678910.....
119+
120+
# [END generativeaionvertexai_prompt_template_create_generate_save]
121+
return prompt1
122+
"""
123+
124+
if __name__ == "__main__":
125+
prompt_create_2()

genai/prompts/prompt_delete.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 delete_prompt() -> None:
20+
"""Deletes specified prompt."""
21+
22+
# [START generativeaionvertexai_prompt_delete]
23+
import vertexai
24+
from vertexai.preview.prompts import Prompt
25+
from vertexai.preview import prompts
26+
27+
# Initialize vertexai
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
# Create local Prompt
31+
prompt = Prompt(
32+
prompt_name="movie-critic",
33+
prompt_data="Compare the movies {movie1} and {movie2}.",
34+
variables=[
35+
{"movie1": "The Lion King", "movie2": "Frozen"},
36+
{"movie1": "Inception", "movie2": "Interstellar"},
37+
],
38+
model_name="gemini-2.0-flash-001",
39+
system_instruction="You are a movie critic. Answer in a short sentence.",
40+
41+
)
42+
# Save a version
43+
prompt1 = prompts.create_version(prompt=prompt)
44+
prompt_id = prompt1.prompt_id
45+
46+
# Delete prompt
47+
prompts.delete(prompt_id=prompt_id)
48+
print(f"Deleted prompt with ID: {prompt_id}")
49+
50+
# Example response:
51+
# Deleted prompt resource with id 12345678910
52+
# [END generativeaionvertexai_prompt_delete]
53+
54+
55+
if __name__ == "__main__":
56+
delete_prompt()

genai/prompts/prompt_get.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
from vertexai.preview.prompts import Prompt
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def get_prompt() -> Prompt:
22+
"""Retrieves a prompt that has been saved to the online resource"""
23+
24+
# [START generativeaionvertexai_prompt_template_load_or_retrieve_prompt]
25+
import vertexai
26+
from vertexai.preview.prompts import Prompt
27+
from vertexai.preview import prompts
28+
29+
# Initialize vertexai
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
31+
32+
# Create local Prompt
33+
prompt = Prompt(
34+
prompt_name="meteorologist",
35+
prompt_data="How should I dress for weather in August?",
36+
model_name="gemini-2.0-flash-001",
37+
system_instruction="You are a meteorologist. Answer in a short sentence.",
38+
39+
)
40+
# Save Prompt to online resource.
41+
prompt1 = prompts.create_version(prompt=prompt)
42+
prompt_id = prompt1.prompt_id
43+
44+
# Get prompt
45+
get_prompt = prompts.get(prompt_id=prompt_id)
46+
47+
print(get_prompt)
48+
49+
# Example response
50+
# How should I dress for weather in August?
51+
# [END generativeaionvertexai_prompt_template_load_or_retrieve_prompt]
52+
return get_prompt
53+
54+
55+
if __name__ == "__main__":
56+
get_prompt()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 list_prompt() -> list:
20+
"""Lists the all prompts saved in the current Google Cloud Project"""
21+
22+
# [START generativeaionvertexai_prompt_template_list_prompt]
23+
import vertexai
24+
from vertexai.preview import prompts
25+
26+
# Initialize vertexai
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
# Get prompt a prompt from list
30+
list_prompts_metadata = prompts.list()
31+
32+
print(list_prompts_metadata)
33+
34+
# Example Response:
35+
# [PromptMetadata(display_name='movie-critic', prompt_id='12345678910'), PromptMetadata(display_name='movie-critic-2', prompt_id='12345678910'
36+
# [END generativeaionvertexai_prompt_template_list_prompt]
37+
return list_prompts_metadata
38+
39+
40+
if __name__ == "__main__":
41+
list_prompt()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
17+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
18+
19+
20+
def list_prompt_version() -> list:
21+
"""Displays a specific prompt version from the versions metadata list."""
22+
23+
# [START generativeaionvertexai_prompt_list_prompt_version]
24+
import vertexai
25+
from vertexai.preview.prompts import Prompt
26+
from vertexai.preview import prompts
27+
28+
# Initialize vertexai
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
30+
31+
# Create local Prompt
32+
prompt = Prompt(
33+
prompt_name="zoologist",
34+
prompt_data="Which animal is the fastest on earth?",
35+
model_name="gemini-2.0-flash-001",
36+
system_instruction="You are a zoologist. Answer in a short sentence.",
37+
)
38+
# Save Prompt to online resource.
39+
prompt1 = prompts.create_version(prompt=prompt)
40+
prompt_id = prompt1.prompt_id
41+
42+
# Get prompt a prompt from list
43+
prompt_versions_metadata = prompts.list_versions(prompt_id=prompt_id)
44+
45+
# Get a specific prompt version from the versions metadata list
46+
prompt1 = prompts.get(
47+
prompt_id=prompt_versions_metadata[0].prompt_id,
48+
version_id=prompt_versions_metadata[0].version_id,
49+
)
50+
51+
print(prompt1)
52+
# Example response:
53+
# Which animal is the fastest on earth?
54+
# [END generativeaionvertexai_prompt_list_prompt_version]
55+
return prompt_versions_metadata
56+
57+
58+
if __name__ == "__main__":
59+
list_prompt_version()

0 commit comments

Comments
 (0)