|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import logging |
| 3 | +import time |
| 4 | +from modelcache import cache |
| 5 | +from modelcache.utils.error import NotInitError |
| 6 | +from modelcache.utils.time import time_cal |
| 7 | +from modelcache.processor.pre import multi_analysis |
| 8 | + |
| 9 | + |
| 10 | +def adapt_query(cache_data_convert, *args, **kwargs): |
| 11 | + chat_cache = kwargs.pop("cache_obj", cache) |
| 12 | + scope = kwargs.pop("scope", None) |
| 13 | + model = scope['model'] |
| 14 | + if not chat_cache.has_init: |
| 15 | + raise NotInitError() |
| 16 | + cache_enable = chat_cache.cache_enable_func(*args, **kwargs) |
| 17 | + context = kwargs.pop("cache_context", {}) |
| 18 | + embedding_data = None |
| 19 | + cache_factor = kwargs.pop("cache_factor", 1.0) |
| 20 | + pre_embedding_data = chat_cache.query_pre_embedding_func( |
| 21 | + kwargs, |
| 22 | + extra_param=context.get("pre_embedding_func", None), |
| 23 | + prompts=chat_cache.config.prompts, |
| 24 | + ) |
| 25 | + |
| 26 | + if cache_enable: |
| 27 | + embedding_data = time_cal( |
| 28 | + chat_cache.embedding_func, |
| 29 | + func_name="embedding", |
| 30 | + report_func=chat_cache.report.embedding, |
| 31 | + )(pre_embedding_data) |
| 32 | + |
| 33 | + if cache_enable: |
| 34 | + cache_data_list = time_cal( |
| 35 | + chat_cache.data_manager.search, |
| 36 | + func_name="milvus_search", |
| 37 | + report_func=chat_cache.report.search, |
| 38 | + )( |
| 39 | + embedding_data, |
| 40 | + extra_param=context.get("search_func", None), |
| 41 | + top_k=kwargs.pop("top_k", -1), |
| 42 | + model=model |
| 43 | + ) |
| 44 | + cache_answers = [] |
| 45 | + cache_questions = [] |
| 46 | + cache_ids = [] |
| 47 | + similarity_threshold = chat_cache.config.similarity_threshold |
| 48 | + similarity_threshold_long = chat_cache.config.similarity_threshold_long |
| 49 | + |
| 50 | + min_rank, max_rank = chat_cache.similarity_evaluation.range() |
| 51 | + rank_threshold = (max_rank - min_rank) * similarity_threshold * cache_factor |
| 52 | + rank_threshold_long = (max_rank - min_rank) * similarity_threshold_long * cache_factor |
| 53 | + rank_threshold = ( |
| 54 | + max_rank |
| 55 | + if rank_threshold > max_rank |
| 56 | + else min_rank |
| 57 | + if rank_threshold < min_rank |
| 58 | + else rank_threshold |
| 59 | + ) |
| 60 | + rank_threshold_long = ( |
| 61 | + max_rank |
| 62 | + if rank_threshold_long > max_rank |
| 63 | + else min_rank |
| 64 | + if rank_threshold_long < min_rank |
| 65 | + else rank_threshold_long |
| 66 | + ) |
| 67 | + |
| 68 | + if cache_data_list is None or len(cache_data_list) == 0: |
| 69 | + rank_pre = -1.0 |
| 70 | + else: |
| 71 | + cache_data_dict = {'search_result': cache_data_list[0]} |
| 72 | + rank_pre = chat_cache.similarity_evaluation.evaluation( |
| 73 | + None, |
| 74 | + cache_data_dict, |
| 75 | + extra_param=context.get("evaluation_func", None), |
| 76 | + ) |
| 77 | + if rank_pre < rank_threshold: |
| 78 | + return |
| 79 | + |
| 80 | + for cache_data in cache_data_list: |
| 81 | + primary_id = cache_data[1] |
| 82 | + start_time = time.time() |
| 83 | + ret = chat_cache.data_manager.get_scalar_data( |
| 84 | + cache_data, extra_param=context.get("get_scalar_data", None) |
| 85 | + ) |
| 86 | + if ret is None: |
| 87 | + continue |
| 88 | + |
| 89 | + if "deps" in context and hasattr(ret.question, "deps"): |
| 90 | + eval_query_data = { |
| 91 | + "question": context["deps"][0]["data"], |
| 92 | + "embedding": None |
| 93 | + } |
| 94 | + eval_cache_data = { |
| 95 | + "question": ret.question.deps[0].data, |
| 96 | + "answer": ret.answers[0].answer, |
| 97 | + "search_result": cache_data, |
| 98 | + "embedding": None, |
| 99 | + } |
| 100 | + else: |
| 101 | + eval_query_data = { |
| 102 | + "question": pre_embedding_data, |
| 103 | + "embedding": embedding_data, |
| 104 | + } |
| 105 | + |
| 106 | + eval_cache_data = { |
| 107 | + "question": ret[0], |
| 108 | + "answer": ret[1], |
| 109 | + "search_result": cache_data, |
| 110 | + "embedding": None |
| 111 | + } |
| 112 | + rank = chat_cache.similarity_evaluation.evaluation( |
| 113 | + eval_query_data, |
| 114 | + eval_cache_data, |
| 115 | + extra_param=context.get("evaluation_func", None), |
| 116 | + ) |
| 117 | + |
| 118 | + if len(pre_embedding_data) <= 256: |
| 119 | + if rank_threshold <= rank: |
| 120 | + cache_answers.append((rank, ret[1])) |
| 121 | + cache_questions.append((rank, ret[0])) |
| 122 | + cache_ids.append((rank, primary_id)) |
| 123 | + else: |
| 124 | + if rank_threshold_long <= rank: |
| 125 | + cache_answers.append((rank, ret[1])) |
| 126 | + cache_questions.append((rank, ret[0])) |
| 127 | + cache_ids.append((rank, primary_id)) |
| 128 | + cache_answers = sorted(cache_answers, key=lambda x: x[0], reverse=True) |
| 129 | + cache_questions = sorted(cache_questions, key=lambda x: x[0], reverse=True) |
| 130 | + cache_ids = sorted(cache_ids, key=lambda x: x[0], reverse=True) |
| 131 | + if len(cache_answers) != 0: |
| 132 | + return_message = chat_cache.post_process_messages_func( |
| 133 | + [t[1] for t in cache_answers] |
| 134 | + ) |
| 135 | + return_query = chat_cache.post_process_messages_func( |
| 136 | + [t[1] for t in cache_questions] |
| 137 | + ) |
| 138 | + return_id = chat_cache.post_process_messages_func( |
| 139 | + [t[1] for t in cache_ids] |
| 140 | + ) |
| 141 | + # 更新命中次数 |
| 142 | + try: |
| 143 | + chat_cache.data_manager.update_hit_count(return_id) |
| 144 | + except Exception: |
| 145 | + logging.info('update_hit_count except, please check!') |
| 146 | + |
| 147 | + chat_cache.report.hint_cache() |
| 148 | + return cache_data_convert(return_message, return_query) |
0 commit comments