How to compute embeddings
This guide turns texts into vectors for semantic search, clustering or RAG. It assumes rzb serve is running.
1. Get an embedding model
rzb models pull BAAI/bge-small-en-v1.5
Models known to work: BAAI/bge-small-en-v1.5 and sentence-transformers/all-MiniLM-L6-v2 (384 dimensions; their vectors match the Python sentence-transformers library within 1e-3). Other BERT-family and Qwen3-Embedding models supported by mlx-swift-lm's embedders should work too.
2. Declare it (recommended)
models:
- id: embed
source: BAAI/bge-small-en-v1.5
kind: embedding
Declared, it is listed as "kind": "embedding" by /v1/models, and a chat request to it is refused clearly. An undeclared Hugging Face id also works on /v1/embeddings.
3. Ask for vectors
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:7920/v1", api_key="unused")
result = client.embeddings.create(model="embed", input=["The cat sits on the mat.", "A feline rests on a rug."])
vectors = [item.embedding for item in result.data]
Vectors are normalised: their dot product is their cosine similarity.
Variants
- Shorter vectors:
dimensions=256keeps the first 256 values and renormalises them. Meaningful for Matryoshka models (Qwen3-Embedding, nomic…); for others it loses quality. - Smaller responses:
encoding_format="base64"(the openai library asks for it by default and decodes it for you). - LangChain:
OpenAIEmbeddings(base_url="http://127.0.0.1:7920/v1", api_key="unused", model="embed", check_embedding_ctx_length=False). Withoutcheck_embedding_ctx_length=False, LangChain sends token arrays, which the server refuses. - Long texts: a text longer than the model accepts (512 tokens for bge-small) is a 400 naming it. Split your documents first.
- Query prefixes: models that expect a prefix (
query:,search_query:, an instruction) need it in your text; the server adds nothing. - Chat and embeddings together: set
limits.max_loaded_models: 2so the two models stay loaded.
See also
- Every field of
/v1/embeddings: HTTP API reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|