Sentence Similarity
sentence-transformers
Safetensors
English
bert
retrieval
legal
agriculture
eu-law
cap
text-embeddings-inference
Instructions to use stonarini/grepac-e5-small with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use stonarini/grepac-e5-small with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("stonarini/grepac-e5-small") sentences = [ "That is a happy person", "That is a happy dog", "That is a very happy person", "Today is a sunny day" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
feat: grepac-e5-small v1
Browse files- .gitattributes +1 -0
- 1_Pooling/config.json +5 -0
- 2_Normalize/config.json +4 -0
- README.md +89 -0
- config.json +30 -0
- config_sentence_transformers.json +14 -0
- model.safetensors +3 -0
- modules.json +20 -0
- sentence_bert_config.json +10 -0
- tokenizer.json +3 -0
- tokenizer_config.json +17 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
1_Pooling/config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embedding_dimension": 384,
|
| 3 |
+
"pooling_mode": "mean",
|
| 4 |
+
"include_prompt": true
|
| 5 |
+
}
|
2_Normalize/config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"module_input_name": "sentence_embedding",
|
| 3 |
+
"module_output_name": "sentence_embedding"
|
| 4 |
+
}
|
README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: [en]
|
| 3 |
+
license: mit
|
| 4 |
+
library_name: sentence-transformers
|
| 5 |
+
pipeline_tag: sentence-similarity
|
| 6 |
+
base_model: intfloat/multilingual-e5-small
|
| 7 |
+
tags: [sentence-transformers, retrieval, legal, agriculture, eu-law, cap]
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# grepac-e5-small
|
| 11 |
+
|
| 12 |
+
`intfloat/multilingual-e5-small` fine-tuned to find the right article of the EU Common
|
| 13 |
+
Agricultural Policy regulations for a farmer's or advisor's question. It powers
|
| 14 |
+
[grePAC](https://huggingface.co/spaces/stonarini/grepac). Code and training pipeline: [github.com/stonarini/grepac](https://github.com/stonarini/grepac).
|
| 15 |
+
|
| 16 |
+
Corpus: Regulation (EU) 2021/2115 (consolidated 2026-08-18) and Regulation (EU) 2021/2116
|
| 17 |
+
(consolidated 2024-05-25), articles only, split into 911 paragraphs.
|
| 18 |
+
|
| 19 |
+
## Usage
|
| 20 |
+
|
| 21 |
+
Same prefixes as the base model.
|
| 22 |
+
|
| 23 |
+
```python
|
| 24 |
+
from sentence_transformers import SentenceTransformer
|
| 25 |
+
model = SentenceTransformer("stonarini/grepac-e5-small")
|
| 26 |
+
q = model.encode(["query: When will I receive my CAP payments?"], normalize_embeddings=True)
|
| 27 |
+
p = model.encode(["passage: Article 44 Payment to beneficiaries\n..."], normalize_embeddings=True)
|
| 28 |
+
print(q @ p.T)
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
Passages were embedded as `passage: Article <n> — <title>\n<lead-in>\n<paragraph text>`.
|
| 32 |
+
|
| 33 |
+
## Training data
|
| 34 |
+
|
| 35 |
+
Generated automatically, no hand-written training examples:
|
| 36 |
+
|
| 37 |
+
- Mistral 7B Instruct v0.3 (Q4_K_M, local via Ollama) wrote 4 questions per paragraph
|
| 38 |
+
(2 farmer-style, 1 keyword search, 1 advisor-style), plus rule-based queries from article
|
| 39 |
+
titles and from the definitions.
|
| 40 |
+
- Filters: a question was dropped if the base model did not rank its article in the top 20
|
| 41 |
+
(likely a misreading, 257 dropped), if it was too similar to a test question (cosine ≥ 0.93,
|
| 42 |
+
153 dropped), or if it was a duplicate (10).
|
| 43 |
+
- Each question got a hard negative: a paragraph from another article that the base model
|
| 44 |
+
scored close to the right one.
|
| 45 |
+
- Result: 2,933 triplets over 239 articles; 287 questions from 26 other articles held out for
|
| 46 |
+
validation.
|
| 47 |
+
|
| 48 |
+
Training: MultipleNegativesRankingLoss with hard negatives, no duplicate texts in a batch,
|
| 49 |
+
2 epochs, batch 32, learning rate 2e-5, max length 256, on a 4-core laptop CPU (73 minutes).
|
| 50 |
+
|
| 51 |
+
## Evaluation
|
| 52 |
+
|
| 53 |
+
60 hand-written and hand-checked questions (38 practical, 18 technical, 4 in Italian, French,
|
| 54 |
+
German and Spanish), each with the article(s) that answer it. None were produced by the
|
| 55 |
+
training LLM. Scored at article level.
|
| 56 |
+
|
| 57 |
+
| | right article first | in top 3 | in top 5 | MRR@10 |
|
| 58 |
+
|---|---|---|---|---|
|
| 59 |
+
| base `multilingual-e5-small` | 0.667 | 0.833 | 0.867 | 0.750 |
|
| 60 |
+
| **grepac-e5-small** | **0.717** | **0.900** | **0.900** | **0.797** |
|
| 61 |
+
|
| 62 |
+
By question type, right article in the top 5: practical 0.816 → 0.868, technical 1.000 → 1.000,
|
| 63 |
+
other languages 0.750 → 0.750.
|
| 64 |
+
|
| 65 |
+
On the 287 held-out generated questions: right article first 0.735 → 0.794, top 5 0.937 → 0.962.
|
| 66 |
+
|
| 67 |
+
**How much to trust this:** 60 questions is a small test. Question by question, 11 got better
|
| 68 |
+
and 8 got worse, which is not statistically significant (sign test p ≈ 0.65). The gains are
|
| 69 |
+
concentrated on farmer-style wording (for example "What counts as a farmer?" moved from rank 31
|
| 70 |
+
to 2), the losses are mostly small. Treat this version as a working first release; it will be
|
| 71 |
+
re-evaluated on real searches from the site.
|
| 72 |
+
|
| 73 |
+
Things that did not help, measured on the same 60 questions: adding a fixed "EU Common
|
| 74 |
+
Agricultural Policy" context or synonyms to the query, and adding the chapter headings or the
|
| 75 |
+
regulation name to the passages.
|
| 76 |
+
|
| 77 |
+
## Known limitations
|
| 78 |
+
|
| 79 |
+
- The LLM misread some passages. Example: for the capping of payments (2115 Art. 17) it wrote
|
| 80 |
+
questions about farm *income* above EUR 60,000, so questions about the payment cap for large
|
| 81 |
+
farms got worse.
|
| 82 |
+
- Trained on English questions only. The other languages were checked on 4 questions.
|
| 83 |
+
- Annexes are not in the corpus (for example the GAEC standards in Annex III of 2021/2115).
|
| 84 |
+
- Built for this corpus; not tested on other legal texts.
|
| 85 |
+
|
| 86 |
+
## License
|
| 87 |
+
|
| 88 |
+
MIT, as the base model. Corpus: EUR-Lex, © European Union, reused under the Commission's
|
| 89 |
+
reuse policy. Training questions generated with Mistral 7B Instruct v0.3 (Apache-2.0).
|
config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_cross_attention": false,
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BertModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"classifier_dropout": null,
|
| 9 |
+
"dtype": "float32",
|
| 10 |
+
"eos_token_id": 2,
|
| 11 |
+
"hidden_act": "gelu",
|
| 12 |
+
"hidden_dropout_prob": 0.1,
|
| 13 |
+
"hidden_size": 384,
|
| 14 |
+
"initializer_range": 0.02,
|
| 15 |
+
"intermediate_size": 1536,
|
| 16 |
+
"is_decoder": false,
|
| 17 |
+
"layer_norm_eps": 1e-12,
|
| 18 |
+
"max_position_embeddings": 512,
|
| 19 |
+
"model_type": "bert",
|
| 20 |
+
"num_attention_heads": 12,
|
| 21 |
+
"num_hidden_layers": 12,
|
| 22 |
+
"pad_token_id": 1,
|
| 23 |
+
"position_embedding_type": "absolute",
|
| 24 |
+
"tie_word_embeddings": true,
|
| 25 |
+
"tokenizer_class": "XLMRobertaTokenizer",
|
| 26 |
+
"transformers_version": "5.22.1",
|
| 27 |
+
"type_vocab_size": 2,
|
| 28 |
+
"use_cache": false,
|
| 29 |
+
"vocab_size": 250037
|
| 30 |
+
}
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"pytorch": "2.15.0+cu132",
|
| 4 |
+
"sentence_transformers": "6.2.0",
|
| 5 |
+
"transformers": "5.22.1"
|
| 6 |
+
},
|
| 7 |
+
"default_prompt_name": null,
|
| 8 |
+
"model_type": "SentenceTransformer",
|
| 9 |
+
"prompts": {
|
| 10 |
+
"document": "",
|
| 11 |
+
"query": ""
|
| 12 |
+
},
|
| 13 |
+
"similarity_fn_name": "cosine"
|
| 14 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:81485d735cbdb202703ed7b2847ea0474ddf92f62c8748fb88915c12f925708a
|
| 3 |
+
size 470637416
|
modules.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.base.modules.transformer.Transformer"
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"idx": 1,
|
| 10 |
+
"name": "1",
|
| 11 |
+
"path": "1_Pooling",
|
| 12 |
+
"type": "sentence_transformers.sentence_transformer.modules.pooling.Pooling"
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"idx": 2,
|
| 16 |
+
"name": "2",
|
| 17 |
+
"path": "2_Normalize",
|
| 18 |
+
"type": "sentence_transformers.base.modules.normalize.Normalize"
|
| 19 |
+
}
|
| 20 |
+
]
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"transformer_task": "feature-extraction",
|
| 3 |
+
"modality_config": {
|
| 4 |
+
"text": {
|
| 5 |
+
"method": "forward",
|
| 6 |
+
"method_output_name": "last_hidden_state"
|
| 7 |
+
}
|
| 8 |
+
},
|
| 9 |
+
"module_output_name": "token_embeddings"
|
| 10 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb02bfe42ffe0fe115de5c74f76c6480d730e60cc1f6ae567ff4d82cb305a113
|
| 3 |
+
size 17082988
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": true,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<s>",
|
| 5 |
+
"clean_up_tokenization_spaces": true,
|
| 6 |
+
"cls_token": "<s>",
|
| 7 |
+
"eos_token": "</s>",
|
| 8 |
+
"is_local": false,
|
| 9 |
+
"local_files_only": true,
|
| 10 |
+
"mask_token": "<mask>",
|
| 11 |
+
"model_max_length": 256,
|
| 12 |
+
"pad_token": "<pad>",
|
| 13 |
+
"sep_token": "</s>",
|
| 14 |
+
"sp_model_kwargs": {},
|
| 15 |
+
"tokenizer_class": "XLMRobertaTokenizer",
|
| 16 |
+
"unk_token": "<unk>"
|
| 17 |
+
}
|