bge-m3 for Kjarni
Multilingual text embeddings, packaged to run locally with Kjarni: one native library, with no Python runtime, server or cloud service. Texts in more than 100 languages map to the same 1024-dimensional space, so a question in English finds its answer in German, and inputs can be up to 8192 tokens long. The model is about 2.3 GB and downloads on first use.
Usage
Tested with Kjarni 0.8.2. Each example compares an English question with three texts.
Command line
curl -fsSL https://kjarni.ai/install.sh | sh # Windows: irm https://kjarni.ai/install.ps1 | iex
kjarni similarity -q -m bge-m3 "How do I reset my password?" "Wie setze ich mein Passwort zurück?"
0.944839
Python
# pip install kjarni
from kjarni import Embedder
embedder = Embedder(model="bge-m3", quiet=True)
query = "How do I reset my password?"
for text in [
"Wie setze ich mein Passwort zurück?",
"¿Cómo restablezco mi contraseña?",
"What time does the shop open?",
]:
print(f"{embedder.similarity(query, text):.3f} {text}")
0.945 Wie setze ich mein Passwort zurück?
0.939 ¿Cómo restablezco mi contraseña?
0.493 What time does the shop open?
Rust
// cargo add kjarni anyhow tokio --features tokio/full
use kjarni::Embedder;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let embedder = Embedder::new("bge-m3").await?;
let query = "How do I reset my password?";
for text in [
"Wie setze ich mein Passwort zurück?",
"¿Cómo restablezco mi contraseña?",
"What time does the shop open?",
] {
println!("{:.3} {text}", embedder.similarity(query, text).await?);
}
Ok(())
}
C#
// dotnet add package Kjarni
using Kjarni;
using var embedder = new Embedder("bge-m3", quiet: true);
var query = "How do I reset my password?";
foreach (var text in new[] { "Wie setze ich mein Passwort zurück?", "¿Cómo restablezco mi contraseña?", "What time does the shop open?" })
Console.WriteLine($"{embedder.Similarity(query, text):F3} {text}");
C++ (C++23)
#include <kjarni.hpp>
#include <cstdio>
int main() {
kjarni::EmbedderOptions options;
options.model = "bge-m3";
auto embedder = kjarni::Embedder::create(options);
if (!embedder) return 1;
auto query = embedder->encode("How do I reset my password?");
if (!query) return 1;
for (const char* text : {"Wie setze ich mein Passwort zurück?",
"¿Cómo restablezco mi contraseña?",
"What time does the shop open?"}) {
auto e = embedder->encode(text);
if (!e) return 1;
std::printf("%.3f %s\n", kjarni::cosine(*query, *e), text);
}
}
Go
// go get github.com/kjarni-ai/kjarni-go
package main
import (
"fmt"
"log"
kjarni "github.com/kjarni-ai/kjarni-go"
)
func main() {
embedder, err := kjarni.NewEmbedder("bge-m3", kjarni.WithQuiet(true))
if err != nil {
log.Fatal(err)
}
defer embedder.Close()
query := "How do I reset my password?"
for _, text := range []string{
"Wie setze ich mein Passwort zurück?",
"¿Cómo restablezco mi contraseña?",
"What time does the shop open?",
} {
score, err := embedder.Similarity(query, text)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%.3f %s\n", score, text)
}
}
See the Kjarni documentation for other languages and installation details.
What changed from the original
This is not a new model. The weights are unchanged: every tensor is bit-for-bit the
same as in the original's pytorch_model.bin. Only the packaging differs:
- The weights are stored as safetensors.
- Only the dense embedding is included. The original's sparse (
sparse_linear.pt) and multi-vector (colbert_linear.pt) heads, its ONNX export and its images are left out. - The tokenizer and configuration files are the original's, unchanged.
Kjarni takes the first token's output and normalises it, as the original does for dense embeddings. On the three examples above, Kjarni's similarities match transformers running the original model to four decimal places.
Attribution
The model is BAAI/bge-m3, by the Beijing Academy of Artificial Intelligence (BAAI). All credit for the model, its training and its evaluation belongs to them. See the original model card for benchmarks, the sparse and multi-vector modes, and limitations.
Kjarni and this packaging are not affiliated with or endorsed by BAAI.
This repository is released under the MIT License, the same license as the original model.
Citation
@misc{bge-m3,
title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
year={2024},
eprint={2402.03216},
archivePrefix={arXiv},
primaryClass={cs.CL}
}