@@ -61,43 +61,21 @@ def __init__(
6161 self ._on_usage = on_usage
6262
6363 def rank (self , query : str , documents : list [Document ]) -> str :
64- candidate_count = len (documents )
65- return self ._complete (
66- system = (
67- "You are a reranking engine. Return only JSON with "
68- 'a "ranking" array. The ranking must be a permutation '
69- "of the candidate numbers. "
70- f"The ranking array must contain exactly { candidate_count } integers: "
71- f"each integer from 1 to { candidate_count } exactly once."
72- ),
73- user = _build_prompt (query , documents ),
74- )
64+ system , user = _rank_messages (query , documents )
65+ return self ._complete (system = system , user = user )
7566
7667 def compare (
7768 self ,
7869 query : str ,
7970 document_a : Document ,
8071 document_b : Document ,
8172 ) -> str :
82- return self ._complete (
83- system = (
84- "You are a pairwise reranking engine. Return only JSON "
85- 'with a "winner" value of "A" or "B".'
86- ),
87- user = _build_pairwise_prompt (query , document_a , document_b ),
88- )
73+ system , user = _compare_messages (query , document_a , document_b )
74+ return self ._complete (system = system , user = user )
8975
9076 def select (self , query : str , documents : list [Document ], top_m : int ) -> str :
91- candidate_count = len (documents )
92- return self ._complete (
93- system = (
94- "You are a tournament reranking engine. Return only JSON "
95- 'with a "selected" array of candidate numbers. '
96- f"The selected array must contain exactly { top_m } integers from "
97- f"1 to { candidate_count } , without duplicates."
98- ),
99- user = _build_selection_prompt (query , documents , top_m ),
100- )
77+ system , user = _select_messages (query , documents , top_m )
78+ return self ._complete (system = system , user = user )
10179
10280 def _complete (self , * , system : str , user : str ) -> str :
10381 try :
@@ -131,43 +109,21 @@ def __init__(
131109 self ._on_usage = on_usage
132110
133111 async def rank (self , query : str , documents : list [Document ]) -> str :
134- candidate_count = len (documents )
135- return await self ._complete (
136- system = (
137- "You are a reranking engine. Return only JSON with "
138- 'a "ranking" array. The ranking must be a permutation '
139- "of the candidate numbers. "
140- f"The ranking array must contain exactly { candidate_count } integers: "
141- f"each integer from 1 to { candidate_count } exactly once."
142- ),
143- user = _build_prompt (query , documents ),
144- )
112+ system , user = _rank_messages (query , documents )
113+ return await self ._complete (system = system , user = user )
145114
146115 async def compare (
147116 self ,
148117 query : str ,
149118 document_a : Document ,
150119 document_b : Document ,
151120 ) -> str :
152- return await self ._complete (
153- system = (
154- "You are a pairwise reranking engine. Return only JSON "
155- 'with a "winner" value of "A" or "B".'
156- ),
157- user = _build_pairwise_prompt (query , document_a , document_b ),
158- )
121+ system , user = _compare_messages (query , document_a , document_b )
122+ return await self ._complete (system = system , user = user )
159123
160124 async def select (self , query : str , documents : list [Document ], top_m : int ) -> str :
161- candidate_count = len (documents )
162- return await self ._complete (
163- system = (
164- "You are a tournament reranking engine. Return only JSON "
165- 'with a "selected" array of candidate numbers. '
166- f"The selected array must contain exactly { top_m } integers from "
167- f"1 to { candidate_count } , without duplicates."
168- ),
169- user = _build_selection_prompt (query , documents , top_m ),
170- )
125+ system , user = _select_messages (query , documents , top_m )
126+ return await self ._complete (system = system , user = user )
171127
172128 async def _complete (self , * , system : str , user : str ) -> str :
173129 try :
@@ -206,6 +162,45 @@ async def _emit_usage_async(
206162 await result
207163
208164
165+ def _rank_messages (query : str , documents : list [Document ]) -> tuple [str , str ]:
166+ candidate_count = len (documents )
167+ system = (
168+ "You are a reranking engine. Return only JSON with "
169+ 'a "ranking" array. The ranking must be a permutation '
170+ "of the candidate numbers. "
171+ f"The ranking array must contain exactly { candidate_count } integers: "
172+ f"each integer from 1 to { candidate_count } exactly once."
173+ )
174+ return system , _build_prompt (query , documents )
175+
176+
177+ def _compare_messages (
178+ query : str ,
179+ document_a : Document ,
180+ document_b : Document ,
181+ ) -> tuple [str , str ]:
182+ system = (
183+ "You are a pairwise reranking engine. Return only JSON "
184+ 'with a "winner" value of "A" or "B".'
185+ )
186+ return system , _build_pairwise_prompt (query , document_a , document_b )
187+
188+
189+ def _select_messages (
190+ query : str ,
191+ documents : list [Document ],
192+ top_m : int ,
193+ ) -> tuple [str , str ]:
194+ candidate_count = len (documents )
195+ system = (
196+ "You are a tournament reranking engine. Return only JSON "
197+ 'with a "selected" array of candidate numbers. '
198+ f"The selected array must contain exactly { top_m } integers from "
199+ f"1 to { candidate_count } , without duplicates."
200+ )
201+ return system , _build_selection_prompt (query , documents , top_m )
202+
203+
209204def _build_prompt (query : str , documents : list [Document ]) -> str :
210205 candidate_count = len (documents )
211206 ranking_example = ", " .join (str (index ) for index in range (1 , candidate_count + 1 ))
0 commit comments