Classifiers¶
The four classification entry points. Each Base… class takes its configuration as
constructor arguments; each thin subclass looks the same configuration up in a registry by
dataset name.
Hierarchy traversal¶
rac4os.agents.HierarchicalNavigation.BaseAgent.BaseHierarchicalNavigationAgent
¶
Bases: Module
Classifies an item by walking the taxonomy top-down instead of retrieving candidates.
A dspy.ReAct agent is given four navigation tools over your classification system
(get_root_categories, get_children, get_parent, get_code) and reasons its way
from the first level down to a code, deciding at each step which branch to descend.
Nothing is embedded and no vector store is involved, so this is the approach that works
on a taxonomy you have only just described.
The tool descriptions are passed in rather than hardcoded, because what a tool should say about itself depends on how your codes are written: the agent reads them to decide which tool to call next.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_name
|
str
|
dataset name resolved through |
required |
api_key
|
str
|
credential for the LM provider. |
required |
model_name
|
str
|
LiteLLM model identifier, e.g. "openai/gpt-4o-mini". |
required |
signature
|
type[Signature]
|
the I/O contract; its docstring is the system prompt. |
required |
get_root_categories_doc
|
str
|
tool description for listing the first level. |
required |
get_children_doc
|
str
|
tool description for descending one level. |
required |
get_code_doc
|
str
|
tool description for reading a single code. |
required |
get_parent_doc
|
str
|
tool description for stepping back up one level. |
required |
api_base
|
str | None
|
base URL of a self-hosted or proxied endpoint; None uses the provider's own. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
classification_system |
HierarchicalClassificationSystem
|
the taxonomy being
navigated. This approach needs the hierarchy, so a flat |
agent |
ReAct
|
the reasoning loop over the four tools. |
lm |
LM
|
the language model this module pins for its own calls. |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.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 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
__init__
¶
__init__(classification_name, api_key, model_name, signature, get_root_categories_doc, get_children_doc, get_code_doc, get_parent_doc, api_base=None)
Builds the LM, loads the classification system and wires up the four navigation tools.
Note that this configures dspy globally as well as storing the LM on the instance, so
constructing a second classifier with a different model changes the global default. The
forward methods pin their own LM, so already-constructed classifiers keep working.
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
get_code_tool
¶
Tool: read the full record of one code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the code to look up. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
the |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
get_children_tool
¶
Tool: list the categories directly below a code, which is how the agent descends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the parent code. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one dict per direct child; empty for a leaf. |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
get_parent_tool
¶
Tool: step back up one level, which is how the agent recovers from a wrong branch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the code whose parent is wanted. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
the parent's |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
get_root_categories_tool
¶
Tool: list the first level of the taxonomy, where every traversal starts.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
forward
¶
Classify one item.
The signature's input fields are checked against the keyword arguments first, so a
misnamed field surfaces immediately instead of as an obscure failure once the
trajectory is already running. The call is wrapped in a dspy.context that pins this
module's own LM, which makes it safe to hold several classifiers with different models
in one process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
one keyword argument per input field of the signature. |
{}
|
Returns:
| Type | Description |
|---|---|
Prediction
|
dspy.Prediction: the signature's output fields, plus the ReAct trajectory under |
Raises:
| Type | Description |
|---|---|
ValueError
|
if an input field declared by the signature was not passed. |
Source code in rac4os/agents/HierarchicalNavigation/BaseAgent.py
rac4os.agents.HierarchicalNavigation.HierarchicalNavigationAgent.HierarchicalNavigationAgent
¶
Bases: BaseHierarchicalNavigationAgent
BaseHierarchicalNavigationAgent with the bundled configurations in front of it.
CONFIGS holds a ready-made signature and the four tool descriptions per shipped
classification, so only the credentials and the model have to be supplied. For your own
taxonomy either add an entry to CONFIGS or construct
BaseHierarchicalNavigationAgent directly and pass the signature and tool descriptions
yourself; both are the same code path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_name
|
str
|
a key of |
required |
**kwargs
|
Any
|
forwarded to |
{}
|
Source code in rac4os/agents/HierarchicalNavigation/HierarchicalNavigationAgent.py
__init__
¶
Looks the configuration up in CONFIGS and hands it to the base class.
Anything you pass yourself is forwarded unchanged, so a bundled entry can be overridden per call.
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in rac4os/agents/HierarchicalNavigation/HierarchicalNavigationAgent.py
Agentic RAG¶
rac4os.agents.RAG.BaseAgentRAG.BaseRAGAgent
¶
Bases: Module
Classifies an item with the retriever exposed to the model as a tool.
A dspy.ReAct agent gets one tool, search_category, and decides for itself how often
and with which query to use it. Where the linear pipeline is bound by its single
initial query, this agent can reformulate and search again when the candidates it got
back do not fit, which is what makes it the approach to reach for when retrieval is the
part that fails.
The cost of that is variance: how many model calls an item takes is decided by the model, not by you.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_system
|
ClassificationSystem
|
the taxonomy to classify against, built by you or resolved from a dataset name with |
required |
api_key
|
str
|
credential for the LM provider. |
required |
model_name
|
str
|
LiteLLM model identifier, e.g. "openai/gpt-4o-mini". |
required |
signature
|
type[Signature]
|
the I/O contract; its docstring is the system prompt. Exactly one output field name must contain "code", which is the field the predicted code is read from. |
required |
search_category_tool_doc
|
str
|
tool description the agent reads to decide how to query the store. Say what a good query looks like for your labels here. |
required |
store
|
Retriever
|
a filled retriever; this class never indexes. |
required |
api_base
|
str | None
|
base URL of a self-hosted or proxied endpoint; None uses the provider's own. |
None
|
output_validation
|
bool
|
whether to check the predicted code against the classification system after each call and record the outcome on the prediction as |
True
|
**generation_kwargs
|
Any
|
forwarded to |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
classification_system |
ClassificationSystem
|
the taxonomy the predicted code is validated against. |
output_code_key |
str
|
the single output field whose name contains "code"; the field |
store |
Retriever
|
the retriever behind the tool. |
agent |
ReAct
|
the reasoning loop over the search tool. |
lm |
LM
|
the language model this module pins for its own calls. |
Source code in rac4os/agents/RAG/BaseAgentRAG.py
10 11 12 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 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
__init__
¶
__init__(api_key, model_name, signature, search_category_tool_doc, classification_system, store, api_base=None, output_validation=True, **generation_kwargs)
Builds the LM and wraps the store's search_category into the agent's single tool.
The signature is required to declare exactly one output field whose name contains
"code"; that field is what forward validates.
Note that this configures dspy globally as well as storing the LM on the instance, so
constructing a second classifier with a different model changes the global default.
forward pins its own LM, so already-constructed classifiers keep working.
Source code in rac4os/agents/RAG/BaseAgentRAG.py
forward
¶
Classify one item.
The signature's input fields are checked against the keyword arguments first, so a
misnamed field surfaces immediately instead of as an obscure failure once the
trajectory is already running. The call is wrapped in a dspy.context that pins this
module's own LM, which makes it safe to hold several classifiers with different models
in one process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
one keyword argument per input field of the signature. |
{}
|
Returns:
| Type | Description |
|---|---|
Prediction
|
dspy.Prediction: the signature's output fields, the ReAct trajectory under |
The status field says what became of the predicted code: "valid_code" if it is
a code of the classification system, "invalid_code" if it is not (which covers a
hallucinated code, a code written in the wrong notation, and an empty or missing
answer alike), or "no_validation" when output_validation is off. The prediction
is returned either way, so an unusable answer has to be filtered on rather than
caught. When it is "invalid_code", the trajectory is worth reading: it shows
whether the agent ever saw a fitting candidate.
Raises:
| Type | Description |
|---|---|
ValueError
|
if an input field declared by the signature was not passed. |
Source code in rac4os/agents/RAG/BaseAgentRAG.py
rac4os.agents.RAG.RAGAgent.RAGAgent
¶
Bases: BaseRAGAgent
BaseRAGAgent with the bundled configurations in front of it.
CONFIGS holds a ready-made signature and search-tool description per shipped
classification, so only the store, the credentials and the model have to be supplied.
For your own taxonomy either add an entry to CONFIGS or construct BaseRAGAgent
directly; both are the same code path.
The classification system is resolved from classification_name unless you pass a
classification_system of your own, which is what lets one already-built taxonomy be
shared between classifiers instead of being loaded again per classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_name
|
str
|
a key of |
required |
**kwargs
|
Any
|
forwarded to |
{}
|
Source code in rac4os/agents/RAG/RAGAgent.py
__init__
¶
Looks the configuration up in CONFIGS and hands it to the base class.
Anything you pass yourself is forwarded unchanged, so a bundled entry can be
overridden per call. The classification system is built from classification_name,
unless one was passed in, in which case that one is used and the name only selects
the configuration.
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in rac4os/agents/RAG/RAGAgent.py
Linear RAG¶
rac4os.rag.BaseRAG.BaseRAGCoT
¶
Bases: Module
Classifies an item by retrieving once and reasoning once.
The item is embedded, the k nearest categories are pulled from the retriever,
serialised to JSON and placed in the signature's context field next to the item, and a
dspy.ChainOfThought picks one of them. One retrieval call and one model call per
item, which makes it the cheapest of the three approaches and the one to try first.
Its structural weakness is worth knowing before you choose it: retrieval happens once, on the raw item, before any reasoning. If the correct category is not among the k retrieved, no amount of generator quality recovers it. The retriever's accuracy at k is therefore a ceiling on the whole pipeline, and measuring it on its own tells you whether you have a retrieval problem or a generation problem.
The classification system is passed in even though the linear pipeline never traverses it: it is what the predicted code is validated against, and having it here means one taxonomy object can be shared by every approach instead of each loading its own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_system
|
ClassificationSystem
|
the taxonomy to classify against, built by you or resolved from a dataset name with |
required |
api_key
|
str
|
credential for the LM provider. |
required |
model_name
|
str
|
LiteLLM model identifier, e.g. "openai/gpt-4o-mini". |
required |
signature
|
type[Signature]
|
the I/O contract; its docstring is the system prompt. Exactly one output field name must contain "code", which is the field the predicted code is read from. |
required |
query_field
|
str
|
name of the signature input field that holds the item. |
required |
context_field
|
str
|
name of the signature input field that holds the candidate list. |
required |
store
|
Retriever
|
a filled retriever; this class never indexes. |
required |
api_base
|
str | None
|
base URL of a self-hosted or proxied endpoint; None uses the provider's own. |
None
|
output_validation
|
bool
|
whether to check the predicted code against the classification system after each call and record the outcome on the prediction as |
True
|
**generation_kwargs
|
Any
|
forwarded to |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
classification_system |
ClassificationSystem
|
the taxonomy, also used to validate the predicted code. |
output_code_key |
str
|
the single output field whose name contains "code"; the field |
store |
Retriever
|
the retriever the candidates come from. |
CoT |
ChainOfThought
|
the generation step. |
lm |
LM
|
the language model configured for this module. |
Source code in rac4os/rag/BaseRAG.py
10 11 12 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 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
__init__
¶
__init__(classification_system, api_key, model_name, signature, query_field, context_field, store, api_base=None, output_validation=True, **generation_kwargs)
Builds the LM, loads the classification system and the chain-of-thought step.
query_field and context_field are asserted against the signature's declared input
fields, and the signature is required to declare exactly one output field whose name
contains "code", so a name that does not exist fails here rather than at the first
call.
Unlike the agentic classifiers, forward does not pin an LM per call: this module
relies on the global dspy.configure made here. If you run several classifiers with
different models in one process, construct each one immediately before use.
Source code in rac4os/rag/BaseRAG.py
forward
¶
Retrieve candidates for one item and classify it over them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the item to classify, used both as the retrieval query and as the item the generator sees. |
required |
k
|
int
|
how many candidates to retrieve. Raising it makes the correct category more likely to be present and gives the model more chances to pick a wrong one, and it spends context that competes with how richly each candidate is described. |
required |
where
|
dict | None
|
metadata filter passed straight to the store, e.g. |
None
|
Returns:
| Type | Description |
|---|---|
Prediction
|
dspy.Prediction: the signature's output fields, |
The status field says what became of the predicted code: "valid_code" if it is
a code of the classification system, "invalid_code" if it is not (which covers a
hallucinated code, a code written in the wrong notation, and an empty or missing
answer alike), or "no_validation" when output_validation is off. The prediction
is returned either way, so an unusable answer has to be filtered on rather than
caught.
Source code in rac4os/rag/BaseRAG.py
rac4os.rag.RAG.RAGChainOfThought
¶
Bases: BaseRAGCoT
BaseRAGCoT with the bundled configurations in front of it.
CONFIGS holds a signature and the two field names per shipped classification, so only
the store, the credentials and the model have to be supplied. For your own taxonomy
either add an entry to CONFIGS or construct BaseRAGCoT directly and pass the
signature and field names yourself; both are the same code path.
The classification system is resolved from classification_name unless you pass a
classification_system of your own, which is what lets one already-built taxonomy be
shared between classifiers instead of being loaded again per classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_name
|
str
|
a key of |
required |
**kwargs
|
Any
|
forwarded to |
{}
|
Source code in rac4os/rag/RAG.py
__init__
¶
Looks the configuration up in CONFIGS and hands it to BaseRAGCoT.
Anything you pass yourself is forwarded unchanged, so a bundled signature or field name
can be overridden per call. The classification system is built from
classification_name, unless one was passed in, in which case that one is used and
the name only selects the configuration.
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in rac4os/rag/RAG.py
Rephrase-Retrieve-Respond¶
rac4os.rag.RAG.QueryRephraser
¶
Bases: Module
Rewrites a raw item into a retrieval query in the vocabulary of the classification's labels. It does not classify.
Picks the rephrasing signature for classification_name from
self.mapping and runs it on its own lm, which can be the same LM as
the Respond step or a smaller, cheaper one.
Called as rephraser(query=...); returns a dspy.Prediction with
rephrased_query. The input field name of the signature
(input_expense / input_activity) is handled internally, so callers
never need to know it.
Source code in rac4os/rag/RAG.py
__init__
¶
Picks the rephrasing signature and builds the one-shot predictor that runs it.
Either classification_name must be a key of the internal mapping or an explicit
signature must be given; passing classification_name=None together with your own
signature is how you rephrase for a taxonomy that is not bundled. The signature's first
input field is read off here, so callers never have to know what it is called.
Raises:
| Type | Description |
|---|---|
AssertionError
|
if neither a known classification name nor a signature was given. |
Source code in rac4os/rag/RAG.py
forward
¶
Rewrite one item into a retrieval query.
Runs on this module's own LM through a dspy.context, so the rewrite can use a smaller
and cheaper model than the classification step. An empty rewrite falls back to the
original item, so the pipeline can never end up retrieving on an empty query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the raw item. |
required |
Returns:
| Type | Description |
|---|---|
Prediction
|
dspy.Prediction: carries |
Source code in rac4os/rag/RAG.py
rac4os.rag.RAG.RRR_RAGChainOfThought
¶
Bases: RAGChainOfThought
Rephrase-Retrieve-Respond on top of RAGChainOfThought.
- Rephrase -
self.rephrasing_module(aQueryRephraser) turns the raw item into a query in the vocabulary of the classification's labels. - Retrieve - the rephrased query is embedded and the k nearest codes are pulled from the vector store.
- Respond - the parent's ChainOfThought classifies the ORIGINAL item over the retrieved candidates. The rephrasing only steers retrieval; it never replaces the item the model has to classify, so detail the rephraser dropped is not lost.
Parameters (on top of RAGChainOfThought):
rephraser (QueryRephraser) - the Rephrase step, with its own LM and prompt. The
two steps are configured independently: **generation_kwargs passed here
reach the Respond step, while the Rephrase step takes its settings from the
dspy.LM given to the rephraser.
Source code in rac4os/rag/RAG.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
__init__
¶
Builds the linear pipeline and puts the Rephrase step in front of it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rephraser
|
QueryRephraser
|
the Rephrase step. |
required |
*args
|
Any
|
forwarded to |
()
|
**kwargs
|
Any
|
forwarded to |
{}
|
Source code in rac4os/rag/RAG.py
forward
¶
Rewrite the item, retrieve on the rewritten query, then classify the original item.
The rewrite steers retrieval only. The generator still sees the item as it came in, so
detail the rephraser dropped cannot be lost from the decision itself. The query that
was actually retrieved on is attached to the prediction as rephrased_query, which is
what you inspect when a result looks wrong: it separates a bad rewrite from a bad
choice among good candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the item to classify. |
required |
k
|
int
|
how many candidates to retrieve. |
required |
where
|
dict | None
|
metadata filter passed straight to the store. |
None
|
Returns:
| Type | Description |
|---|---|
Prediction
|
dspy.Prediction: the signature's output fields, |
status is set exactly as in BaseRAGCoT.forward. Read it together with
rephrased_query: an "invalid_code" next to a rewrite that drifted away from the
item points at the Rephrase step, not at the generator.