ChatKBQA: A Generate-then-Retrieve Framework for Knowledge Base Question Answering with Fine-tuned Large Language Models (2024)

Abstract: Knowledge Base Question Answering (KBQA) aims to answer natural language questions over large-scale knowledge bases (KBs), which can be summarized into two crucial steps: knowledge retrieval and semantic parsing. However, three core challenges remain: inefficient knowledge retrieval, mistakes of retrieval adversely impacting semantic parsing, and the complexity of previous KBQA methods. To tackle these challenges, we introduce ChatKBQA, a novel and simple generate-thenretrieve KBQA framework, which proposes first generating the logical form with finetuned LLMs, then retrieving and replacing entities and relations with an unsupervised retrieval method, to improve both generation and retrieval more directly. Experimental results show that ChatKBQA achieves new stateof-the-art performance on standard KBQA datasets, WebQSP, and CWQ. This work can also be regarded as a new paradigm for combining LLMs with knowledge graphs (KGs) for interpretable and knowledge-required question answering. Our code is publicly available1.

heading: 1 Introduction
text: Knowledge Base Question Answering (KBQA) is a classical NLP task to answer natural language questions based on facts over a large-scale knowledge base (KB), such as Freebase (Bollacker et al., 2008), Wikidata (Vrandečić and Krötzsch, 2014), and DBpedia (Auer et al., 2007), which are composed of structured knowledge graphs (KGs) built from triples consisting of (head entity, relation, tail entity). Previous KBQA methods primarily addressed two core issues: knowledge retrieval (Yao et al., 2007) and semantic parsing (Berant et al., 2013). Knowledge retrieval mainly aims to locate the most relevant entities, relations, or triples according to the question from KB, to narrow the
* Corresponding author. 1https://github.com/LHRLAB/ChatKBQA
scope of consideration. Then, semantic parsing essentially converts the question from unstructured natural language into a structured logical form (such as S-expression (Gu et al., 2021)), which can then be converted into an executable graph database query (such as SPARQL (Pérez et al., 2006)) to obtain precise answers and interpretable paths, as shown in Figure 1.
Previous KBQA work (Miller et al., 2016; Sun et al., 2019; Zhang et al., 2022) proposed different knowledge retrieval methods with technologies of named entity recognition (NER) (Devlin et al., 2019), entity linking (Li et al., 2020) or subgraph retrieval (Zhang et al., 2022) to align natural language questions with structured KB. After retrieving factual triples, some studies (Yih et al., 2016; Lan and Jiang, 2020; Jiang et al., 2023b) utilized strategies of step-wise query graph generation and search answers with semantic parsing. On the other hand, other work (Ye et al., 2022; Hu et al., 2022b; Shu et al., 2022; Yu et al., 2023; Zhang et al., 2023) performed semantic parsing by using a seq2seq model like T5 (Raffel et al., 2020) to generate a
2039

heading: Benjamin Netanyahu

heading: Previous Retrieve-then-Generate KBQA Framework ChatKBQA: Generate-then-Retrieve KBQA Framework
text: logical form and then converted it to an SPARQL query to fetch answers when executed over KB.
Despite this, three main challenges remain, as shown on the left side of Figure 2: (i) Low retrieval efficiency. Traditional methods first identify the span of candidate entities and then do entity retrieval and relation retrieval. Since the structure of natural language questions differs from KB facts, most approaches require training dedicated models for extraction and linking inefficiently. (ii) Incorrect retrieval results will mislead semantic parsing. Previous methods have utilized retrieved triples also as input of reference to the seq2seq model along with the original question. However, since the retrieved triples are not always accurate, they adversely impact semantic parsing outcomes. Additionally, if there are numerous retrieved triples, the seq2seq model requires a much longer context length. (iii) Multiple processing steps make KBQA a redundantly complex task. Previous work decomposed the KBQA task into multiple sub-tasks (Hu et al., 2022b; Shu et al., 2022; Yu et al., 2023), forming a complex pipeline, which made reproduction and migration challenging. In the era when large language models (LLMs) (OpenAI, 2023; Touvron et al., 2023; Pan et al., 2023) are restructuring traditional NLP tasks (Chung et al., 2022; Pan et al., 2023), a more straightforward solution utilizing LLMs to reformulate the traditional KBQA paradigm is promising.
To overcome these challenges, we introduce ChatKBQA, a novel generate-then-retrieve KBQA framework based on open-source LLMs, such as Llama (Touvron et al., 2023), ChatGLM (Zeng
et al., 2023) and Baichuan (Yang et al., 2023). As illustrated on the right side of Figure 2, ChatKBQA simplifies KBQA into two efficient phases: generating logical forms and then retrieving relevant entities and relations. In the generation phase, leveraging instruction tuning (Mangrulkar et al., 2022), fine-tuned LLMs exhibit high accuracy in semantic parsing of natural language questions without retrieval. The generated logical forms are not only mostly correct in skeleton (entities and relations masked) but also semantically consistent or close to the ground truth in terms of entities and relations. In the retrieval phase, ChatKBQA proposes an unsupervised retrieval method that employs phraselevel semantic retrieval within knowledge bases to improve generation accuracy and retrieval efficiency further. Additionally, ChatKBQA features a plug-and-play characteristic, ensuring compatibility with various LLMs and retrieval models, making it a flexible solution for KBQA tasks.
To valid the performance of our proposed framework, we conduct experiments on two standard KBQA datasets, WebQSP (Yih et al., 2016) and ComplexWebQuestions (CWQ) (Talmor and Berant, 2018), with both settings of using and not using golden entities. The experimental results demonstrate that ChatKBQA achieves a new stateof-the-art performance in the KBQA task. We also set up additional experiments to validate that our generate-then-retrieve approach improves both generation and retrieval results efficiency. Finally, we also discuss how insights from this framework lead us to envision future combinations of LLMs and KGs for knowledgable and interpretable Q&A.

heading: 2 Related Work

heading: 2.1 Knowledge Base Question Answering
text: Existing Knowledge Base Question Answering (KBQA) methods can be broadly categorized into Information Retrieval-based (IR-based) and Semantic Parsing-based (SP-based) methods. Recently, there have been some KBQA methods based on large language models (LLM-based) as well.
(a) IR-based KBQA methods (Miller et al., 2016; Sun et al., 2019; Saxena et al., 2020; He et al., 2021; Shi et al., 2021; Zhang et al., 2022) primarily retrieve relevant factual triples or text from KBs based on natural language questions, forming a subgraph to determine answers.
(b) SP-based KBQA methods focus on translating questions into logical forms executable against KBs, such as SPARQL, query graph, and S-expression. Some SP-based approaches (Yih et al., 2016; Chen et al., 2019; Lan et al., 2019; Bhutani et al., 2019; Lan and Jiang, 2020; Jiang et al., 2023b) utilize strategies of step-wise query graph generation and search for semantic parsing. Alternatively, other SP-based methods (Das et al., 2021; Ye et al., 2022; Cao et al., 2022; Shu et al., 2022; Hu et al., 2022b; Xie et al., 2022; Yu et al., 2023; Zhang et al., 2023) employ seq2seq models to generate S-expressions completely and offer various enhancements to the semantic parsing process.
(c) LLM-based KBQA methods (Jiang et al., 2023a; Gu et al., 2023; Sun et al., 2024) utilize the thinking capabilities of LLMs to find answers by retrieving from the graph in a step-wise manner.
In this paper, our proposed ChatKBQA is the first SP-based KBQA method using fine-tuned LLMs, which innovatively proposes a generatethen-retrieve approach to simplify KBQA method.

heading: 2.2 Large Language Models
text: With ChatGPT (OpenAI, 2023) displaying the prowess of decoder-only large language models (LLMs), many traditional NLP tasks are becoming simplified (Pan et al., 2023). Subsequently, opensource LLMs like Llama (Touvron et al., 2023), ChatGLM (Zeng et al., 2023), and Baichuan (Yang et al., 2023) emerged and can be supervisedly finetuned (SFT) using Parameter-Efficient Fine-Tuning (PEFT) technologies (Mangrulkar et al., 2022) such as LoRA (Hu et al., 2022a), QLoRA (Dettmers et al., 2023), P-Tuning v2 (Liu et al., 2022a), and Freeze (Geva et al., 2021), enhancing the capabilities of LLMs for specific tasks.

heading: 2.3 Knowledge Retrieval for KBQA
text: General retrieval methods are typically divided into lexical methods, such as BM25 (Robertson and Zaragoza, 2009), and dense retrieval models, such as Dense Passage Retrieval (DPR) (Karpukhin et al., 2020), SimCSE (Gao et al., 2021), and Contriever (Izacard et al., 2022). In KBQA task, to better utilize knowledge related to the question from KB, ELQ (Li et al., 2020) and FACC1 (Evgeniy et al., 2013) are commonly used to entity retrieval.
In this paper, our ChatKBQA framework proposes a phrase-level retrieval method for entities and relations in an unsupervised manner after LLM’s generation of logical form, improving both generation performance and retrieval efficiency.

heading: 3 Preliminaries
text: In this section, we define two basic concepts of our work: the knowledge base and the logical form, followed by the problem statement for KBQA task.
Definition 1: Knowledge Base (KB). A KB K = {(s, r, o)|s ∈ E , r ∈ R, o ∈ E ∪ L} is an RDF graph consisting of triples (s, r, o) where s is an entity, r is a relation , and o can be an entity or a literal. Each entity e ∈ E in the entity set E is represented by a unique ID, e.g., e.id="m.0fm2h", which can be queried to get its label as e.label="Benjamin Netanyahu". Each relation r ∈ R in the relation set R has a multiple-level label, e.g. r="government. government_position_held. appointed_by".
Definition 2: Logical Form. A logical form is a structured representation of a natural language question. Taking the S-expression as an example, a logical form usually consists of projection and various operators. Projection operation represents a one-hop query of a triple (s, r, o) on s or o, where, (?, r, o) is denoted as (JOIN r o), while (s, r, ?) is denoted as (JOIN (R r) s). Other operators, e.g. "AND", "COUNT", and "ARGMAX", are introduced in Appendix A.
Problem Statement. For KBQA task, given a natural language question Q, and a knowledge base K, we need to first convert Q into a logical form F = Sp(Q), where Sp(.) is a semantic parsing function. Then convert F to the equivalent SPARQL query q = Convert(F ), where Convert(.) is the fixed conversion function. Finally, the final set of answers A = Execute(q|K) is obtained by executing q against K, where Execute(.) is the query execution function.

heading: Open-source

heading: Entities: ID2label

heading: WebQSP

heading: 4 Methodology
text: In this section, we first present an overview of the ChatKBQA framework as shown in Figure 3, and introduce efficient fine-tuning on large language models (LLMs), logical form generation by finetuned LLMs, unsupervised retrieval for entities and relations, and interpretable query execution.

heading: 4.1 Overview of ChatKBQA
text: ChatKBQA is a generate-then-retrieve KBQA framework with fine-tuned LLMs. First, the ChatKBQA framework needs to efficiently fine-tune an open-source LLM based on the (natural language question, logical form) pairs in the KBQA dataset by instruction tuning. The fine-tuned LLM is then used to convert the new natural language questions to according candidate logical forms by semantic parsing. Then, ChatKBQA retrieves the entities and relations in these logical forms at the phrase level, and searches for the logical forms that can be executed against KB after being converted to SPARQL. Finally, the converted SPARQL generates the final set of answers, resulting in interpretable and knowledge-required responses to natural language questions.

heading: 4.2 Efficient Fine-Tuning on LLMs
text: To construct the instruction fine-tuning training data, ChatKBQA first converts the SPARQL corresponding to the natural language questions of the train set in the KBQA dataset into equivalent logical forms and then replaces the entity IDs (e.g., "m.06w2sn5") in these logical forms with the corresponding entity tags (e.g., "[ Justin Bieber
]"), to let LLMs understand entity labels better than meaningless entity IDs. We then combine the natural language question (e.g. "What is the name of Justin Bieber’s brother?") and the processed corresponding logical form (e.g. "(AND (JOIN [ people, person, gender ] [ Male ]) (JOIN (R [ people, sibling relationship, sibling ]) (JOIN (R [ people, person, sibling s ]) [ Justin Bieber ])))") as "input" and "output" respectively, and add "instruction" as "Generate a Logical Form query that retrieves the information corresponding to the given question." constitutes the instruction fine-tuning training data for LLMs.
ChatKBQA employs Parameter Efficient FineTuning (PEFT) (Mangrulkar et al., 2022) techniques including various efficient fine-tuning methods, such as LoRA (Hu et al., 2022a), QLoRA (Dettmers et al., 2023), P-tuning v2 (Liu et al., 2022a), and Freeze (Geva et al., 2021), to minimize the cost of fine-tuning LLMs with a large number of parameters. ChatKBQA can switch between all the above fine-tuning methods as well as open-source LLMs, such as Llama-2-7B (Touvron et al., 2023) and ChatGLM2-6B (Zeng et al., 2023).

heading: 4.3 Logical Form Generation by LLMs
text: Through fine-tuning, the LLMs have acquired expertise in semantic parsing, enabling them to convert natural language questions into logical forms. We apply the fine-tuned LLMs to perform semantic parsing on the new questions in the test set and observe that approximately 63% of the samples match the ground truth logical forms exactly. When employing beam search, the set of candidate logical
forms C generated by our LLMs includes approximately 74% of the instances with correct logical forms, indicating that fine-tuned LLMs possess effective learning and parsing abilities for semantic parsing tasks. In addition, by replacing the entities and relations in the candidate logical forms with "[]" (for example, "(AND (JOIN [] []) (JOIN (R []) (JOIN (R []) [])))"), more than 91% of the samples contain the candidate skeleton. Hence, the next step involves retrieving the entities and relations in the logical form with the corresponding ones from the KB to enhance performance further.

heading: 4.4 Unsupervised Retrieval for Ents and Rels
text: Due to the strong generative capabilities of finetuned LLMs for logical form skeletons, we employ an unsupervised retrieval approach during the retrieval phase. This method involves subjecting the entities and relations in the candidate logical forms to phrase-level semantic retrieval and replacement. The result is a final logical form that can be executed as a SPARQL query against the KB.
Algorithm 1: Unsupervised Retrieval Input :Candidate logical forms generated from LLM
C, top-k threshold ke, kr, k1, k2, probability threshold te, tr, t1, t2, the entity set of KB E
Output :The equivalent SPARQL query q C′ ← ∅ foreach F ∈ C do
foreach e ∈ F do elist ← ∅ foreach e′ ∈ E do
se ← SimiEntities(e, e′); elist.append((e ′, se));
elist ← TopKwithThreshold(elist, ke, te); F.attach(elist);
Flist ← PermuteByEntity(F ); C′.append(TopKwithThreshold(Flist, k1, t1));
C′′ ← ∅ foreach F ∈ C′ do foreach e ∈ F do
rlist ← ∅ foreach r ∈ Neighborhood(EF ) do sr ← SimiRelations(r, r′); rlist.append((r ′, sr)); rlist ← TopKwithThreshold(rlist, kr, tr); F.attach(rlist);
Flist ← PermuteByRelation(F ); C′′.append(TopKwithThreshold(Flist, k2, t2));
foreach F ∈ C′′ do q = Convert(F ) if q is valid to execute then
return q;
return ∅;
Specifically, as shown in the Algorithm 1, the input is the generated candidate logical form list C, and we traverse each of these logical forms F in order. First, we perform the entity retrieval. For each entity e in F , we compute the similar-
ity se ← SimiEntities(e, e′) with the label of each entity e′ in the knowledge base K entity set E . We sort the retrieved entities based on the similarities, take the top ke and greater than the threshold te to get the retrieval result for that entity elist ← TopKwithThreshold(elist, ke, te). Function PermuteByEntity performs permutation on the retrieved entities at each position, and we get the result Flist after entity retrieval. Based on probabilities in Flist, we take top k1 and greater than threshold t1 to get a new candidate logical form list C′.append(TopKwithThreshold(Flist, k1, t1)).
Then, we perform the relation retrieval. Similar to entity retrieval, for each relation r in F ∈ C′, we compute the similarity sr ← SimiRelations(r, r′) with each candidate relation r′ according to the neighborhood of entity set of the logical form EF . We also sort the retrieved relations according to the similarities, take the top kr and greater than the threshold tr to get the retrieval result rlist ← TopKwithThreshold(rlist, kr, tr). By permuting the retrieval results of the relations at each position, we get the result Flist after relation retrieval and then take top k2 and greater than the threshold t2 to get a new list of candidate logical forms C′′.append(TopKwithThreshold(Flist, k2, t2)).
Given a query, unsupervised retrieval methods such as SimCSE (Gao et al., 2021), Contriever (Izacard et al., 2022), and BM25 (Robertson and Zaragoza, 2009), require no additional training to identify the top k most semantically similar candidates from the set of retrieved answers. ChatKBQA can switch between all the above unsupervised retrieval methods. We also discuss the retrieval complexity in Appendix B.

heading: 4.5 Interpretable Query Execution
text: After retrieval, we get a final candidate logical form list C′′, which we sequentially iterate through the logical form F ∈ C′′ and convert to the equivalent of the SPARQL query q = Convert(F ). When the first q that can be executed against KB K is found, we execute to get the final answer set A = Execute(q|K). With this approach, we can also get a complete reasoning path for natural language questions based on SPARQL query with good interpretability. To summarize, ChatKBQA proposes a thought taking both the advantages of using LLMs to do natural language semantic parsing for graph query generation and calling external KBs to interpretably reason with queries.

heading: 5 Experiments
text: This section presents the experimental setup, results, and analysis. We answer the following research questions (RQs): RQ1: Does ChatKBQA outperform other KBQA methods? RQ2: Does the main components of ChatKBQA work? RQ3: Why use Generate-then-Retrieve method instead of Retrieve-then-Generate method? RQ4: Why use fine-tuned open-source LLMs instead of calling ChatGPT or training traditional T5 models? RQ5: Does Generate-then-Retrieve method improve retrieval efficiency? RQ6: Does ChatKBQA has plug-and-play characteristics?

heading: 5.1 Experimental Setup
text: Datasets. All experiments are conducted on two standard KBQA datasets: WebQuestionsSP (WebQSP) (Yih et al., 2016) containing 4,737 natural language questions with SPARQL queries and ComplexWebQuestions (CWQ) (Talmor and Berant, 2018) containing 34,689 natural language questions with SPARQL queries. Both datasets are based on Freebase (Bollacker et al., 2008) KB. More details of datasets are in Appendix C.
Baselines. We compare ChatKBQA with numerous KBQA baseline methods, including IRbased methods (Miller et al., 2016), SP-based methods (Yih et al., 2016; Das et al., 2021), and LLMbased methods (Jiang et al., 2023a) in Section 2. Details of more baselines are in Appendix D.
Evaluation Metrics. Following previous work (Shu et al., 2022; Yu et al., 2023), we use F1 score, Hits@1, and Accuracy (Acc) to denote coverage of all the answers, single top-ranked answer, and strict exact-match accuracy, respectively.
Hyperparameters and Enviroment. We finetune LLMs 100 epochs on WebQSP and 10 epochs on CWQ with batch size 4 and learning rate 5e-5, detailed in Apendix E. All experiments were done on a single NVIDIA A40 GPU (48GB), with results averaged from five randomly seeded experiments.

heading: 5.2 Main Results (RQ1)
text: For the KBQA task, Table 1 lists the experimental results for our proposed generate-then-retrieve ChatKBQA framework, with the best setup of LoRA (Hu et al., 2022a) fine-tuning Llama-27B (Touvron et al., 2023) (beam size = 15) on WebQSP, Llama-2-13B (Touvron et al., 2023) (beam size = 8) on CWQ, with SimCSE (Gao et al., 2021) for unsupervised retrieval, and other baseline mod-
els. We can see that ChatKBQA has shown improvement over all existing KBQA methods on both WebQSP and CWQ datasets. The F1 score, Hits@1, and Acc have improved by about 4, 4, and 4 percentage points on WebQSP and about 4, 16, and 4 percentage points on CWQ, respectively, compared to the previous best results, which reflects ChatKBQA’s superior KBQA capability to reach the new state-of-the-art performance.

heading: 5.3 Ablation Study (RQ2)
text: To validate the effectiveness of the generation and retrieval phases of ChatKBQA, we ablate the two phases separately. For the generation phase, we use 20%, 40%, 60%, and 80% of the training data for fine-tuning versus full training set fine-tuning. For the retrieval phase, to validate entity retrieval (ER) and relation retrieval (RR) separately, we remove ER or RR from the framework and obtain three simplified variants for comparison.

heading: Effectiveness of LLM’s Fine-tuning. As
text: shown in Figure 4(a), the performance of KBQA gets better as the training volume increases, proving the effectiveness of fine-tuning. We also observe that the F1 score has exceeded 70% when only using 20% training data to fine-tune, which indicates that the fine-tuned LLMs are also effective at learning from a limited dataset. As shown in Figure 4(b), we also utilize beam search to improve the generation performance, detailed in Appendix F.
Effectiveness of Entity Retrieval (ER). As shown in Figure 4(b), ER improves about 15 percentage points on average over no oracle entity linking in the F1 score at different beam sizes. This is because, after LLM’s fine-tuning, the generated logical forms contain entities unseen in the train set, which can be further aligned to KB after retrieving the entities from the KB entity set.
Effectiveness of Relation Retrieval (RR). As shown in Figure 4(b), RR enhances the F1 score by an average of 5% across various beam sizes in ablation experiments. Although relations are rarely directly present in natural language questions, the number of thousand-level relations in the KB is still small compared to the tens of millions of entities, and the LLM perceives relational information well during fine-tuning. Thus, RR does not improve performance as much as ER, but combined with ER, RR makes KBQA perform at its best.

heading: 5.4 Generate-then-Retrieve Or Retrieve-then-Generate (RQ3)
text: To verify that our proposed LLM-based Generatethen-Retrieve method is better than previous Retrieve-then-Generate methods, we add Top1, Top2, Top5, and Top10 retrieval knowledge fragments obtained in DecAF (Yu et al., 2023) to the instruction, respectively, compared with the finetuning of Llama-2-7B without retrieval.

heading: Fine-tuning Settings WebQSPMax Token↓EM↑ %BM↑ %SM↑ %
text: As shown in Table 2, we find that without retrieval is better than with retrieval in the logical form generation in terms of extract match ratio (EM), match after beam search ratio (BM), and skeleton match ratio (SM), because the information obtained from retrieval will have erroneous interfering information and increase Max Token of instruction, which leads to catastrophic forgetting of the original problem for LLMs and increases the difficulty of training. At the same time, we observe that Llama-2-7B fine-tuning without retrieval achieves a BM of 74.7% and SM hits 91.1%, with good performance because of LLM’s well-learned schema of entities and relations, which provides the basis for the retrieval after generation.

heading: 5.5 Comparison with ChatGPT and T5 in Generation Phase (RQ4)
text: To illustrate why ChatKBQA chooses to fine-tune open-source generative LLMs such as Llama-2-7B and ChatGLM2-6B, we replace the LLMs in the generation phase with ChatGPT and GPT-4 (OpenAI, 2023) with API call in a zero-shot setting, T5 (Raffel et al., 2020) and Flan-T5 (Chung et al., 2022) with seq2seq training, respectively, and observe their results in Extract Match (EM) and Skeleton Match (SM) results without beam search.
Comparison with zero-shot ChatGPT. As shown in Figure 4(c), ChatGPT and GPT-4, al-
though having large parametric quantities, cannot generate standard logical forms well because they aren’t open-source to be fine-tuned. They can generate the SPARQL language, but it is challenging to build the correct query skeleton, entities, and relations because they cannot perceive the complex structure of the external KB well through designing prompts in limited context length.
Comparison with fine-tuned T5 & Flan-T5. While T5 and Flan-T5 can capture the skeletons well after fine-tuning, the EM is only about 10%, which is much worse than the 63% of Llama-2-7B, and therefore does not guarantee subsequent unsupervised entity and relation retrieval. Fine-tuned open-source LLMs such as Llama-2-7B (Touvron et al., 2023) and ChatGLM2-6B (Zeng et al., 2023) show stronger semantic parsing ability than models such as T5 and ChatGPT and can generate higherquality logical forms in both EM & SM.

heading: 5.6 Analysis of Efficiency of Retrieval in Retrieval Phase (RQ5)
text: To embody the Generate-then-Retrieve method improving the efficiency of retrieval, we compare entity retrieval (ER) and retrieval (RR) after logical form generation (AG-R) with traditional retrieval from natural language questions (NL-R). We define the efficiency of retrieval as the average similarity ranging [0,1] between the text to be retrieved and the set of retrieved answers, which is scored by different retrieval models. Note that the BM25 score needs to be mapped to the similarity range of [0,1].
Efficiency gains in both ER & RR. As Figure 4(d) shows, all three retrieval methods SimCSE (Gao et al., 2021), Contriever (Izacard et al., 2022), and BM25 (Robertson and Zaragoza, 2009) consider AG-R to be more efficient than NL-R for both ER and RR. This is due to NL-R still needs to determine the boundaries of the entities or relations. However, this step has been completed in AG-R after LLM generates the logical forms.
RR has more efficiency gains than ER. Moreover, although the generated logical form has fewer kinds of relations than entities in general, the relations generally exist implicitly in natural language questions. Thus, relations are more difficult to determine the boundaries than entities in natural language questions, and the generation of logical forms with the help of fine-tuned LLMs can help us to better determine the boundaries of relations, resulting in a more significant improvement in the efficiency of RR over ER.

heading: 5.7 Plug-and-Play Characteristics (RQ6)
text: ChatKBQA is a KBQA framework based on LLMs with plug-and-play characteristics that can flexibly replace three parts: LLM, efficient tuning method, and unsupervised retrieval method. We choose Llama-2-13B (Touvron et al., 2023) for LLM, LoRA (Hu et al., 2022a) for the tuning method, and SimCSE (Gao et al., 2021) for the retrieval method as the basic variant, setting the beam size for all variants to 8 for comparison.
We replace Baichuan2-7B (Yang et al., 2023), Baichuan2-13B (Yang et al., 2023), ChatGLM26B (Zeng et al., 2023), Llama-2-7B (Touvron et al., 2023) in the LLM part, QLoRA (Dettmers et al., 2023), P-Tuning v2 (Liu et al., 2022a), Freeze (Geva et al., 2021) in the tuning part, and Contriever (Izacard et al., 2022), BM25 (Robertson and Zaragoza, 2009) in the retrieval part. Benefiting from the plug-and-play characteristics of the ChatKBQA framework, as the LLMs and the methods of tuning and retrieval are upgraded, the KBQA task will be solved better with good flexibility and extensibility. More details are in Appendix G.

heading: 6 Conclusion
text: In this work, we introduce ChatKBQA, a generatethen-retrieve KBQA framework that utilizes advanced fine-tuned LLMs, which overcomes traditional challenges like retrieval inefficiencies, semantic parsing errors, and complexity of KBQA methods. Experimental results on WebQSP and CWQ benchmarks show that ChatKBQA achieves a new state-of-the-art KBQA performance. Its simplicity, flexibility, and plug-and-play make it an effective approach for combining LLM and KG in interpretable knowledge-required KBQA tasks.

heading: Acknowledgments
text: This work is supported by the National Science Foundation of China (Grant No. 62176026). This work is also supported by the BUPT Excellent Ph.D. Students Foundation (No. CX2023133) and the BUPT Postgraduate Innovation and Entrepreneurship Project (No. 2024-YC-A091).

heading: Limitations
text: In Appendix H, we provide an error analysis of ChatKBQA, revealing significant room for improvement. Furthermore, we also discuss more limitations of ChatKBQA for future directions, which are detailed in Appendix I.

heading: Ethics Statement
text: This paper investigates the problem of Knowledge Base Question Answering. We use large language models and retrieval methods to promote generation and retrieval performance. Therefore, we believe it does not violate any ethics.

heading: Appendix

heading: A Operators in Logical Form
text: Various operators include "AND" (AND E1 E2) to denote taking the intersection of E1 and E2, "COUNT" (COUNT E1) to denote counting E1, "ARGMAX" (ARGMAX E1 r) to denote taking the max literal obtained after the projection of E1 in the r relation, "ARGMIN" (ARGMIN E1 r) to denote taking the min literal obtained after the projection of the r relation for E1, "GT" (GT E1 l) means to take the portion of E1 that is greater than l, "GE" (GE E1 l) to denote taking the part of E1 greater than or equal to l, "LT" (LT E1 l) to denote taking the part of E1 less than l, "LE" (LE E1 l) to denote taking the part of E1 which is less than or equal to l, where E1 or E2 denote a sublayer logical form.

heading: B Retrieval Complexity Analysis
text: During the retrieval phase, we measure the complexity of the algorithm using two indicators: the number of times vector similarity is calculated and the number of attempts to execute the logical form. Assuming the beam size in the generation phase is set to b, the size of the KB entity set is E, and the average logical form skeleton has ne entities, the complexity of entity retrieval is O(bneE). For each entity’s position, we select entities that rank in the top ke in similarity and are greater than the threshold te for replacement. For the logical form as a whole, we select the top k1 logical forms with a combined probability greater than the threshold t1 as the result of entity retrieval.
In the relation retrieval phase, similarly, assuming the size of the KB relation set is R, and the average logical form skeleton has nr entities, the complexity of entity retrieval is O(k1nrR). For each position’s relation, we select relations that rank in the top kr in similarity and are greater than the threshold tr for replacement. For the logical form as a whole, based on the combination probability of the relation retrieval results, we select the top k2 logical forms greater than the threshold t2 as the result of relation retrieval.
Therefore, the complexity of the number of vector similarity calculations is O(bneE + k1nrR). For the number of attempts to execute the logical form, we initially attempt with the first b logical forms; if none can be executed, we proceed with entity retrieval and attempt up to k1 times. If there is still no executable logical form, we move to rela-
tion retrieval and attempt up to k2 times. Thus, the complexity of the number of logical form execution attempts is O(b + k1 + k2).
In this way, for KBQA tasks with large entity and relation sets, other parameters are much smaller than E and R, making the complexity of vector similarity calculations in the order of O(n) and the complexity of logical form execution attempts in the order of O(1), both of which are controllable.

heading: C Dataset Statistics
text: As shown in Table 4, this is the statistical information of the two KBQA datasets, WebQSP and CWQ, made by the ChatKBQA experiment.
WebQSP dataset (Yih et al., 2016) is developed to evaluate the importance of gathering semantic parses compared to just answers for a set of questions. WebQSP consists of 4,737 KBQA questions, with 34 logical form skeletons and 2,461 entities involved. There are 628 relations specified within the dataset, which is divided into a training set of 3,098 questions and a test set of 1,639 questions. This dataset utilizes Freebase as its knowledge base and is tailored for developing systems that can process and answer natural language questions using structured data.
CWQ dataset (Talmor and Berant, 2018) is designed to answer complex questions requiring reasoning over multiple web snippets, which contains a large set of complex questions in natural language and is versatile in its applications. CWQ is considerably larger with 34,689 questions, underpinned by 174 logical form skeletons. It encompasses a more extensive set of entities amounting to 11,422 and includes 845 relations. The training set comprises 27,639 questions, supplemented by a validation set of 3,519 questions and a test set of 3,531 questions. CWQ also leverages Freebase as its knowledge base and is designed for complex question-answering tasks that require the interpretation and synthesis of information from various sources.

heading: D More Baseline KBQA Methods
text: We compared ChatKBQA with more KBQA models as follows in order of publication.
KV-Mem (Miller et al., 2016) uses a key-value structured memory model to enhance document comprehension and question-answering by encoding facts and reasoning over them for accurate predictions.
STAGG (Yih et al., 2016) presents a KBQA method using semantic parse labeling, showing improvements in query accuracy compared to relying solely on question-answer pairs.
GRAFT-Net (Sun et al., 2018) introduces a novel graph convolution-based neural network that enhances open-domain question answering by com-
bining information from knowledge bases and text documents into a single model.
UHop (Chen et al., 2019) introduces a framework for unrestricted-hop relation extraction to handle queries requiring any number of relational hops in a knowledge graph, improving the capability to answer complex and indirect questions.
Topic Units (Lan et al., 2019) utilizes a wide range of knowledge base units for question answering, employing a generation-and-scoring approach and reinforcement learning to enhance the identification and ranking of relevant topic units.
TextRay (Bhutani et al., 2019) decomposes complex questions into simpler queries, processes them individually, and combines the results, using a semantic matching model.
PullNet (Sun et al., 2019) presents a method that iteratively constructs a question-specific subgraph from knowledge bases and text for effective multihop reasoning in open-domain question answering.
QGG (Lan and Jiang, 2020) introduces a method that enhances complex question answering by generating flexible query graphs for multi-hop questions and integrating constraints early.
EmbedKGQA (Saxena et al., 2020) introduces a method that uses knowledge graph embeddings to improve multi-hop question answering, addressing knowledge graph sparsity.
EMQL (Sun et al., 2020) presents a method that combines centroid-sketch entity set representations with neural retrieval over embedded knowledge base triples.
NSM+h (He et al., 2021) introduces a teacherstudent framework for multi-hop KBQA, where the teacher network learns intermediate supervision signals through forward and backward reasoning to enhance the student network’s reasoning capability.
GrailQA Ranking (Gu et al., 2021) presents a BERT-based KBQA model, demonstrating the critical role of pre-trained contextual embeddings, focusing on three levels of generalization - i.i.d., compositional, and zero-shot.
ReTraCk (Chen et al., 2021) introduces a neural semantic parsing framework, which combines
retriever, transducer, and checker components for efficient and effective KBQA.
TransferNet (Shi et al., 2021) introduces a model that combines a transparent, attention-based approach with the ability to handle both label and text relations in a unified framework.
Relation Learning (Yan et al., 2021) presents a method that integrates pre-trained language models with auxiliary tasks like relation extraction and reasoning.
Rigel (Sen et al., 2021) introduces a method for enhancing end-to-end question answering using differentiable knowledge graphs, and adds an intersection operation to handle multiple-entity questions more effectively.
CBR-KBQA (Das et al., 2021) employs a casebased reasoning framework that retrieves similar cases (questions and logical forms) from a nonparametric memory, then reuses and revises these cases to generate logical forms for new questions, demonstrating its capability to handle complex questions and unseen relations without retraining.
Subgraph Retrieval (Zhang et al., 2022) introduces a method devising a trainable subgraph retriever (SR) decoupled from the reasoning process, which efficiently retrieves relevant subgraphs for question answering, enhancing performance by focusing on more relevant and smaller subgraphs and combining with subgraph-oriented reasoners.
RnG-KBQA (Ye et al., 2022) introduces a framework that combines ranking and generation, using a rank-and-generate approach, where a ranker model identifies candidate logical forms and a generation model refines them.
Program Transfer (Cao et al., 2022) proposes a novel two-stage parsing framework with an efficient ontology-guided pruning strategy for complex KBQA, which involves a sketch parser that translates questions into high-level program sketches and an argument parser that fills in detailed arguments.
TIARA (Shu et al., 2022) introduces a novel method that enhances question answering over knowledge bases by using multi-grained retrieval, which improves the performance of pre-trained language models by focusing on the most relevant knowledge base contexts, including entities, logical forms, and schema items, and employs constrained decoding to control the output space, reducing generation errors and enhancing robustness in various generalization settings.
UniK-QA (Oguz et al., 2022) proposes a frame-
work that integrates structured, unstructured, and semi-structured knowledge sources, such as text, tables, lists, and knowledge bases, which flattens all data into text and applies a unified retriever-reader model.
ArcaneQA (Gu and Su, 2022) introduces a generation-based KBQA model that addresses large search space and schema linking challenges in KBQA, which employs dynamic program induction for efficient search space navigation and dynamic contextualized encoding for improved schema linking.
GMT-KBQA (Hu et al., 2022b) proposes a multi-task learning framework with a shared T5 encoder to improve question answering over knowledge bases by simultaneously learning entity disambiguation, relation classification, and logical form generation.
Uni-Parser (Liu et al., 2022b) unifies semantic parsing for question answering on both knowledge bases and databases by using a three-module approach: primitive enumeration, ranking, and compositional generation.
UnifiedSKG (Xie et al., 2022) unifies 21 structured knowledge grounding tasks into a text-to-text format, leveraging T5 models and multi-task learning to improve performance across diverse tasks and facilitate zero-shot and few-shot learning investigations.
UniKGQA (Jiang et al., 2023b) integrates retrieval and reasoning for multi-hop question answering over knowledge graphs, employing a unified architecture that combines a semantic matching module and a matching information propagation module, enhanced by pre-training and finetuning strategies.
DecAF (Yu et al., 2023) combines the generation of logical forms and direct answers, leveraging a sequence-to-sequence framework with retrieval from linearized knowledge bases.
BeamQA (Atif et al., 2023) combines sequenceto-sequence prediction and beam search for multihop knowledge graph question answering, using a fine-tuned BART model for path generation and a novel beam search execution algorithm to traverse the knowledge graph and find answers.
HGNet (Chen et al., 2023) proposes a hierarchical query graph generation approach with an outlining stage for structural constraints and a filling stage for instance selection.
SKP (Dong et al., 2023) introduces structured knowledge-aware pre-training tasks, an efficient
linearization strategy, and an interval attention mechanism, leading to significant improvements in subgraph retrieval and encoding.
StructGPT (Jiang et al., 2023a) enhances LLMs’ reasoning over structured data using an Iterative Reading-then-Reasoning (IRR) approach, which includes specialized interfaces for efficient data access, a novel invoking-linearization-generation procedure, and iterative reasoning to effectively utilize structured data in answering complex questions.
FC-KBQA (Zhang et al., 2023) introduces a Fine-to-Coarse composition framework for question answering over knowledge bases, utilizing finegrained component detection, middle-grained component constraints, and coarse-grained component composition.
Pangu (Gu et al., 2023) proposes a grounded language understanding framework that combines a symbolic agent and a neural language model, which allows for the incremental construction of valid plans and utilizes the language model to evaluate the plausibility of these plans.
ToG (Sun et al., 2024) integrates LLMs with KGs for deep and responsible reasoning, using a beam search algorithm in KG/LLM reasoning, which allows the LLM to dynamically explore multiple reasoning paths in KG and make decisions accordingly, enhancing LLMs’ deep reasoning capabilities for knowledge-intensive tasks.

heading: E Hyperparameter Settings
text: We use the grid search method to select the optimal hyperparameter settings for the network. The F1 score of KBQA predicted without oracle entity linking is chosen as the evaluation metric. The hyperparameters that we can adjust and the possible values of the hyperparameters are first determined according to the structure of our model in Table 6.
Afterward, the different hyperparameter choices are combined to judge the merit of the hyperparameter combinations. The optimal hyperparameter combinations of the model are obtained by circular traversal of all combinations. The optimal hyperparameter combinations are shown in bold.
For example, WebQSP hyperparameter choices select the Llama-2-7B model, as shown by bolded values, for optimal model performance. LoRA is the fine-tuning type chosen, suggesting low-rank adjustments to model parameters. A train batch size of 4, learning rate of 5e-4, and 50 training epochs indicate a preference for moderate-sized
data processing batches and a faster learning rate over many epochs. Test batch size of 4 and beam size of 5 indicate evaluation and prediction generation configuration. The retrieval algorithm was SimCSE because it compares sentence embeddings well. The top-k and threshold values for Entity Retrieval (ER) and Relation Retrieval (RR) were set to balance retrieving relevant information and computational efficiency.

heading: F Effectiveness of Beam Search
text: Beam search is a heuristic algorithm usually used in sequence generation tasks, which expands the search space by generating multiple highly probable logical forms instead of only one. As shown in Figure 4(b), an increase in beam size enhances the likelihood of executing SPARQL queries based on candidate logical forms, improving the KBQA performance.

heading: G Plug-and-Play Settings
text: ChatKBQA has a plug-and-play characteristic, as shown in 3 parts, including the Open-source LLMs, PEFT methods, and Unsupervised Retrieval methods, all of which have different candidates. The following is a description of these candidates.

heading: G.1 Open-source Large Language Models
text: In the open-sourced macro modelling part, we choose Llama-2, ChatGLM2, and Baichuan2.
Llama-2-7B / Llama-2-13B (Touvron et al., 2023): Part of Meta AI’s Llama series, these models are auto-regressive transformers with 7 and 13 billion parameters, trained on 2 trillion tokens. They are optimized for dialogue and general language tasks, leveraging supervised fine-tuning and reinforcement learning for better alignment with human preferences.
ChatGLM2-6B (Zeng et al., 2023): Developed by Tsinghua University, this 6.2 billion-parameter bilingual Chinese-English chat model improves upon its predecessor with enhanced performance, longer context support, and efficient inference. It’s designed for fluent, coherent conversations in both languages.
Baichuan2-7B / Baichuan2-13B (Yang et al., 2023): From Baichuan Intelligent Technology, these multilingual models have 7 and 13 billion parameters and are trained on 2.6 trillion tokens. They support Chinese and English, offering competitive performance on various language process-
ing benchmarks and are available for open-source commercial use.

heading: G.2 Parameter-Efficient Fine-Tuning Methods
text: In the PEFT part, we choose LoRA, QLoRA, Ptuning v2, and Freeze.
LoRA (Low-Rank Adaptation) (Hu et al., 2022a) is a PEFT method that introduces low-rank matrices to adapt large pre-trained models. Instead of fine-tuning all parameters, LoRA modifies only a small number of additional trainable parameters, effectively reducing the computational cost. It alters the weights of a pre-trained model in a low-rank decomposed space, allowing for efficient adaptation while maintaining the original model’s structure and size.
QLoRA (Quantized Low-Rank Adaptation) (Dettmers et al., 2023) is an extension of LoRA, combining low-rank adaptation with quantization techniques. It aims to further reduce the computational and memory overhead associated with fine-tuning large models. By quantizing the additional low-rank matrices introduced in LoRA, QLoRA provides a more memory-efficient approach to adapting pre-trained models.
P-tuning v2 (Liu et al., 2022a) advances the concept of prompt tuning, where trainable prompts are added to a fixed pre-trained model to guide its predictions. P-tuning v2 introduces trainable continuous prompts at the embedding layer and employs a sophisticated bi-level optimization strategy. This approach enhances the model’s ability to adapt to specific tasks with minimal parameter updates, making it more efficient than traditional
fine-tuning methods. Freeze (Geva et al., 2021) is a parameterefficient approach where most of the layers of a pre-trained model are frozen, and only a small fraction of the parameters are fine-tuned. This technique significantly reduces the computational resources required for fine-tuning, making it ideal for scenarios with limited budgets. By selectively updating only certain layers or parts of a model, Freeze retains the general knowledge of the pretrained model while adapting it to specific tasks.

heading: G.3 Unsupervised Retrieval Methods
text: In the Unsupervised Retrieval part, we choose SimCSE, Contriever and BM25.
SimCSE (Gao et al., 2021) is an unsupervised method for generating sentence embeddings using contrastive learning. It enhances semantic understanding by using variations of the same sentence to train neural networks, improving performance in tasks like textual similarity and natural language inference.
Contriever (Izacard et al., 2022) is an unsupervised technique for creating dense passage embeddings, designed for effective retrieval in large document collections. It focuses on semantic content, offering an advanced alternative to traditional keyword-based retrieval methods.
BM25 (Robertson and Zaragoza, 2009) is a probabilistic ranking function used in search engines. It evaluates document relevance to a search query, improving upon models like TF-IDF by incorporating document length normalization and term frequency saturation.

heading: H Error Analysis
text: We analyze the questions in the WebQSP test set that were not answered correctly by ChatKBQA without oracle entity linking, and errors can be summarized as follows.
Logical form skeleton error (40.10%). We discover that the majority of the errors are caused by ChatKBQA failing to provide the correct logical form skeleton for the question, e.g. predicting "(JOIN (R []) (JOIN (R []) []))" as "(JOIN (R []) [])". This is due to the limited representation of certain complex skeletons in train set.
Entity retrieval error (27.17%). Then, a portion of the samples that predicted the correct logical form skeletons, but did not retrieve the correct entities, e.g. predicting "(JOIN (R []) m.0d3k14)" as "(JOIN (R []) m.07618sw)".
Relation retrieval error (19.48%). In the case of successful skeleton prediction and entity retrieval, errors in relation retrieval can also lead to failed logical form generation that does not match the ground truth, e.g. predicting "(JOIN (R finance.currency.countries_used) m.0kz1h)" as "(JOIN (R finance.currency. currency_code) m.0kz1h)".
SPARQL convertion error (13.26%). Finally, a small proportion of the remaining errors arise from the fact that, although the generated logical form is consistent with the ground truth, it fails to execute or the answers are inconsistent when converted to SPARQL, which may be caused by the loss of the conversion from logical form to SPARQL.

heading: I Discussion of LLM combined with KG.
text: I.1 Insights from ChatKBQA. (1) We propose a straightforward KBQA framework that uses fine-tuned open-source large models for the first time. (2) Innovatively, we adopt a generate-then-retrieve approach to enhance generation outcomes and retrieval efficiency separately, ultimately boosting KBQA performance. (3) Our framework has plug-and-play capabilities, allowing flexible replacement of LLMs and retrieval models to address the KBQA challenge. (4) Our approach introduces a new paradigm for LLMs to conduct interpretable knowledge-based Q&A, offering a fresh perspective on merging LLMs and KGs.
To summarize, ChatKBQA proposes a thought taking both the advantages of using LLMs to do natural language semantic parsing for graph query generation and calling external KBs to interpretably
reason with queries, which we name Graph Query of Thoughts (GQoT), a promising LLM+KG combination paradigm to better utilize the external knowledge, improve Q&A’s interpretability, and avoid LLM’s hallucinations.

heading: I.2 Future Directions.
text: ChatKBQA still has much room for improvement, such as in the design of the training set, the decomposition of complex questions, support for various graph query languages, and applications in specific domains, which are our future research directions:
Training set design: ChatKBQA is the first method to fine-tune open-source large models using unsupervised retrieval methods for the KBQA task, achieving state-of-the-art results. Therefore, the effectiveness of fine-tuning depends on the quality of the dataset used to map natural language to logical forms. In future work, we plan to enhance the training set by extracting computation graphs from the knowledge graph using graph sampling, then converting them into natural language, and exploring ways to achieve maximum training effectiveness with the least amount of training data.
Decomposition of complex questions: We have seen that for some simple tasks, such as one-hop and two-hop queries, ChatKBQA performs very well because the logical form skeletons involved are very similar and the fine-tuned LLM can generate them effectively. However, generating the corresponding long logical forms for more complex questions is a challenge. Therefore, in future work, we plan to use techniques such as CoT or Agent to decompose natural language questions into simpler logical forms for better performance.
Support for various graph query languages: Currently, ChatKBQA converts generated logical forms into SPARQL queries in two datasets, as the Freebase KB stores knowledge in RDF format. We will explore more KBs and datasets, such as those using the Cypher language like Neo4j, where the methodology of generating and then retrieving with ChatKBQA is also promising.
Open-domain and specific-domain applications: There is a demand for precision knowledge question answering in fields such as open-domain, medicine, finance, and telecommunications. We can first use UIE or LLM information extraction technology to build a knowledge graph, then finetune ChatKBQA to understand the structure of the knowledge graph, achieving interpretable knowledge Q&A in open and specific domains.

heading: Future Work
text: ChatKBQA still has much room for improvement, such as in the design of the training set, the decomposition of complex questions, support for various graph query languages, and applications in specific domains, which are our future research directions:
Training set design: ChatKBQA is the first method to fine-tune open-source large models using unsupervised retrieval methods for the KBQA task, achieving state-of-the-art results. Therefore, the effectiveness of fine-tuning depends on the quality of the dataset used to map natural language to logical forms. In future work, we plan to enhance the training set by extracting computation graphs from the knowledge graph using graph sampling, then converting them into natural language, and exploring ways to achieve maximum training effectiveness with the least amount of training data. Decomposition of complex questions: We have seen that for some simple tasks, such as one-hop and two-hop queries, ChatKBQA performs very well because the logical form skeletons involved are very similar and the fine-tuned LLM can generate them effectively. However, generating the corresponding long logical forms for more complex questions is a challenge. Therefore, in future work, we plan to use techniques such as CoT or Agent to decompose natural language questions into simpler logical forms for better performance. Support for various graph query languages: Currently, ChatKBQA converts generated logical forms into SPARQL queries in two datasets, as the Freebase KB stores knowledge in RDF format. We will explore more KBs and datasets, such as those using the Cypher language like Neo4j, where the methodology of generating and then retrieving with ChatKBQA is also promising. Open-domain and specific-domain applications: There is a demand for precision knowledge question answering in fields such as open-domain, medicine, finance, and telecommunications. We can first use UIE or LLM information extraction technology to build a knowledge graph, then finetune ChatKBQA to understand the structure of the knowledge graph, achieving interpretable knowledge Q&A in open and specific domains