<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <issn pub-type="ppub">1613-0073</issn>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>A family of natural language interfaces for databases based on ChatGPT and LangChain</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Eduardo Nascimento</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Grettel García</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Wendy Victorio</string-name>
          <email>wendyzv@tecgraf.puc-rio.br</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Melissa Lemos</string-name>
          <email>melissa@tecgraf.puc-rio.br</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Yenier Izquierdo</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Robinson Garcia</string-name>
          <email>ggarcia@tecgraf.puc-rio.br</email>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff3">3</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Luiz A.P. Paes Leme</string-name>
          <email>lapaesleme@ic.uff.br</email>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Marco A. Casanova</string-name>
          <email>casanova@inf.puc-rio.br</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Instituto Tecgraf and Departamento de Informática</institution>
          ,
          <addr-line>PUC-Rio, Rio de Janeiro, 22451-900, RJ</addr-line>
          ,
          <country country="BR">Brazil</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Instituto de Computação, UFF</institution>
          ,
          <addr-line>Niterói, 24210-310, RJ</addr-line>
          ,
          <country country="BR">Brazil</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Natural Language Interfaces for Databases</institution>
          ,
          <addr-line>ChatGPT, LangChain</addr-line>
          ,
          <country>Database Keyword Search</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>Petrobras</institution>
          ,
          <addr-line>Rio de Janeiro, 20031-912, RJ</addr-line>
          ,
          <country country="BR">Brazil</country>
        </aff>
        <aff id="aff4">
          <label>4</label>
          <institution>Project Exhibitions</institution>
          ,
          <addr-line>Posters and Demos, and Doctoral Consortium</addr-line>
        </aff>
      </contrib-group>
      <abstract>
        <p>This poster paper proposes a family of Natural Language (NL) interfaces for databases (NLIDBs) that use ChatGPT and LangChain features to compile NL sentences expressing database questions into SQL queries or to extract keywords from NL sentences, which are passed to a database keyword search tool. The use of ChatGPT reduces dealing with NL questions to a few-shot learning process for the benefit of developers interested in creating NLIDBs. The paper concludes by comparing the NLIDBs in the family.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>CEUR
ceur-ws.org</p>
    </sec>
    <sec id="sec-2">
      <title>1. Introduction</title>
      <p>
        Natural language interfaces for databases (NLIDBs) have long been investigated with relative
success [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. The recent availability of the so-called Large Language Models [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] ofers a possible
strategy to simplify the development of NLIDBs worth exploring. A Large Language Model
(LLM) is a language model based on a deep neural network architecture with a very large number
of parameters, trained on enormous quantities of unlabeled text, using self-supervised or
semisupervised learning. LLMs came to the foreground with the announcement of conversational
interfaces, such as ChatGPT [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], that generate high-quality textual answers, among other tasks.
      </p>
      <p>The primary motivation for this paper is the construction of LLM-based NLIDBs, which poses
new challenges to database designers. This paper contributes to meeting such challenges by
proposing strategies to build NLIDBs that pass a suitable database description to the LLM in
a context prompt. In detail, the paper first introduces an NLIDB family that uses ChatGPT
and LangChain features (see Section 3) to compile NL sentences expressing database questions
into SQL queries or to extract keywords from NL sentences, which are passed to a database
nEvelop-O
CEUR
Workshop
Proceedings
keyword search tool. Some of the NLIDBs in the family explore ChatGPT’s few-shot learning to
improve the accuracy of processing NL questions. Then, the paper describes early experiments
to compare the NLIDBs in the family.</p>
      <p>The paper is organized as follows. Section 2 summarizes related work. Section 3 covers the
required ChatGPT and LangChain features. Section 4 introduces the NLIDB family. Section 5
describes experiments with the NLIDB family. Finally, Section 6 contains the conclusions.</p>
    </sec>
    <sec id="sec-3">
      <title>2. Related work</title>
      <p>
        Afolters et al. [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] ofers a comparative survey of 24 NLIDBs, classified as keyword-, pattern-,
parsing- and grammar-based. As lessons learned, the authors indicate that, for simple questions,
keyword-based systems are enough, whereas for complex questions, an NLIDB depends on
manually designed rules. Diefenbach et al. [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] assumes that an NL question can be correctly
interpreted considering only the semantics of the words. For example, the NL question “Give
me actors born in Berlin.” is reformulated as a keyword question “Berlin, actors, born in”, that
is, the semantics of the words “Berlin”, “actors”, and “born” sufice to deduce the intention of
the user. The authors proceed to map the NL question into one of four templates, which limits
the usefulness of the process.
      </p>
      <p>
        As for database keyword search, QUIOW [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] is an automatic, schema-based tool that supports
keyword-based query processing for relational and RDF environments. Izquierdo et al. [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]
introduced an RDF keyword search tool that does not rely on an RDF schema but synthesizes
SPARQL queries by exploring the similarity between instance sets observed in the RDF dataset.
      </p>
      <p>
        Manning [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] classifies NLP approaches in roughly four eras. The last era, from 2013 to
the present, extended the use of machine learning, allowing a more generalized language
understanding by using self-supervised learning and deep learning-based models to represent
words with dense vectors. Within this context, ChatGPT from OpenAI was released on Nov.
30th, 2022, and uses the GPT LLM family.
      </p>
      <p>
        Diferently from [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ][
        <xref ref-type="bibr" rid="ref4">4</xref>
        ], the NLIDB family proposed relies on ChatGPT to process NL questions,
does not depend on grammar-based rules, and is not limited to a few database query templates.
      </p>
    </sec>
    <sec id="sec-4">
      <title>3. A summary of the required ChatGPT and LangChain features</title>
      <p>The OpenAI API provides the Chat Completions API 1 that takes a list of messages as input
and returns a model-generated message as output. Each message comprises a role (“system”,
“user” or “assistant”) and the content. The system message defines the assistant’s behavior from
specific instructions about how it should behave during the conversation. The user messages
provide requests or comments for the assistant to answer. Finally, the assistant messages refer
to responses from ChatGPT, which is called assistant in this context.</p>
      <p>ChatGPT (and the OpenAI API) can translate NL questions into SQL queries and extract
keywords from a text block2. To translate NL questions into SQL queries, the user can create a
simple input, with practically no context passed, and rely on the knowledge of the model to
1https://platform.openai.com/docs/guides/gpt/chat-completions-api
2https://platform.openai.com/examples
perform the translation. Alternatively, the user can manually create a system message to pass
more context about the data and the translation task, such as metadata describing the database
tables.</p>
      <p>Another approach would be to use frameworks to develop LLM-powered applications, such as
LangChain3. Chains, in LangChain, go beyond a single LLM call and involve chaining together
sequences of calls (whether to an LLM or a diferent utility). The chain named SQLDatabaseChain
inspects the schema, tables, and joins in the database, then provides context to an LLM in an
automated way; the user can also manually add a prompt and choose which tables to inspect.
Using such context, ChatGPT constructs an SQL query for the original NL question, which is
then executed and returned by LangChain.</p>
      <p>Finally, the Chat Completions API allows describing function calls to be used to respond
when the information the LLM has is not enough or is incomplete. This feature provides a
straightforward way to create an NL front-end to a database keyword search (KwS) tool.</p>
    </sec>
    <sec id="sec-5">
      <title>4. The NLIDB family</title>
      <p>In the process of exploring ChatGPT and LangChain features to create NLIDBs, we identified two
alternatives – (1) the SQL alternative and (2) the KwS alternative – which induce the proposed
NLIDB family4.</p>
      <p>The SQL alternative translates NL questions directly into SQL queries. We explored four
options to create an NLIDB in this case:
• Option 1.1 – ChatGPT-SQL uses only the knowledge embedded in the LLM, with no
context.
• Option 1.2 – ChatGPT-SQL-Prompt uses a manually created system message to pass
context information about the database and query examples, and runs an application on
the database for validation.
• Option 1.3 – ChatGPT-SQL-LangChain uses LangChain with ChatGPT to generate and
execute an SQL query; to avoid exceeding the token limit, LangChain creates chains to
inspect specific tables, connects with the database, and calls the ChatGPT API.
• Option 1.4 – ChatGPT-SQL-LangChain-Prompt is the same as Option 1.3, extended with
prompts.</p>
      <p>
        The KwS alternative uses ChatGPT to extract keywords from an NL question and then uses a
KwS tool [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ][
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] to find the answers. We implemented two options to create an NLIDB:
• Option 2.1 – ChatGPT-KwS uses the knowledge of the LLM to extract the keywords directly.
• Option 2.2 – ChatGPT-KwS-Prompt fine-tunes the model by passing the main terms of the
domain, and examples of NL questions and their translations to keywords via prompt
messages.
      </p>
      <p>In both cases, ChatGPT extracts keywords from the NL question and sends them to the KwS
tool via the function shown in Figure 1. The results are passed back to ChatGPT, which presents
them to the user.
3https://python.langchain.com/docs/modules/chains/popular/sqlite
4The prototypes and a brief video can be found at https://github.com/dudursn/nl_interface_tools_based_chatgpt_kws
{
}
” f u n c t i o n _ c a l l ” : { ” name ” : ” s e a r c h _ k e y w o r d s ” } ,
” f u n c t i o n s ” : [ {
” name ” : ” s e a r c h _ k e y w o r d s ” ,
” d e s c r i p t i o n ” : ” Send keywords t o keyword s e a r c h t o o l ” ,
” p a r a m e t e r s ” : { ” t y p e ” : ” o b j e c t ” ,
” p r o p e r t i e s ” : { ” keywords ” : { ” t y p e ” : ” s t r i n g ” } , } ,
” r e q u i r e d ” : [ ” keywords ” ] } } ]</p>
    </sec>
    <sec id="sec-6">
      <title>5. Experiments</title>
      <p>The experiments used the Mondial database and 27 NL questions5, which were submitted to
each NLIDB of Section 4. Table 1 shows the results obtained, summarized as:
• Option 1.1 – ChatGPT-SQL, which translates NL questions into SQL queries using only
knowledge from the LLM, generated only incorrect SQL queries. The LLM was not
schema-aware and, in some responses, did not generate an SQL query but responded
with its knowledge, and in others, it asked to provide information about the database.
• Option 1.2 – ChatGPT-SQL-Prompt, which uses a manually provided prompt with schema
information, succeeded on most questions. The errors were due to mismatches between
the NL question terms and table or column names.
• Option 1.3 – ChatGPT-SQL-LangChain, which uses LangChain to provide schema
information to ChatGPT, incurred more errors than Option 1.2. Some errors resulted from
misplaced or non-existent column names. Also, some responses were empty due to
incorrect filters.
• Option 1.4 – ChatGPT-SQL-LangChain-Prompt, which uses a prompt with information for</p>
      <p>LangChain, showed some improvement, but repeated some of the errors of Option 1.3.
• Option 2.1 – ChatGPT-KwS, which uses knowledge from the LLM, incurred in problems
with plural names (“Country” versus “Countries”) and synonyms. For some NL questions,
the NLIDB returned an error the first time the question was submitted, but executed
correctly if the question was re-submitted. For other NL questions, the KwS tool could
not return a response for the keywords generated, and in others, ChatGPT responded
using its knowledge (since GPT was trained with geographic data).
• Option 2.2 – ChatGPT-KwS-Prompt, which uses prompts with context information and
examples, was the best-performing NLIDB. ChatGPT was able to generate most of the
keywords that the KwS tool expected. For example, for the NL question “Cities with area
more than 1000”, ChatGPT extracted “City Area &gt; 1000”, which the KwS tool could handle.
However, some errors occurred because the KwS tool did not respond correctly to the
keywords ChatGPT extracted.
5https://github.com/dudursn/nl_interface_tools_based_chatgpt_kws/blob/main/queries_results.xlsx</p>
    </sec>
    <sec id="sec-7">
      <title>6. Conclusions</title>
      <p>ChatGPT-SQL- ChatGPT-SQL-
ChatGPT-SQLPrompt LangChain LangChain-Prompt
This paper demonstrated how ChatGPT and LangChain can help create Natural Language
interfaces for databases (NLIDBs). In the SQL alternative, such tools were used to generate an
SQL query from an NL question. In the KwS alternative, they were adopted to extract keywords
from an NL question, which were then passed to a KwS tool, thereby extending the usefulness
of the KwS tool to accept NL questions. The experiments suggest that ChatGPT-KwS-Prompt,
which uses prompts with context information and examples to help extract keywords, is the
best-performing NLIDB.</p>
    </sec>
    <sec id="sec-8">
      <title>Acknowledgments</title>
      <p>This work was partly funded by FAPERJ under grant E-26/200.834/2021, by CAPES under grants
88881.134081/2016-01 and 88887.694383/2022-00, and by CNPq under grant 305.587/2021-8.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>K.</given-names>
            <surname>Afolter</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Stockinger</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Bernstein</surname>
          </string-name>
          ,
          <article-title>A comparative survey of recent natural language interfaces for databases</article-title>
          ,
          <source>The VLDB Journal</source>
          <volume>28</volume>
          (
          <year>2019</year>
          ).
          <source>doi:10.1007/s00778- 019- 00567- 8.</source>
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>C. D.</given-names>
            <surname>Manning</surname>
          </string-name>
          ,
          <source>Human Language Understanding &amp; Reasoning, Daedalus</source>
          <volume>151</volume>
          (
          <year>2022</year>
          )
          <fpage>127</fpage>
          -
          <lpage>138</lpage>
          . URL: https://doi.org/10.1162/daed_a_
          <year>01905</year>
          , doi: 10.1162/daed_a_
          <year>01905</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          <article-title>[3] OpenAI, Chatgpt: Optimizing language models for dialogue</article-title>
          , https://openai.com/blog/chatgpt/,
          <year>2022</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>D.</given-names>
            <surname>Diefenbach</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Both</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Singh</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Maret</surname>
          </string-name>
          ,
          <article-title>Towards a question answering system over the semantic web</article-title>
          ,
          <source>Semant. Web</source>
          <volume>11</volume>
          (
          <year>2020</year>
          )
          <fpage>421</fpage>
          -
          <lpage>439</lpage>
          . doi:
          <volume>10</volume>
          .3233/SW- 190343.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>Y. T.</given-names>
            <surname>Izquierdo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G. M.</given-names>
            <surname>García</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E. S.</given-names>
            <surname>Menendez</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. A.</given-names>
            <surname>Casanova</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Dartayre</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C. H.</given-names>
            <surname>Levy</surname>
          </string-name>
          ,
          <article-title>Quiow: a keyword-based query processing tool for rdf datasets and relational databases</article-title>
          ,
          <source>in: International Conference on Database and Expert Systems Applications (DEXA)</source>
          , Springer,
          <year>2018</year>
          , pp.
          <fpage>259</fpage>
          -
          <lpage>269</lpage>
          . doi:
          <volume>10</volume>
          .1007/978- 3-
          <fpage>319</fpage>
          - 98812- 2_
          <fpage>22</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Y. T.</given-names>
            <surname>Izquierdo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G. M.</given-names>
            <surname>García</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Menendez</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L. A. P.</given-names>
            <surname>Leme</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Neves</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Lemos</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A. C.</given-names>
            <surname>Finamore</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Oliveira</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. A.</given-names>
            <surname>Casanova</surname>
          </string-name>
          ,
          <article-title>Keyword search over schema-less rdf datasets by sparql query compilation</article-title>
          ,
          <source>Information Systems</source>
          <volume>102</volume>
          (
          <year>2021</year>
          )
          <article-title>101814</article-title>
          . doi:
          <volume>10</volume>
          .1016/j.is.
          <year>2021</year>
          .
          <volume>101814</volume>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>Y. T.</given-names>
            <surname>Izquierdo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G. M.</given-names>
            <surname>Garcia</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Lemos</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Novello</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Novelli</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Damasceno</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L. A. P. P.</given-names>
            <surname>Leme</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. A.</given-names>
            <surname>Casanova</surname>
          </string-name>
          ,
          <article-title>A platform for keyword search and its application for covid19 pandemic data</article-title>
          ,
          <source>Journal of Information and Data Management</source>
          <volume>12</volume>
          (
          <year>2021</year>
          ). URL: https: //sol.sbc.org.br/journals/index.php/jidm/article/view/1904. doi:
          <volume>10</volume>
          .5753/jidm.
          <year>2021</year>
          .
          <year>1904</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>