This is a sentence-transformers model: it maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search. The model was trained on the LLeQA dataset for legal information retrieval in French.
1from sentence_transformers import SentenceTransformer
2sentences =["This is an example sentence","Each sentence is converted"]34model = SentenceTransformer('maastrichtlawtech/distilcamembert-lleqa')5embeddings = model.encode(sentences)6print(embeddings)
🤗 Transformers
Without sentence-transformers, you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
python
1from transformers import AutoTokenizer, AutoModel
2import torch
345#Mean Pooling - Take attention mask into account for correct averaging6defmean_pooling(model_output, attention_mask):7 token_embeddings = model_output[0]#First element of model_output contains all token embeddings8 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()9return torch.sum(token_embeddings * input_mask_expanded,1)/ torch.clamp(input_mask_expanded.sum(1),min=1e-9)1011# Sentences we want sentence embeddings for12sentences =['This is an example sentence','Each sentence is converted']1314# Load model from HuggingFace Hub15tokenizer = AutoTokenizer.from_pretrained('maastrichtlawtech/distilcamembert-lleqa')16model = AutoModel.from_pretrained('maastrichtlawtech/distilcamembert-lleqa')1718# Tokenize sentences19encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')2021# Compute token embeddings22with torch.no_grad():23 model_output = model(**encoded_input)2425# Perform pooling. In this case, mean pooling.26sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])27print(sentence_embeddings)
Evaluation
We evaluate the model on the test set of LLeQA, which consists of 195 legal questions with a knowlegde corpus of 27.9K candidate articles. We report the mean reciprocal rank (MRR), normalized discounted cumulative gainand (NDCG), mean average precision (MAP), and recall at various cut-offs (R@k).
MRR@10
NDCG@10
MAP@10
R@10
R@100
R@500
36.67
37.24
29.26
52.95
78.07
90.17
Training
Background
We utilize the distilcamembert-base model and fine-tuned it on 9.3K question-article pairs in French. We used a contrastive learning objective: given a short legal question, the model should predict which out of a set of sampled legal articles, was actually paired with it in the dataset. Formally, we compute the cosine similarity from each possible pairs from the batch. We then apply the cross entropy loss with a temperature of 0.05 by comparing with true pairs.
Hyperparameters
We trained the model on a single Tesla V100 GPU with 32GBs of memory during 20 epochs (i.e., 5.4k steps) using a batch size of 32. We used the AdamW optimizer with an initial learning rate of 2e-05, weight decay of 0.01, learning rate warmup over the first 50 steps, and linear decay of the learning rate. The sequence length was limited to 384 tokens.
Data
We use the Long-form Legal Question Answering (LLeQA) dataset to fine-tune the model. LLeQA is a French native dataset for studying legal information retrieval and question answering. It consists of a knowledge corpus of 27,941 statutory articles collected from the Belgian legislation, and 1,868 legal questions posed by Belgian citizens and labeled by experienced jurists with a comprehensive answer rooted in relevant articles from the corpus.
Citation
bibtex
1@article{louis2023interpretable,
2 author = {Louis, Antoine and van Dijck, Gijs and Spanakis, Gerasimos},
3 title = {Interpretable Long-Form Legal Question Answering with Retrieval-Augmented Large Language Models},
4 journal = {CoRR},
5 volume = {abs/2309.17050},
6 year = {2023},
7 url = {https://arxiv.org/abs/2309.17050},
8 eprinttype = {arXiv},
9 eprint = {2309.17050},
10}