|
| 1 | +import unittest |
| 2 | + |
| 3 | +from dacite import from_dict |
| 4 | +from transformers import T5Tokenizer |
| 5 | + |
| 6 | +from rank_llm.data import Result |
| 7 | +from rank_llm.rerank.pairwise.pairwise_inference_handler import PairwiseInferenceHandler |
| 8 | + |
| 9 | +r = from_dict( |
| 10 | + data_class=Result, |
| 11 | + data={ |
| 12 | + "query": {"text": "Sample Query", "qid": "q1"}, |
| 13 | + "candidates": [ |
| 14 | + { |
| 15 | + "doc": { |
| 16 | + "contents": "Title1: Sample Title1 Content1: Sample Text1", |
| 17 | + }, |
| 18 | + "docid": "d1", |
| 19 | + "score": 0.5, |
| 20 | + }, |
| 21 | + { |
| 22 | + "doc": { |
| 23 | + "contents": "Title2: Sample Title2 Content2: Sample Text2", |
| 24 | + }, |
| 25 | + "docid": "d2", |
| 26 | + "score": 0.4, |
| 27 | + }, |
| 28 | + { |
| 29 | + "doc": { |
| 30 | + "contents": "Title3: Sample Title3 Content3: Sample Text3", |
| 31 | + }, |
| 32 | + "docid": "d3", |
| 33 | + "score": 0.4, |
| 34 | + }, |
| 35 | + { |
| 36 | + "doc": { |
| 37 | + "contents": "Title4: Sample Title4 Content4: Sample Text4", |
| 38 | + }, |
| 39 | + "docid": "d4", |
| 40 | + "score": 0.3, |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +VALID_PAIRWISE_TEMPLATE = { |
| 48 | + "method": "pairwise", |
| 49 | + "body": "Query: {query} Document0: {doc1} Document1: {doc2}", |
| 50 | +} |
| 51 | +INVALID_PAIRWISE_TEMPLATES = [ |
| 52 | + { |
| 53 | + "method": "singleturn_listwise", |
| 54 | + "body": "{query} {doc1} {doc2}", |
| 55 | + }, # Wrong method type |
| 56 | + { |
| 57 | + "method": "pairwise", |
| 58 | + "body": "{query} {doc1}", |
| 59 | + }, # Missing required placeholder: {doc2} |
| 60 | + { |
| 61 | + "method": "pairwise", |
| 62 | + "body": "{query} {doc1} {doc2}", |
| 63 | + "unknown_key": "value", |
| 64 | + }, # Unknown key |
| 65 | +] |
| 66 | +tokenizer = T5Tokenizer.from_pretrained("castorini/duot5-3b-msmarco-10k") |
| 67 | + |
| 68 | + |
| 69 | +class TestPairwiseInferenceHandler(unittest.TestCase): |
| 70 | + def test_pairwise_valid_template_initialization(self): |
| 71 | + pairwise_inference_handler = PairwiseInferenceHandler(VALID_PAIRWISE_TEMPLATE) |
| 72 | + self.assertEqual(pairwise_inference_handler.template, VALID_PAIRWISE_TEMPLATE) |
| 73 | + |
| 74 | + def test_invalid_templates(self): |
| 75 | + for template in INVALID_PAIRWISE_TEMPLATES: |
| 76 | + with self.subTest(template=template): |
| 77 | + with self.assertRaises(ValueError): |
| 78 | + PairwiseInferenceHandler(template) |
| 79 | + |
| 80 | + def test_body_generation(self): |
| 81 | + pairwise_inference_handler = PairwiseInferenceHandler(VALID_PAIRWISE_TEMPLATE) |
| 82 | + body_text_1 = pairwise_inference_handler._generate_body( |
| 83 | + result=r, index1=0, index2=1, single_doc_max_token=6000, tokenizer=tokenizer |
| 84 | + ) |
| 85 | + body_text_2 = pairwise_inference_handler._generate_body( |
| 86 | + result=r, index1=0, index2=2, single_doc_max_token=6000, tokenizer=tokenizer |
| 87 | + ) |
| 88 | + expected_body_1 = "Query: Sample Query Document0: Title1: Sample Title1 Content1: Sample Text1 Document1: Title2: Sample Title2 Content2: Sample Text2" |
| 89 | + expected_body_2 = "Query: Sample Query Document0: Title1: Sample Title1 Content1: Sample Text1 Document1: Title3: Sample Title3 Content3: Sample Text3" |
| 90 | + |
| 91 | + self.assertEqual(body_text_1, expected_body_1) |
| 92 | + self.assertEqual(body_text_2, expected_body_2) |
| 93 | + |
| 94 | + def test_prompt_generation(self): |
| 95 | + pairwise_inference_handler = PairwiseInferenceHandler(VALID_PAIRWISE_TEMPLATE) |
| 96 | + prompt_text_1 = pairwise_inference_handler.generate_prompt( |
| 97 | + result=r, index1=0, index2=1, max_token=6000, tokenizer=tokenizer |
| 98 | + ) |
| 99 | + prompt_text_2 = pairwise_inference_handler.generate_prompt( |
| 100 | + result=r, index1=0, index2=2, max_token=6000, tokenizer=tokenizer |
| 101 | + ) |
| 102 | + expected_prompt_1 = "Query: Sample Query Document0: Title1: Sample Title1 Content1: Sample Text1 Document1: Title2: Sample Title2 Content2: Sample Text2" |
| 103 | + expected_prompt_2 = "Query: Sample Query Document0: Title1: Sample Title1 Content1: Sample Text1 Document1: Title3: Sample Title3 Content3: Sample Text3" |
| 104 | + |
| 105 | + self.assertEqual(prompt_text_1, expected_prompt_1) |
| 106 | + self.assertEqual(prompt_text_2, expected_prompt_2) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + unittest.main() |
0 commit comments