Skip to content

Commit 58a30b8

Browse files
sararobcopybara-github
authored andcommitted
feat: GenAI SDK client - Update client.prompts.create to create a prompt and prompt version for a prompt. Deprecate client.prompts.create_version in favor of create()
PiperOrigin-RevId: 889955089
1 parent 0e5037d commit 58a30b8

5 files changed

Lines changed: 370 additions & 64 deletions

File tree

tests/unit/vertexai/genai/replays/test_create_prompt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ def test_create(client):
147147
assert isinstance(prompt_resource.dataset, types.Dataset)
148148

149149

150+
def test_create_version_updated_e2e(client):
151+
prompt_resource = client.prompts.create_version(
152+
prompt=TEST_PROMPT,
153+
config=TEST_CREATE_PROMPT_CONFIG,
154+
)
155+
assert isinstance(prompt_resource, types.Prompt)
156+
assert isinstance(prompt_resource.dataset, types.Dataset)
157+
assert isinstance(prompt_resource.dataset_version, types.DatasetVersion)
158+
159+
150160
def test_create_e2e(client):
151161
prompt_resource = client.prompts.create(
152162
prompt=TEST_PROMPT,
@@ -361,3 +371,14 @@ async def test_create_version_async(client):
361371
prompt_version_resource.prompt_data.contents[0].parts[0].text
362372
== "Is this Alice?"
363373
)
374+
375+
376+
@pytest.mark.asyncio
377+
async def test_create_version_updated_e2e_async(client):
378+
prompt_resource = await client.aio.prompts.create_version(
379+
prompt=TEST_PROMPT,
380+
config=TEST_CREATE_PROMPT_CONFIG,
381+
)
382+
assert isinstance(prompt_resource, types.Prompt)
383+
assert isinstance(prompt_resource.dataset, types.Dataset)
384+
assert isinstance(prompt_resource.dataset_version, types.DatasetVersion)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)