Merge pull request #2823 from pipecat-ai/pk/vertex-init-args-fixup
Move `location` and `project_id` out of `InputParams` in `GoogleVerte…
This commit is contained in:
@@ -53,12 +53,44 @@ class GoogleVertexLLMService(OpenAILLMService):
|
|||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
location: GCP region for Vertex AI endpoint (e.g., "us-east4").
|
location: GCP region for Vertex AI endpoint (e.g., "us-east4").
|
||||||
|
|
||||||
|
.. deprecated:: 0.0.90
|
||||||
|
Use `location` as a direct argument to
|
||||||
|
`GoogleVertexLLMService.__init__()` instead.
|
||||||
|
|
||||||
project_id: Google Cloud project ID.
|
project_id: Google Cloud project ID.
|
||||||
|
|
||||||
|
.. deprecated:: 0.0.90
|
||||||
|
Use `project_id` as a direct argument to
|
||||||
|
`GoogleVertexLLMService.__init__()` instead.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations
|
# https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations
|
||||||
location: str = "us-east4"
|
location: Optional[str] = None
|
||||||
project_id: str
|
project_id: Optional[str] = None
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
"""Initializes the InputParams."""
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("always")
|
||||||
|
if "location" in kwargs and kwargs["location"] is not None:
|
||||||
|
warnings.warn(
|
||||||
|
"GoogleVertexLLMService.InputParams.location is deprecated. "
|
||||||
|
"Please provide 'location' as a direct argument to GoogleVertexLLMService.__init__() instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "project_id" in kwargs and kwargs["project_id"] is not None:
|
||||||
|
warnings.warn(
|
||||||
|
"GoogleVertexLLMService.InputParams.project_id is deprecated. "
|
||||||
|
"Please provide 'project_id' as a direct argument to GoogleVertexLLMService.__init__() instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -66,7 +98,8 @@ class GoogleVertexLLMService(OpenAILLMService):
|
|||||||
credentials: Optional[str] = None,
|
credentials: Optional[str] = None,
|
||||||
credentials_path: Optional[str] = None,
|
credentials_path: Optional[str] = None,
|
||||||
model: str = "google/gemini-2.0-flash-001",
|
model: str = "google/gemini-2.0-flash-001",
|
||||||
params: Optional[InputParams] = None,
|
location: Optional[str] = None,
|
||||||
|
project_id: Optional[str] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
"""Initializes the VertexLLMService.
|
"""Initializes the VertexLLMService.
|
||||||
@@ -75,33 +108,56 @@ class GoogleVertexLLMService(OpenAILLMService):
|
|||||||
credentials: JSON string of service account credentials.
|
credentials: JSON string of service account credentials.
|
||||||
credentials_path: Path to the service account JSON file.
|
credentials_path: Path to the service account JSON file.
|
||||||
model: Model identifier (e.g., "google/gemini-2.0-flash-001").
|
model: Model identifier (e.g., "google/gemini-2.0-flash-001").
|
||||||
params: Vertex AI input parameters including location and project.
|
location: GCP region for Vertex AI endpoint (e.g., "us-east4").
|
||||||
|
project_id: Google Cloud project ID.
|
||||||
**kwargs: Additional arguments passed to OpenAILLMService.
|
**kwargs: Additional arguments passed to OpenAILLMService.
|
||||||
"""
|
"""
|
||||||
params = params or OpenAILLMService.InputParams()
|
# Handle deprecated InputParams fields
|
||||||
base_url = self._get_base_url(params)
|
if "params" in kwargs and isinstance(kwargs["params"], GoogleVertexLLMService.InputParams):
|
||||||
|
params = kwargs["params"]
|
||||||
|
# Extract location and project_id from params if not provided
|
||||||
|
# directly, for backward compatibility
|
||||||
|
if project_id is None:
|
||||||
|
project_id = params.project_id
|
||||||
|
if location is None:
|
||||||
|
location = params.location
|
||||||
|
# Convert to base InputParams
|
||||||
|
params = OpenAILLMService.InputParams(
|
||||||
|
**params.model_dump(exclude={"location", "project_id"}, exclude_unset=True)
|
||||||
|
)
|
||||||
|
kwargs["params"] = params
|
||||||
|
|
||||||
|
# Validate project_id and location parameters
|
||||||
|
# NOTE: once we remove Vertex-spcific InputParams class, we can update
|
||||||
|
# __init__() signature as follows:
|
||||||
|
# - location: str = "us-east4",
|
||||||
|
# - project_id: str,
|
||||||
|
# But for now, we need them as-is to maintain proper backward
|
||||||
|
# compatibility.
|
||||||
|
if project_id is None:
|
||||||
|
raise ValueError("project_id is required")
|
||||||
|
if location is None:
|
||||||
|
location = "us-east4" # Default location if not provided
|
||||||
|
|
||||||
|
base_url = self._get_base_url(location, project_id)
|
||||||
self._api_key = self._get_api_token(credentials, credentials_path)
|
self._api_key = self._get_api_token(credentials, credentials_path)
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
api_key=self._api_key,
|
api_key=self._api_key,
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
model=model,
|
model=model,
|
||||||
params=params,
|
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_base_url(params: InputParams) -> str:
|
def _get_base_url(location: str, project_id: str) -> str:
|
||||||
"""Construct the base URL for Vertex AI API."""
|
"""Construct the base URL for Vertex AI API."""
|
||||||
# Determine the correct API host based on location
|
# Determine the correct API host based on location
|
||||||
if params.location == "global":
|
if location == "global":
|
||||||
api_host = "aiplatform.googleapis.com"
|
api_host = "aiplatform.googleapis.com"
|
||||||
else:
|
else:
|
||||||
api_host = f"{params.location}-aiplatform.googleapis.com"
|
api_host = f"{location}-aiplatform.googleapis.com"
|
||||||
return (
|
return f"https://{api_host}/v1/projects/{project_id}/locations/{location}/endpoints/openapi"
|
||||||
f"https://{api_host}/v1/"
|
|
||||||
f"projects/{params.project_id}/locations/{params.location}/endpoints/openapi"
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_api_token(credentials: Optional[str], credentials_path: Optional[str]) -> str:
|
def _get_api_token(credentials: Optional[str], credentials_path: Optional[str]) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user