Database / ChromaDB Interview Questions
How do you create a custom embedding function for ChromaDB?
ChromaDB defines a simple protocol for embedding functions: a class with a __call__ method that accepts a list of strings and returns a list of embedding vectors. Implementing this interface lets you plug in any model β a local transformer, a third-party API, or even a mock for testing.
import chromadb from chromadb import Documents, EmbeddingFunction, Embeddings from typing import List # Custom embedding function Γ’ΒΒ must implement __call__ class MyCustomEmbeddingFunction(EmbeddingFunction): """Wraps any embedding model in ChromaDB's interface.""" def __init__(self, model_name: str = "my-model"): # Load your model here self.model_name = model_name # self.model = load_model(model_name) def __call__(self, input: Documents) -> Embeddings: """ input: list of strings to embed return: list of lists of floats (one vector per string) """ embeddings = [] for text in input: # Replace with your actual embedding logic vector = self._embed_text(text) embeddings.append(vector) return embeddings def _embed_text(self, text: str) -> List[float]: # Example: fixed-dim hash-based mock (not for production) import hashlib h = hashlib.md5(text.encode()).digest() return [b / 255.0 for b in h] # 16-dim mock vector # Use your custom function exactly like a built-in one client = chromadb.Client() custom_ef = MyCustomEmbeddingFunction() collection = client.create_collection( name="custom_embed", embedding_function=custom_ef, ) collection.add( documents=["Test document one", "Test document two"], ids=["c1", "c2"], ) results = collection.query( query_texts=["test"], n_results=1, ) print(results["ids"]) # [["c1"]] or [["c2"]]
When to write a custom embedding function:
- Your company uses a proprietary or self-hosted embedding model
- You need to embed data from a provider not in ChromaDB's built-in list
- You want to add preprocessing (text cleaning, chunking, domain adaptation) before embedding
- Testing β inject a deterministic mock that returns predictable vectors
More Related questions...