Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 228 additions & 20 deletions google/genai/_live_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@
from ._common import set_value_by_path as setv


def _ThinkingLevel_to_vertex_enum_validate(enum_value: Any) -> None:
if enum_value in set(['MINIMAL', 'MEDIUM']):
raise ValueError(f'{enum_value} enum value is not supported in Vertex AI.')


def _AuthConfig_to_mldev(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['api_key']) is not None:
setv(to_object, ['apiKey'], getv(from_object, ['api_key']))

if getv(from_object, ['api_key_config']) is not None:
raise ValueError('api_key_config parameter is not supported in Gemini API.')

if getv(from_object, ['auth_type']) is not None:
raise ValueError('auth_type parameter is not supported in Gemini API.')

if getv(from_object, ['google_service_account_config']) is not None:
raise ValueError(
'google_service_account_config parameter is not supported in Gemini'
' API.'
)

if getv(from_object, ['http_basic_auth_config']) is not None:
raise ValueError(
'http_basic_auth_config parameter is not supported in Gemini API.'
)

if getv(from_object, ['oauth_config']) is not None:
raise ValueError('oauth_config parameter is not supported in Gemini API.')

if getv(from_object, ['oidc_config']) is not None:
raise ValueError('oidc_config parameter is not supported in Gemini API.')

return to_object


def _Blob_to_mldev(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
Expand Down Expand Up @@ -61,6 +100,27 @@ def _Content_to_mldev(
return to_object


def _Content_to_vertex(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['parts']) is not None:
setv(
to_object,
['parts'],
[
_Part_to_vertex(item, to_object)
for item in getv(from_object, ['parts'])
],
)

if getv(from_object, ['role']) is not None:
setv(to_object, ['role'], getv(from_object, ['role']))

return to_object


def _FileData_to_mldev(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
Expand Down Expand Up @@ -235,7 +295,13 @@ def _GenerationConfig_to_vertex(
setv(to_object, ['temperature'], getv(from_object, ['temperature']))

if getv(from_object, ['thinking_config']) is not None:
setv(to_object, ['thinkingConfig'], getv(from_object, ['thinking_config']))
setv(
to_object,
['thinkingConfig'],
_ThinkingConfig_to_vertex(
getv(from_object, ['thinking_config']), to_object
),
)

if getv(from_object, ['top_k']) is not None:
setv(to_object, ['topK'], getv(from_object, ['top_k']))
Expand All @@ -257,7 +323,11 @@ def _GoogleMaps_to_mldev(
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['auth_config']) is not None:
raise ValueError('auth_config parameter is not supported in Gemini API.')
setv(
to_object,
['authConfig'],
_AuthConfig_to_mldev(getv(from_object, ['auth_config']), to_object),
)

if getv(from_object, ['enable_widget']) is not None:
setv(to_object, ['enableWidget'], getv(from_object, ['enable_widget']))
Expand All @@ -270,14 +340,14 @@ def _GoogleSearch_to_mldev(
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['exclude_domains']) is not None:
if getv(from_object, ['blocking_confidence']) is not None:
raise ValueError(
'exclude_domains parameter is not supported in Gemini API.'
'blocking_confidence parameter is not supported in Gemini API.'
)

if getv(from_object, ['blocking_confidence']) is not None:
if getv(from_object, ['exclude_domains']) is not None:
raise ValueError(
'blocking_confidence parameter is not supported in Gemini API.'
'exclude_domains parameter is not supported in Gemini API.'
)

if getv(from_object, ['time_range_filter']) is not None:
Expand Down Expand Up @@ -309,6 +379,27 @@ def _LiveClientContent_to_mldev(
return to_object


def _LiveClientContent_to_vertex(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['turns']) is not None:
setv(
to_object,
['turns'],
[
_Content_to_vertex(item, to_object)
for item in getv(from_object, ['turns'])
],
)

if getv(from_object, ['turn_complete']) is not None:
setv(to_object, ['turnComplete'], getv(from_object, ['turn_complete']))

return to_object


def _LiveClientMessage_to_mldev(
api_client: BaseApiClient,
from_object: Union[dict[str, Any], object],
Expand Down Expand Up @@ -364,7 +455,13 @@ def _LiveClientMessage_to_vertex(
)

if getv(from_object, ['client_content']) is not None:
setv(to_object, ['clientContent'], getv(from_object, ['client_content']))
setv(
to_object,
['clientContent'],
_LiveClientContent_to_vertex(
getv(from_object, ['client_content']), to_object
),
)

if getv(from_object, ['realtime_input']) is not None:
setv(
Expand Down Expand Up @@ -558,7 +655,9 @@ def _LiveClientSetup_to_vertex(
setv(
to_object,
['systemInstruction'],
t.t_content(getv(from_object, ['system_instruction'])),
_Content_to_vertex(
t.t_content(getv(from_object, ['system_instruction'])), to_object
),
)

if getv(from_object, ['tools']) is not None:
Expand Down Expand Up @@ -843,7 +942,9 @@ def _LiveConnectConfig_to_vertex(
setv(
parent_object,
['setup', 'generationConfig', 'thinkingConfig'],
getv(from_object, ['thinking_config']),
_ThinkingConfig_to_vertex(
getv(from_object, ['thinking_config']), to_object
),
)

if getv(from_object, ['enable_affective_dialog']) is not None:
Expand All @@ -857,7 +958,9 @@ def _LiveConnectConfig_to_vertex(
setv(
parent_object,
['setup', 'systemInstruction'],
t.t_content(getv(from_object, ['system_instruction'])),
_Content_to_vertex(
t.t_content(getv(from_object, ['system_instruction'])), to_object
),
)

if getv(from_object, ['tools']) is not None:
Expand Down Expand Up @@ -1265,6 +1368,67 @@ def _Part_to_mldev(
if getv(from_object, ['video_metadata']) is not None:
setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))

if getv(from_object, ['part_metadata']) is not None:
setv(to_object, ['partMetadata'], getv(from_object, ['part_metadata']))

return to_object


def _Part_to_vertex(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['media_resolution']) is not None:
setv(
to_object, ['mediaResolution'], getv(from_object, ['media_resolution'])
)

if getv(from_object, ['code_execution_result']) is not None:
setv(
to_object,
['codeExecutionResult'],
getv(from_object, ['code_execution_result']),
)

if getv(from_object, ['executable_code']) is not None:
setv(to_object, ['executableCode'], getv(from_object, ['executable_code']))

if getv(from_object, ['file_data']) is not None:
setv(to_object, ['fileData'], getv(from_object, ['file_data']))

if getv(from_object, ['function_call']) is not None:
setv(to_object, ['functionCall'], getv(from_object, ['function_call']))

if getv(from_object, ['function_response']) is not None:
setv(
to_object,
['functionResponse'],
getv(from_object, ['function_response']),
)

if getv(from_object, ['inline_data']) is not None:
setv(to_object, ['inlineData'], getv(from_object, ['inline_data']))

if getv(from_object, ['text']) is not None:
setv(to_object, ['text'], getv(from_object, ['text']))

if getv(from_object, ['thought']) is not None:
setv(to_object, ['thought'], getv(from_object, ['thought']))

if getv(from_object, ['thought_signature']) is not None:
setv(
to_object,
['thoughtSignature'],
getv(from_object, ['thought_signature']),
)

if getv(from_object, ['video_metadata']) is not None:
setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))

if getv(from_object, ['part_metadata']) is not None:
raise ValueError('part_metadata parameter is not supported in Vertex AI.')

return to_object


Expand All @@ -1282,6 +1446,28 @@ def _SessionResumptionConfig_to_mldev(
return to_object


def _ThinkingConfig_to_vertex(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
to_object: dict[str, Any] = {}
if getv(from_object, ['include_thoughts']) is not None:
setv(
to_object, ['includeThoughts'], getv(from_object, ['include_thoughts'])
)

if getv(from_object, ['thinking_budget']) is not None:
setv(to_object, ['thinkingBudget'], getv(from_object, ['thinking_budget']))

if getv(from_object, ['thinking_level']) is not None:
_ThinkingLevel_to_vertex_enum_validate(
getv(from_object, ['thinking_level'])
)
setv(to_object, ['thinkingLevel'], getv(from_object, ['thinking_level']))

return to_object


def _Tool_to_mldev(
from_object: Union[dict[str, Any], object],
parent_object: Optional[dict[str, Any]] = None,
Expand All @@ -1296,6 +1482,13 @@ def _Tool_to_mldev(
if getv(from_object, ['file_search']) is not None:
setv(to_object, ['fileSearch'], getv(from_object, ['file_search']))

if getv(from_object, ['google_maps']) is not None:
setv(
to_object,
['googleMaps'],
_GoogleMaps_to_mldev(getv(from_object, ['google_maps']), to_object),
)

if getv(from_object, ['code_execution']) is not None:
setv(to_object, ['codeExecution'], getv(from_object, ['code_execution']))

Expand All @@ -1311,13 +1504,6 @@ def _Tool_to_mldev(
[item for item in getv(from_object, ['function_declarations'])],
)

if getv(from_object, ['google_maps']) is not None:
setv(
to_object,
['googleMaps'],
_GoogleMaps_to_mldev(getv(from_object, ['google_maps']), to_object),
)

if getv(from_object, ['google_search']) is not None:
setv(
to_object,
Expand All @@ -1332,9 +1518,21 @@ def _Tool_to_mldev(
getv(from_object, ['google_search_retrieval']),
)

if getv(from_object, ['parallel_ai_search']) is not None:
raise ValueError(
'parallel_ai_search parameter is not supported in Gemini API.'
)

if getv(from_object, ['url_context']) is not None:
setv(to_object, ['urlContext'], getv(from_object, ['url_context']))

if getv(from_object, ['mcp_servers']) is not None:
setv(
to_object,
['mcpServers'],
[item for item in getv(from_object, ['mcp_servers'])],
)

return to_object


Expand All @@ -1352,6 +1550,9 @@ def _Tool_to_vertex(
if getv(from_object, ['file_search']) is not None:
raise ValueError('file_search parameter is not supported in Vertex AI.')

if getv(from_object, ['google_maps']) is not None:
setv(to_object, ['googleMaps'], getv(from_object, ['google_maps']))

if getv(from_object, ['code_execution']) is not None:
setv(to_object, ['codeExecution'], getv(from_object, ['code_execution']))

Expand All @@ -1372,9 +1573,6 @@ def _Tool_to_vertex(
],
)

if getv(from_object, ['google_maps']) is not None:
setv(to_object, ['googleMaps'], getv(from_object, ['google_maps']))

if getv(from_object, ['google_search']) is not None:
setv(to_object, ['googleSearch'], getv(from_object, ['google_search']))

Expand All @@ -1385,9 +1583,19 @@ def _Tool_to_vertex(
getv(from_object, ['google_search_retrieval']),
)

if getv(from_object, ['parallel_ai_search']) is not None:
setv(
to_object,
['parallelAiSearch'],
getv(from_object, ['parallel_ai_search']),
)

if getv(from_object, ['url_context']) is not None:
setv(to_object, ['urlContext'], getv(from_object, ['url_context']))

if getv(from_object, ['mcp_servers']) is not None:
raise ValueError('mcp_servers parameter is not supported in Vertex AI.')

return to_object


Expand Down
Loading