Repository navigation
Expand file tree
/
Copy pathadapter_query.py
More file actions
214 lines (199 loc) · 8.39 KB
/
Copy pathadapter_query.py
File metadata and controls
214 lines (199 loc) · 8.39 KB
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
# -*- coding: utf-8 -*-
import asyncio
import logging
from modelcache.embedding import MetricType
from modelcache.utils.time import time_cal
from FlagEmbedding import FlagReranker
USE_RERANKER = False # 如果为 True 则启用 reranker,否则使用原有逻辑
async def adapt_query(cache_data_convert, *args, **kwargs):
chat_cache = kwargs.pop("cache_obj")
scope = kwargs.pop("scope")
model = scope['model']
context = kwargs.pop("cache_context", {})
cache_factor = kwargs.pop("cache_factor", 1.0)
pre_embedding_data = chat_cache.query_pre_embedding_func(
kwargs,
extra_param=context.get("pre_embedding_func", None),
prompts=chat_cache.prompts,
)
embedding_data = await time_cal(
chat_cache.embedding_func,
func_name="embedding",
report_func=chat_cache.report.embedding,
cache_obj=chat_cache
)(pre_embedding_data)
cache_data_list = time_cal(
chat_cache.data_manager.search,
func_name="vector_search",
report_func=chat_cache.report.search,
cache_obj=chat_cache
)(
embedding_data,
extra_param=context.get("search_func", None),
top_k=kwargs.pop("top_k", -1),
model=model
)
cache_answers = []
cache_questions = []
cache_ids = []
cosine_similarity = None
if chat_cache.similarity_metric_type == MetricType.COSINE:
cosine_similarity = cache_data_list[0][0]
# This code uses the built-in cosine similarity evaluation in milvus
if cosine_similarity < 0.9:
return None
elif chat_cache.similarity_metric_type == MetricType.L2:
## this is the code that uses L2 for similarity evaluation
similarity_threshold = chat_cache.similarity_threshold
similarity_threshold_long = chat_cache.similarity_threshold_long
min_rank, max_rank = chat_cache.similarity_evaluation.range()
rank_threshold = (max_rank - min_rank) * similarity_threshold * cache_factor
rank_threshold_long = (max_rank - min_rank) * similarity_threshold_long * cache_factor
rank_threshold = (
max_rank
if rank_threshold > max_rank
else min_rank
if rank_threshold < min_rank
else rank_threshold
)
rank_threshold_long = (
max_rank
if rank_threshold_long > max_rank
else min_rank
if rank_threshold_long < min_rank
else rank_threshold_long
)
if cache_data_list is None or len(cache_data_list) == 0:
rank_pre = -1.0
else:
cache_data_dict = {'search_result': cache_data_list[0]}
rank_pre = chat_cache.similarity_evaluation.evaluation(
None,
cache_data_dict,
extra_param=context.get("evaluation_func", None),
)
if rank_pre < rank_threshold:
return None
else:
raise ValueError(
f"Unsupported similarity metric type: {chat_cache.similarity_metric_type}"
)
if USE_RERANKER:
reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=False)
for cache_data in cache_data_list:
primary_id = cache_data[1]
ret = chat_cache.data_manager.get_scalar_data(
cache_data, extra_param=context.get("get_scalar_data", None),model=model
)
if ret is None:
continue
rank = reranker.compute_score([pre_embedding_data, ret[0]], normalize=True)[0]
if "deps" in context and hasattr(ret.question, "deps"):
eval_query_data = {
"question": context["deps"][0]["data"],
"embedding": None
}
eval_cache_data = {
"question": ret.question.deps[0].data,
"answer": ret.answers[0].answer,
"search_result": cache_data,
"embedding": None,
}
else:
eval_query_data = {
"question": pre_embedding_data,
"embedding": embedding_data,
}
eval_cache_data = {
"question": ret[0],
"answer": ret[1],
"search_result": cache_data,
"embedding": None
}
if len(pre_embedding_data) <= 256:
if rank_threshold <= rank:
cache_answers.append((rank, ret[0]))
cache_questions.append((rank, ret[1]))
cache_ids.append((rank, primary_id))
else:
if rank_threshold_long <= rank:
cache_answers.append((rank, ret[0]))
cache_questions.append((rank, ret[1]))
cache_ids.append((rank, primary_id))
else:
# 不使用 reranker 时,走原来的逻辑
for cache_data in cache_data_list:
primary_id = cache_data[1]
ret = chat_cache.data_manager.get_scalar_data(
cache_data, extra_param=context.get("get_scalar_data", None),model=model
)
if ret is None:
continue
if chat_cache.similarity_metric_type == MetricType.COSINE:
assert cosine_similarity is not None, "cosine_similarity should not be None"
cache_answers.append((cosine_similarity, ret[0]))
cache_questions.append((cosine_similarity, ret[1]))
cache_ids.append((cosine_similarity, primary_id))
elif chat_cache.similarity_metric_type == MetricType.L2:
if "deps" in context and hasattr(ret.question, "deps"):
eval_query_data = {
"question": context["deps"][0]["data"],
"embedding": None
}
eval_cache_data = {
"question": ret.question.deps[0].data,
"answer": ret.answers[0].answer,
"search_result": cache_data,
"embedding": None,
}
else:
eval_query_data = {
"question": pre_embedding_data,
"embedding": embedding_data,
}
eval_cache_data = {
"question": ret[0],
"answer": ret[1],
"search_result": cache_data,
"embedding": None
}
rank = chat_cache.similarity_evaluation.evaluation(
eval_query_data,
eval_cache_data,
extra_param=context.get("evaluation_func", None),
)
if len(pre_embedding_data) <= 256:
if rank_threshold <= rank:
cache_answers.append((rank, ret[0]))
cache_questions.append((rank, ret[1]))
cache_ids.append((rank, primary_id))
else:
if rank_threshold_long <= rank:
cache_answers.append((rank, ret[0]))
cache_questions.append((rank, ret[1]))
cache_ids.append((rank, primary_id))
else:
raise ValueError(
f"Unsupported similarity metric type: {chat_cache.similarity_metric_type}"
)
cache_answers = sorted(cache_answers, key=lambda x: x[0], reverse=True)
cache_questions = sorted(cache_questions, key=lambda x: x[0], reverse=True)
cache_ids = sorted(cache_ids, key=lambda x: x[0], reverse=True)
if len(cache_answers) != 0:
return_message = chat_cache.post_process_messages_func(
[t[1] for t in cache_answers]
)
return_query = chat_cache.post_process_messages_func(
[t[1] for t in cache_questions]
)
return_id = chat_cache.post_process_messages_func(
[t[1] for t in cache_ids]
)
# 更新命中次数
try:
asyncio.create_task(asyncio.to_thread(chat_cache.data_manager.update_hit_count,return_id))
except Exception:
logging.info('update_hit_count except, please check!')
chat_cache.report.hint_cache()
return cache_data_convert(return_message, return_query)
return None