Integrated Chat Bot to Pull Data from Tables

  1. Goal:
    I have an app that pulls data from my database and shows it in tables. I want to place a chatbot on its own page, but still let it read those same tables so I can ask questions like “What’s our cash flow this month?” and get a real‑time answer straight from the data.

  2. Current problem:
    Right now each table‑refresh query writes its results into a variable that I feed into the chatbot’s system prompt. In theory, that should give the bot direct access to the latest data, but in practice the answers are often off.

  3. What I’ve tried:

  • Tweaked model settings (temperature, model version).
  • Expanded and trimmed the system prompt to see if more or less context helps.
  • Confirmed the data reaching the variable is correct.

None of those changes have fixed the accuracy issue.
4) Additional info:
Database is cloud hosted through my company, I think the application is self hosted. I have thought about using vectors, but I don't know if that will help with my issue. I also don't know how to make vectors auto update with consistent data right now.

Any help would be very much appreciated. Thank you!

Hello @Isaac_Giordano Welcome to the Retool Community,

You may find this Retool forum thread helpful: Building Chat Bot That Uses Context from Imported Tables

In my experience, the best approach is to set up a custom AI agent in Retool. You can either use Retool’s Custom AI Agent feature or connect an external provider like the OpenAI API. The key is to define tools that your agent can call to access live data from your app.

Here’s a streamlined setup for using the Retool AI Agent:

  1. Create Queries
    Build Retool queries that return the real-time data you want your agent to access.

  2. Add the AI Agent Component
    Place the AI Agent component on its own dedicated page in your app.

  3. Define Tools for the Agent
    Within the agent settings, define tools that link to your Retool queries. These tools allow the agent to call specific queries and use their responses to answer user questions in real time.

4 Likes

Hi @Isaac_Giordano,

Interesting issue. I am surprised that the answers are off, but also models are not yet perfect :thinking: very curious if you could provide me some examples.

At the highest level, the best option is to give the agent a tool for reading data from the database. This way it is getting the most up to date data and all the context it needs to answer the question.

Vectors are good for static data that will not be changing(think official documents) and must be manually updates so they are not very dynamic.

Passing data across Retool pages is not well supported currently, the efficiency tradeoff of having pages is that the web app will only load the data of the current page, leaving the data on the other pages to "not exist" until the page is changed and the queries run to fetch data.

I like the idea of saving the table's data into a variable to pass to the agent, but I am not sure this use case has been completely flushed out. I was working on two other threads where users were trying to do this to varying degrees of success.

It sounds like this method was almost working for you but not perfectly, I am wondering if you already tried to pass the query results in directly instead of having the table transform the data's shape and then having to pass that data into a variable to then go to the agent :sweat_smile:

As the fewer steps between the better. And the table data is formatted for being displayed, where as the results of a DB query will have the data structured in a way that the agent's model is likely much more familiar with accessing and understanding :bulb:

1 Like