Retrieve similar QA examples on the fly so LLMs write correct SPARQL without fine-tuning

July 1, 20246 min

Overview

Decision SnapshotReady For Pilot

DFSL shows strong empirical gains on multiple public KGQA benchmarks using large open LLMs and dense retrieval; it depends on gold entity/relation inputs and a relevant example storage, and it was not tested on small models or non-English data.

Citations0

Evidence Strength0.80

Confidence0.80

Risk Signals10

Trust Signals

Findings with numeric evidence: 4/4

Findings with evidence refs: 4/4

Results with explicit delta: 3/3

Reproducibility

Status: Partial assets available

Open source: Partial

At A Glance

Cost impact: 70%

Production readiness: 60%

Novelty: 60%

Authors

Jacopo D'Abramo, Andrea Zugarini, Paolo Torroni

Links

Abstract / PDF / Data

Why It Matters For Business

DFSL gives near state-of-the-art KGQA without dataset fine-tuning, cutting training cost and enabling faster deployment across knowledge graphs.

Who Should Care

Summary TLDR

The paper introduces DFSL, a way to improve SPARQL generation by retrieving k similar question→SPARQL examples and injecting them into an LLM prompt (in-context learning). DFSL (and a multi-query extension DFSL-MQ) raises F1 substantially across Wikidata/DBpedia benchmarks, matching or beating fine-tuned systems in 3 of 4 datasets. Key caveats: DFSL needs gold entities/relations present and benefits when the training storage contains similar examples; triple-flip errors remain on some datasets.

Problem Statement

Generating SPARQL queries from natural language is brittle. Fine-tuning helps but is expensive and generalizes poorly. The paper asks: can we use in-context learning (ICL — asking a large model to follow examples in the prompt) plus semantic retrieval of similar examples to get SOTA or near-SOTA KGQA performance without fine-tuning?

Main Contribution

Dynamic Few-Shot Learning (DFSL): retrieve k most similar question→SPARQL examples and inject them into the LLM prompt at inference time.

DFSL-MQ: keep multiple beam-search SPARQL hypotheses and select answers with heuristics (First Set and Largest Set) to reduce triple-flip errors.

Key Findings

DFSL can turn an unfinetuned LLM into a competitive KGQA system.

NumbersLC-QuAD 2.0 F1: zero-shot 38.40 → DFSL 85.45 (+47.05)

Practical UseIf you have a text-to-SPARQL corpus, retrieve similar examples to include in prompts to get large F1 gains without training.

Evidence RefTable 1 (CodeLlama row) and Table 3 DFSL entries

Multi-query generation plus a First-Set selection further increases accuracy.

NumbersQALD-9 Plus F1: DFSL 76.59 → DFSL-MQ (First Set) 84.45 (+7.86 to +8.60 reported)

Practical UseRun beam search, keep multiple SPARQL outputs, and pick the first non-empty result to recover subject/object order errors.

Evidence RefTable 2 and Table 3 (DFSL-MQ beam FS)

Results

MetricValueBaselineDeltaSplit / DatasetEvidenceEvidence Ref
F1 (DFSL, CodeLlama backbone)QALD-9 Plus 76.59; QALD-10 57.69; LC-QuAD 2.0 85.45; QALD-9 DB 75.14few-shot / zero-shot baselinessubstantial gains over zero-shot; DFSL > few-shot on most setsTable 3 / Table 1 aggregatedTables 1 and 3Tables 1, 3
F1 (DFSL-MQ with First-Set selection)QALD-9 Plus 84.45; QALD-10 62.20; LC-QuAD 2.0 89.10; QALD-9 DB 77.89DFSL single-queryimprovements up to ~8.6 F1 points (QALD-9 Plus); mixed on LC-QuADTable 3 / Table 2Tables 2 and 3Tables 2, 3

What To Try In 7 Days

Assemble a storage of question→SPARQL examples from your KG training set.

Encode inputs with all-mpnet-base-v2 using 'question + entities + relations' and index by cosine similarity.

At inference, retrieve top-k=5 similar examples and append them as demonstrations to an LLM prompt (k=5 is a good trade).

Reproducibility

Code AvailableNo
Data AvailableYes
Open Source StatusPartial
LicenseUnknown

Data URLs

QALD-9 DBQALD-9 PlusQALD-10LC-QuAD 2.0

Risks & Boundaries

Limitations

All experiments use English datasets; multilingual behavior is untested.

Possible data contamination from LLM pretraining is acknowledged but not measured.

When Not To Use

When you cannot extract reliable entities or relations from the question, since performance drops sharply.

When your training storage lacks similar examples to the target questions (out-of-distribution test sets).

Failure Modes

Triple-flip: model swaps subject/object in triples; only partially mitigated by multi-query strategies.

Largest-Set heuristic can pick under-constrained queries that return many irrelevant results.

Core Entities

Models

Mixtral 8x7BLlama-3 70BCodeLlama 70B

Metrics

F1

Datasets

QALD-9 DBQALD-9 PlusQALD-10LC-QuAD 2.0

Benchmarks

QALD seriesLC-QuAD 2.0