Get startedGet started for free

From text-to-SQL to TAG - Creating Table Assisted Generation

1. From text-to-SQL to TAG - Creating Table Assisted Generation

Back to the SQL. The SQL that was returned is pretty good, but we'll not stop here. Let's continue and get to a natural language response. Let's go see this in action and go back to the notebook. Text-to-SQL is great, but how can we push this even further? What if we want a full natural language response instead of just SQL responses? Let's talk about a term I like to use, TAG. TAG stands for Table Assisted Generation, which is what we're going to do with Cortex Analyst. Just like in the last module, where RAG augmented the LLM generation with a retrieval service for unstructured data, TAG augments the LLM with SQL results. Before we move on, make sure that you have installed the tabulate library. This will allow you to convert the SQL output using pandas into LLM-readable markdown format, which can then be sent to the LLM with the query. We see the output here. Now we will send the query and SQL output to the LLM. We set the messages, the system role, and provide a prompt to focus the LLM. For the user role, we provide instructions and guidance for the LLM. We inform it of the text-to-SQL process and provide the results of executing the SQL. Then we instruct it to answer our question. Under options, we set guardrails to equal true, so we don't get any offensive or harmful content in the response, and we'll set complete to use Claude 3.5. Then we'll run the cell and see the response below. Now we have a single method, which we can use to answer questions from tabular data. All we have to do is pull it all together. Let me show you how to do this. We'll define a method, answer a question using Cortex Analyst, which we'll use to generate the SQL that we'll need to answer our query. We define the output to get the SQL from Cortex Analyst, and this executes the query. We then transform the output into the LLM readable format, and from there, we send the query and the SQL results to the LLM to generate our response, just like we did before. We use the same messages, options, and response that we tested above and set it to return our response. Now we'll run the method and ask it to answer the question, what is the highest daily revenue recorded in a single day in each sales region? Nice. That worked out pretty well. We got a natural language response that's clear and easy to understand, and that we can count on to be factually correct. Awesome. You have built out an entire text-to-SQL workflow, and it's generating valid and accurate SQL, which is answering user questions in natural language. TAG, Table Assisted Generation, is a game changer, and much like how RAG assists LLM generation with unstructured data, this is the equivalent for structured. Tag it and bag it. This job is done. Well, almost. The only way we could make this even better is to expand the semantic model so that it covers questions about product lines. Actually, that sounds like a great idea. Let's go do that in the next video.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.