Don't classify, hallucinate!
This post introduces a clever, counter-intuitive technique for classifying data using Large Language Models by making them "hallucinate" categories instead of strictly adhering to a predefined schema. The method involves prompting a simpler LLM to invent a plausible, non-existent classification, then using vector embeddings to map this hallucination to the most similar valid category in a real taxonomy. This approach offers a cheaper and more scalable alternative to traditional structured output methods, circumventing context window limits and showcasing a novel LLM application.
The Lowdown
The article proposes an innovative approach to overcome the limitations of using Large Language Models (LLMs) for classification tasks, particularly when dealing with extensive taxonomies. Instead of forcing LLMs to select from a rigid, predefined list, the author suggests leveraging their generative capabilities to "hallucinate" new categories, which are then mapped back to existing ones using vector embeddings.
- Traditional LLM classification relies on structured outputs, like Pydantic Literals, to constrain the model's response to a predefined set of valid categories.
- This conventional method can become expensive and hit context window limitations when the list of legal classifications is very large, potentially including hundreds or thousands of entries.
- The proposed "hallucination" technique involves prompting a smaller, cheaper LLM to generate a novel, plausible classification for a given query, even if it doesn't exist in the actual taxonomy (e.g., 'brown coffee table' might yield 'Furniture / Living Room / Tables / Coffee').
- This LLM-generated, hypothetical classification is then transformed into a vector embedding.
- A pre-computed vector index of all real, valid classifications is then used to find the closest semantic match to the hallucinated embedding via dot product similarity.
- This methodology enables the use of less powerful, more cost-effective LLMs and eliminates the need to pass large classification schemas with each prompt, effectively addressing both cost and context window constraints.
This ingenious method flips the traditional LLM classification paradigm, demonstrating how LLMs' creative "hallucination" can be harnessed as a powerful intermediate step to achieve precise and cost-effective data categorization.