Skip to content

Commit 5c41e47

Browse files
Thoughtseize1holtskinner
authored andcommitted
refactor: (GenAI) Reorganized System Instructions Sample (Group C) (GoogleCloudPlatform#12615)
* Created new System Instructions folder and new Sample with test. * Update generative_ai/system_instructions/system_instructions_example.py Co-authored-by: Holt Skinner <[email protected]> --------- Co-authored-by: Holt Skinner <[email protected]>
1 parent 683f296 commit 5c41e47

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.environ["GOOGLE_CLOUD_PROJECT"]
17+
18+
19+
def set_system_instruction() -> str:
20+
# [START generativeaionvertexai_gemini_system_instruction]
21+
import vertexai
22+
23+
from vertexai.generative_models import 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+
model = GenerativeModel(
30+
model_name="gemini-1.5-flash-001",
31+
system_instruction=[
32+
"You are a helpful language translator.",
33+
"Your mission is to translate text in English to French.",
34+
],
35+
)
36+
37+
prompt = """
38+
User input: I like bagels.
39+
Answer:
40+
"""
41+
response = model.generate_content([prompt])
42+
print(response.text)
43+
# Example response:
44+
# J'aime les bagels.
45+
46+
# [END generativeaionvertexai_gemini_system_instruction]
47+
return response.text
48+
49+
50+
if __name__ == "__main__":
51+
set_system_instruction()
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 system_instructions_example
16+
17+
18+
def test_set_system_instruction() -> None:
19+
text = system_instructions_example.set_system_instruction()
20+
assert len(text) > 0

0 commit comments

Comments
 (0)