Skip to content

Commit c31ad73

Browse files
committed
support vertex_ai global endpoints for chat
1 parent b8b78f1 commit c31ad73

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

litellm/llms/vertex_ai/common_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ def _get_vertex_url(
8484
endpoint = "generateContent"
8585
if stream is True:
8686
endpoint = "streamGenerateContent"
87-
url = f"https://{vertex_location}-aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model}:{endpoint}?alt=sse"
87+
if vertex_location== "global":
88+
url = f"https://aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/global/publishers/google/models/{model}:{endpoint}?alt=sse"
89+
else:
90+
url = f"https://{vertex_location}-aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model}:{endpoint}?alt=sse"
8891
else:
89-
url = f"https://{vertex_location}-aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model}:{endpoint}"
92+
if vertex_location == "global":
93+
url = f"https://aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/global/publishers/google/models/{model}:{endpoint}"
94+
else:
95+
url = f"https://{vertex_location}-aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model}:{endpoint}"
9096

9197
# if model is only numeric chars then it's a fine tuned gemini model
9298
# model = 4965075652664360960

tests/litellm/llms/vertex_ai/test_vertex_ai_common_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
get_vertex_location_from_url,
1818
get_vertex_project_id_from_url,
1919
set_schema_property_ordering,
20+
_get_vertex_url
2021
)
2122

2223

@@ -292,3 +293,38 @@ def test_process_items_basic():
292293
}
293294
process_items(schema)
294295
assert schema["properties"]["nested"]["items"] == {"type": "object"}
296+
297+
def test_get_vertex_url_global_region(stream):
298+
"""
299+
Test _get_vertex_url when vertex_location is 'global' for chat mode.
300+
"""
301+
mode = "chat"
302+
model = "gemini-1.5-pro-preview-0409"
303+
vertex_project = "test-g-project"
304+
vertex_location = "global"
305+
vertex_api_version = "v1"
306+
307+
# Mock litellm.VertexGeminiConfig.get_model_for_vertex_ai_url to return model as is
308+
# as we are not testing that part here, just the URL construction
309+
with patch("litellm.VertexGeminiConfig.get_model_for_vertex_ai_url", side_effect=lambda model: model):
310+
url, endpoint = _get_vertex_url(
311+
mode=mode,
312+
model=model,
313+
stream=stream,
314+
vertex_project=vertex_project,
315+
vertex_location=vertex_location,
316+
vertex_api_version=vertex_api_version,
317+
)
318+
319+
expected_url_base = f"https://aiplatform.googleapis.com/{vertex_api_version}/projects/{vertex_project}/locations/global/publishers/google/models/{model}"
320+
321+
if stream:
322+
expected_endpoint = "streamGenerateContent"
323+
expected_url = f"{expected_url_base}:{expected_endpoint}?alt=sse"
324+
else:
325+
expected_endpoint = "generateContent"
326+
expected_url = f"{expected_url_base}:{expected_endpoint}"
327+
328+
329+
assert endpoint == expected_endpoint
330+
assert url == expected_url

0 commit comments

Comments
 (0)