-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_text_embedding_models.py
276 lines (236 loc) · 10.1 KB
/
evaluate_text_embedding_models.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from metaflow import FlowSpec, step, Parameter, current, card
from metaflow.cards import Table, Markdown
from transformers import AutoTokenizer, AutoModel
import torch
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import torch.nn.functional as F
class EvaluateTextEmbeddingModelsFlow(FlowSpec):
txt_embed_models = Parameter(
"txt_embed_models",
help="List of text embedding models",
default=[
"sentence-transformers/all-MiniLM-L6-v2",
"sentence-transformers/all-mpnet-base-v2",
"sentence-transformers/multi-qa-mpnet-base-cos-v1",
"intfloat/e5-large-v2",
"clips/mfaq",
],
)
corpus_file = Parameter(
"corpus_file",
help="Path to the text file containing the corpus",
default="./data/corpus.txt",
)
queries_file = Parameter(
"queries_file",
help="Path to the text file containing the queries",
default="./data/queries.txt",
)
@step
def start(self):
# Read corpus and queries from text files
with open(self.corpus_file, "r") as f:
self.corpus = [line.strip() for line in f]
with open(self.queries_file, "r") as f:
self.queries = [line.strip() for line in f]
self.model_paths = self.txt_embed_models
self.next(self.encode, foreach="model_paths")
@step
def encode(self):
self.model_path = self.input
tokenizer = AutoTokenizer.from_pretrained(self.model_path)
model = AutoModel.from_pretrained(self.model_path)
# Check if the model is 'intfloat/e5-large-v2' and apply the appropriate encoding method
if self.model_path == "intfloat/e5-large-v2":
# Format data as required by the model
self.corpus, self.queries = self.format_data_for_model()
# Encode corpus
self.corpus_embeddings = self.encode_sentences_v2(
self.corpus, tokenizer, model
)
# Encode queries
self.query_embeddings = [
self.encode_sentences_v2(query, tokenizer, model)
for query in self.queries
]
elif self.model_path == "clips/mfaq":
# Format data as required by the model
self.corpus, self.queries = self.format_data_for_model()
# Encode corpus
self.corpus_embeddings = self.encode_sentences(
self.corpus, tokenizer, model
)
# Encode queries
self.query_embeddings = [
self.encode_sentences(query, tokenizer, model) for query in self.queries
]
else:
# Format data as required by the model
self.corpus, self.queries = self.format_data_for_model()
# Encode corpus
self.corpus_embeddings = self.encode_sentences(
self.corpus, tokenizer, model
)
# Encode queries
self.query_embeddings = [
self.encode_sentences(query, tokenizer, model) for query in self.queries
]
self.next(self.join)
@step
def join(self, inputs):
self.corpus_embeddings = [inp.corpus_embeddings for inp in inputs]
self.query_embeddings = [inp.query_embeddings for inp in inputs]
self.model_paths = [inp.model_path for inp in inputs]
self.queries = inputs[0].queries # the queries are the same for all inputs
self.corpus = inputs[0].corpus # the corpus is the same for all input
self.results = []
self.next(self.calculate_similarity)
@step
def calculate_similarity(self):
print("Inside calculate_similarity")
self.top3_hits = []
for model_path, corpus_embeddings, query_embeddings in zip(
self.model_paths, self.corpus_embeddings, self.query_embeddings
):
for query, query_embedding in zip(self.queries, query_embeddings):
# Calculate cosine similarities
cos_scores = cosine_similarity(query_embedding, corpus_embeddings)[0]
cos_scores = cos_scores.flatten()
# Get top 3 document ids
top3_doc_ids = np.argpartition(-cos_scores, range(3))[:3]
top3_scores = cos_scores[top3_doc_ids]
self.top3_hits.append((top3_doc_ids.tolist(), top3_scores.tolist()))
# Print top 3 hits for each model
for hit_id, score in zip(top3_doc_ids, top3_scores):
hit_text = self.corpus[hit_id]
self.results.append((model_path, query, hit_text, score))
# print(
# f"Model: {model_path}, Query: {query}, Hit: {hit_text}, Score: {score}"
# )
self.next(self.end)
@card(type="blank")
@step
def end(self):
data = self.results
result = {}
# Add top level heading for the card
current.card.append(Markdown(f"# Embedding models evaluation"))
# Extract query, hit_text, and score for each unique model_path
for model_path, query, hit_text, score in data:
if model_path not in result:
result[model_path] = []
result[model_path].append(
{"query": query, "hit_text": hit_text, "score": score}
)
# print(result)
# Print the extracted data for each unique model_path
for model_path, entries in result.items():
print(f"Model Path: {model_path}")
current.card.append(Markdown(f"## Model = {model_path}"))
rows = []
for entry in entries:
query = entry["query"]
hit_text = entry["hit_text"]
score = "{:.4f}".format(entry["score"])
# print(f"Query: {query}, Hit: {hit_text}, Score: {score}")entry["score"]
rows.append(
[
Markdown(f"**{query}**"),
Markdown(f"*{hit_text}*"),
Markdown(f"**{score}**"),
]
)
headers = ["Query", "hit_text", "cosine_similarity_score"]
current.card.append(Table(rows, headers))
"""
Format the data for the card
clips/mfaq:
You can use MFAQ with sentence-transformers or directly with a HuggingFace model.
In both cases, questions need to be prepended with <Q>, and answers with <A>.
intfloat/e5-large-v2:
Each input text should start with "query: " or "passage: ".
For tasks other than retrieval, you can simply use the "query: " prefix.
"""
# Check if the model is 'intfloat/e5-large-v2' and apply the appropriate encoding method
if self.model_path == "intfloat/e5-large-v2":
# Each input text should start with "query: " or "passage: ".
# For tasks other than retrieval, you can simply use the "query: " prefix.
corpus = []
queries = []
for i in range(len(self.corpus)):
corpus.append(f"passage: {self.corpus[i]}")
for i in range(len(self.queries)):
queries.append(f"query: {self.queries[i]}")
elif self.model_path == "clips/mfaq":
# You can use MFAQ with sentence-transformers or directly with a HuggingFace model.
# In both cases, questions need to be prepended with <Q>, and answers with <A>.
corpus = []
queries = []
for i in range(len(self.corpus)):
corpus.append(f"<A>{self.corpus[i]}")
for i in range(len(self.queries)):
queries.append(f"<Q>{self.queries[i]}")
else:
# Use the default encoding method
corpus = self.corpus
queries = self.queries
# print(f"queries: {queries}")
return corpus, queries
def encode_sentences(self, sentences, tokenizer, model, normalize=True):
# Tokenize sentences
encoded_input = tokenizer(
sentences,
padding=True,
truncation=True,
max_length=128,
return_tensors="pt",
)
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling. In this case, mean pooling
sentence_embeddings = self.mean_pooling(
model_output, encoded_input["attention_mask"]
)
# Normalize embeddings
if normalize:
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings
def encode_sentences_v2(self, sentences, tokenizer, model, normalize=True):
# Tokenize sentences
encoded_input = tokenizer(
sentences,
padding=True,
truncation=True,
max_length=512,
return_tensors="pt",
)
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform average pooling
sentence_embeddings = self.average_pool(
model_output.last_hidden_state, encoded_input["attention_mask"]
)
# Normalize embeddings
if normalize:
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings
def mean_pooling(self, model_output, attention_mask):
token_embeddings = model_output[
0
] # First element of model_output contains all token embeddings
input_mask_expanded = (
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
)
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
return sum_embeddings / sum_mask
def average_pool(self, last_hidden_states, attention_mask):
last_hidden = last_hidden_states.masked_fill(
~attention_mask[..., None].bool(), 0.0
)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
if __name__ == "__main__":
EvaluateTextEmbeddingModelsFlow()