Two-Layer RAG for Value Grounding and Schema Linking in LLM-Based Text-to-SQL Systems
Resumo
Large Language Models translate natural language into SQL. In environments with dynamic schemas, models hallucinate column names and filter values absent from the target database. We present a two-layer Retrieval-Augmented Generation architecture that addresses schema linking and value grounding within an orchestration pipeline. The structural layer retrieves validated SQL examples via cosine similarity over dense embeddings, while the semantic layer extracts entities from the user query and matches them against real column values through a MinHash-then-Embedding cascade. The architecture improved Execution Accuracy from 23.6% to 42.5%. By successfully grounding filter values, the semantic layer reduced the Empty Result Rate from 26.4% to 7.5%.
Referências
Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., et al. (2020). Language models are few-shot learners. In Advances in Neural Information Processing Systems (NeurIPS), volume 33, pages 1877–1901.
Dong, X., Zhang, C., Ge, Y., Mao, Y., Gao, Y., Lin, J., Lou, D., et al. (2023). C3: Zero-shot text-to-SQL with ChatGPT. In Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing (EMNLP).
Floratou, A., Psallidas, F., Agrawal, A., et al. (2024). NL2SQL is not solved yet: Challenges in real-world enterprise settings. In Proceedings of the VLDB Endowment, volume 17.
Gao, D., Wang, H., Li, Y., Sun, X., Qian, Y., Ding, B., and Zhou, J. (2024). Text-to-SQL empowered by large language models: A benchmark evaluation. Proceedings of the VLDB Endowment, 17(5):1132–1145.
Lei, W., Wang, W., Ma, Z., Gan, T., Lu, W., Kan, M.-Y., and Chua, T.-S. (2020). Re-examining the role of schema linking in text-to-SQL. In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP), pages 6943–6954. Association for Computational Linguistics.
Li, J., Hui, B., Qu, G., Yang, J., Li, B., Li, B., Wang, B., Qin, B., Geng, R., Huo, N., Zhou, X., Ma, C., Li, G., Chang, K. C., Huang, F., Cheng, R., and Li, Y. (2024). Can LLM already serve as a database interface? a big bench for large-scale database grounded text-to-SQLs. In Advances in Neural Information Processing Systems (NeurIPS 2024).
Ouyang, L., Wu, J., Jiang, X., Almeida, D., Wainwright, C., Mishkin, P., Zhang, C., Agarwal, S., Slama, K., Ray, A., et al. (2022). Training language models to follow instructions with human feedback. Advances in Neural Information Processing Systems, 35:27730–27744.
Pourreza, M. and Rafiei, D. (2024). DIN-SQL: Decomposed in-context learning of text-to-SQL with self-correction. In Advances in Neural Information Processing Systems (NeurIPS 2024).
Pydantic AI Team (2024). Pydantic AI: Agent framework for model serving. [link].
Rajkumar, N., Li, R., and Bahdanau, D. (2022). Evaluating the text-to-SQL capabilities of large language models. arXiv preprint arXiv:2204.00498.
Yu, T., Zhang, R., Yang, K., Yasunaga, M., Wang, D., Li, Z., Ma, J., Li, I., Yao, Q., Roman, S., Zhang, Z., and Radev, D. (2018). Spider: A large-scale human-labeled dataset for complex and cross-domain semantic parsing and text-to-SQL task. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing (EMNLP), pages 3911–3921. Association for Computational Linguistics.
