Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

add vertex ai countTokens endpoint

Ishaan Jaffc8438715af43
diff --git a/litellm/llms/fine_tuning_apis/vertex_ai.py b/litellm/llms/fine_tuning_apis/vertex_ai.py
index f964e7bba22..c24deca941b 100644
--- a/litellm/llms/fine_tuning_apis/vertex_ai.py
+++ b/litellm/llms/fine_tuning_apis/vertex_ai.py
@@ -274,6 +274,11 @@ class VertexFineTuningAPI(VertexLLM):
url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}{request_route}"
elif "predict" in request_route:
url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}{request_route}"
+ elif "/batchPredictionJobs" in request_route:
+ url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}{request_route}"
+ elif "countTokens" in request_route:
+ url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}{request_route}"
+
if self.async_handler is None:
raise ValueError("VertexAI Fine Tuning - async_handler is not initialized")
diff --git a/litellm/proxy/vertex_ai_endpoints/vertex_endpoints.py b/litellm/proxy/vertex_ai_endpoints/vertex_endpoints.py
index da63aa02505..c7ce354b25f 100644
--- a/litellm/proxy/vertex_ai_endpoints/vertex_endpoints.py
+++ b/litellm/proxy/vertex_ai_endpoints/vertex_endpoints.py
@@ -182,6 +182,69 @@ async def vertex_predict_endpoint(
raise exception_handler(e) from e
+ "/vertex-ai/publishers/google/models/{model_id:path}:countTokens",
+ dependencies=[Depends(user_api_key_auth)],
+ tags=["Vertex AI endpoints"],
+)
+async def vertex_countTokens_endpoint(
+ request: Request,
+ fastapi_response: Response,
+ model_id: str,
+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
+):
+ """
+ this is a pass through endpoint for the Vertex AI API. /countTokens endpoint
+ https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/count-tokens#curl
+
+
+ Example Curl:
+ ```
+ curl http://localhost:4000/vertex-ai/publishers/google/models/gemini-1.5-flash-001:countTokens \
+ -H "Content-Type: application/json" \
+ -H "Authorization: Bearer sk-1234" \
+ -d '{"contents":[{"role": "user", "parts":[{"text": "hi"}]}]}'
+ ```
+
+ it uses the vertex ai credentials on the proxy and forwards to vertex ai api
+ """
+ try:
+ response = await execute_post_vertex_ai_request(
+ request=request,
+ route=f"/publishers/google/models/{model_id}:countTokens",
+ )
+ return response
+ except Exception as e:
+ raise exception_handler(e) from e
+
+
+ "/vertex-ai/batchPredictionJobs",
+ dependencies=[Depends(user_api_key_auth)],
+ tags=["Vertex AI endpoints"],
+)
+async def vertex_create_batch_prediction_job(
+ request: Request,
+ fastapi_response: Response,
+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
+):
+ """
+ this is a pass through endpoint for the Vertex AI API. /batchPredictionJobs endpoint
+
+ Vertex API Reference: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/batch-prediction-api#syntax
+
+ it uses the vertex ai credentials on the proxy and forwards to vertex ai api
+ """
+ try:
+ response = await execute_post_vertex_ai_request(
+ request=request,
+ route="/batchPredictionJobs",
+ )
+ return response
+ except Exception as e:
+ raise exception_handler(e) from e
+
+
@router.post(
"/vertex-ai/tuningJobs",
dependencies=[Depends(user_api_key_auth)],