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]

Feat/sambanova embeddings (#13308)

Jorge Piedrahita Ortiz6a77780c0e8f
diff --git a/docs/my-website/docs/providers/sambanova.md b/docs/my-website/docs/providers/sambanova.md
index 290b64a1f09..f7be5d3ce77 100644
--- a/docs/my-website/docs/providers/sambanova.md
+++ b/docs/my-website/docs/providers/sambanova.md
@@ -307,3 +307,16 @@ response = litellm.completion(
print(response.choices[0].message.content))
```
+
+## SambaNova - Embeddings
+
+```python
+import litellm
+
+response = litellm.embedding(
+ model="sambanova/E5-Mistral-7B-Instruct",
+ input=["sample text to embed", "another sample text to embed"]
+)
+
+print(response.data)
+```
diff --git a/litellm/__init__.py b/litellm/__init__.py
index 7c4a9211455..06d71ff4328 100644
--- a/litellm/__init__.py
+++ b/litellm/__init__.py
@@ -519,6 +519,7 @@ anyscale_models: List = []
cerebras_models: List = []
galadriel_models: List = []
sambanova_models: List = []
+sambanova_embedding_models: List = []
novita_models: List = []
assemblyai_models: List = []
snowflake_models: List = []
@@ -695,6 +696,8 @@ def add_known_models():
galadriel_models.append(key)
elif value.get("litellm_provider") == "sambanova":
sambanova_models.append(key)
+ elif value.get("litellm_provider") == "sambanova-embedding-models":
+ sambanova_embedding_models.append(key)
elif value.get("litellm_provider") == "novita":
novita_models.append(key)
elif value.get("litellm_provider") == "nebius-chat-models":
@@ -879,7 +882,7 @@ models_by_provider: dict = {
"anyscale": anyscale_models,
"cerebras": cerebras_models,
"galadriel": galadriel_models,
- "sambanova": sambanova_models,
+ "sambanova": sambanova_models + sambanova_embedding_models,
"novita": novita_models,
"nebius": nebius_models + nebius_embedding_models,
"assemblyai": assemblyai_models,
@@ -933,6 +936,7 @@ all_embedding_models = (
+ vertex_embedding_models
+ fireworks_ai_embedding_models
+ nebius_embedding_models
+ + sambanova_embedding_models
)
####### IMAGE GENERATION MODELS ###################
@@ -1185,6 +1189,7 @@ nvidiaNimEmbeddingConfig = NvidiaNimEmbeddingConfig()
from .llms.featherless_ai.chat.transformation import FeatherlessAIConfig
from .llms.cerebras.chat import CerebrasConfig
from .llms.sambanova.chat import SambanovaConfig
+from .llms.sambanova.embedding.transformation import SambaNovaEmbeddingConfig
from .llms.ai21.chat.transformation import AI21ChatConfig
from .llms.fireworks_ai.chat.transformation import FireworksAIConfig
from .llms.fireworks_ai.completion.transformation import FireworksAITextCompletionConfig
diff --git a/litellm/litellm_core_utils/get_supported_openai_params.py b/litellm/litellm_core_utils/get_supported_openai_params.py
index 6f2299c777f..5fcd2ddb70a 100644
--- a/litellm/litellm_core_utils/get_supported_openai_params.py
+++ b/litellm/litellm_core_utils/get_supported_openai_params.py
@@ -140,7 +140,10 @@ def get_supported_openai_params( # noqa: PLR0915
model=model
)
elif custom_llm_provider == "sambanova":
- return litellm.SambanovaConfig().get_supported_openai_params(model=model)
+ if request_type == "embeddings":
+ litellm.SambaNovaEmbeddingConfig().get_supported_openai_params(model=model)
+ else:
+ return litellm.SambanovaConfig().get_supported_openai_params(model=model)
elif custom_llm_provider == "nebius":
if request_type == "chat_completion":
return litellm.NebiusConfig().get_supported_openai_params(model=model)
diff --git a/litellm/llms/sambanova/common_utils.py b/litellm/llms/sambanova/common_utils.py
new file mode 100644
index 00000000000..b622f705845
--- /dev/null
+++ b/litellm/llms/sambanova/common_utils.py
@@ -0,0 +1,6 @@
+from litellm.llms.base_llm.chat.transformation import BaseLLMException
+
+
+class SambaNovaError(BaseLLMException):
+ def __init__(self, status_code, message, headers):
+ super().__init__(status_code=status_code, message=message, headers=headers)
diff --git a/litellm/llms/sambanova/embedding/handler.py b/litellm/llms/sambanova/embedding/handler.py
new file mode 100644
index 00000000000..c3629e4d75f
--- /dev/null
+++ b/litellm/llms/sambanova/embedding/handler.py
@@ -0,0 +1,5 @@
+"""
+SambaNova Embedding - uses `llm_http_handler.py` to make httpx requests
+
+Request/Response transformation is handled in `transformation.py`
+"""
diff --git a/litellm/llms/sambanova/embedding/transformation.py b/litellm/llms/sambanova/embedding/transformation.py
new file mode 100644
index 00000000000..eca44c7c039
--- /dev/null
+++ b/litellm/llms/sambanova/embedding/transformation.py
@@ -0,0 +1,139 @@
+"""
+This is OpenAI compatible - no transformation is applied
+
+"""
+from typing import List, Optional, Union
+
+import httpx
+
+from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+from litellm.llms.base_llm.chat.transformation import BaseLLMException
+from litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfig
+from litellm.secret_managers.main import get_secret_str
+from litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues
+from litellm.types.utils import EmbeddingResponse, Usage
+
+from ..common_utils import SambaNovaError
+
+
+class SambaNovaEmbeddingConfig(BaseEmbeddingConfig):
+ def __init__(self) -> None:
+ pass
+
+ def get_complete_url(
+ self,
+ api_base: Optional[str],
+ api_key: Optional[str],
+ model: str,
+ optional_params: dict,
+ litellm_params: dict,
+ stream: Optional[bool] = None,
+ ) -> str:
+ if api_base is None:
+ raise ValueError("api_base is required for SambaNova embeddings")
+ # Remove trailing slashes and ensure clean base URL
+ api_base = api_base.rstrip("/")
+ if not api_base.endswith("/embeddings"):
+ api_base = f"{api_base}/embeddings"
+ return api_base
+
+ def validate_environment(
+ self,
+ headers: dict,
+ model: str,
+ messages: List[AllMessageValues],
+ optional_params: dict,
+ litellm_params: dict,
+ api_key: Optional[str] = None,
+ api_base: Optional[str] = None,
+ ) -> dict:
+ if api_key is None:
+ api_key = get_secret_str("SAMBANOVA_API_KEY")
+
+ default_headers = {
+ "Authorization": f"Bearer {api_key}",
+ "accept": "application/json",
+ "Content-Type": "application/json",
+ }
+
+ # If 'Authorization' is provided in headers, it overrides the default.
+ if "Authorization" in headers:
+ default_headers["Authorization"] = headers["Authorization"]
+
+ # Merge other headers, overriding any default ones except Authorization
+ return {**default_headers, **headers}
+
+ def get_supported_openai_params(self, model: str):
+ """
+ Non additional params supported, placeholder method for future supported params
+ https://docs.sambanova.ai/cloud/api-reference/endpoints/embeddings-api
+ """
+ return []
+
+ def map_openai_params(
+ self,
+ non_default_params: dict,
+ optional_params: dict,
+ model: str,
+ drop_params: bool,
+ ):
+ """
+ No transformation is applied - SambaNova is openai compatible
+ """
+ supported_openai_params = self.get_supported_openai_params(model)
+ for param, value in non_default_params.items():
+ if param in supported_openai_params:
+ optional_params[param] = value
+ return optional_params
+
+ def transform_embedding_request(
+ self,
+ model: str,
+ input: AllEmbeddingInputValues,
+ optional_params: dict,
+ headers: dict,
+ ) -> dict:
+ return {
+ "input": input,
+ "model": model,
+ **optional_params,
+ }
+
+ def transform_embedding_response(
+ self,
+ model: str,
+ raw_response: httpx.Response,
+ model_response: EmbeddingResponse,
+ logging_obj: LiteLLMLoggingObj,
+ api_key: Optional[str],
+ request_data: dict,
+ optional_params: dict,
+ litellm_params: dict,
+ ) -> EmbeddingResponse:
+ try:
+ raw_response_json = raw_response.json()
+ except Exception:
+ raise SambaNovaError(
+ message=raw_response.text,
+ status_code=raw_response.status_code,
+ headers=raw_response.headers,
+ )
+
+ model_response.model = raw_response_json.get("model")
+ model_response.data = raw_response_json.get("data")
+ model_response.object = raw_response_json.get("object")
+
+ usage = Usage(
+ prompt_tokens=raw_response_json.get("usage", {}).get("prompt_tokens", 0),
+ total_tokens=raw_response_json.get("usage", {}).get("total_tokens", 0),
+ )
+
+ model_response.usage = usage
+ return model_response
+
+ def get_error_class(
+ self, error_message: str, status_code: int, headers: Union[dict, httpx.Headers]
+ ) -> BaseLLMException:
+ return SambaNovaError(
+ message=error_message, status_code=status_code, headers=headers
+ )
diff --git a/litellm/main.py b/litellm/main.py
index ffe58503758..754b824e7dd 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -4279,6 +4279,22 @@ def embedding( # noqa: PLR0915
client=client,
aembedding=aembedding,
)
+ elif custom_llm_provider == "sambanova":
+ api_key = api_key or litellm.api_key or get_secret_str("SAMBANOVA_API_KEY")
+ response = base_llm_http_handler.embedding(
+ model=model,
+ input=input,
+ custom_llm_provider=custom_llm_provider,
+ api_base=api_base,
+ api_key=api_key,
+ logging_obj=logging,
+ timeout=timeout,
+ model_response=EmbeddingResponse(),
+ optional_params=optional_params,
+ client=client,
+ aembedding=aembedding,
+ litellm_params={},
+ )
elif custom_llm_provider == "voyage":
response = base_llm_http_handler.embedding(
model=model,
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index 1001aca9c09..cf1e7590906 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -16902,6 +16902,15 @@
"supports_reasoning": true,
"source": "https://cloud.sambanova.ai/plans/pricing"
},
+ "sambanova/E5-Mistral-7B-Instruct": {
+ "max_tokens": 4096,
+ "max_input_tokens": 4096,
+ "input_cost_per_token": 1.3e-07,
+ "output_cost_per_token": 0.0,
+ "litellm_provider": "sambanova-embedding-models",
+ "mode": "embedding",
+ "source": "https://cloud.sambanova.ai/plans/pricing"
+ },
"assemblyai/nano": {
"mode": "audio_transcription",
"input_cost_per_second": 0.00010278,
diff --git a/litellm/utils.py b/litellm/utils.py
index 54628c05faa..908844d06fe 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -2831,6 +2831,19 @@ def get_optional_params_embeddings( # noqa: PLR0915
optional_params = litellm.FireworksAIEmbeddingConfig().map_openai_params(
non_default_params=non_default_params, optional_params={}, model=model
)
+ elif custom_llm_provider == "sambanova":
+ supported_params = get_supported_openai_params(
+ model=model,
+ custom_llm_provider="sambanova",
+ request_type="embeddings",
+ )
+ _check_valid_arg(supported_params=supported_params)
+ optional_params = litellm.SambaNovaEmbeddingConfig().map_openai_params(
+ non_default_params=non_default_params,
+ optional_params={},
+ model=model,
+ drop_params=drop_params if drop_params is not None else False,
+ )
elif (
custom_llm_provider != "openai"
@@ -7006,6 +7019,8 @@ class ProviderConfigManager:
return litellm.IBMWatsonXEmbeddingConfig()
elif litellm.LlmProviders.INFINITY == provider:
return litellm.InfinityEmbeddingConfig()
+ elif litellm.LlmProviders.SAMBANOVA == provider:
+ return litellm.SambaNovaEmbeddingConfig()
elif (
litellm.LlmProviders.COHERE == provider
or litellm.LlmProviders.COHERE_CHAT == provider
diff --git a/tests/test_litellm/llms/sambanova/tests_sambanova_embedding_transformation.py b/tests/test_litellm/llms/sambanova/tests_sambanova_embedding_transformation.py
new file mode 100644
index 00000000000..8d445807fdc
--- /dev/null
+++ b/tests/test_litellm/llms/sambanova/tests_sambanova_embedding_transformation.py
@@ -0,0 +1,41 @@
+from unittest.mock import patch
+
+import litellm
+
+
+def mock_embedding_response(*args, **kwargs):
+ """Mock response mimicking litellm.embedding output."""
+
+ class MockResponse:
+ def __init__(self):
+ self.data = [{"embedding": [0.1, 0.2, 0.3]}] # Example embedding vector
+ self.usage = litellm.Usage() # Mock Usage object
+ self.model = kwargs.get("model", "sambanova/E5-Mistral-7B-Instruct")
+ self.object = "embedding"
+
+ def __getitem__(self, key):
+ return getattr(self, key)
+
+ return MockResponse()
+
+
+def test_sambanova_embeddings():
+ """Mocked test for SambaNova embeddings using MagicMock."""
+ with patch("litellm.embedding", side_effect=mock_embedding_response) as mock_embed:
+ response = litellm.embedding(
+ model="sambanova/E5-Mistral-7B-Instruct",
+ input=["good morning from litellm"],
+ )
+
+ # Assertions to verify that the mock was called correctly
+ mock_embed.assert_called_once_with(
+ model="sambanova/E5-Mistral-7B-Instruct",
+ input=["good morning from litellm"],
+ )
+
+ # Assertions to check the structure of the mocked response
+ assert isinstance(response.data, list)
+ assert "embedding" in response.data[0]
+ assert isinstance(response.data[0]["embedding"], list)
+ assert response.model == "sambanova/E5-Mistral-7B-Instruct"
+ assert response.object == "embedding"