Introduction
In-context learning (ICL) has revolutionized how we interact with large language models (LLMs), enabling them to perform complex tasks by following a few provided examples. However, the performance of ICL is highly dependent on the quality and relevance of these demonstrations. The conventional approach, raw-input-based selection, typically employs K-Nearest Neighbors (KNN) to retrieve examples based on surface-level text similarity. This method often fails because it is susceptible to "surface feature bias"—where the retriever is distracted by specific entities or domain-specific vocabulary rather than the underlying logic required for the task.
For instance, in a text-to-SQL scenario, a query regarding "employees under age 30" might be incorrectly paired with an example about "employees in London" simply because they share the keyword "employees." The former requires a numerical constraint skill, while the latter involves a simple filtering operation. This mismatch prevents the LLM from receiving the most helpful logical guidance.
The SKILL-KNN Framework
To overcome these limitations, we propose SKILL-KNN, a training-free "rewrite-then-retrieve" framework. SKILL-KNN optimizes the input to the embedding model by first prompting an LLM to generate a "skill-based description" of the logic required to solve a query. This description serves as a denoised representation of the task's structural skeleton.
The SKILL-KNN pipeline follows three main steps:
- Prompting-Based Rewriting: Given a new query, the system uses a small set of human-annotated demonstrations to prompt an LLM to generate a skill-based description (e.g., "This task requires joining two tables and applying an interval constraint").
- Skill-Based Selection: These descriptions are converted into embeddings using an off-the-shelf model like SBERT. The system then retrieves the $k$ most similar examples from a bank based on these skill-centric embeddings.
- In-Context Learning: The retrieved examples are integrated into a final prompt for the backbone LLM to solve the original task.
To address potential variability in LLM-generated descriptions, the framework includes two variants: Consistency-Based, which uses the central embedding of multiple candidate descriptions, and Distinctiveness-Based, which emphasizes unique informative features.
Experimental Evaluation
We evaluated SKILL-KNN across several challenging benchmarks, including semantic parsing (Spider, BIRD), compositional generalization (COGS), and mathematical reasoning (GSM8K). The framework was tested using various backbone models, including GPT-4 and GPT-3.5-Turbo.
Key Results and Insights
- Superior Accuracy: SKILL-KNN consistently outperformed standard KNN across all evaluated tasks. For example, on the Spider dataset using
text-chat-davinci-002, SKILL-KNN achieved 78.3% execution accuracy, a significant improvement over the 74.6% achieved by standard KNN. - Effective Denoising: Skill descriptions act as a filter, stripping away surface noise (like specific entities) and focusing on core operations (like "JOIN" or "GROUP BY"). T-SNE visualizations demonstrate that skill-based embeddings create much clearer and tighter clusters compared to raw-input embeddings.
- Robustness and Generalization: The method proved highly resilient to data perturbations and generalized effectively even with as few as 4-8 human-annotated examples for the rewriting stage. Notably, this training-free approach often rivals or exceeds the performance of state-of-the-art methods that require extensive fine-tuning.
Conclusion and Future Work
SKILL-KNN demonstrates that shifting the focus from raw text similarity to underlying logic significantly enhances the effectiveness of in-context learning. While the additional LLM call for rewriting increases inference latency, the gains in accuracy and robustness are substantial. Future research will focus on reducing computational costs and automating the generation of skill descriptions for the example bank.
Summary updated