Skip to content

Commit f5e51eb

Browse files
feat(genai): add prompt template example (#12764)
* feat(genai): add prompt template example * feat(genai): fix spelling, restructure comments, fix syntax * feat(genai): update comment, restructure variables and prompt assembly and fix syntax * feat(genai): update comment and fix syntax * feat(genai): update example comment * feat(genai): renaming def / adding testing file * feat(genai): updating commenting and logic on sample * feat(genai): fixing list type and updating list length based on number of items in list to be more specific
1 parent 3477e6a commit f5e51eb

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.generative_models import GenerationResponse
17+
18+
19+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
20+
21+
22+
def prompt_template_example() -> list[GenerationResponse]:
23+
"""Build a parameterized prompt template to generate content with multiple variable sets"""
24+
25+
# [START generativeaionvertexai_prompt_template]
26+
import vertexai
27+
from vertexai.preview.prompts import Prompt
28+
29+
# Initialize vertexai
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
31+
32+
variables = [
33+
{"animal": "Eagles", "activity": "eat berries"},
34+
{"animal": "Coyotes", "activity": "jump"},
35+
{"animal": "Squirrels", "activity": "fly"}
36+
]
37+
38+
# define prompt template
39+
prompt = Prompt(
40+
prompt_data="Do {animal} {activity}?",
41+
model_name="gemini-1.5-flash-002",
42+
variables=variables,
43+
system_instruction="You are a helpful zoologist"
44+
# generation_config=generation_config, # Optional
45+
# safety_settings=safety_settings, # Optional
46+
)
47+
48+
# Generates content using the assembled prompt.
49+
responses = []
50+
for variable_set in prompt.variables:
51+
response = prompt.generate_content(
52+
contents=prompt.assemble_contents(**variable_set)
53+
)
54+
responses.append(response)
55+
56+
for response in responses:
57+
print(response.text, end="")
58+
59+
# Example response
60+
# Assembled prompt replacing: 1 instances of variable animal, 1 instances of variable activity
61+
# Eagles are primarily carnivorous. While they might *accidentally* ingest a berry......
62+
# [END generativeaionvertexai_prompt_template]
63+
return responses
64+
65+
66+
if __name__ == "__main__":
67+
prompt_template_example()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
import prompt_template
16+
17+
18+
def test_prompt_template() -> None:
19+
text = prompt_template.prompt_template_example()
20+
assert len(text) > 2

0 commit comments

Comments
 (0)