Sentence Similarity
sentence-transformers
Safetensors
Thai
feature-extraction
Inference Endpoints
chompk commited on
Commit
07ac188
·
verified ·
1 Parent(s): 3602db5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +23 -20
README.md CHANGED
@@ -87,26 +87,27 @@ from FlagEmbedding import BGEM3FlagModel
87
 
88
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
89
 
90
- sentences_1 = ["What is BGE M3?", "Defination of BM25"]
91
- sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
92
- "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
 
93
 
94
  output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
95
  output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
96
 
97
  # you can see the weight for each token:
98
  print(model.convert_id_to_token(output_1['lexical_weights']))
99
- # [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
100
- # {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
101
 
102
 
103
  # compute the scores via lexical mathcing
104
  lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
105
  print(lexical_scores)
106
- # 0.19554901123046875
107
 
108
  print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
109
- # 0.0
110
  ```
111
 
112
  - Multi-Vector (ColBERT)
@@ -115,17 +116,18 @@ from FlagEmbedding import BGEM3FlagModel
115
 
116
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True)
117
 
118
- sentences_1 = ["What is BGE M3?", "Defination of BM25"]
119
- sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
120
- "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
 
121
 
122
  output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
123
  output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
124
 
125
  print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
126
  print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
127
- # 0.7797
128
- # 0.4620
129
  ```
130
 
131
 
@@ -136,9 +138,10 @@ from FlagEmbedding import BGEM3FlagModel
136
 
137
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True)
138
 
139
- sentences_1 = ["What is BGE M3?", "Defination of BM25"]
140
- sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
141
- "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
 
142
 
143
  sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
144
 
@@ -147,11 +150,11 @@ print(model.compute_score(sentence_pairs,
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.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
151
- # 'sparse': [0.195556640625, 0.00879669189453125, 0.0, 0.1802978515625],
152
- # 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
153
- # 'sparse+dense': [0.482503205537796, 0.23454029858112335, 0.2332356721162796, 0.5122477412223816],
154
- # 'colbert+sparse+dense': [0.6013619303703308, 0.3255828022956848, 0.32089319825172424, 0.6232916116714478]
155
  # }
156
  ```
157
 
 
87
 
88
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
89
 
90
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
91
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
92
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
93
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
94
 
95
  output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
96
  output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
97
 
98
  # you can see the weight for each token:
99
  print(model.convert_id_to_token(output_1['lexical_weights']))
100
+ # [{'สถาบัน': 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},
101
+ # {'กรณี': 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}]
102
 
103
 
104
  # compute the scores via lexical mathcing
105
  lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
106
  print(lexical_scores)
107
+ # 0.10838508605957031
108
 
109
  print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
110
+ # 0.06803131103515625
111
  ```
112
 
113
  - Multi-Vector (ColBERT)
 
116
 
117
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True)
118
 
119
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
120
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
121
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
122
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
123
 
124
  output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
125
  output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
126
 
127
  print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
128
  print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
129
+ # tensor(0.5813)
130
+ # tensor(0.5718)
131
  ```
132
 
133
 
 
138
 
139
  model = BGEM3FlagModel('VISAI-AI/nitibench-ccl-auto-finetuned-bge-m3', use_fp16=True)
140
 
141
+ sentences_1 = ["สถาบันทางการเงินสามารถลงทุนในหลักทรัพย์ เป็นอัตราส่วนร้อยละสิบของเงินกองทุนทั้งหมดของสถาบันการเงินนั้น สำหรับการถือหรือมีหุ้นในทุกบริษัทรวมกันได้หรือไม่?",
142
+ "ในกรณีที่ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงิน เนื่องสถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนด จะต้องนำเสนอต่อบุคคลใดหรือหน่วยงานใดเพื่อเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
143
+ sentences_2 = ["พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 33 ภายใต้บังคับมาตรา 34 และมาตรา 35 ให้สถาบันการเงินลงทุนในหลักทรัพย์เพื่อเป็นกรรมสิทธิ์ของตนได้ ตามหลักเกณฑ์ที่ธนาคารแห่งประเทศไทยประกาศกำหนด",
144
+ "พระราชบัญญัติธุรกิจสถาบันการเงิน พ.ศ. 2551 มาตรา 97 ในกรณีที่สถาบันการเงินดำรงเงินกองทุนต่ำกว่าร้อยละสามสิบห้าของอัตราตามที่กำหนดในมาตรา 30 ให้ธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการของสถาบันการเงินนั้น เว้นแต่ในกรณีที่ธนาคารแห่งประเทศไทยเห็นว่าการมีคำสั่งปิดกิจการจะก่อให้เกิดผลกระทบ หรือความเสียหายต่อระบบเศรษฐกิจโดยรวมอย่างรุนแรง ธนาคารแห่งประเทศไทยอาจยังไม่สั่งปิดกิจการของสถาบันการเงินก็ได้\nเมื่อธนาคารแห่งประเทศไทยมีคำสั่งปิดกิจการตามวรรคหนึ่งแล้ว ให้เสนอรัฐมนตรีเพิกถอนใบอนุญาตของสถาบันการเงินนั้น"]
145
 
146
  sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
147
 
 
150
  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
151
 
152
  # {
153
+ # 'colbert': [0.5812647342681885, 0.5717734098434448, 0.6460118889808655, 0.8784525990486145],
154
+ # 'sparse': [0.1083984375, 0.07684326171875, 0.07061767578125, 0.314208984375],
155
+ # 'dense': [0.61865234375, 0.58935546875, 0.666015625, 0.8916015625],
156
+ # 'sparse+dense': [0.4485676884651184, 0.41851806640625, 0.4675496518611908, 0.6991373896598816],
157
+ # 'colbert+sparse+dense': [0.5016465187072754, 0.47982022166252136, 0.538934588432312, 0.7708634734153748]
158
  # }
159
  ```
160