RAG types
Base class for every RAG type.
To add one, inherit from BaseRAG and: 1. describe its steps in FLOW (the docs playground draws this diagram) 2. implement run(), recording every step with a Trace answer() then works for free.
BaseRAG
Common interface for RAG types.
FLOW lists the steps and the arrows between them
{"nodes": [{"id": "retrieve", "label": "Retrieve", "kind": "search"}, ...], "edges": [["retrieve", "rerank"], ...]}
Every step that run() records must be a node, and two steps in a row must be joined by an edge.
Source code in src/rag_titan/rag_types/base_rag.py
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 | |
answer(query)
Return only the answer text.
Source code in src/rag_titan/rag_types/base_rag.py
38 39 40 | |
run(query)
Answer the question and show how.
Returns:
| Type | Description |
|---|---|
|
A dict with "answer" (the text), "sources" (the chunks used for |
|
|
the answer) and "steps" (one {"step", "input", "output", "ms"} |
|
|
dict per step, in order). |
Source code in src/rag_titan/rag_types/base_rag.py
27 28 29 30 31 32 33 34 35 36 | |
Records every step a RAG type takes, so you can see (and draw) how an answer was made.
trace = Trace()
matches = trace.record("retrieve", retriever.retrieve, query="leave?", top_k=4)
print(describe_steps(trace.steps))
Trace
A list of steps. Each step is a dict: {"step": "retrieve", "input": {...}, "output": ..., "ms": 1.25}
Source code in src/rag_titan/rag_types/trace.py
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 | |
add(step, inputs, output, ms=0.0)
Save a step that was not made by calling a function (e.g. giving up).
Source code in src/rag_titan/rag_types/trace.py
40 41 42 | |
record(step, function, **inputs)
Call function(**inputs), save it as a step, and return its output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
the step name, one of the RAG type's FLOW node ids. |
required | |
function
|
the function to call, e.g. retriever.retrieve. |
required | |
inputs
|
the keyword arguments for the function. |
{}
|
Source code in src/rag_titan/rag_types/trace.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
result(answer, sources=None)
Return what run() gives back: the answer, its sources and the steps.
Source code in src/rag_titan/rag_types/trace.py
44 45 46 | |
describe_steps(steps)
Return a short, readable log of the steps, one line per step.
Example line
- retrieve 0.21 ms query='how many leave days?', top_k=4 -> 3 chunks: chunk_0 hr.pdf (0.894), chunk_2 it.pdf (0.000), ...
Source code in src/rag_titan/rag_types/trace.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Simple RAG
Question -> Retrieve -> Rerank -> Generate
SimpleRAG
Bases: BaseRAG
The classic RAG: one search, then one answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever
|
any BaseRetriever, for example a DenseRetriever. |
required | |
reranker
|
a BaseReranker. |
required | |
generator
|
a BaseGenerator. |
required | |
top_k
|
how many chunks to retrieve. |
5
|
|
top_n
|
how many chunks to keep after reranking. |
2
|
Source code in src/rag_titan/rag_types/simple_rag.py
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 | |
run(query)
Answer the question from the indexed documents and record each step.
Source code in src/rag_titan/rag_types/simple_rag.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
Adaptive RAG and its helpers (router, evidence checker, query rewriter).
AdaptiveRAG
Bases: BaseRAG
A RAG that changes its effort based on the question.
Greetings get a direct reply, simple questions a small search, complex questions a bigger search. When the evidence is weak, the question is rewritten and the search is tried again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
any BaseLLM, used for routing, checking and rewriting. |
required | |
retriever
|
any BaseRetriever, for example a DenseRetriever. |
required | |
reranker
|
a BaseReranker. |
required | |
generator
|
a BaseGenerator. |
required | |
max_attempts
|
how many searches to try before giving up. |
2
|
Source code in src/rag_titan/rag_types/adaptive/adaptive_rag.py
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 | |
run(query)
Answer the question from the indexed documents and record each step.
Source code in src/rag_titan/rag_types/adaptive/adaptive_rag.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
EvidenceChecker
Asks the LLM whether the chunks are enough to answer the question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
any BaseLLM. |
required |
Source code in src/rag_titan/rag_types/adaptive/evidence_checker.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
has_enough(query, chunks)
Return True when the LLM replies "yes".
Source code in src/rag_titan/rag_types/adaptive/evidence_checker.py
18 19 20 21 22 23 24 25 26 | |
QueryRewriter
Rewrites a question into a better search query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
any BaseLLM. |
required |
Source code in src/rag_titan/rag_types/adaptive/query_rewriter.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
rewrite(query)
Return the rewritten query, or the original if the LLM is empty.
Source code in src/rag_titan/rag_types/adaptive/query_rewriter.py
17 18 19 20 | |
Router
Decides how much work a question needs: direct, simple or complex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
any BaseLLM. |
required | |
default_route
|
route used when the LLM reply is not a known label. |
'simple'
|
Source code in src/rag_titan/rag_types/adaptive/router.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
route(query)
Return "direct", "simple" or "complex".
Source code in src/rag_titan/rag_types/adaptive/router.py
21 22 23 24 25 26 27 28 29 30 | |