Implementation:AI Northstar Tech Vector io Call Litellm
| Knowledge Sources | |
|---|---|
| Domains | Embedding, Re_embedding, Multi_Backend |
| Last Updated | 2026-10-15 00:00 GMT |
Overview
Wrapper around LiteLLM, Sentence Transformers, and MLX embedding backends with automatic fallback, provided by the vector-io library.
Description
The call_litellm and call_sentence_transformers functions wrap external embedding libraries to generate vector embeddings for text batches. They implement a multi-backend fallback chain:
- LiteLLM (default): Calls litellm.embedding() which routes to OpenAI, Azure, Cohere, and other API providers. Used when the model name follows LiteLLM conventions (e.g. text-embedding-3-small). Supports custom dimensions and input type hints. Includes automatic quantization via sentence_transformers.quantize_embeddings when --quantize is not float32.
- Sentence Transformers (fallback): Used when LiteLLM raises a BadRequestError indicating the model is a HuggingFace model (e.g. huggingface/TaylorAI/bge-micro-v2). Loads the model via SentenceTransformer and calls encode(). Once switched, a global flag (use_sentence_transformers) stays True to avoid repeated failures.
- MLX (Apple Silicon fallback): On Apple Silicon (arm64) when no CUDA is available and --disable_mlx is not set, MLX is used via mlx_embedding_models.EmbeddingModel.from_registry().
The call_litellm function is decorated with tenacity retry logic: random exponential backoff (max 10s), up to 5 attempts, retrying on any exception.
strip_hf_prefix removes the huggingface/ prefix from model names when passing to Sentence Transformers or MLX.
is_apple_silicon detects Apple Silicon by checking if uname -m returns arm64.
This is a Wrapper Doc: it documents how vector-io uses external embedding libraries with repo-specific fallback logic.
Usage
Import these functions when you need to generate embeddings for text batches within the reembedding workflow. They are called by reembed_impl during the re-embedding pipeline.
Code Reference
Source Location
- Repository: vector-io
- File: src/vdf_io/scripts/reembed.py
Signature
@retry(
wait=wait_random_exponential(multiplier=1, max=10),
stop=stop_after_attempt(5),
retry=retry_if_exception_type(Exception),
)
def call_litellm(args: dict, batch_text: List[str]) -> EmbeddingResponse:
"""
Generate embeddings via LiteLLM with fallback to Sentence Transformers.
Fallback chain: LiteLLM API -> Sentence Transformers -> MLX (Apple Silicon).
Applies quantization if args['quantize'] != 'float32'.
Retries up to 5 times with random exponential backoff.
Args:
args: CLI args with 'new_model_name', 'dimensions', 'quantize', 'input_type'
batch_text: List of text strings to embed
Returns:
EmbeddingResponse with .data list of {'index': int, 'embedding': List[float]}
"""
def call_sentence_transformers(args: dict, batch_text: List[str]) -> EmbeddingResponse:
"""
Generate embeddings via Sentence Transformers or MLX.
On Apple Silicon without CUDA (and --disable_mlx not set): uses MLX.
Otherwise: uses SentenceTransformer with GPU if available.
Args:
args: CLI args with 'new_model_name', 'quantize', 'disable_mlx'
batch_text: List of text strings to embed
Returns:
EmbeddingResponse with .data list of {'index': int, 'embedding': List[float]}
"""
def is_apple_silicon() -> bool:
"""Detect Apple Silicon by checking if uname -m returns 'arm64'."""
def strip_hf_prefix(args: dict) -> str:
"""Remove 'huggingface/' prefix from model name for ST/MLX compatibility."""
Import
from vdf_io.scripts.reembed import call_litellm, call_sentence_transformers, is_apple_silicon, strip_hf_prefix
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| args | dict | Yes | CLI args with 'new_model_name', 'dimensions', 'quantize', 'input_type', 'disable_mlx' |
| batch_text | List[str] | Yes | Text strings to embed |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | EmbeddingResponse | LiteLLM response with .data list of {'index', 'embedding'} |
Usage Examples
Using LiteLLM (OpenAI API)
from vdf_io.scripts.reembed import call_litellm
args = {
"new_model_name": "text-embedding-3-small",
"dimensions": 1536,
"quantize": "float32",
"input_type": None,
"disable_mlx": False,
}
batch_text = ["Hello world", "Vector databases are great"]
response = call_litellm(args, batch_text)
# Extract embeddings sorted by index
vectors = [x["embedding"] for x in sorted(response.data, key=lambda x: x["index"])]
print(f"Generated {len(vectors)} embeddings of dim {len(vectors[0])}")
Using Sentence Transformers (HuggingFace model)
from vdf_io.scripts.reembed import call_litellm
args = {
"new_model_name": "huggingface/TaylorAI/bge-micro-v2",
"quantize": "int8",
"disable_mlx": False,
}
# call_litellm detects the huggingface/ prefix and falls back to Sentence Transformers
response = call_litellm(args, ["Text to embed"])
Using MLX (Apple Silicon)
# On an Apple Silicon Mac without CUDA, MLX is used automatically
# when --disable_mlx is not set
args = {
"new_model_name": "huggingface/BAAI/bge-small-en-v1.5",
"disable_mlx": False,
"quantize": "float32",
}
response = call_litellm(args, ["Apple Silicon embedding"])
With Quantization
args = {
"new_model_name": "text-embedding-3-large",
"dimensions": 3072,
"quantize": "int8", # Quantize to int8 precision
}
response = call_litellm(args, ["Quantized embedding"])
# Embeddings are quantized via sentence_transformers.quantize_embeddings
Related Pages
Implements Principle
Requires Environment
Related Implementations
- Used by Implementation:AI_Northstar_Tech_Vector_io_Reembed (embedding generation in reembed pipeline)