Building Chat Bot that uses context from imported tables

  • Goal: I'm building an application that leverages 4 separate tables (users, orders, products, line_items) that are pulling data from a postgresql db I have running in AWS. As such, I have 4 respective queries for the tables (getAllUsers, getAllOrders, etc). I would like to build a chatbot that uses those queries to provide the user with context about the tables. For example, let's say I have an "orders" page that has ordersTable and ordersChatBot components. I'd like to be able to type into the chat bot "how many of my orders have been delivered?" and have it know from the query how many of my orders show "delivered" in their "status" column.

  • Steps: Creating a chat component automatically creates an associated query with it. In the input field, I've put:

{{ Here is the data you can use to answer my question:\n Users: ${JSON.stringify(getAllUsers.data)}\n Orders: ${JSON.stringify(getAllOrders.data)}\n Products: ${JSON.stringify(getAllProducts.data)}\n Line Items: ${JSON.stringify(getAllLineItems.data)}\n }}

{{ chatInput.lastMessage }}


and at first, it seemed like the .stringify method was producing too much data for the "input" box to handle, so I decided I could create 4 separate chat bots each dedicated to its own table i.e.:

{{ Here is the data you can use to answer my question:\n Users: ${JSON.stringify(getAllUsers.data)}\n
}}

{{ usersChat.lastMessage }}


but that didn't seem to do the trick either. Perhaps there's some sort of easier way to accomplish this than what I'm trying. Please let me know your thoughts and thanks in advance.

How much data is in each of the tables? The context window can fill up fast. You may need to play around to prompt the AI to return an array that you can parse into a query that generates the result in a separate component. Or perhaps to generate a query that you can parse and execute, in which case you would only need to pass the table definition (though you have to be careful - what if the AI generates DELETE TABLE xyz?)

If the types of questions that might be asked of your data are fairly consistent and common, AI might not really be the way to go an instead you might be better off generating those answers directly.

Perhaps you are hoping for a more robust use case, such as being able to ask "which type of order is most likely to result in a delay in delivery" or "project the date on which all current orders will have been delivered" or other such questions. This often requires integrating more complex pipelines that allow the LLM to create and execute code (for example) which are a bit beyond the simple chatBot component (though it can serve as a front end for them).

1 Like

Hello @t2na!

For training an AI model, the common practice is to insert your data into a vector database. This can store a large amount of data and the AI tool will be able to 'remember' the datapoints you have given it to give you insights.

Trying to pass that same amount of data every time you ask a question is a major bottleneck. Once you get all the tables from your postgresql db you can add new data when needed but don't need to duplicate work.

Depending on how large your tables are, you might need to use chunking to break up the data uploads to the Vector DB to avoid overloading it with too much at one time but that shouldn't be too hard to do.

You can check out our docs for Retool's hosted Vector DB as well as use other online resources for different vector DB resources and can use AI to find out the best ways to get your table data converted int vectors for AI training as well :sweat_smile:

1 Like