|
| 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() |
0 commit comments