Overview
text2vec converts text — words, sentences or paragraphs — into vectors, and computes similarity between them. It is a single Python package that implements several representation methods side by side: Word2Vec, RankBM25, BERT, Sentence-BERT, CoSENT and BGE, each with both inference and training code, so you can compare approaches on the same semantic-matching task rather than committing to one upfront.
Its centre of gravity is Chinese text. The project trains and publishes its own matching models on Hugging Face — `text2vec-base-chinese` (a CoSENT model trained on Chinese STS data), `text2vec-base-chinese-sentence` and `text2vec-base-chinese-paraphrase` for sentence-to-paragraph matching, `text2vec-bge-large-chinese` supervised with CoSENT on top of BAAI/bge-large-zh-noinstruct, and `text2vec-base-multilingual` for cross-language matching including English.
The library's own contribution on the modelling side is CoSENT (Cosine Sentence), a ranking-style loss that brings training closer to how the model is actually used at prediction time — you take the sentence vector and compare cosines — which the project reports converges faster and scores better than Sentence-BERT on its evaluation sets. Every published model can be fine-tuned further on your own data. Version 1.2.9 added multi-process inference across several GPUs or CPUs and a command-line tool for batch vectorising text from a script.
What it does
- One API over Word2Vec, RankBM25, Sentence-BERT, CoSENT and BGE representations
- Pretrained Chinese and multilingual matching models published on the Hugging Face Hub
- CoSENT ranking loss with training code, plus fine-tuning support on every published model
- Multi-GPU and multi-CPU inference via multi-process execution (v1.2.9+)
- Command-line tool for batch text vectorisation from a script
- Published comparisons of each method's effectiveness on semantic-matching benchmarks
Getting started
text2vec is on PyPI and needs PyTorch. Install torch first, then the package.
Install
Install PyTorch for your platform, then text2vec.
pip install torch # or: conda install pytorch
pip install -U text2vecEncode a sentence
SentenceModel with no arguments loads the default model and returns a NumPy array — a 768-dimensional vector per sentence.
from text2vec import SentenceModel
m = SentenceModel()
m.encode("如何更换花呗绑定银行卡")
# Embedding shape: (768,)Pick the model that matches your task
Pass a Hugging Face model id to SentenceModel. Use the Chinese CoSENT model for Chinese semantic matching, the multilingual one for mixed Chinese and English, and Word2Vec for literal matching or a cold start.
from text2vec import SentenceModel, Word2Vec
t2v_model = SentenceModel("shibing624/text2vec-base-chinese")
sbert_model = SentenceModel("shibing624/text2vec-base-multilingual")
w2v_model = Word2Vec("w2v-light-tencent-chinese")Embed a batch
encode also accepts a list and returns one row per input sentence.
sentences = [
"银行卡",
"如何更换花呗绑定银行卡",
"This framework generates embeddings for each input sentence",
]
sentence_embeddings = model.encode(sentences)
print(type(sentence_embeddings), sentence_embeddings.shape)
# <class 'numpy.ndarray'> (3, 768)Install from source instead
Clone the repository if you want to train or modify the models.
git clone https://github.com/shibing624/text2vec.git
cd text2vec
pip install -r requirements.txt
pip install --no-deps .Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Embed Chinese documents for a RAG index where English-first embedding models underperform
- Build a semantic search or deduplication pipeline over a Chinese or mixed-language corpus
- Compare Word2Vec, BM25, Sentence-BERT and CoSENT on your own matching data before choosing one
- Fine-tune a published Chinese matching model on in-domain pairs with the included training code
- Batch-vectorise a large text dump from the command line across several GPUs
How text2vec compares
text2vec alongside other open-source embedding models & inference tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| Sentence Transformers | ★ 19.1k | The standard Python framework for loading, training, and computing embeddings with sentence and reranking models. |
| OpenCLIP | ★ 14.1k | An open implementation of CLIP for training and running image-text embedding models, with a large catalogue of pretrained checkpoints for zero-shot classification and retrieval. |
| EmbeddingGemma (Gemma) | ★ 5.7k | Google DeepMind's Gemma repo, home to EmbeddingGemma, a 308M multilingual embedding model small enough to run on-device for RAG and semantic search. |
| Text Embeddings Inference (TEI) | ★ 5.1k | Hugging Face's Rust-based server for deploying embedding, reranking, and sequence-classification models with high throughput on GPU or CPU. |
| text2vec | ★ 5k | Turn Chinese or multilingual text into sentence embeddings, with training included |
| Infinity (Embeddings) | ★ 2.9k | A high-throughput serving engine for text embeddings, rerankers, CLIP, and ColPali models, exposing an OpenAI-compatible API. |
| ColPali | ★ 2.8k | A vision-language embedding model that indexes whole document page images for retrieval, avoiding the need to parse PDFs into text first. |
| Model2Vec | ★ 2.2k | A tool that distills any sentence transformer into a tiny, fast static embedding model (the Potion models) that runs on CPU without a neural network at inference. |