|
| 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 | + |
| 16 | +# [START aiplatform_genai_prompt_template_create] |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +import agentplatform |
| 21 | + |
| 22 | +from google import genai |
| 23 | + |
| 24 | + |
| 25 | +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 26 | +LOCATION_ID = os.getenv("LOCATION_ID", "us-central1") |
| 27 | +GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-flash") |
| 28 | + |
| 29 | + |
| 30 | +def prompt_create_2() -> None: |
| 31 | + agent_client = agentplatform.Client(project=PROJECT_ID, location=LOCATION_ID) |
| 32 | + |
| 33 | + system_part = genai.types.Part(text="You are a knowledgeable local tour guide.") |
| 34 | + |
| 35 | + user_part = genai.types.Part( |
| 36 | + text="Write a short, exciting welcome message for a tourist named {name} visiting {city}. Highlight one famous local food." |
| 37 | + ) |
| 38 | + |
| 39 | + system_content = genai.types.Content( |
| 40 | + role="system", |
| 41 | + parts=[system_part] |
| 42 | + ) |
| 43 | + |
| 44 | + user_content = genai.types.Content( |
| 45 | + role="user", |
| 46 | + parts=[user_part] |
| 47 | + ) |
| 48 | + |
| 49 | + variables = { |
| 50 | + "name": genai.types.Part(text="Roberto"), |
| 51 | + "city": genai.types.Part(text="Tijuana") |
| 52 | + } |
| 53 | + |
| 54 | + prompt_data_payload = agentplatform.types.PromptData( |
| 55 | + model=GEMINI_MODEL, |
| 56 | + contents=[user_content], |
| 57 | + system_instruction=system_content, |
| 58 | + variables=[variables] |
| 59 | + ) |
| 60 | + |
| 61 | + prompt_config = agentplatform.types.Prompt( |
| 62 | + prompt_data=prompt_data_payload |
| 63 | + ) |
| 64 | + |
| 65 | + managed_prompt = agent_client.prompts.create( |
| 66 | + prompt=prompt_config |
| 67 | + ) |
| 68 | + |
| 69 | + genai_client = genai.Client( |
| 70 | + enterprise=True, |
| 71 | + project=PROJECT_ID, |
| 72 | + location=LOCATION_ID |
| 73 | + ) |
| 74 | + |
| 75 | + assembled_contents = managed_prompt.assemble_contents() |
| 76 | + |
| 77 | + response = genai_client.models.generate_content( |
| 78 | + model=managed_prompt.prompt_data.model, |
| 79 | + contents=assembled_contents |
| 80 | + ) |
| 81 | + |
| 82 | + print(response.text) |
| 83 | +""" |
| 84 | +def prompt_create() -> genai.types.Prompt: |
| 85 | +
|
| 86 | + client = agentplatform.Client(project=PROJECT_ID, location=LOCATION_ID) |
| 87 | +
|
| 88 | + # Create local Prompt |
| 89 | + agentplatform.types.Prompt |
| 90 | + local_prompt = client.prompts.create( |
| 91 | + prompt_name="movie-critic", |
| 92 | + prompt_data="Compare the movies {movie1} and {movie2}.", |
| 93 | + variables=[ |
| 94 | + {"movie1": "The Lion King", "movie2": "Frozen"}, |
| 95 | + {"movie1": "Inception", "movie2": "Interstellar"}, |
| 96 | + ], |
| 97 | + model_name=GEMINI_MODEL, |
| 98 | + system_instruction="You are a movie critic. Answer in a short sentence.", |
| 99 | + # generation_config=GenerationConfig, # Optional, |
| 100 | + # safety_settings=SafetySetting, # Optional, |
| 101 | + ) |
| 102 | +
|
| 103 | + # Generate content using the assembled prompt for each variable set. |
| 104 | + for i in range(len(local_prompt.variables)): |
| 105 | + response = local_prompt.generate_content( |
| 106 | + contents=local_prompt.assemble_contents(**local_prompt.variables[i]) |
| 107 | + ) |
| 108 | + print(response) |
| 109 | +
|
| 110 | + # Save a version |
| 111 | + prompt1 = prompts.create_version(prompt=local_prompt) |
| 112 | +
|
| 113 | + print(prompt1) |
| 114 | +
|
| 115 | + # Example response |
| 116 | + # Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2 |
| 117 | + # Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2 |
| 118 | + # Created prompt resource with id 12345678910..... |
| 119 | +
|
| 120 | + # [END generativeaionvertexai_prompt_template_create_generate_save] |
| 121 | + return prompt1 |
| 122 | +""" |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + prompt_create_2() |
0 commit comments