pip install "sentence-transformers"★Pulls in Transformers + PyTorch. Add[train]extras for the training stack.from sentence_transformers import SentenceTransformer model = SentenceTransformer("all-MiniLM-L6-v2")★Small, fast, 384-dim — the classic default. Bigger/better:all-mpnet-base-v2,BAAI/bge-*,Qwen/Qwen3-Embedding-*.SentenceTransformer(id, device="cuda", model_kwargs={"dtype":"auto"})Choose device/precision;truncate_dim=for Matryoshka models,backend="onnx"/"openvino"for fast CPU inference.model.get_sentence_embedding_dimension() · model.max_seq_lengthInspect vector size and the token limit (inputs longer than this are truncated).
emb = model.encode(["a sentence", "another"])★Returns an(n, dim)numpy array (or a torch tensor withconvert_to_tensor=True). Batched & parallelized internally.model.encode(texts, normalize_embeddings=True, batch_size=64, show_progress_bar=True)★Normalize to unit length so cosine == dot product. Tunebatch_sizefor throughput.scores = model.similarity(emb1, emb2) # (n1, n2) matrix★Uses the model's configured metric (cosine by default).similarity_pairwisefor row-aligned pairs.model.similarity_fn_name # "cosine" | "dot" | "euclidean" | "manhattan"Set at load viasimilarity_fn_name=; controls bothsimilarity()and search defaults.
corpus_emb = model.encode_document(corpus)★Encode the corpus once, keep the tensor as your index. Applies the model's document prompt if it has one.q_emb = model.encode_query("my question")★Encode each incoming query with the query prompt. Query/document methods (v5) replace manually prepending prompts.model.similarity(q_emb, corpus_emb)Then rank the corpus for the query — the core of a retrieval pipeline.match query & doc encodersgotchaUse the same model for both sides. Many retrieval models are asymmetric — skipping query/document prompts silently hurts recall.