xiaowenbin commited on
Commit
e2f70eb
1 Parent(s): dc25e36

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +239 -12
README.md CHANGED
@@ -1061,31 +1061,258 @@ model-index:
1061
 
1062
  ---
1063
 
1064
- # Dmeta-embedding
 
 
1065
 
 
1066
 
1067
- <!--- Describe your model here -->
 
 
 
 
 
 
 
 
1068
 
1069
- ## Usage (Sentence-Transformers)
1070
 
1071
- Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
 
 
 
 
 
 
 
 
 
 
 
 
1072
 
1073
  ```
1074
  pip install -U sentence-transformers
1075
  ```
1076
 
1077
- Then you can use the model like this:
1078
-
1079
  ```python
1080
  from sentence_transformers import SentenceTransformer
1081
- sentences = ["This is an example sentence", "Each sentence is converted"]
1082
 
1083
- model = SentenceTransformer('{MODEL_NAME}')
1084
- embeddings = model.encode(sentences)
1085
- print(embeddings)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1086
  ```
1087
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088
 
1089
- ## Citing & Authors
1090
 
1091
- <!--- Describe where people can find more information -->
 
1061
 
1062
  ---
1063
 
1064
+ <div align="center">
1065
+ <img src="logo.png" alt="icon" width="100px"/>
1066
+ </div>
1067
 
1068
+ <h1 align="center">Dmeta-embedding</h1>
1069
 
1070
+ <h4 align="center">
1071
+ <p>
1072
+ <a href=#usage>用法</a> |
1073
+ <a href="#evaluation">评测</a> |
1074
+ <a href=#FAQ>FAQ</a> |
1075
+ <a href="#contact">联系</a> |
1076
+ <a href="#license">版权(免费商用)</a>
1077
+ <p>
1078
+ </h4>
1079
 
1080
+ Dmeta-embedding 是一款跨领域、跨任务、开箱即用的中文 Embedding 模型,适用于搜索、问答、智能客服、LLM+RAG 等各种业务场景。
1081
 
1082
+ 优势特点如下:
1083
+
1084
+ - 多任务、场景泛化性能优异,目前已取得 [MTEB](https://huggingface.co/spaces/mteb/leaderboard) 中文榜单第二成绩(2024.01.25)
1085
+ - 模型参数大小仅 400MB,对比参数量超过 GB 级模型,可以极大降低推理成本
1086
+ - 支持上下文窗口长度达到 1024,对于长文本检索、RAG 等场景更适配
1087
+
1088
+ ## 用法
1089
+
1090
+ 目前模型支持通过 [Sentence-Transformers](#sentence-transformers), [Langchain](#langchain), [Huggingface Transformers](#huggingface-transformers) 等主流框架进行推理,具体用法参考各个框架的示例。
1091
+
1092
+ ### Sentence-Transformers
1093
+
1094
+ Dmeta-embedding 模型支持通过 [sentence-transformers](https://www.SBERT.net) 来加载推理:
1095
 
1096
  ```
1097
  pip install -U sentence-transformers
1098
  ```
1099
 
 
 
1100
  ```python
1101
  from sentence_transformers import SentenceTransformer
 
1102
 
1103
+ texts1 = ["胡子长得太快怎么办?", "在香港哪里买手表好"]
1104
+ texts2 = ["胡子长得快怎么办?", "怎样使胡子不浓密!", "香港买手表哪里好", "在杭州手机到哪里买"]
1105
+
1106
+ model = SentenceTransformer('DMetaSoul/Dmeta-embedding')
1107
+ embs1 = model.encode(texts1, normalize_embeddings=True)
1108
+ embs2 = model.encode(texts2, normalize_embeddings=True)
1109
+
1110
+ # 计算两两相似度
1111
+ similarity = embs1 @ embs2.T
1112
+ print(similarity)
1113
+
1114
+ # 获取 texts1[i] 对应的最相似 texts2[j]
1115
+ for i in range(len(texts1)):
1116
+ scores = []
1117
+ for j in range(len(texts2)):
1118
+ scores.append([texts2[j], similarity[i][j]])
1119
+ scores = sorted(scores, key=lambda x:x[1], reverse=True)
1120
+
1121
+ print(f"查询文本:{texts1[i]}")
1122
+ for text2, score in scores:
1123
+ print(f"相似文本:{text2},打分:{score}")
1124
+ print()
1125
+ ```
1126
+
1127
+ 示例输出如下:
1128
+
1129
+ ```
1130
+ 查询文本:胡子长得太快怎么办?
1131
+ 相似文本:胡子长得快怎么办?,打分:0.9535336494445801
1132
+ 相似文本:怎样使胡子不浓密!,打分:0.6776421070098877
1133
+ 相似文本:香港买手表哪里好,打分:0.2297907918691635
1134
+ 相似文本:在杭州手机到哪里买,打分:0.11386542022228241
1135
+
1136
+ 查询文本:在香港哪里买手表好
1137
+ 相似文本:香港买手表哪里好,打分:0.9843372106552124
1138
+ 相似文本:在杭州手机到哪里买,打分:0.45211508870124817
1139
+ 相似文本:胡子长得快怎么办?,打分:0.19985519349575043
1140
+ 相似文本:怎样使胡子不浓密!,打分:0.18558596074581146
1141
+ ```
1142
+
1143
+ ### Langchain
1144
+
1145
+ Dmeta-embedding 模型支持通过 LLM 工具框架 [langchain](https://www.langchain.com/) 来加载推理:
1146
+
1147
+ ```
1148
+ pip install -U langchain
1149
  ```
1150
 
1151
+ ```python
1152
+ import torch
1153
+ import numpy as np
1154
+ from langchain.embeddings import HuggingFaceEmbeddings
1155
+
1156
+ model_name = "DMetaSoul/Dmeta-embedding"
1157
+ model_kwargs = {'device': 'cuda' if torch.cuda.is_available() else 'cpu'}
1158
+ encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
1159
+
1160
+ model = HuggingFaceEmbeddings(
1161
+ model_name=model_name,
1162
+ model_kwargs=model_kwargs,
1163
+ encode_kwargs=encode_kwargs,
1164
+ )
1165
+
1166
+ texts1 = ["胡子长得太快怎么办?", "在香港哪里买手表好"]
1167
+ texts2 = ["胡子长得快怎么办?", "怎样使胡子不浓密!", "香港买手表哪里好", "在杭州手机到哪里买"]
1168
+
1169
+ embs1 = model.embed_documents(texts1)
1170
+ embs2 = model.embed_documents(texts2)
1171
+ embs1, embs2 = np.array(embs1), np.array(embs2)
1172
+
1173
+ # 计算两两相似度
1174
+ similarity = embs1 @ embs2.T
1175
+ print(similarity)
1176
+
1177
+ # 获取 texts1[i] 对应的最相似 texts2[j]
1178
+ for i in range(len(texts1)):
1179
+ scores = []
1180
+ for j in range(len(texts2)):
1181
+ scores.append([texts2[j], similarity[i][j]])
1182
+ scores = sorted(scores, key=lambda x:x[1], reverse=True)
1183
+
1184
+ print(f"查询文本:{texts1[i]}")
1185
+ for text2, score in scores:
1186
+ print(f"相似文本:{text2},打分:{score}")
1187
+ print()
1188
+ ```
1189
+
1190
+ ### HuggingFace Transformers
1191
+
1192
+ Dmeta-embedding 模型支持通过 [HuggingFace Transformers](https://huggingface.co/docs/transformers/index) 框架来加载推理:
1193
+
1194
+ ```
1195
+ pip install -U transformers
1196
+ ```
1197
+
1198
+ ```python
1199
+ import torch
1200
+ from transformers import AutoTokenizer, AutoModel
1201
+
1202
+
1203
+ def mean_pooling(model_output, attention_mask):
1204
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
1205
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
1206
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
1207
+
1208
+ def cls_pooling(model_output):
1209
+ return model_output[0][:, 0]
1210
+
1211
+
1212
+ texts1 = ["胡子长得太快怎么办?", "在香港哪里买手表好"]
1213
+ texts2 = ["胡子长得快怎么办?", "怎样使胡子不浓密!", "香港买手表哪里好", "在杭州手机到哪里买"]
1214
+
1215
+ tokenizer = AutoTokenizer.from_pretrained('DMetaSoul/Dmeta-embedding')
1216
+ model = AutoModel.from_pretrained('DMetaSoul/Dmeta-embedding')
1217
+ model.eval()
1218
+
1219
+ with torch.no_grad():
1220
+ inputs1 = tokenizer(texts1, padding=True, truncation=True, return_tensors='pt')
1221
+ inputs2 = tokenizer(texts2, padding=True, truncation=True, return_tensors='pt')
1222
+
1223
+ model_output1 = model(**inputs1)
1224
+ model_output2 = model(**inputs2)
1225
+ embs1, embs2 = cls_pooling(model_output1), cls_pooling(model_output2)
1226
+ embs1 = torch.nn.functional.normalize(embs1, p=2, dim=1).numpy()
1227
+ embs2 = torch.nn.functional.normalize(embs2, p=2, dim=1).numpy()
1228
+
1229
+ # 计算两两相似度
1230
+ similarity = embs1 @ embs2.T
1231
+ print(similarity)
1232
+
1233
+ # 获取 texts1[i] 对应的最相似 texts2[j]
1234
+ for i in range(len(texts1)):
1235
+ scores = []
1236
+ for j in range(len(texts2)):
1237
+ scores.append([texts2[j], similarity[i][j]])
1238
+ scores = sorted(scores, key=lambda x:x[1], reverse=True)
1239
+
1240
+ print(f"查询文本:{texts1[i]}")
1241
+ for text2, score in scores:
1242
+ print(f"相似文本:{text2},打分:{score}")
1243
+ print()
1244
+ ```
1245
+
1246
+ ## Evaluation
1247
+
1248
+ Dmeta-embedding 模型在 [MTEB 中文榜单](https://huggingface.co/spaces/mteb/leaderboard)取得开源第一的成绩(2024.01.25,Baichuan 榜单第一、未开源),具体关于评测数据和代码可参考 MTEB 官方[仓库](https://github.com/embeddings-benchmark/mteb)。
1249
+
1250
+ **MTEB Chinese**:
1251
+
1252
+ 该[榜单数据集](https://github.com/FlagOpen/FlagEmbedding/blob/master/C_MTEB)由智源研究院团队(BAAI)收集整理,包含 6 个经典任务共计 35 个中文数据集,涵盖了分类、检索、排序、句对、STS 等任务,是目前 Embedding 模型全方位能力评测的全球权威榜单。
1253
+
1254
+ | Model | Vendor | Embedding dimension | Avg | Retrieval | STS | PairClassification | Classification | Reranking | Clustering |
1255
+ |:-------------------------------------------------------------------------------------------------------- | ------ |:-------------------:|:-----:|:---------:|:-----:|:------------------:|:--------------:|:---------:|:----------:|
1256
+ | [Dmeta-embedding](https://huggingface.co/DMetaSoul/Dmeta-embedding) | 数元灵 | 1024 | 67.51 | 70.41 | 64.09 | 88.92 | 70 | 67.17 | 50.96 |
1257
+ | [gte-large-zh](https://huggingface.co/thenlper/gte-large-zh) | 阿里达摩院 | 1024 | 66.72 | 72.49 | 57.82 | 84.41 | 71.34 | 67.4 | 53.07 |
1258
+ | [BAAI/bge-large-zh-v1.5](https://huggingface.co/BAAI/bge-large-zh-v1.5) | 智源 | 1024 | 64.53 | 70.46 | 56.25 | 81.6 | 69.13 | 65.84 | 48.99 |
1259
+ | [BAAI/bge-base-zh-v1.5](https://huggingface.co/BAAI/bge-base-zh-v1.5) | 智源 | 768 | 63.13 | 69.49 | 53.72 | 79.75 | 68.07 | 65.39 | 47.53 |
1260
+ | [text-embedding-ada-002(OpenAI)](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings) | OpenAI | 1536 | 53.02 | 52.0 | 43.35 | 69.56 | 64.31 | 54.28 | 45.68 |
1261
+ | [text2vec-base](https://huggingface.co/shibing624/text2vec-base-chinese) | 个人 | 768 | 47.63 | 38.79 | 43.41 | 67.41 | 62.19 | 49.45 | 37.66 |
1262
+ | [text2vec-large](https://huggingface.co/GanymedeNil/text2vec-large-chinese) | 个人 | 1024 | 47.36 | 41.94 | 44.97 | 70.86 | 60.66 | 49.16 | 30.02 |
1263
+
1264
+ ## FAQ
1265
+
1266
+ <details>
1267
+ <summary>1. 为何模型多任务、场景泛化能力优异,可开箱即用适配诸多应用场景?</summary>
1268
+
1269
+ <!-- ### 为何模型多任务、场景泛化能力优异,可开箱即用适配诸多应用场景? -->
1270
+
1271
+ 简单来说,模型优异的泛化能力来自于预训练数据的广泛和多样,以及模型优化时面向多任务场景设计了不同优化目标。
1272
+
1273
+ 具体来说,技术要点有:
1274
+
1275
+ 1)首先是大规模弱标签对比学习。业界经验表明开���即用的语言模型在 Embedding 相关任务上表现不佳,但由于监督数据标注、获取成本较高,因此大规模、高质量的弱标签学习成为一条可选技术路线。通过在互联网上论坛、新闻、问答社区、百科等半结构化数据中提取弱标签,并利用大模型进行低质过滤,得到 10 亿级别弱监督文本对数据。
1276
+
1277
+ 2)其次是高质量监督学习。我们收集整理了大规模开源标注的语句对数据集,包含百科、教育、金融、医疗、法律、新闻、学术等多个领域共计 3000 万句对样本。同时挖掘难负样本对,借助对比学习更好的进行模型优化。
1278
+
1279
+ 3)最后是检索任务针对性优化。考虑到搜索、问答以及 RAG 等场景是 Embedding 模型落地的重要应用阵地,为了增强模型跨领域、跨场景的效果性能,我们专门针对检索任务进行了模型优化,核心在于从问答、检索等数据中挖掘难负样本,借助稀疏和稠密检索等多种手段,构造百万级难负样本对数据集,显著提升了模型跨领域的检索性能。
1280
+
1281
+ </details>
1282
+
1283
+ <details>
1284
+ <summary>2. 模型可以商用吗?</summary>
1285
+
1286
+ <!-- ### 模型可以商用吗 -->
1287
+
1288
+ 我们的开源模型基于 Apache-2.0 协议,完全支持免费商用。
1289
+
1290
+ </details>
1291
+
1292
+ <details>
1293
+ <summary>3. 如何复现 MTEB 评测结果?</summary>
1294
+
1295
+ <!-- ### 如何复现 MTEB 评测结果? -->
1296
+
1297
+ 我们在模型仓库中提供了脚本 mteb_eval.py,您可以直接运行此脚本来复现我们的评测结果。
1298
+
1299
+ </details>
1300
+
1301
+ <details>
1302
+ <summary>4. 后续规划有哪些?</summary>
1303
+
1304
+ <!-- ### 后续规划有哪些? -->
1305
+
1306
+ 我们将不断致力于为社区提供效果优异、推理轻量、多场景开箱即用的 Embedding 模型,同时我们也会将 Embedding 逐步整合到目前已经的技术生态中,跟随社区一起成长!
1307
+
1308
+ </details>
1309
+
1310
+ ## Contact
1311
+
1312
+ 您如果在使用过程中,遇到任何问题,欢迎前往[讨论区](https://huggingface.co/DMetaSoul/Dmeta-embedding/discussions)建言献策。
1313
+
1314
+ 您也可以联系我们:赵中昊 <zhongh@dmetasoul.com>, 肖文斌 <xiaowenbin@dmetasoul.com>, 孙凯 <xiaowenbin@dmetasoul.com>
1315
 
1316
+ ## License
1317
 
1318
+ Dmeta-embedding 模型采用 Apache-2.0 License,开源模型可以进行免费商用私有部署。