Skip to content

How It Works

The pipeline

Retrieval-Augmented Generation gives a language model access to knowledge it does not have in its weights — here, your classification. In its naïve form the pipeline has two components and runs each of them once:

item ──► embed ──► nearest k categories ──► JSON context ──► chain-of-thought ──► code + explanation
             │                                      │
        your retriever                         your signature
  1. Index (once, up front). Every category is embedded into a vector store. No chunking happens — a category label is already a short text, and splitting it would destroy the unit of retrieval.
  2. Retrieve. The item is embedded with the same model and the k nearest categories are returned.
  3. Augment. Those candidates are serialised to JSON and placed in the signature's context field, next to the item. What each candidate looks like is yours to decide.
  4. Read. A dspy.ChainOfThought reasons over the candidates and emits your code field plus an explanation.

The cost per item is a function of k, not of the size of your taxonomy, and nothing needs labelled data: the knowledge base is built from the classification's own documentation.

Why chain-of-thought

Choosing between candidate categories is a comparison under exclusion rules, not a lookup. The model has to weigh a candidate against the others, notice that a more specific category exists, or see that an apparently fitting label is excluded by its own notes. Reasoning before answering suits that, and it leaves a trace a reviewer can audit — which is why explanation is a declared output field rather than an afterthought.

Chain-of-thought produces its whole reasoning path in one pass, before any observation comes back. That is exactly what makes it cheap, and exactly what distinguishes it from the agentic variant.

The retrieval bottleneck

The structural weakness is worth understanding before you choose this approach: the pipeline is bound by its initial query. 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 answer is not in the room.

In practice this, rather than generation quality, is usually what limits a RAG classification pipeline. Two consequences:

  • The retriever's accuracy at k is a ceiling on the pipeline. Measure it on its own before touching any prompt. Whether the correct code is present in the candidate set tells you immediately whether you have a retrieval problem or a generation problem, and the two are fixed by completely different work.
  • Raising k trades one error for another. A larger candidate set is more likely to contain the correct category and gives the model more opportunity to pick a wrong one. It also spends context, which competes with how richly each candidate is described.

The two other approaches in the package exist to address this bottleneck from different directions: Rephrase-Retrieve-Respond improves the single query, agentic RAG allows more than one.

Components

Component Role Where it comes from
classification system validates the predicted code; the flat interface suffices Classification Systems
retriever finds the candidates Retrievers
signature the system prompt and the I/O contract Prompt Overview
query_field / context_field which input field holds the item, which the candidates your signature's field names
dspy.ChainOfThought the generation built from the signature
code validation checks the answer is a code of the taxonomy, sets status output_validation, is_valid_code
LM model_name, api_key, api_base Installation
generation settings temperature, max_tokens and any other dspy.LM kwarg Generation settings

The classification system is loaded even though the linear pipeline never traverses it: it is there so that the same classification_name means the same thing across all approaches, and so you can enrich candidates from the hierarchy yourself.

What forward does

def forward(self, query: str, k: int, where: dict | None = None):
    retrieved_context = self.store.search_category(query, k, where=where)
    retrieved_context_str = json.dumps(retrieved_context, indent=4, ensure_ascii=False)
    return self.CoT(**{
        self.query_field: query,
        self.context_field: retrieved_context_str,
    })

That is the entire pipeline — the three runtime steps are four lines, because everything specific to your taxonomy lives in the store you passed and the signature you wrote.