DEV Community

Cover image for Building a PDF Chatbot with RAG using SQL
Mrunmay Shelar for LangDB

Posted on

Building a PDF Chatbot with RAG using SQL

In the realm of AI chatbot development, there are many ways to create a PDF chatbot using Retrieval Augmented Generation. There are tools like LangChain and LlamaIndex, which are the popular choice. However, in this post, we’ll be exploring how you can achieve this using SQL.

Let’s go over the general steps of building a RAG chatbot:

  1. Loading and Extracting PDF
  2. Generating and Storing Vector Embeddings
  3. Retrieving relevant information using Vector Search
  4. Generating and Returning Answer

Why SQL?

  • SQL queries are straightforward and efficient, making data manipulation and retrieval easier on large datasets.
  • Combining structured and unstructured data is accessible on the go.
  • Streamlines the development process by enabling the creation of workflows with minimal code.

Here are a few examples of how easily you can create embeddings, create a prompt for the LLM and finally create a model.

-- Embeddings Model 
CREATE EMBEDDING MODEL IF NOT EXISTS generate_embed(
input COMMENT 'This is the input of the content whose embeddings are created'
) USING open_ai_provider(embedding_model='text-embedding-ada-002', encoding_format='float', dimensions=100)

-- Completions Model
CREATE MODEL IF NOT EXISTS search(
  input
) USING open_ai_provider()
PROMPT (system "You are a helpful assistant. You have been provided with similar tool to search over the SEC fillings. 
                 Your task is also to help users with any queries they might have about the SEC filings. 
                 Go through all the content and then respond.",
  human "{{input}}")
TOOLS (similar)
SETTINGS retries = 1;
Enter fullscreen mode Exit fullscreen mode

Using SQL in this manner provides a robust framework for building a PDF chatbot, leveraging the power of SQL for workflow management.
So, the final execution of the model will look like this:

Gif showing how to query a model in LangDB

You can follow the full tutorial here: https://app.langdb.ai/samples/preview?file_name=QA%20on%20PDF%20%26%20RAG%20using%20LangDB

Join our Slack community for more discussions and support: https://join.slack.com/t/langdbcommunity/shared_invite/zt-2haf5kj6a-d7NX6TFJUPX45w~Ag4dzlg

Top comments (0)