Text2vec-Large-Chinese 私有化部署:中文句向量服务搭建

记录中文句向量模型 text2vec-large-chinese 的私有化部署方式:基于 sentence-transformers 封装编码函数,给出可直接使用的服务端代码与调用示例。

模型部署

私有化部署(text2vec-large-chinese

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description: Base sentence model function, add encode function.
Parts of this file is adapted from the sentence-transformers library at https://github.com/UKPLab/sentence-transformers.
"""
import json
import os
import sys
from enum import Enum
from typing import List, Union, Optional

import numpy as np
import torch
import uvicorn
from fastapi import FastAPI
from loguru import logger
from tqdm.auto import trange
from tqdm.autonotebook import trange
from transformers import BertTokenizer, BertModel

project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
sys.path.insert(0, project_path)

from config.paths import text2vec_model_path
from config.message import EmbeddingRequest

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
os.environ["TOKENIZERS_PARALLELISM"] = "TRUE"


class EncoderType(Enum):
FIRST_LAST_AVG = 0
LAST_AVG = 1
CLS = 2
POOLER = 3
MEAN = 4

def __str__(self):
return self.name

@staticmethod
def from_string(s):
try:
return EncoderType[s]
except KeyError:
raise ValueError()


class SentenceModel:
def __init__(
self,
model_name_or_path: str = "shibing624/text2vec-base-chinese",
encoder_type: Union[str, EncoderType] = "FIRST_LAST_AVG",
max_seq_length: int = 128,
device: Optional[str] = None,
):
"""
Initializes the base sentence model.

:param model_name_or_path: The name of the model to load from the huggingface models library.
:param encoder_type: The type of encoder to use, See the EncoderType enum for options:
FIRST_LAST_AVG, LAST_AVG, CLS, POOLER(cls + dense), MEAN(mean of last_hidden_state)
:param max_seq_length: The maximum sequence length.
:param device: Device (like 'cuda' / 'cpu') that should be used for computation. If None, checks if GPU.

bert model: https://huggingface.co/transformers/model_doc/bert.html?highlight=bert#transformers.BertModel.forward
BERT return: <last_hidden_state>, <pooler_output> [hidden_states, attentions]
Note that: in doc, it says <last_hidden_state> is better semantic summery than <pooler_output>.
thus, we use <last_hidden_state>.
"""
self.model_name_or_path = model_name_or_path
encoder_type = EncoderType.from_string(encoder_type) if isinstance(encoder_type, str) else encoder_type
if encoder_type not in list(EncoderType):
raise ValueError(f"encoder_type must be in {list(EncoderType)}")
self.encoder_type = encoder_type
self.max_seq_length = max_seq_length
self.tokenizer = BertTokenizer.from_pretrained(model_name_or_path)
self.bert = BertModel.from_pretrained(model_name_or_path)
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
self.device = torch.device(device)
logger.debug("Use device: {}".format(self.device))
self.bert.to(self.device)
self.results = {} # Save training process evaluation result

def __str__(self):
return f"<SentenceModel: {self.model_name_or_path}, encoder_type: {self.encoder_type}, " \
f"max_seq_length: {self.max_seq_length}>"

def get_sentence_embeddings(self, input_ids, attention_mask, token_type_ids):
"""
Returns the model output by encoder_type as embeddings.

Utility function for self.bert() method.
"""
model_output = self.bert(input_ids, attention_mask, token_type_ids, output_hidden_states=True)

if self.encoder_type == EncoderType.FIRST_LAST_AVG:
# Get the first and last hidden states, and average them to get the embeddings
# hidden_states have 13 list, second is hidden_state
first = model_output.hidden_states[1]
last = model_output.hidden_states[-1]
seq_length = first.size(1) # Sequence length

first_avg = torch.avg_pool1d(first.transpose(1, 2), kernel_size=seq_length).squeeze(-1) # [batch, hid_size]
last_avg = torch.avg_pool1d(last.transpose(1, 2), kernel_size=seq_length).squeeze(-1) # [batch, hid_size]
final_encoding = torch.avg_pool1d(
torch.cat([first_avg.unsqueeze(1), last_avg.unsqueeze(1)], dim=1).transpose(1, 2),
kernel_size=2).squeeze(-1)
return final_encoding

if self.encoder_type == EncoderType.LAST_AVG:
sequence_output = model_output.last_hidden_state # [batch_size, max_len, hidden_size]
seq_length = sequence_output.size(1)
final_encoding = torch.avg_pool1d(sequence_output.transpose(1, 2), kernel_size=seq_length).squeeze(-1)
return final_encoding

if self.encoder_type == EncoderType.CLS:
sequence_output = model_output.last_hidden_state
return sequence_output[:, 0] # [batch, hid_size]

if self.encoder_type == EncoderType.POOLER:
return model_output.pooler_output # [batch, hid_size]

if self.encoder_type == EncoderType.MEAN:
"""
Mean Pooling - Take attention mask into account for correct averaging
"""
token_embeddings = model_output.last_hidden_state # Contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
final_encoding = torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
input_mask_expanded.sum(1), min=1e-9)
return final_encoding # [batch, hid_size]

def encode(
self,
sentences: Union[str, List[str]],
batch_size: int = 64,
show_progress_bar: bool = False,
convert_to_numpy: bool = True,
convert_to_tensor: bool = False,
device: str = None,
):
"""
Returns the embeddings for a batch of sentences.

:param sentences: str/list, Input sentences
:param batch_size: int, Batch size
:param show_progress_bar: bool, Whether to show a progress bar for the sentences
:param convert_to_numpy: If true, the output is a list of numpy vectors. Else, it is a list of pytorch tensors.
:param convert_to_tensor: If true, you get one large tensor as return. Overwrites any setting from convert_to_numpy
:param device: Which torch.device to use for the computation
"""
self.bert.eval()
if device is None:
device = self.device
if convert_to_tensor:
convert_to_numpy = False
input_is_string = False
if isinstance(sentences, str) or not hasattr(sentences, "__len__"):
sentences = [sentences]
input_is_string = True

all_embeddings = []
length_sorted_idx = np.argsort([-len(s) for s in sentences])
sentences_sorted = [sentences[idx] for idx in length_sorted_idx]
for start_index in trange(0, len(sentences), batch_size, desc="Batches", disable=not show_progress_bar):
sentences_batch = sentences_sorted[start_index: start_index + batch_size]
# Compute sentences embeddings
with torch.no_grad():
embeddings = self.get_sentence_embeddings(
**self.tokenizer(sentences_batch, max_length=self.max_seq_length,
padding=True, truncation=True, return_tensors='pt').to(device)
)
embeddings = embeddings.detach()
if convert_to_numpy:
embeddings = embeddings.cpu()
all_embeddings.extend(embeddings)
all_embeddings = [all_embeddings[idx] for idx in np.argsort(length_sorted_idx)]
if convert_to_tensor:
all_embeddings = torch.stack(all_embeddings)
elif convert_to_numpy:
all_embeddings = np.asarray([emb.numpy() for emb in all_embeddings])

if input_is_string:
all_embeddings = all_embeddings[0]

return all_embeddings


def load_pretrain_model(model_path: str, device='gpu'):
return SentenceModel(model_name_or_path=model_path,
device=device)


embedding_server = FastAPI()
semantic_pretrain_model = load_pretrain_model(text2vec_model_path,
device='cpu')

# logger.info(semantic_pretrain_model.encode(["你好"]))


@embedding_server.post("/text2vec/encode/")
def encode(request: EmbeddingRequest):
"""
搜索应用构建接口
:param request:{
"texts": [
"你好",
"你是谁"
]
}
:return:{
embeddings: ["", ""]
}
"""
embeddings = []
result = {
"state": True,
"embeddings": embeddings
}

try:
request = json.loads(request.json())
logger.info(request)
texts = request["texts"]
embeddings = semantic_pretrain_model.encode(texts, show_progress_bar=True)
except KeyError as e:
result['state'] = False
result['error'] = repr(e)
logger.error(result)

result['embeddings'] = [embedding.tolist() for embedding in embeddings]
return result


if __name__ == "__main__":
"""
nohup python text2vec.py > text2vec.log 2>&1 &
"""
uvicorn.run(app=embedding_server,
host='0.0.0.0',
port=8099)
本文结束 感谢您的阅读