Skip to content

Commit 4899bf0

Browse files
cleop-googlecopybara-github
authored andcommitted
feat: GenAI SDK client(multimodal) - Add single_turn_template helper to GeminiRequestReadConfig.
PiperOrigin-RevId: 890422497
1 parent a8085e5 commit 4899bf0

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

tests/unit/vertexai/genai/test_multimodal_datasets_genai.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from vertexai._genai import _datasets_utils
1919
from vertexai._genai import types
20+
from google.genai import types as genai_types
2021
import pytest
2122

2223

@@ -155,3 +156,63 @@ def test_to_bigframes(self, mock_import_bigframes):
155156
mock_import_bigframes.return_value.pandas.read_gbq_table.assert_called_once_with(
156157
"project.dataset.table"
157158
)
159+
160+
161+
class TestGeminiRequestReadConfig:
162+
def test_single_turn_template(self):
163+
read_config = types.GeminiRequestReadConfig.single_turn_template(
164+
model="gemini-1.5-flash",
165+
prompt="test_prompt",
166+
response="test_response",
167+
system_instruction="test_system_instruction",
168+
cached_content="test_cached_content",
169+
tools=[{"function_declarations": [{"name": "test_tool"}]}],
170+
tool_config={"function_calling_config": {"mode": "ANY"}},
171+
safety_settings=[{"category": "HARM_CATEGORY_DANGEROUS_CONTENT"}],
172+
generation_config={"temperature": 0.5},
173+
field_mapping={"test_placeholder": "test_column"},
174+
)
175+
176+
expected_read_config = types.GeminiRequestReadConfig(
177+
template_config=types.GeminiTemplateConfig(
178+
gemini_example=types.GeminiExample(
179+
model="gemini-1.5-flash",
180+
contents=[
181+
genai_types.Content(
182+
role="user",
183+
parts=[genai_types.Part.from_text(text="test_prompt")],
184+
),
185+
genai_types.Content(
186+
role="model",
187+
parts=[genai_types.Part.from_text(text="test_response")],
188+
),
189+
],
190+
system_instruction=genai_types.Content(
191+
parts=[
192+
genai_types.Part.from_text(text="test_system_instruction")
193+
],
194+
),
195+
cached_content="test_cached_content",
196+
tools=[
197+
genai_types.Tool(
198+
function_declarations=[
199+
genai_types.FunctionDeclaration(name="test_tool")
200+
]
201+
)
202+
],
203+
tool_config=genai_types.ToolConfig(
204+
function_calling_config=genai_types.FunctionCallingConfig(
205+
mode="ANY"
206+
)
207+
),
208+
safety_settings=[
209+
genai_types.SafetySetting(
210+
category="HARM_CATEGORY_DANGEROUS_CONTENT"
211+
)
212+
],
213+
generation_config=genai_types.GenerationConfig(temperature=0.5),
214+
),
215+
field_mapping={"test_placeholder": "test_column"},
216+
),
217+
)
218+
assert read_config == expected_read_config

vertexai/_genai/types/common.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12279,6 +12279,92 @@ class GeminiRequestReadConfig(_common.BaseModel):
1227912279
description="""Column name in the underlying BigQuery table that contains already fully assembled Gemini requests.""",
1228012280
)
1228112281

12282+
@classmethod
12283+
def single_turn_template(
12284+
cls,
12285+
*,
12286+
prompt: str,
12287+
response: Optional[str] = None,
12288+
system_instruction: Optional[str] = None,
12289+
model: Optional[str] = None,
12290+
cached_content: Optional[str] = None,
12291+
tools: Optional[list[Union[genai_types.Tool, dict[str, Any]]]] = None,
12292+
tool_config: Optional[Union[genai_types.ToolConfig, dict[str, Any]]] = None,
12293+
safety_settings: Optional[
12294+
list[Union[genai_types.SafetySetting, dict[str, Any]]]
12295+
] = None,
12296+
generation_config: Optional[
12297+
Union[genai_types.GenerationConfig, dict[str, Any]]
12298+
] = None,
12299+
field_mapping: Optional[dict[str, str]] = None,
12300+
) -> "GeminiRequestReadConfig":
12301+
"""Constructs a GeminiRequestReadConfig object for single-turn cases.
12302+
12303+
Example:
12304+
read_config = GeminiRequestReadConfig.single_turn_template(
12305+
prompt="Which flower is this {flower_image}?",
12306+
response="This is a {label}.",
12307+
system_instruction="You are a botanical classifier."
12308+
)
12309+
12310+
Args:
12311+
prompt: Required. User input.
12312+
response: Optional. Model response to user input.
12313+
system_instruction: Optional. System instructions for the model.
12314+
model: Optional. The model to use for the GeminiExample.
12315+
cached_content: Optional. The cached content to use for the GeminiExample.
12316+
tools: Optional. The tools to use for the GeminiExample.
12317+
tool_config: Optional. The tool config to use for the GeminiExample.
12318+
safety_settings: Optional. The safety settings to use for the GeminiExample.
12319+
generation_config: Optional. The generation config to use for the GeminiExample.
12320+
field_mapping: Optional. Mapping of placeholders to dataset columns.
12321+
12322+
Returns:
12323+
A GeminiRequestReadConfig object.
12324+
"""
12325+
contents = []
12326+
contents.append(
12327+
genai_types.Content(
12328+
role="user",
12329+
parts=[
12330+
genai_types.Part.from_text(text=prompt),
12331+
],
12332+
)
12333+
)
12334+
if response:
12335+
contents.append(
12336+
genai_types.Content(
12337+
role="model",
12338+
parts=[
12339+
genai_types.Part.from_text(text=response),
12340+
],
12341+
)
12342+
)
12343+
12344+
system_instruction_content = None
12345+
if system_instruction:
12346+
system_instruction_content = genai_types.Content(
12347+
parts=[
12348+
genai_types.Part.from_text(text=system_instruction),
12349+
],
12350+
)
12351+
12352+
return cls(
12353+
template_config=GeminiTemplateConfig(
12354+
gemini_example=GeminiExample(
12355+
model=model,
12356+
contents=contents,
12357+
system_instruction=system_instruction_content,
12358+
cached_content=cached_content,
12359+
tools=tools,
12360+
tool_config=tool_config,
12361+
safety_settings=safety_settings,
12362+
generation_config=generation_config,
12363+
),
12364+
field_mapping=field_mapping,
12365+
),
12366+
)
12367+
1228212368

1228312369
class GeminiRequestReadConfigDict(TypedDict, total=False):
1228412370
"""Represents the config for reading Gemini requests."""

0 commit comments

Comments
 (0)