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
65 changes: 65 additions & 0 deletions agentplatform/_genai/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,68 @@ def t_inline_results(
api_results.append(api_eval_result)

return api_results


_ENDPOINT_RES_NAME_RES = (
re.compile(r"^projects/[^/]+/locations/[^/]+/endpoints/[^/]+$"),
re.compile(r"^endpoints/[^/]+$"),
)
_PUBLISHER_MODEL_RES_NAME_RES = (
re.compile(r"^projects/[^/]+/locations/[^/]+/publishers/[^/]+/models/[^/]+$"),
re.compile(r"^publishers/[^/]+/models/[^/]+$"),
)


def is_endpoint_resource_name(name: str) -> bool:
"""Returns whether the name addresses an Endpoint resource."""
return any(pattern.match(name) for pattern in _ENDPOINT_RES_NAME_RES)


def _is_publisher_model_resource_name(name: str) -> bool:
return any(pattern.match(name) for pattern in _PUBLISHER_MODEL_RES_NAME_RES)


def t_endpoint(endpoint: str) -> str:
"""Validates a name that may address an Endpoint or a publisher model."""
if not endpoint:
raise ValueError("endpoint is required.")

if is_endpoint_resource_name(endpoint) or _is_publisher_model_resource_name(
endpoint
):
return endpoint

raise ValueError(
f"Invalid endpoint format: {endpoint}. Must be in the format of"
" projects/.../locations/.../endpoints/... or"
" projects/.../locations/.../publishers/.../models/... or"
" endpoints/... or publishers/.../models/..."
)


def t_strict_endpoint(endpoint: str) -> str:
"""Validates a name that must address an Endpoint resource.

A publisher model is served by the shared prediction endpoint and has no
Endpoint resource behind it, so it can be predicted against but never
fetched, undeployed or deleted. Methods that address the resource itself
must reject it here rather than issue a request against a publishers/... URL.
"""
if not endpoint:
raise ValueError("endpoint is required.")

if is_endpoint_resource_name(endpoint):
return endpoint

if _is_publisher_model_resource_name(endpoint):
raise ValueError(
f"{endpoint} is a publisher model, which does not support this"
" method. Only endpoints in the format of"
" projects/.../locations/.../endpoints/... or endpoints/... are"
" supported."
)

raise ValueError(
f"Invalid endpoint format: {endpoint}. Must be in the format of"
" projects/.../locations/.../endpoints/... or endpoints/..."
)
23 changes: 23 additions & 0 deletions agentplatform/_genai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
from agentplatform._genai import (
feedback_entries as feedback_entries_module,
)
from agentplatform._genai import (
endpoints as endpoints_module,
)

_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"

Expand Down Expand Up @@ -92,6 +95,7 @@ def __init__(self, api_client: genai_client.BaseApiClient): # type: ignore[name
self._rag: Optional[ModuleType] = None
self._model_garden: Optional[ModuleType] = None
self._feedback_entries: Optional[ModuleType] = None
self._endpoints: Optional[ModuleType] = None

@property
@_common.experimental_warning(
Expand Down Expand Up @@ -182,6 +186,15 @@ def feedback_entries(self) -> "feedback_entries_module.AsyncFeedbackEntries":
)
return self._feedback_entries.AsyncFeedbackEntries(self._api_client) # type: ignore[no-any-return]

@property
def endpoints(self) -> "endpoints_module.AsyncEndpoints":
if self._endpoints is None:
self._endpoints = importlib.import_module(
".endpoints",
__package__,
)
return self._endpoints.AsyncEndpoints(self._api_client) # type: ignore[no-any-return]

@property
@_common.experimental_warning(
"The Vertex SDK GenAI async rag module is experimental, "
Expand Down Expand Up @@ -314,6 +327,7 @@ def __init__(
self._rag: Optional[ModuleType] = None
self._model_garden: Optional[ModuleType] = None
self._feedback_entries: Optional[ModuleType] = None
self._endpoints: Optional[ModuleType] = None

@property
def evals(self) -> "evals_module.Evals":
Expand Down Expand Up @@ -429,6 +443,15 @@ def feedback_entries(self) -> "feedback_entries_module.FeedbackEntries":
)
return self._feedback_entries.FeedbackEntries(self._api_client) # type: ignore[no-any-return]

@property
def endpoints(self) -> "endpoints_module.Endpoints":
if self._endpoints is None:
self._endpoints = importlib.import_module(
".endpoints",
__package__,
)
return self._endpoints.Endpoints(self._api_client) # type: ignore[no-any-return]

@property
@_common.experimental_warning(
"The Vertex SDK GenAI rag module is experimental, "
Expand Down
Loading
Loading