Skip to content

Commit 91e6d7e

Browse files
feat(genai):add new samples for prompt templating (#13007)
* feat(genai):add new samples for prompt templating * feat(genai):update testing file for prompt templating * feat(genai):update requirements file / add sample for list prompts * feat(genai):update testing file / load or retreive sample * feat(genai):update testing file / adding samples * feat(genai):update testing file / renaming samples and truncating function names * feat(genai):update testing file / adding prompt creation for dependencies * feat(genai):update region tag * feat(genai):updating noxfile * feat(genai):updating list_version.py * feat(genai):update testing file / renaming files / changing function names * feat(genai):update hint and return * feat(genai):update hint add export statement * Triggering a change due to pipeline 500 error on last run
1 parent 0571240 commit 91e6d7e

File tree

8 files changed

+374
-1
lines changed

8 files changed

+374
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 prompt_create() -> Prompt:
22+
"""Create a local prompt, generates content and saves prompt"""
23+
24+
# [START generativeaionvertexai_prompt_template_create_generate_save]
25+
import vertexai
26+
from vertexai.preview import prompts
27+
from vertexai.preview.prompts import Prompt
28+
29+
# from vertexai.generative_models import GenerationConfig, SafetySetting # Optional
30+
31+
# Initialize vertexai
32+
vertexai.init(project=PROJECT_ID, location="us-central1")
33+
34+
# Create local Prompt
35+
local_prompt = Prompt(
36+
prompt_name="movie-critic",
37+
prompt_data="Compare the movies {movie1} and {movie2}.",
38+
variables=[
39+
{"movie1": "The Lion King", "movie2": "Frozen"},
40+
{"movie1": "Inception", "movie2": "Interstellar"},
41+
],
42+
model_name="gemini-1.5-pro-002",
43+
system_instruction="You are a movie critic. Answer in a short sentence.",
44+
# generation_config=GenerationConfig, # Optional,
45+
# safety_settings=SafetySetting, # Optional,
46+
)
47+
48+
# Generate content using the assembled prompt for each variable set.
49+
for i in range(len(local_prompt.variables)):
50+
response = local_prompt.generate_content(
51+
contents=local_prompt.assemble_contents(**local_prompt.variables[i])
52+
)
53+
print(response)
54+
55+
# Save a version
56+
prompt1 = prompts.create_version(prompt=local_prompt)
57+
58+
print(prompt1)
59+
60+
# Example response
61+
# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2
62+
# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2
63+
# Created prompt resource with id 12345678910.....
64+
65+
# [END generativeaionvertexai_prompt_template_create_generate_save]
66+
return prompt1
67+
68+
69+
if __name__ == "__main__":
70+
prompt_create()
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-1.5-pro-002",
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()

generative_ai/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-1.5-pro-002",
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-1.5-pro-002",
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()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 restore_prompt_version() -> Prompt:
22+
"""Restores specified version for specified prompt."""
23+
24+
# [START generativeaionvertexai_prompt_restore_version]
25+
import vertexai
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-1.5-pro-002",
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+
# Restore to prompt version id 1 (original)
43+
prompt_version_metadata = prompts.restore_version(prompt_id=prompt_id, version_id="1")
44+
45+
# Fetch the newly restored latest version of the prompt
46+
prompt1 = prompts.get(prompt_id=prompt_version_metadata.prompt_id)
47+
48+
# Example response:
49+
# Restored prompt version 1 under prompt id 12345678910 as version number 2
50+
# [END generativeaionvertexai_prompt_restore_version]
51+
return prompt1
52+
53+
54+
if __name__ == "__main__":
55+
restore_prompt_version()

generative_ai/prompts/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pandas==2.0.3; python_version == '3.8'
33
pandas==2.1.4; python_version > '3.8'
44
pillow==10.3.0; python_version < '3.8'
55
pillow==10.3.0; python_version >= '3.8'
6-
google-cloud-aiplatform[all]==1.69.0
6+
google-cloud-aiplatform[all]==1.74.0
77
sentencepiece==0.2.0
88
google-auth==2.29.0
99
anthropic[vertex]==0.28.0

generative_ai/prompts/test_prompt_template.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,45 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import prompt_create
16+
import prompt_delete
17+
import prompt_get
18+
import prompt_list_prompts
19+
import prompt_list_version
20+
import prompt_restore_version
1521
import prompt_template
1622

1723

1824
def test_prompt_template() -> None:
1925
text = prompt_template.prompt_template_example()
2026
assert len(text) > 2
27+
28+
29+
def test_prompt_create() -> None:
30+
response = prompt_create.prompt_create()
31+
assert response
32+
33+
34+
def test_prompt_list_prompts() -> None:
35+
list_prompts = prompt_list_prompts.list_prompt()
36+
assert list_prompts
37+
38+
39+
def test_prompt_get() -> None:
40+
get_prompt = prompt_get.get_prompt()
41+
assert get_prompt
42+
43+
44+
def test_prompt_list_version() -> None:
45+
list_versions = prompt_list_version.list_prompt_version()
46+
assert list_versions
47+
48+
49+
def test_prompt_delete() -> None:
50+
delete_prompt = prompt_delete.delete_prompt()
51+
assert delete_prompt is None
52+
53+
54+
def test_prompt_restore_version() -> None:
55+
prompt1 = prompt_restore_version.restore_prompt_version()
56+
assert prompt1

0 commit comments

Comments
 (0)