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