Abstract
kNN-ICL enhances Large Language Models (LLMs) for task-oriented parsing by combining standard In-Context Learning (ICL) with a retrieval-based $k$-Nearest Neighbor mechanism. By allowing the model to "look up" similar examples from a massive datastore during the generation process, it overcomes the context window limits of LLMs and significantly improves performance on complex, nested user commands.
Introduction
Task-Oriented Parsing (TOP) serves as the fundamental engine for virtual assistants, translating natural language utterances (e.g., "Find me a route to the Eagles game") into structured API calls. Traditionally, achieving high accuracy in TOP required massive amounts of labeled data and specialized architectures.
While Large Language Models (LLMs) have shown promise in this domain via In-Context Learning (ICL)—where a few examples are provided in the prompt—they face two significant hurdles:
- The Length Constraint: LLMs have strict token limits, preventing them from utilizing the thousands of examples often available in a training set.
- Structural Complexity: Standard ICL often fails when user requests are "nested" or compositional, requiring the model to call one API inside another.
Methodology
The authors propose a two-pronged approach to bridge the gap between natural language and structured code: mapping TOP structures to code and implementing a $k$NN-based retrieval mechanism.
Reduction to Code Generation
Instead of using traditional semantic tags (like [IN:GET_DIRECTIONS]), the authors map TOP structures to Python-style API calls. Since LLMs are heavily pre-trained on code, this format allows them to leverage their existing "programming logic" to generate more accurate and syntactically correct parses.
kNN-ICL: The Core Innovation
The $k$NN-ICL mechanism allows the model to access a vast external memory during inference:
- Datastore Construction: An offline datastore is created containing all available training examples, stored as pairs of (context, next token).
- Retrieval & Interpolation: During inference, as the LLM generates a parse token-by-token, the system performs a $k$NN search to find the $k$ most similar contexts in the datastore.
- Probability Mixing: The final probability for the next token is a weighted average (interpolation) of the LLM’s own prediction and the distribution of tokens found in the $k$-nearest neighbors. This effectively provides the model with a "cheat sheet" of the entire training set at every step of generation.
Experimental Setup
The study utilizes TOPv2, a large-scale dataset for task-oriented parsing across multiple domains. The authors focus on four representative domains to test varying levels of complexity:
- Navigation & Reminder: Characterized by high complexity and deeply nested structures.
- Alarm & Weather: Characterized by lower complexity and flatter structures.
Key Results and Insights
Performance Gains and Scaling
$k$NN-ICL demonstrates significant improvements over standard ICL. On the TOPv2 Navigation domain, $k$NN-ICL improved GPT-NeoX's Exact Match (EM) score from 1.81% to 5.69% and CodeGen's from 3.94% to 8.37%. When using a larger datastore of 2,000 examples, $k$NN-ICL outperformed standard retrieval-based baselines (like $k$NN-LM) by over 20% in some domains.
Surpassing Supervised Baselines
One of the most striking findings is that the Codex-based $k$NN-ICL implementation achieved an average EM of 47.00%. This performance outstrips the specialized, fully-supervised RINE model (35.94%) across several domains without requiring any fine-tuning.
Robustness to Complexity
The research highlights that standard ICL performance drops significantly as the "depth" of the API call increases. In contrast, $k$NN-ICL remains remarkably stable, showing that the retrieval mechanism provides the structural guidance the LLM lacks on its own for compositional tasks.
Conclusion and Future Work
While $k$NN-ICL represents a major step forward, several limitations remain. The method requires access to the model's internal probability distributions (logits), making it difficult to apply to "closed" models like GPT-4 via standard APIs. Additionally, performing a $k$NN search at every decoding step adds computational overhead that may impact real-time performance.
Future research directions include optimizing inference latency for real-time applications and developing models that can autonomously select the most effective prompt strategy based on their own parameter scale and capabilities.
Summary updated