Skip to content

Commit 751cb18

Browse files
jsondaicopybara-github
authored andcommitted
feat: GenAI Client(evals) - Add support for Vertex Model Garden MaaS models.
PiperOrigin-RevId: 806024875
1 parent cdd73d2 commit 751cb18

File tree

3 files changed

+62
-24
lines changed

3 files changed

+62
-24
lines changed

tests/unit/vertexai/genai/test_evals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ def test_run_inference_with_litellm_import_error(self, mock_api_client_fixture):
11021102
prompt_df = pd.DataFrame([{"prompt": "test"}])
11031103
with pytest.raises(
11041104
ImportError,
1105-
match="The 'litellm' library is required to use third-party models",
1105+
match="The 'litellm' library is required to use this model.",
11061106
):
11071107
evals_module.run_inference(model="gpt-4o", src=prompt_df)
11081108

vertexai/_genai/_evals_common.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pandas as pd
3030
from tqdm import tqdm
3131

32+
from . import _evals_constant
3233
from . import _evals_data_converters
3334
from . import _evals_metric_handlers
3435
from . import _evals_utils
@@ -370,6 +371,14 @@ def _run_litellm_inference(
370371
return responses
371372

372373

374+
def _is_litellm_vertex_maas_model(model: str) -> bool:
375+
"""Checks if the model is a Vertex MAAS model to be handled by LiteLLM."""
376+
return any(
377+
model.startswith(prefix)
378+
for prefix in _evals_constant.SUPPORTED_VERTEX_MAAS_MODEL_PREFIXES
379+
)
380+
381+
373382
def _is_litellm_model(model: str) -> bool:
374383
"""Checks if the model name corresponds to a valid LiteLLM model name."""
375384
return model in litellm.utils.get_valid_models(model)
@@ -431,47 +440,64 @@ def _run_inference_internal(
431440
}
432441
processed_responses.append(json.dumps(error_payload))
433442
responses = processed_responses
443+
elif callable(model):
444+
logger.info("Running inference with custom callable function.")
445+
custom_responses_raw = _run_custom_inference(
446+
model_fn=model, prompt_dataset=prompt_dataset
447+
)
448+
processed_custom_responses = []
449+
for resp_item in custom_responses_raw:
450+
if isinstance(resp_item, str):
451+
processed_custom_responses.append(resp_item)
452+
elif isinstance(resp_item, dict) and "error" in resp_item:
453+
processed_custom_responses.append(json.dumps(resp_item))
454+
else:
455+
try:
456+
processed_custom_responses.append(json.dumps(resp_item))
457+
except TypeError:
458+
processed_custom_responses.append(str(resp_item))
459+
responses = processed_custom_responses
434460
elif isinstance(model, str):
435461
if litellm is None:
436462
raise ImportError(
437-
"The 'litellm' library is required to use third-party models."
463+
"The 'litellm' library is required to use this model."
438464
" Please install it using 'pip install"
439465
" google-cloud-aiplatform[evaluation]'."
440466
)
441-
if _is_litellm_model(model):
442-
logger.info("Running inference with LiteLLM for model: %s", model)
443-
raw_responses = _run_litellm_inference( # type: ignore[assignment]
444-
model=model, prompt_dataset=prompt_dataset
467+
468+
processed_model_id = model
469+
if model.startswith("vertex_ai/"):
470+
# Already correctly prefixed for LiteLLM's Vertex AI provider
471+
pass
472+
elif _is_litellm_vertex_maas_model(model):
473+
processed_model_id = f"vertex_ai/{model}"
474+
logger.info(
475+
"Detected Vertex AI Model Garden managed MaaS model. "
476+
"Using LiteLLM ID: %s",
477+
processed_model_id,
445478
)
446-
responses = [json.dumps(resp) for resp in raw_responses]
479+
elif _is_litellm_model(model):
480+
# Other LiteLLM supported model
481+
logger.info("Running inference with LiteLLM for model: %s", model)
447482
else:
483+
# Unsupported model string
448484
raise TypeError(
449485
f"Unsupported string model name: {model}. Expecting a Gemini model"
450-
" name (e.g., 'gemini-2.5-pro', 'projects/.../models/...') or a"
486+
" name (e.g., 'gemini-1.5-pro', 'projects/.../models/...') or a"
451487
" LiteLLM supported model name (e.g., 'openai/gpt-4o')."
452488
" If using a third-party model via LiteLLM, ensure the"
453489
" necessary environment variables are set (e.g., for OpenAI:"
454490
" `os.environ['OPENAI_API_KEY'] = 'Your API Key'`). See"
455491
" LiteLLM documentation for details:"
456492
" https://docs.litellm.ai/docs/set_keys#environment-variables"
457493
)
458-
elif callable(model):
459-
logger.info("Running inference with custom callable function.")
460-
custom_responses_raw = _run_custom_inference(
461-
model_fn=model, prompt_dataset=prompt_dataset
494+
495+
logger.info("Running inference via LiteLLM for model: %s", processed_model_id)
496+
raw_responses = _run_litellm_inference(
497+
model=processed_model_id, prompt_dataset=prompt_dataset
462498
)
463-
processed_custom_responses = []
464-
for resp_item in custom_responses_raw:
465-
if isinstance(resp_item, str):
466-
processed_custom_responses.append(resp_item)
467-
elif isinstance(resp_item, dict) and "error" in resp_item:
468-
processed_custom_responses.append(json.dumps(resp_item))
469-
else:
470-
try:
471-
processed_custom_responses.append(json.dumps(resp_item))
472-
except TypeError:
473-
processed_custom_responses.append(str(resp_item))
474-
responses = processed_custom_responses
499+
responses = [json.dumps(resp) for resp in raw_responses]
500+
475501
else:
476502
raise TypeError(
477503
f"Unsupported model type: {type(model)}. Expecting string (model"

vertexai/_genai/_evals_constant.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@
2727
"final_response_reference_free_v1",
2828
}
2929
)
30+
31+
SUPPORTED_VERTEX_MAAS_MODEL_PREFIXES = frozenset(
32+
{
33+
"meta/", # Meta/Llama
34+
"deepseek-ai/", # DeepSeek AI
35+
"qwen/", # Qwen
36+
"openai/", # OpenAI (GPT-OSS)
37+
"claude-", # Anthropic (Claude)
38+
"mistral-", # Mistral AI
39+
"jamba-", # AI21 (Jamba)
40+
}
41+
)

0 commit comments

Comments
 (0)