File size: 14,341 Bytes
b61f04d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc5e4e
 
 
 
 
b61f04d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37db0f5
b61f04d
37db0f5
b61f04d
37db0f5
b61f04d
 
 
37db0f5
b61f04d
37db0f5
b61f04d
 
37db0f5
 
 
 
 
b61f04d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f345a8
 
b61f04d
 
 
 
 
 
 
76c6a11
b61f04d
 
 
 
 
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import re
import copy
from enum import Enum

from abc import ABC, abstractmethod
from typing import Optional, List, Dict, Union, Tuple
from .result import Result, RankingExecInfo

Prompt = Union[str, List[Dict[str, str]]]


class PromptMode(Enum):
    UNSPECIFIED = "unspecified"
    RANK_GPT = "rank_GPT"
    LRL = "LRL"

    def __str__(self):
        return self.value


class RankLLM(ABC):
    """
    Abstract base class for the language model to be used for reranking
    """

    def __init__(
        self,
        model: str,
        context_size: int,
        prompt_mode: PromptMode,
        num_few_shot_examples: int = 0,
    ):
        self.model = model
        self._context_size = context_size
        self._prompt_mode = prompt_mode
        self._num_few_shot_examples = num_few_shot_examples
        self._history = []
        self._rerank_type = "code_reasoning"

    def max_tokens(self):
        """
        Returns the maximum number of tokens that can be processed by the model
        """
        return self._context_size

    @abstractmethod
    def run_llm(self, prompt: Prompt, current_window_size: int) -> Tuple[str, int]:
        """
        Abstract method to run the target language model with a passed in prompt.

        Args:
            prompt (Union[str, List[Dict[str, str]]]): The prompt to be processed by the model.

        Returns:
            Tuple[str, int]: A tuple object containing the text response and the number of tokens in the response.
        """
        pass

    @abstractmethod
    def create_prompt_batched(
        self,
        results: List[Result],
        rank_start: int,
        rank_end: int,
        batch_size: int,
    ) -> List[Tuple[Prompt, int]]:
        """
        Abstract method to create prompts for reranking in batches.

        Args:
            results (List[Result]): The results to be reranked.
            rank_start (int): The starting index for ranking.
            rank_end (int): The ending index for ranking.
            batch_size (int): The size of each batch.

        Returns:
            Tuple[List[Prompt], List[int]]: A tuple containing a list of prompts and a list of indices.
        """
        pass

    @abstractmethod
    def create_prompt(
        self, result: Result, rank_start: int, rank_end: int
    ) -> Tuple[Prompt, int]:
        """
        Abstract method to create a prompt based on the result and given ranking range.

        Args:
            result (Result): The result object containing data for prompt generation.
            rank_start (int): The starting rank for prompt generation.
            rank_end (int): The ending rank for prompt generation.

        Returns:
            Tuple[Union[str, List[Dict[str, str]]], int]: A tuple object containing the generated prompt and the number of tokens in the generated prompt.
        """
        pass

    def permutation_pipeline(
        self,
        result: Result,
        rank_start: int,
        rank_end: int,
        logging: bool = False,
    ) ->  Result:
        """
        Runs the permutation pipeline on the passed in result set within the passed in rank range.

        Args:
            result (Result): The result object to process.
            rank_start (int): The start index for ranking.
            rank_end (int): The end index for ranking.
            logging (bool, optional): Flag to enable logging of operations. Defaults to False.

        Returns:
            Result: The processed result object after applying permutation.
        """
        prompt, in_token_count = self.create_prompt(result, rank_start, rank_end)
        if logging:
            print(f"prompt: {prompt}")

        permutation, out_token_count = self.run_llm(
            prompt, current_window_size=rank_end - rank_start
        )
        if logging:
            print(f"output: {permutation}")

        ranking_exec_info = RankingExecInfo(
            prompt, permutation, in_token_count, out_token_count
        )
        if result.ranking_exec_summary is None:
            result.ranking_exec_summary = []

        result.ranking_exec_summary.append(ranking_exec_info)

        result = self.receive_permutation(result, permutation, rank_start, rank_end)

        prompt, in_token_count = self.create_prompt(result, rank_start, rank_end)
        if logging:
            print(f"After receiving permutation: {prompt}")
        
        return result

    def permutation_pipeline_batched(
        self,
        results: List[Result],
        rank_start: int,
        rank_end: int,
        logging: bool = False,
    ) -> List[Result]:
        """
        Runs the permutation pipeline on the passed in result set within the passed in rank range for a batch of results.
        Args:
            results (List[Result]): The list of result objects to process.
            rank_start (int): The start index for ranking.
            rank_end (int): The end index for ranking.
            logging (bool, optional): Flag to enable logging of operations. Defaults to False.
        Returns:
            List[Result]: The processed list of result objects after applying permutation.
        """
        prompts = []
        prompts = self.create_prompt_batched(
            results, rank_start, rank_end, batch_size=32
        )
        batched_results = self.run_llm_batched(
            [prompt for prompt, _ in prompts], current_window_size=rank_end - rank_start
        )
        results = []
        for index, (result, (prompt, in_token_count)) in enumerate(
            zip(results, prompts)
        ):
            permutation, out_token_count = batched_results[index]
            if logging:
                print(f"output: {permutation}")
            ranking_exec_info = RankingExecInfo(
                prompt, permutation, in_token_count, out_token_count
            )
            if result.ranking_exec_summary is None:
                result.ranking_exec_summary = []
            result.ranking_exec_summary.append(ranking_exec_info)
            result = self.receive_permutation(result, permutation, rank_start, rank_end)
            results.append(result)
        return results

    def sliding_window(
        self,
        retrieved_result: Result,
        rank_start: int,
        rank_end: int,
        window_size: int,
        step: int,
        logging: bool = False,
    ):
        """
        Applies the sliding window algorithm to the reranking process.

        Args:
            retrieved_result (Result): The result object to process.
            rank_start (int): The start index for ranking.
            rank_end (int): The end index for ranking.
            window_size (int): The size of each sliding window.
            step (int): The step size for moving the window.
            logging (bool, optional): Flag to enable logging of operations. Defaults to False.

        Returns:
            Result: The result object after applying the sliding window technique.
        """
        rerank_result = copy.deepcopy(retrieved_result)
        end_pos = rank_end
        start_pos = rank_end - window_size
        while end_pos > rank_start and start_pos + step != rank_start:
            start_pos = max(start_pos, rank_start)
            rerank_result = self.permutation_pipeline(
                rerank_result, start_pos, end_pos, logging=logging
            )
            end_pos -= step
            start_pos -= step
        return rerank_result

    def sliding_windows_batched(
        self,
        retrieved_results: List[Result],
        rank_start: int,
        rank_end: int,
        window_size: int,
        step: int,
        logging: bool = False,
    ) -> List[Result]:
        """
        Applies the sliding window algorithm to the reranking process for a batch of result objects.
        Args:
            retrieved_results (List[Result]): The list of result objects to process.
            rank_start (int): The start index for ranking.
            rank_end (int): The end index for ranking.
            window_size (int): The size of each sliding window.
            step (int): The step size for moving the window.
            logging (bool, optional): Flag to enable logging of operations. Defaults to False.
        Returns:
            List[Result]: The list of result objects after applying the sliding window technique.
        """
        rerank_results = [copy.deepcopy(result) for result in retrieved_results]
        end_pos = rank_end
        start_pos = rank_end - window_size
        permutated_results = rerank_results
        while end_pos > rank_start and start_pos + step != rank_start:
            start_pos = max(start_pos, rank_start)
            permutated_results = self.permutation_pipeline_batched(
                rerank_results, start_pos, end_pos, logging=logging
            )
            end_pos -= step
            start_pos -= step
        return permutated_results

    def receive_permutation(
        self,
        result: Result,
        permutation: str,
        rank_start: int,
        rank_end: int,
    ) -> Result:
        """
        Processes and applies a permutation to the ranking results.

        This function takes a permutation string, representing the new order of items,
        and applies it to a subset of the ranking results. It adjusts the ranks and scores in the
        'result' object based on this permutation.

        Args:
            result (Result): The result object containing the initial ranking results.
            permutation (str): A string representing the new order of items.
                            Each item in the string should correspond to a rank in the results.
            rank_start (int): The starting index of the range in the results to which the permutation is applied.
            rank_end (int): The ending index of the range in the results to which the permutation is applied.

        Returns:
            Result: The updated result object with the new ranking order applied.

        Note:
            This function assumes that the permutation string is a sequence of integers separated by spaces.
            Each integer in the permutation string corresponds to a 1-based index in the ranking results.
            The function first normalizes these to 0-based indices, removes duplicates, and then reorders
            the items in the specified range of the 'result.hits' list according to the permutation.
            Items not mentioned in the permutation string remain in their original sequence but are moved after
            the permuted items.
        """
        response = self._clean_response(permutation)
        print(f"response after cleaning: {response}")
        response = [int(x) - 1 for x in response.split()]
        print(f"response after splitting: {response}")
        response = self._remove_duplicate(response)
        print(f"response after deduplication: {response}")
        cut_range = copy.deepcopy(result.hits[rank_start:rank_end])
        original_rank = [tt for tt in range(len(cut_range))]
        response = [ss for ss in response if ss in original_rank]
        print(f"response after selection: {response}")
        response = response + [tt for tt in original_rank if tt not in response]
        print(f"response after appending all original: {response}")
        for j, x in enumerate(response):
            result.hits[j + rank_start] = copy.deepcopy(cut_range[x])
            
            # if "rank" in result.hits[j + rank_start]:
            #     result.hits[j + rank_start]["rank"] = cut_range[j]["rank"]
            # if "score" in result.hits[j + rank_start]:
            #     result.hits[j + rank_start]["score"] = cut_range[j]["score"]
        return result

    def parse_reasoning_permutation(self, response: str) -> Tuple[str, bool]:
        ranked_list_pattern = r"\s*(\[\d+\](?:\s*>\s*\[\d+\])*)\s*"
        end_of_reasoning_tag = "</think>"
        start_of_answer_tag = "<answer>"
        end_of_answer_tag = "</answer>"
        matched_ranked_list = None
        if end_of_answer_tag in response and end_of_reasoning_tag in response:
            parsed_answer = (
                response[
                    response.index(end_of_reasoning_tag) : response.index(
                        end_of_answer_tag
                    )
                ]
                .replace(start_of_answer_tag, "")
                .strip()
            )
            match = re.findall(ranked_list_pattern, parsed_answer)
            if match:
                print(len(match))
                matched_ranked_list = match[0].strip()
        if matched_ranked_list:
            print(f"re matched output: {matched_ranked_list}")
            return matched_ranked_list, True
        else:
            match = re.findall(ranked_list_pattern, response, re.DOTALL | re.MULTILINE)
            first_correct_match = None
            for cand in match:
                if ">" not in cand:
                    continue
                else:
                    first_correct_match = cand
                    break

            if first_correct_match:
                print(f"re matched output: {first_correct_match}")
                return first_correct_match, True
            else:
                print(f"re match FAILED: {response}")
                return response, False

    def run_llm_batched(
        self,
        prompts: List[Union[str, List[Dict[str, str]]]],
        current_window_size: Optional[int] = None,
    ) -> List[Tuple[str, int]]:
        ...

    def _remove_duplicate(self, response: List[int]) -> List[int]:
        seen = set()
        unique_response = []
        for item in response:
            if item not in seen:
                seen.add(item)
                unique_response.append(item)
        return unique_response

    def _clean_response(self, response: str) -> str:
        # if self._rerank_type == "code_reasoning":
        #     response, _ = self.parse_reasoning_permutation(response)

        new_response = ""
        for char in response:
            if not char.isdigit():
                new_response += " "
            else:
                new_response += char
        new_response = new_response.strip()

        return new_response

    def _replace_number(self, s: str) -> str:
        return re.sub(r"\[(\d+)\]", r"(\1)", s)