|
| 1 | +# Copyright 2025 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 | +# http://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 | +# pylint: disable=protected-access,bad-continuation,missing-function-docstring |
| 16 | + |
| 17 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 18 | +from vertexai._genai import types |
| 19 | +from google.genai import types as genai_types |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +TEST_PROMPT_DATASET_ID = "8005484238453342208" |
| 24 | +TEST_VARIABLES = [ |
| 25 | + {"name": genai_types.Part(text="Alice")}, |
| 26 | + {"name": genai_types.Part(text="Bob")}, |
| 27 | +] |
| 28 | +TEST_RESPONSE_SCHEMA = { |
| 29 | + "type": "object", |
| 30 | + "properties": {"response": {"type": "string"}}, |
| 31 | +} |
| 32 | +TEST_PROMPT = types.Prompt( |
| 33 | + prompt_data=types.PromptData( |
| 34 | + contents=[ |
| 35 | + genai_types.Content( |
| 36 | + role="user", |
| 37 | + parts=[genai_types.Part(text="Hello, {name}! How are you?")], |
| 38 | + ) |
| 39 | + ], |
| 40 | + safety_settings=[ |
| 41 | + genai_types.SafetySetting( |
| 42 | + category="HARM_CATEGORY_DANGEROUS_CONTENT", |
| 43 | + threshold="BLOCK_MEDIUM_AND_ABOVE", |
| 44 | + method="SEVERITY", |
| 45 | + ), |
| 46 | + ], |
| 47 | + generation_config=genai_types.GenerationConfig( |
| 48 | + temperature=0.1, |
| 49 | + candidate_count=1, |
| 50 | + top_p=0.95, |
| 51 | + top_k=40, |
| 52 | + response_modalities=["TEXT"], |
| 53 | + response_schema=TEST_RESPONSE_SCHEMA, |
| 54 | + ), |
| 55 | + system_instruction=genai_types.Content( |
| 56 | + parts=[genai_types.Part(text="Please answer in a short sentence.")] |
| 57 | + ), |
| 58 | + tools=[ |
| 59 | + genai_types.Tool( |
| 60 | + google_search_retrieval=genai_types.GoogleSearchRetrieval( |
| 61 | + dynamic_retrieval_config=genai_types.DynamicRetrievalConfig( |
| 62 | + mode="MODE_DYNAMIC" |
| 63 | + ) |
| 64 | + ) |
| 65 | + ), |
| 66 | + ], |
| 67 | + tool_config=genai_types.ToolConfig( |
| 68 | + retrieval_config=genai_types.RetrievalConfig( |
| 69 | + lat_lng=genai_types.LatLng(latitude=37.7749, longitude=-122.4194) |
| 70 | + ) |
| 71 | + ), |
| 72 | + model="gemini-2.0-flash-001", |
| 73 | + variables=TEST_VARIABLES, |
| 74 | + ), |
| 75 | +) |
| 76 | +TEST_CREATE_PROMPT_CONFIG = types.CreatePromptConfig( |
| 77 | + prompt_display_name="my_prompt", |
| 78 | +) |
| 79 | + |
| 80 | +TEST_CREATE_PROMPT_VERSION_CONFIG = types.CreatePromptVersionConfig( |
| 81 | + version_display_name="my_version", |
| 82 | +) |
| 83 | + |
| 84 | + |
| 85 | +def test_update_creates_new_version(client): |
| 86 | + updated_prompt = TEST_PROMPT.model_copy(deep=True) |
| 87 | + updated_prompt.prompt_data.contents[0].parts[0].text = "Is this Alice?" |
| 88 | + |
| 89 | + prompt_resource = client.prompts.update( |
| 90 | + prompt_id=TEST_PROMPT_DATASET_ID, |
| 91 | + prompt=updated_prompt, |
| 92 | + config=types.UpdatePromptConfig( |
| 93 | + version_display_name="my_version", |
| 94 | + ), |
| 95 | + ) |
| 96 | + assert isinstance(prompt_resource, types.Prompt) |
| 97 | + assert isinstance(prompt_resource.dataset, types.Dataset) |
| 98 | + assert isinstance(prompt_resource.dataset_version, types.DatasetVersion) |
| 99 | + assert prompt_resource.prompt_data.contents[0].parts[0].text == "Is this Alice?" |
| 100 | + |
| 101 | + |
| 102 | +pytestmark = pytest_helper.setup( |
| 103 | + file=__file__, |
| 104 | + globals_for_file=globals(), |
| 105 | + test_method="prompts.update", |
| 106 | +) |
| 107 | + |
| 108 | +pytest_plugins = ("pytest_asyncio",) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_update_async(client): |
| 113 | + prompt_resource = await client.aio.prompts.create( |
| 114 | + prompt=TEST_PROMPT.model_dump(), |
| 115 | + config=TEST_CREATE_PROMPT_CONFIG.model_dump(), |
| 116 | + ) |
| 117 | + assert isinstance(prompt_resource, types.Prompt) |
| 118 | + assert isinstance(prompt_resource.dataset, types.Dataset) |
0 commit comments