This section will guide you to implement a typical iterative reasoning RAG workflow: IterRetGen. This process supports repeatedly updating queries based on model outputs, gradually converging to better answers.
Let’s first review the original workflow diagram of IterRetGen proposed in the paper:In UltraRAG, you can quickly implement this workflow based on existing modules. Its overall architecture can be abstracted as the following diagram:As shown in the figure:
The answer generated by the model in each round is concatenated with the original question as the query for the next round of retrieval.
This process is executed N times according to the set maximum number of loops.
Except for the prompt and custom modules that need to be implemented by yourself, other functions can be directly reused from UltraRAG built-in tools.
IterRetGen concatenates the query and the answer generated in the current round as the query for this round. To do this, add the following code in servers/custom/src/custom.py:
servers/custom/src/custom.py
Copy
@app.tool(output="q_ls,ret_psg->nextq_ls")def iterretgen_nextquery( q_ls: List[str], ans_ls: List[str | Any],) -> Dict[str, List[str]]: ret = [] for q, ans in zip(q_ls, ans_ls): next_query = f"{q} {ans}" ret.append(next_query) return {"nextq_ls": ret}