MCPcopy Index your code
hub / github.com/cpcdoy/rust-sbert

github.com/cpcdoy/rust-sbert @v0.5.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.5.0 ↗ · + Follow
59 symbols 130 edges 20 files 2 documented · 3%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Rust SBert Latest Version Latest Doc Build Status

Rust port of sentence-transformers using rust-bert and tch-rs.

Supports both rust-tokenizers and Hugging Face's tokenizers.

Supported models

  • distiluse-base-multilingual-cased: Supported languages: Arabic, Chinese, Dutch, English, French, German, Italian, Korean, Polish, Portuguese, Russian, Spanish, Turkish. Performance on the extended STS2017: 80.1

  • DistilRoBERTa-based classifiers

Usage

Example

The API is made to be very easy to use and enables you to create quality multilingual sentence embeddings in a straightforward way.

Load SBert model with weights by specifying the directory of the model:

let mut home: PathBuf = env::current_dir().unwrap();
home.push("path-to-model");

You can use different versions of the models that use different tokenizers:

// To use Hugging Face tokenizer
let sbert_model = SBertHF::new(home.to_str().unwrap(), None);

// To use Rust-tokenizers
let sbert_model = SBertRT::new(home.to_str().unwrap(), None);

Now, you can encode your sentences:

let texts = ["You can encode",
             "As many sentences",
             "As you want",
             "Enjoy ;)"];

let batch_size = 64;

let output = sbert_model.forward(texts.to_vec(), batch_size).unwrap();

The parameter batch_size can be left to None to let the model use its default value.

Then you can use the output sentence embedding in any application you want.

Convert models from Python to Rust

Firstly, get a model provided by UKPLabs (all models are here):

mkdir -p models/distiluse-base-multilingual-cased

wget -P models https://public.ukp.informatik.tu-darmstadt.de/reimers/sentence-transformers/v0.2/distiluse-base-multilingual-cased.zip

unzip models/distiluse-base-multilingual-cased.zip -d models/distiluse-base-multilingual-cased

Then, you need to convert the model in a suitable format (requires pytorch):

python utils/prepare_distilbert.py models/distiluse-base-multilingual-cased

A dockerized environment is also available for running the conversion script:

docker build -t tch-converter -f utils/Dockerfile .

docker run \
  -v $(pwd)/models/distiluse-base-multilingual-cased:/model \
  tch-converter:latest \
  python prepare_distilbert.py /model

Finally, set "output_attentions": true in distiluse-base-multilingual-cased/0_distilbert/config.json.

Extension points exported contracts — how you extend this code

Tokenizer (Interface)
(no doc) [3 implementers]
src/tokenizers/mod.rs

Core symbols most depended-on inside this repo

forward
called by 11
src/layers/dense.rs
pad_sort
called by 6
src/models/mod.rs
tokenize
called by 4
src/tokenizers/hf_tokenizers.rs
pre_tokenize
called by 3
src/tokenizers/hf_tokenizers.rs
convert_to_c_array
called by 2
utils/prepare_models.py
convert_to_c_array
called by 2
utils/prepare_distilbert.py
rand_string
called by 2
benches/bench_sbert.rs
forward
called by 2
src/models/sbert.rs

Shape

Function 27
Method 20
Class 9
Enum 2
Interface 1

Languages

Rust90%
Python10%

Modules by API surface

tests/test_sbert.rs7 symbols
src/layers/dense.rs6 symbols
benches/bench_sbert.rs6 symbols
src/models/sbert.rs5 symbols
src/tokenizers/rust_tokenizers_sentencepiece.rs4 symbols
src/tokenizers/rust_tokenizers.rs4 symbols
src/tokenizers/hf_tokenizers.rs4 symbols
src/models/distilroberta.rs4 symbols
src/layers/pooling.rs4 symbols
benches/bench_distilroberta.rs3 symbols
utils/test_sbert.py2 symbols
utils/prepare_models.py2 symbols

For agents

$ claude mcp add rust-sbert \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page