Instructions to use ai-forever/FRIDA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use ai-forever/FRIDA with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("ai-forever/FRIDA") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Transformers
How to use ai-forever/FRIDA with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="ai-forever/FRIDA")# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("ai-forever/FRIDA") model = AutoModel.from_pretrained("ai-forever/FRIDA", device_map="auto") - Inference
- Notebooks
- Google Colab
- Kaggle
# Load model directly
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("ai-forever/FRIDA")
model = AutoModel.from_pretrained("ai-forever/FRIDA", device_map="auto")Model Card for FRIDA
FRIDA is a full-scale finetuned general text embedding model inspired by denoising architecture based on T5. The model is based on the encoder part of FRED-T5 model and continues research of text embedding models (ruMTEB, ru-en-RoSBERTa). It has been pre-trained on a Russian-English dataset and fine-tuned for improved performance on the target task.
For more model details please refer to our article (RU). The model's results are presented on the MTEB and rusBEIR leaderboards.
Usage
The model can be used as is with prefixes. It is recommended to use CLS pooling. The choice of prefix and pooling depends on the task.
We use the following basic rules to choose a prefix:
"search_query: "and"search_document: "prefixes are for answer or relevant paragraph retrieval"paraphrase: "prefix is for symmetric paraphrasing related tasks (STS, paraphrase mining, deduplication)"categorize: "prefix is for asymmetric matching of document title and body (e.g. news, scientific papers, social posts)"categorize_sentiment: "prefix is for any tasks that rely on sentiment features (e.g. hate, toxic, emotion)"categorize_topic: "prefix is intended for tasks where you need to group texts by topic"categorize_entailment: "prefix is for textual entailment task (NLI)
To better tailor the model to your needs, you can fine-tune it with relevant high-quality Russian and English datasets.
Below are examples of texts encoding using the Transformers and SentenceTransformers libraries.
Transformers
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, T5EncoderModel
def pool(hidden_state, mask, pooling_method="cls"):
if pooling_method == "mean":
s = torch.sum(hidden_state * mask.unsqueeze(-1).float(), dim=1)
d = mask.sum(axis=1, keepdim=True).float()
return s / d
elif pooling_method == "cls":
return hidden_state[:, 0]
inputs = [
#
"paraphrase: Π Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΎΠΉ ΠΎΠ±Π»Π°ΡΡΠΈ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΡ Π±Π°Π½Ρ, Π½ΠΎ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ",
"categorize_entailment: ΠΠ΅Π½ΡΠΈΠ½Ρ Π΄ΠΎΡΡΠ°Π²ΠΈΠ»ΠΈ Π² Π±ΠΎΠ»ΡΠ½ΠΈΡΡ, Π·Π° Π΅Π΅ ΠΆΠΈΠ·Π½Ρ ΡΠ΅ΠΉΡΠ°Ρ Π±ΠΎΡΡΡΡΡ Π²ΡΠ°ΡΠΈ.",
"search_query: Π‘ΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠΎΠ² Π½ΡΠΆΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ?",
#
"paraphrase: Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΈΠΌ Π±Π°Π½ΡΠΌ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ",
"categorize_entailment: ΠΠ΅Π½ΡΠΈΠ½Ρ ΡΠΏΠ°ΡΠ°ΡΡ Π²ΡΠ°ΡΠΈ.",
"search_document: Π§ΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ, ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠ°: ΠΎΠ΄ΠΈΠ½ Π½Π°ΠΏΠΈΡΠ΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π΄ΡΡΠ³ΠΎΠΉ β Π²ΠΊΡΡΡΠΈΠ²Π°Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π° ΡΡΠ΅ΡΠΈΠΉ ΠΏΡΠΎΠ²Π΅Π΄Π΅Ρ ΡΠ΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅."
]
tokenizer = AutoTokenizer.from_pretrained("ai-forever/FRIDA")
model = T5EncoderModel.from_pretrained("ai-forever/FRIDA")
tokenized_inputs = tokenizer(inputs, max_length=512, padding=True, truncation=True, return_tensors="pt")
with torch.no_grad():
outputs = model(**tokenized_inputs)
embeddings = pool(
outputs.last_hidden_state,
tokenized_inputs["attention_mask"],
pooling_method="cls" # or try "mean"
)
embeddings = F.normalize(embeddings, p=2, dim=1)
sim_scores = embeddings[:3] @ embeddings[3:].T
print(sim_scores.diag().tolist())
# [0.9360030293464661, 0.8591322302818298, 0.728583037853241]
SentenceTransformers
from sentence_transformers import SentenceTransformer
inputs = [
#
"paraphrase: Π Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΎΠΉ ΠΎΠ±Π»Π°ΡΡΠΈ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΡ Π±Π°Π½Ρ, Π½ΠΎ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ",
"categorize_entailment: ΠΠ΅Π½ΡΠΈΠ½Ρ Π΄ΠΎΡΡΠ°Π²ΠΈΠ»ΠΈ Π² Π±ΠΎΠ»ΡΠ½ΠΈΡΡ, Π·Π° Π΅Π΅ ΠΆΠΈΠ·Π½Ρ ΡΠ΅ΠΉΡΠ°Ρ Π±ΠΎΡΡΡΡΡ Π²ΡΠ°ΡΠΈ.",
"search_query: Π‘ΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠΎΠ² Π½ΡΠΆΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ?",
#
"paraphrase: Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΈΠΌ Π±Π°Π½ΡΠΌ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ",
"categorize_entailment: ΠΠ΅Π½ΡΠΈΠ½Ρ ΡΠΏΠ°ΡΠ°ΡΡ Π²ΡΠ°ΡΠΈ.",
"search_document: Π§ΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ, ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠ°: ΠΎΠ΄ΠΈΠ½ Π½Π°ΠΏΠΈΡΠ΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π΄ΡΡΠ³ΠΎΠΉ β Π²ΠΊΡΡΡΠΈΠ²Π°Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π° ΡΡΠ΅ΡΠΈΠΉ ΠΏΡΠΎΠ²Π΅Π΄Π΅Ρ ΡΠ΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅."
]
# loads model with CLS pooling
model = SentenceTransformer("ai-forever/FRIDA")
# embeddings are normalized by default
embeddings = model.encode(inputs, convert_to_tensor=True)
sim_scores = embeddings[:3] @ embeddings[3:].T
print(sim_scores.diag().tolist())
# [0.9360026717185974, 0.8591331243515015, 0.7285830974578857]
or using prompts (sentence-transformers>=2.4.0):
from sentence_transformers import SentenceTransformer
# loads model with CLS pooling
model = SentenceTransformer("ai-forever/FRIDA")
paraphrase = model.encode(["Π Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΎΠΉ ΠΎΠ±Π»Π°ΡΡΠΈ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΡ Π±Π°Π½Ρ, Π½ΠΎ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ", "Π―ΡΠΎΡΠ»Π°Π²ΡΠΊΠΈΠΌ Π±Π°Π½ΡΠΌ ΡΠ°Π·ΡΠ΅ΡΠΈΠ»ΠΈ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈΡΠ΅Π»Π΅ΠΉ"], prompt_name="paraphrase")
print(paraphrase[0] @ paraphrase[1].T) # 0.9360032
categorize_entailment = model.encode(["ΠΠ΅Π½ΡΠΈΠ½Ρ Π΄ΠΎΡΡΠ°Π²ΠΈΠ»ΠΈ Π² Π±ΠΎΠ»ΡΠ½ΠΈΡΡ, Π·Π° Π΅Π΅ ΠΆΠΈΠ·Π½Ρ ΡΠ΅ΠΉΡΠ°Ρ Π±ΠΎΡΡΡΡΡ Π²ΡΠ°ΡΠΈ.", "ΠΠ΅Π½ΡΠΈΠ½Ρ ΡΠΏΠ°ΡΠ°ΡΡ Π²ΡΠ°ΡΠΈ."], prompt_name="categorize_entailment")
print(categorize_entailment[0] @ categorize_entailment[1].T) # 0.8591322
query_embedding = model.encode("Π‘ΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠΎΠ² Π½ΡΠΆΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ?", prompt_name="search_query")
document_embedding = model.encode("Π§ΡΠΎΠ±Ρ Π²ΠΊΡΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ, ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠ°: ΠΎΠ΄ΠΈΠ½ Π½Π°ΠΏΠΈΡΠ΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π΄ΡΡΠ³ΠΎΠΉ β Π²ΠΊΡΡΡΠΈΠ²Π°Π½ΠΈΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ, Π° ΡΡΠ΅ΡΠΈΠΉ ΠΏΡΠΎΠ²Π΅Π΄Π΅Ρ ΡΠ΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅.", prompt_name="search_document")
print(query_embedding @ document_embedding.T) # 0.7285831
Results
FRIDA is the top-1 model among models with up to 3 billion parameters (11.08.26).
Authors
- SaluteDevices AI for B2C RnD Team.
- Artem Snegirev: HF profile, Github;
- Anna Maksimova HF profile;
- Aleksandr Abramov: HF profile, Github, Kaggle Competitions Master
Citation
@misc{TODO
}
Limitations
The model is designed to process texts in Russian, the quality in English is unknown. Maximum input text length is limited to 512 tokens.
- Downloads last month
- 123,385
Model tree for ai-forever/FRIDA
Base model
ai-forever/FRED-T5-1.7B
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="ai-forever/FRIDA")