Sentence Similarity
sentence-transformers
PyTorch
Safetensors
Thai
feature-extraction
chompk commited on
Commit
7fc47c7
·
verified ·
1 Parent(s): 81b8aa7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +128 -97
README.md CHANGED
@@ -1,144 +1,175 @@
1
  ---
2
- datasets: []
3
- language: []
4
  library_name: sentence-transformers
5
  pipeline_tag: sentence-similarity
6
  tags:
7
  - sentence-transformers
8
  - sentence-similarity
9
  - feature-extraction
10
- widget: []
 
 
 
 
 
 
 
11
  ---
12
 
13
- # SentenceTransformer
14
 
15
- This is a [sentence-transformers](https://www.SBERT.net) model trained. It maps sentences & paragraphs to a 1024-dimensional dense vector space and can be used for semantic textual similarity, semantic search, paraphrase mining, text classification, clustering, and more.
16
 
17
- ## Model Details
18
 
19
- ### Model Description
20
- - **Model Type:** Sentence Transformer
21
- <!-- - **Base model:** [Unknown](https://huggingface.co/unknown) -->
22
- - **Maximum Sequence Length:** 8192 tokens
23
- - **Output Dimensionality:** 1024 tokens
24
- - **Similarity Function:** Cosine Similarity
25
- <!-- - **Training Dataset:** Unknown -->
26
- <!-- - **Language:** Unknown -->
27
- <!-- - **License:** Unknown -->
28
 
29
- ### Model Sources
 
 
 
 
 
 
 
 
30
 
31
- - **Documentation:** [Sentence Transformers Documentation](https://sbert.net)
32
- - **Repository:** [Sentence Transformers on GitHub](https://github.com/UKPLab/sentence-transformers)
33
- - **Hugging Face:** [Sentence Transformers on Hugging Face](https://huggingface.co/models?library=sentence-transformers)
34
-
35
- ### Full Model Architecture
36
 
 
 
 
 
 
37
  ```
38
- SentenceTransformer(
39
- (0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
40
- (1): Pooling({'word_embedding_dimension': 1024, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
41
- (2): Normalize()
42
- )
43
  ```
44
 
45
- ## Usage
46
 
47
- ### Direct Usage (Sentence Transformers)
48
 
49
- First install the Sentence Transformers library:
50
 
51
- ```bash
52
- pip install -U sentence-transformers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  ```
 
 
 
54
 
55
- Then you can load this model and run inference.
56
  ```python
57
- from sentence_transformers import SentenceTransformer
58
-
59
- # Download from the 🤗 Hub
60
- model = SentenceTransformer("sentence_transformers_model_id")
61
- # Run inference
62
- sentences = [
63
- 'The weather is lovely today.',
64
- "It's so sunny outside!",
65
- 'He drove to the stadium.',
66
- ]
67
- embeddings = model.encode(sentences)
68
- print(embeddings.shape)
69
- # [3, 1024]
70
-
71
- # Get the similarity scores for the embeddings
72
- similarities = model.similarity(embeddings, embeddings)
73
- print(similarities.shape)
74
- # [3, 3]
75
- ```
76
 
77
- <!--
78
- ### Direct Usage (Transformers)
79
 
80
- <details><summary>Click to see the direct usage in Transformers</summary>
 
 
 
81
 
82
- </details>
83
- -->
84
 
85
- <!--
86
- ### Downstream Usage (Sentence Transformers)
 
 
87
 
88
- You can finetune this model on your own dataset.
89
 
90
- <details><summary>Click to expand</summary>
 
 
 
91
 
92
- </details>
93
- -->
 
94
 
95
- <!--
96
- ### Out-of-Scope Use
 
97
 
98
- *List how the model may foreseeably be misused and address what users ought not to do with the model.*
99
- -->
100
 
101
- <!--
102
- ## Bias, Risks and Limitations
 
 
103
 
104
- *What are the known or foreseeable issues stemming from this model? You could also flag here known failure cases or weaknesses of the model.*
105
- -->
106
 
107
- <!--
108
- ### Recommendations
 
 
 
109
 
110
- *What are recommendations with respect to the foreseeable issues? For example, filtering explicit content.*
111
- -->
112
 
113
- ## Training Details
 
 
 
114
 
115
- ### Framework Versions
116
- - Python: 3.10.14
117
- - Sentence Transformers: 3.0.1
118
- - Transformers: 4.34.0
119
- - PyTorch: 2.1.0+cu121
120
- - Accelerate: 0.21.0
121
- - Datasets: 2.21.0
122
- - Tokenizers: 0.14.1
123
 
124
- ## Citation
 
 
 
125
 
126
- ### BibTeX
127
 
128
- <!--
129
- ## Glossary
 
130
 
131
- *Clearly define terms in order to be accessible across audiences.*
132
- -->
 
 
 
 
 
 
133
 
134
- <!--
135
- ## Model Card Authors
136
 
137
- *Lists the people who create the model card, providing recognition and accountability for the detailed work that goes into its construction.*
138
- -->
139
 
140
- <!--
141
- ## Model Card Contact
142
 
143
- *Provides a way for people who have updates to the Model Card, suggestions, or questions, to contact the Model Card authors.*
144
- -->
 
 
 
 
 
 
 
 
 
 
 
1
  ---
 
 
2
  library_name: sentence-transformers
3
  pipeline_tag: sentence-similarity
4
  tags:
5
  - sentence-transformers
6
  - sentence-similarity
7
  - feature-extraction
8
+ license: mit
9
+ datasets:
10
+ - airesearch/WangchanX-Legal-ThaiCCL-RAG
11
+ - VISAI-AI/nitibench
12
+ language:
13
+ - th
14
+ base_model:
15
+ - BAAI/bge-m3
16
  ---
17
 
18
+ # Auto-Finetuned BGE-M3 CCL
19
 
20
+ This is a finetuned [`BAAI/bge-m3`](https://huggingface.co/BAAI/bge-m3) model on [`airesearch/WangchanX-Legal-ThaiCCL-RAG`](https://huggingface.co/datasets/airesearch/WangchanX-Legal-ThaiCCL-RAG) queries.
21
 
22
+ ## Finetuning Details
23
 
24
+ Apart from the original [`airesearch/WangchanX-Legal-ThaiCCL-RAG`](https://huggingface.co/datasets/airesearch/WangchanX-Legal-ThaiCCL-RAG) which requires human to rerank and remove irrelevant documents, the model was finetuned on a completely automated environment.
25
+ Specifically, given the query in the WangchanX-Legal-ThaiCCL-RAG dataset and a set of law sections to be retrieved, we follow the following procedure:
26
+ 1. Use [`BAAI/bge-m3`](https://huggingface.co/BAAI/bge-m3) to retrieve N positive law sections based on thresholding score of 0.8
27
+ 2. Among those N documents, we use [`BAAI/bge-reranker-v2-m3`](https://huggingface.co/BAAI/bge-reranker-v2-m3) to rerank documents and filtered any document that reranker scores less than 0.8 - achieving final positive law sections
28
+ 3. Using positives from (2), we finetuned BGE-M3 model
 
 
 
 
29
 
30
+ ## Model Performance
31
+ | **Dataset** | **Top-K** | **HR@k** | **Multi HR@k** | **Recall@k** | **MRR@k** | **Multi MRR@k** |
32
+ |:----------------:|:---------:|:-------:|:-------------:|:-----------:|:--------:|:---------------:|
33
+ | **NitiBench-CCL** | 1 | 0.735 | – | 0.735 | 0.735 | – |
34
+ | **NitiBench-CCL** | 5 | 0.906 | – | 0.906 | 0.805 | – |
35
+ | **NitiBench-CCL** | 10 | 0.938 | – | 0.938 | 0.809 | – |
36
+ | **NitiBench-Tax**| 1 | 0.480 | 0.140 | 0.255 | 0.480 | 0.255 |
37
+ | **NitiBench-Tax**| 5 | 0.740 | 0.220 | 0.411 | 0.565 | 0.320 |
38
+ | **NitiBench-Tax**| 10 | 0.800 | 0.280 | 0.499 | 0.574 | 0.333 |
39
 
40
+ ## Usage
 
 
 
 
41
 
42
+ Install:
43
+ ```
44
+ git clone https://github.com/FlagOpen/FlagEmbedding.git
45
+ cd FlagEmbedding
46
+ pip install -e .
47
  ```
48
+ or:
49
+ ```
50
+ pip install -U FlagEmbedding
 
 
51
  ```
52
 
 
53
 
 
54
 
55
+ ### Generate Embedding for text
56
 
57
+ - Dense Embedding
58
+ ```python
59
+ from FlagEmbedding import BGEM3FlagModel
60
+
61
+ model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-human-finetuned-bge-m3',
62
+ use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
63
+
64
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
65
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
66
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
67
+
68
+ embeddings_1 = model.encode(sentences_1,
69
+ batch_size=12,
70
+ max_length=8192, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
71
+ )['dense_vecs']
72
+ embeddings_2 = model.encode(sentences_2)['dense_vecs']
73
+ similarity = embeddings_1 @ embeddings_2.T
74
+ print(similarity)
75
+ # [[0.6265, 0.3477], [0.3499, 0.678 ]]
76
  ```
77
+ You also can use sentence-transformers and huggingface transformers to generate dense embeddings.
78
+ Refer to [baai_general_embedding](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/baai_general_embedding#usage) for details.
79
+
80
 
81
+ - Sparse Embedding (Lexical Weight)
82
  ```python
83
+ from FlagEmbedding import BGEM3FlagModel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-human-finetuned-bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
 
86
 
87
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
88
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
89
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
90
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
91
 
92
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
93
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
94
 
95
+ # you can see the weight for each token:
96
+ print(model.convert_id_to_token(output_1['lexical_weights']))
97
+ # [{'สถาบัน': 0.126, 'การเงิน': 0.10956, 'สามารถ': 0.07, 'ลงทุน': 0.1417, 'ใน': 0.01715, 'หลัก': 0.0758, 'ทรัพย์': 0.1702, 'อัตรา': 0.04926, 'ส่วน': 0.06107, 'ร้อย���ะ': 0.09, 'สิบ': 0.14, 'เงิน': 0.05026, 'กองทุน': 0.1205, 'ทั้งหมด': 0.03644, 'ถือ': 0.0987, 'หุ้น': 0.0928, 'ในทุก': 0.04883, 'บริษัท': 0.0999, 'รวม': 0.0835, 'กันได้': 0.09814, 'หรือไม่': 0.0398},
98
+ # {'กรณี': 0.0323, 'ธนาคาร': 0.08136, 'แห่งประเทศไทย': 0.151, 'คําสั่ง': 0.161, 'ปิด': 0.1583, 'กิจการ': 0.1199, 'สถาบัน': 0.08545, 'การเงิน': 0.1334, 'เนื่อง': 0.006992, 'ดํารง': 0.1523, 'เงิน': 0.12146, 'กองทุน': 0.1776, 'ต่ํากว่า': 0.1335, 'ร้อยละ': 0.10126, 'สาม': 0.02498, 'ห้า': 0.1158, 'อัตรา': 0.12256, 'กําหนด': 0.0572, 'จะต้อง': 0.07074, 'นําเสนอ': 0.1752, 'ต่อ': 0.0696, 'บุคคล': 0.0817, 'ใด': 0.0577, 'หรือ': 0.0248, 'หน่วยงาน': 0.076, 'เพ': 0.02034, 'ิก': 0.0921, 'ถอน': 0.1582, 'ใบ': 0.04617, 'อนุญาต': 0.179}]
99
 
 
100
 
101
+ # compute the scores via lexical mathcing
102
+ lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
103
+ print(lexical_scores)
104
+ # 0.10838508605957031
105
 
106
+ print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
107
+ # 0.06803131103515625
108
+ ```
109
 
110
+ - Multi-Vector (ColBERT)
111
+ ```python
112
+ from FlagEmbedding import BGEM3FlagModel
113
 
114
+ model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-human-finetuned-bge-m3', use_fp16=True)
 
115
 
116
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
117
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
118
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
119
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
120
 
121
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
122
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
123
 
124
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
125
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
126
+ # tensor(0.5813)
127
+ # tensor(0.5718)
128
+ ```
129
 
 
 
130
 
131
+ ### Compute score for text pairs
132
+ Input a list of text pairs, you can get the scores computed by different methods.
133
+ ```python
134
+ from FlagEmbedding import BGEM3FlagModel
135
 
136
+ model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-human-finetuned-bge-m3', use_fp16=True)
 
 
 
 
 
 
 
137
 
138
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
139
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
140
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
141
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
142
 
143
+ sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
144
 
145
+ print(model.compute_score(sentence_pairs,
146
+ max_passage_length=128, # a smaller max length leads to a lower latency
147
+ weights_for_different_modes=[0.4, 0.2, 0.4])) # weights_for_different_modes(w) is used to do weighted sum: w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
148
 
149
+ # {
150
+ # 'colbert': [0.5812647342681885, 0.5717734098434448, 0.6460118889808655, 0.8784525990486145],
151
+ # 'sparse': [0.1083984375, 0.07684326171875, 0.07061767578125, 0.314208984375],
152
+ # 'dense': [0.61865234375, 0.58935546875, 0.666015625, 0.8916015625],
153
+ # 'sparse+dense': [0.4485676884651184, 0.41851806640625, 0.4675496518611908, 0.6991373896598816],
154
+ # 'colbert+sparse+dense': [0.5016465187072754, 0.47982022166252136, 0.538934588432312, 0.7708634734153748]
155
+ # }
156
+ ```
157
 
 
 
158
 
159
+ ## Acknowledgement
160
+ Thanks to Pirat Pothavorn for evaluating the model performance on NitiBench, Supavish Punchun for finetuning the model. Additionally, we thank you all authors of this open-sourced project.
161
 
162
+ ## Citation
 
163
 
164
+ ### BibTeX
165
+ ```
166
+ @misc{akarajaradwong2025nitibenchcomprehensivestudiesllm,
167
+ title={NitiBench: A Comprehensive Studies of LLM Frameworks Capabilities for Thai Legal Question Answering},
168
+ author={Pawitsapak Akarajaradwong and Pirat Pothavorn and Chompakorn Chaksangchaichot and Panuthep Tasawong and Thitiwat Nopparatbundit and Sarana Nutanong},
169
+ year={2025},
170
+ eprint={2502.10868},
171
+ archivePrefix={arXiv},
172
+ primaryClass={cs.CL},
173
+ url={https://arxiv.org/abs/2502.10868},
174
+ }
175
+ ```