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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(embeddings_handler.py): initial working commit for google ai studio text embeddings /embedContent endpoint
Krrish Dholakia5b29ddd2a677
diff --git a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_handler.py b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_handler.pyindex bc0d4ac16fc..98cebfc313b 100644--- a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_handler.py+++ b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_handler.py@@ -10,7 +10,14 @@ import httpximport litellmfrom litellm import EmbeddingResponsefrom litellm.llms.custom_httpx.http_handler import HTTPHandler-+from litellm.types.llms.vertex_ai import (+ VertexAITextEmbeddingsRequestBody,+ VertexAITextEmbeddingsResponseObject,+)+from litellm.types.utils import Embedding+from litellm.utils import get_formatted_prompt++from .embeddings_transformation import transform_openai_input_gemini_contentfrom .vertex_and_google_ai_studio_gemini import VertexLLM@@ -34,7 +41,7 @@ class GoogleEmbeddings(VertexLLM):timeout=300,client=None,) -> EmbeddingResponse:- return model_response+auth_header, url = self._get_token_and_url(model=model,gemini_api_key=api_key,@@ -63,59 +70,58 @@ class GoogleEmbeddings(VertexLLM):optional_params = optional_params or {}- # request_data = VertexMultimodalEmbeddingRequest()-- # if "instances" in optional_params:- # request_data["instances"] = optional_params["instances"]- # elif isinstance(input, list):- # request_data["instances"] = input- # else:- # # construct instances- # vertex_request_instance = Instance(**optional_params)-- # if isinstance(input, str):- # vertex_request_instance["text"] = input-- # request_data["instances"] = [vertex_request_instance]-- # headers = {- # "Content-Type": "application/json; charset=utf-8",- # "Authorization": f"Bearer {auth_header}",- # }-- # ## LOGGING- # logging_obj.pre_call(- # input=input,- # api_key="",- # additional_args={- # "complete_input_dict": request_data,- # "api_base": url,- # "headers": headers,- # },- # )-- # if aembedding is True:- # pass-- # response = sync_handler.post(- # url=url,- # headers=headers,- # data=json.dumps(request_data),- # )-- # if response.status_code != 200:- # raise Exception(f"Error: {response.status_code} {response.text}")-- # _json_response = response.json()- # if "predictions" not in _json_response:- # raise litellm.InternalServerError(- # message=f"embedding response does not contain 'predictions', got {_json_response}",- # llm_provider="vertex_ai",- # model=model,- # )- # _predictions = _json_response["predictions"]-- # model_response.data = _predictions- # model_response.model = model-- # return model_response+ ### TRANSFORMATION ###+ content = transform_openai_input_gemini_content(input=input)++ request_data: VertexAITextEmbeddingsRequestBody = {+ "content": content,+ **optional_params,+ }++ headers = {+ "Content-Type": "application/json; charset=utf-8",+ }++ ## LOGGING+ logging_obj.pre_call(+ input=input,+ api_key="",+ additional_args={+ "complete_input_dict": request_data,+ "api_base": url,+ "headers": headers,+ },+ )++ if aembedding is True:+ pass++ response = sync_handler.post(+ url=url,+ headers=headers,+ data=json.dumps(request_data),+ )++ if response.status_code != 200:+ raise Exception(f"Error: {response.status_code} {response.text}")++ _json_response = response.json()+ _predictions = VertexAITextEmbeddingsResponseObject(**_json_response) # type: ignore++ model_response.data = [+ Embedding(+ embedding=_predictions["embedding"]["values"],+ index=0,+ object="embedding",+ )+ ]++ model_response.model = model++ input_text = get_formatted_prompt(data={"input": input}, call_type="embedding")+ prompt_tokens = litellm.token_counter(model=model, text=input_text)+ model_response.usage = litellm.Usage(+ prompt_tokens=prompt_tokens, total_tokens=prompt_tokens+ )++ return model_responsediff --git a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_transformation.py b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_transformation.pyindex 2e3d156f510..dd5abfa3800 100644--- a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_transformation.py+++ b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/embeddings_transformation.py@@ -3,3 +3,25 @@ Transformation logic from OpenAI /v1/embeddings format to Google AI Studio /embeWhy separate file? Make it easy to see how transformation works"""++from typing import List++from litellm.types.llms.openai import EmbeddingInput+from litellm.types.llms.vertex_ai import ContentType, PartType++from ..common_utils import VertexAIError+++def transform_openai_input_gemini_content(input: EmbeddingInput) -> ContentType:+ """+ The content to embed. Only the parts.text fields will be counted.+ """+ if isinstance(input, str):+ return ContentType(parts=[PartType(text=input)])+ elif isinstance(input, list) and len(input) == 1:+ return ContentType(parts=[PartType(text=input[0])])+ else:+ raise VertexAIError(+ status_code=422,+ message="/embedContent only generates a single text embedding vector. File an issue, to add support for /batchEmbedContent - https://github.com/BerriAI/litellm/issues",+ )diff --git a/litellm/main.py b/litellm/main.pyindex 8896f1faf18..e9a3d2898ba 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -126,6 +126,9 @@ from .llms.vertex_ai_and_google_ai_studio import (vertex_ai_anthropic,vertex_ai_non_gemini,)+from .llms.vertex_ai_and_google_ai_studio.gemini.embeddings_handler import (+ GoogleEmbeddings,+)from .llms.vertex_ai_and_google_ai_studio.gemini.vertex_and_google_ai_studio_gemini import (VertexLLM,)@@ -172,6 +175,7 @@ triton_chat_completions = TritonChatCompletion()bedrock_chat_completion = BedrockLLM()bedrock_converse_chat_completion = BedrockConverseLLM()vertex_chat_completion = VertexLLM()+google_embeddings = GoogleEmbeddings()vertex_partner_models_chat_completion = VertexAIPartnerModels()vertex_text_to_speech = VertexTextToSpeechAPI()watsonxai = IBMWatsonXAI()@@ -3533,7 +3537,7 @@ def embedding(gemini_api_key = api_key or get_secret("GEMINI_API_KEY") or litellm.api_key- response = vertex_chat_completion.multimodal_embedding( # type: ignore+ response = google_embeddings.text_embeddings( # type: ignoremodel=model,input=input,encoding=encoding,diff --git a/litellm/tests/test_embedding.py b/litellm/tests/test_embedding.pyindex 2fbc70f0248..c318264d454 100644--- a/litellm/tests/test_embedding.py+++ b/litellm/tests/test_embedding.py@@ -697,7 +697,8 @@ async def test_gemini_embeddings():print(f"response: {response}")# stubbed endpoint is setup to return this- assert response.data[0]["embedding"] == [0.1, 0.2]+ assert isinstance(response.data[0]["embedding"], list)+ assert response.usage.prompt_tokens > 0except Exception as e:pytest.fail(f"Error occurred: {e}")diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.pyindex 470f72c5b6f..138441a7eba 100644--- a/litellm/types/llms/openai.py+++ b/litellm/types/llms/openai.py@@ -30,6 +30,7 @@ from openai.types.beta.threads.message import Message as OpenAIMessagefrom openai.types.beta.threads.message_content import MessageContentfrom openai.types.beta.threads.run import Runfrom openai.types.chat import ChatCompletionChunk+from openai.types.embedding import Embedding as OpenAIEmbeddingfrom pydantic import BaseModel, Fieldfrom typing_extensions import Dict, Required, TypedDict, override@@ -47,6 +48,9 @@ FileTypes = Union[]+EmbeddingInput = Union[str, List[str]]++class NotGiven:"""A sentinel singleton class used to distinguish omitted keyword argumentsdiff --git a/litellm/types/llms/vertex_ai.py b/litellm/types/llms/vertex_ai.pyindex 90730d75fe9..bacb4d22523 100644--- a/litellm/types/llms/vertex_ai.py+++ b/litellm/types/llms/vertex_ai.py@@ -336,3 +336,29 @@ class VertexMultimodalEmbeddingRequest(TypedDict, total=False):class VertexAICachedContentResponseObject(TypedDict):name: strmodel: str+++class TaskTypeEnum(Enum):+ TASK_TYPE_UNSPECIFIED = "TASK_TYPE_UNSPECIFIED"+ RETRIEVAL_QUERY = "RETRIEVAL_QUERY"+ RETRIEVAL_DOCUMENT = "RETRIEVAL_DOCUMENT"+ SEMANTIC_SIMILARITY = "SEMANTIC_SIMILARITY"+ CLASSIFICATION = "CLASSIFICATION"+ CLUSTERING = "CLUSTERING"+ QUESTION_ANSWERING = "QUESTION_ANSWERING"+ FACT_VERIFICATION = "FACT_VERIFICATION"+++class VertexAITextEmbeddingsRequestBody(TypedDict, total=False):+ content: Required[ContentType]+ taskType: TaskTypeEnum+ title: str+ outputDimensionality: int+++class ContentEmbeddings(TypedDict):+ values: List[int]+++class VertexAITextEmbeddingsResponseObject(TypedDict):+ embedding: ContentEmbeddings