Views
No views yet
Retro*: Optimizing LLMs for Reasoning-Intensive Document Retrieval
1import re
2import sglang as sgl
3
4
5PROMPT_TEMPLATE = """\
6Here is the **relevance definition** in a retrieval task: {relevance_definition}
7
8Now given a **query** ({query_type}) and a **document** ({doc_type}) in this retrieval task, your mission is to perform the following steps.
9
101. Query Analysis: Think to reason and describe what information would be most helpful in answering the query.
112. Document Analysis: Discuss how the information provided by the document fulfills or fails to fulfill the requirements implied by the query.
123. Relevance Annotation: Based on the relevance definition and the insights from the previous two steps, clearly justify your final relevance annotation result and annotate an integer score from a scale of 0 to 100. Please use the following guide:
13 - **80-100 (Highly Relevant):** The document directly and comprehensively addresses the query's intent. It is a core and authoritative answer.
14 - **60-80 (Relevant):** The document substantially addresses the query's intent, providing most of the key information, but might miss some minor details.
15 - **40-60 (Moderately Relevant):** The document is on-topic and addresses a part of the query's intent, but it is not a comprehensive answer.
16 - **20-40 (Slightly Relevant):** The document mentions keywords from the query, but its main topic is different. It offers very limited value.
17 - **0-20 (Irrelevant):** The document does not address the query's intent at all and is off-topic.
18
19After providing your detailed analysis and justification for all the steps above, conclude your entire response with the final relevance score. The score must be placed strictly between the <score> tags. There should be no other text or explanation inside the tags:
20<score>
21[From a scale of 0 to 100, annotate the degree of relevance between the query and the document.]
22</score>
23
24Query ({query_type}):
25[Begin of Query]
26{query}
27[End of Query]
28
29Document ({doc_type}):
30[Begin of Document]
31{doc}
32[End of Document]
33"""
34
35
36def main():
37 query = "In a party, how many guests do you need to have to ensure that either four people all know each other or four people are all complete strangers to one another?"
38 doc = "\\section{Infinite Ramsey's Theorem}\nTags: Ramsey Theory, Named Theorems\n\n\\begin{theorem}\nLet $k, n \\in \\N$.\nFor any set $S$, let $S^{\\paren n}$ denote the set $\\set {\\set {s_1, \\ldots, s_n}: \\text{each } s_i \\in S}$ of cardinality $n$ subsets of $S$.\nLet $X$ be an infinite set.\nThen:\n:for every partition $P$ of $X^{\\paren n}$ into $k$ many components\n:there is an infinite subset $Y \\subseteq X$\nsuch that:\n:each member of $Y^{\\paren n}$ is in the same component of $P$.\n\\end{theorem}\n\n\\begin{proof}\nWe will prove the theorem for fixed $k$ by induction on $n$.\n\\end{proof}\n\n"
39 query_type = "math problem"
40 doc_type = "math-related passage"
41 relevance_definition = "Given a query (math problem) and a document (math-related passage), the document is relevant to the query if the theorem described in the document can help solve the problem in the query."
42
43 prompts = [
44 PROMPT_TEMPLATE.format(
45 relevance_definition=relevance_definition,
46 query_type=query_type,
47 doc_type=doc_type,
48 query=query,
49 doc=doc
50 )
51 ]
52
53 llm = sgl.Engine(
54 model_path="ljw13/retro-star-qwen3-32b-0928",
55 tp_size=8,
56 dp_size=1,
57 )
58
59 tokenizer = llm.tokenizer_manager.tokenizer
60 messages = [[{"role": "user", "content": prompt}] for prompt in prompts]
61 input_texts = tokenizer.apply_chat_template(
62 messages,
63 tokenize=False,
64 add_generation_prompt=True,
65 enable_thinking=False
66 )
67
68 sampling_params = {
69 "n": 1,
70 "temperature": 0.6,
71 "max_new_tokens": 1024,
72 "skip_special_tokens": False,
73 "spaces_between_special_tokens": False,
74 }
75
76 outputs = llm.generate(
77 input_texts,
78 sampling_params=sampling_params,
79 )
80
81 llm.shutdown()
82
83 scores = []
84 for i, output in enumerate(outputs):
85 print(output["text"])
86 print("==" * 30)
87 try:
88 score = int(re.search(r"<score>\s*(\d+)\s*</score>", output["text"]).group(1))
89 except AttributeError:
90 score = 0
91 scores.append(score)
92
93 print("Scores:", scores)
94
95if __name__ == "__main__":
96 main()@article{lan2025retro,
title={Retro*: Optimizing LLMs for Reasoning-Intensive Document Retrieval},
author={Lan, Junwei and Chen, Jianlyu and Liu, Zheng and Li, Chaofan and Bao, Siqi and Lian, Defu},
journal={arXiv preprint arXiv:2509.24869},
year={2025}
}