Get AI-Ready With Erik: Retrieval Augmented Generation
Summary
In this video, I delve into Retrieval Augmented Generation (RAG) and its application in SQL Server databases, particularly focusing on how it can be used to build developer support chatbots. I explain the process of using a stored procedure to retrieve relevant posts from your database and send them as context to an LLM (Language Model), which then generates answers based on community knowledge. The video demonstrates this with a practical example where we build a store procedure that includes source URLs, allowing users to verify information provided by the LLM. I also discuss the importance of embedding questions using AI generate embeddings and highlight how SQL Server can serve as a knowledge base for storing and retrieving relevant content efficiently.
Chapters
- *00:00:00* – Introduction
- *00:01:01* – Retrieval Augmented Generation Overview
- *00:02:02* – Basic Process of RAG
- *00:02:21* – LLM Verification with Source URLs
- *00:03:02* – Store Procedure for Embeddings
- *00:04:26* – Example Store Procedure Execution
- *00:05:38* – Summary of RAG Workflow
Full Transcript
Erik Darling here with Darling Data. If you have hated all of the vector content that I’ve been covering over the last month or so, your long national nightmare is over. This will be the last, like, dedicated course promoting video that I have on it. Again, that is available on my training site. The full link is available down in the video description. There is an active coupon code called AIReady where you can get $500 off. And in today’s video, we’re going to talk about Retrieval Augmented Generation, or RAG, which is, you know, probably what most of you will have to deal with within, like, within the context of, like, a SQL Server database doing things with vectors, embeddings, stuff like that. So, let’s say, let’s say something like, you can even just pretend, right? You can, you can totally just cosplay as this person that I’m, this fictional person that I’m talking about. But let’s say you’re building a developer support chatbot. And developers need to know stuff about Git, like, I screwed up, how do I undo that? Or, like, I don’t know, like, left join, and left join, what is that? Or, like, maybe even something as ambiguous as, why is my query slow? Which, if you need an answer to that question, let me tell you this, my rates are reasonable, I can answer that for you, like, immediately. Bam, it’s done. But the general flow of this is, you know, you would, like, you know, find some relevant posts in your database. And then you would take those posts and send them as context to an LLM, right? Because SQL Server doesn’t do this stuff internally, right? You need, like, a, you need, you need an LLM in some fashion, where you can send bulks of information to, and then the LLM is supposed to, like, distill some answer based on, like, community knowledge around a topic. And that’s basically, that’s the basics of RAG, or Retrieval Augmented Generation.
SQL Server handles the data storage and data retrieval. The LLM handles the generation, and you have some application in the middle that connects them, right? Just says, oh, I need to, like, send in this search string to SQL Server and find relevant stuff. And then I take the relevant stuff that I found, and I send it to the LLM, and the LLM gets it all wrong, right? So, that’s the basic process there. So, if your chatbot, you know, wants to know, like, you know, you can undo a commit with git reset, users might be like, well, how do you know? Like, can you cite some sources? Can you, can you back that up with anything factual? Are you sure that’s a real command?
Does git exist? Like, there’s lots of stuff. People would rightfully question and ask about, when an LLM does stuff. So, what we’re going to do is use a store procedure to include source URLs that will let users verify the information, right? Because you might, if you’re the type of person who just takes what LLMs say at face value, you might be in for some interesting times in your life.
But, anyway, this is what the store procedure looks like. The name is unimportant, but we’re going to pass in a query. We’re going to define a max number of rows we want back. We’re also going to filter on distance, something that I’ve said a few times is pretty important with vector distance stuff. And then we’re going to, like, output a prompt. Just, this isn’t necessary, necessarily. This is just something for me to show, so I can show you that, like, what the full results would look like.
Inside the procedure, we’re going to make a variable that’s going to hold the embedding that we want, and we’re going to go grab that embedding using AI generate embeddings. I talked through a little bit about how to, like, get that set up in another video, but I’m not going to redo all that here. And then this is the query that runs to return stuff, and then, like, what it’s going to do is, like, you know, the usual, like, here’s the distance, here’s the source URL.
And we’re going to dump that into a temp table called prompt, which hopefully will happen promptly. And then we’re going to, I mean, I’m doing this so it makes sense in this context. You might not do this. You might do something different. But we’re going to say our prompt is, based on the following Stack Overflow questions, answer the questions, and then the query that the user originally passed in, right?
So, like, that’s, like, the question that needs to get answered. And then what we can do is run the store procedure, right? So, we’re going to declare a prompt variable just to hold the output here.
The query that we’re sending in is how to undo the most recent commits in Git. We’re asking for the top 10 results with a max distance of 0.20. And when we run this, what the prompt returns is, I mean, I have some XML content here so I can click and open it for you.
You might choose to do something differently. But it says, based on the following Stack Overflow questions, and all these questions are things that we found using the search string that someone passed in. Like, based on these 10 URLs, answer the question, how to undo the most recent commits in Git.
So, that’s, like, sort of the basics of, like, what you would do to, like, A, like, you have all this stuff stored in SQL Server. You have your vectors. You have your embeddings.
And then, you know, you have your users go in, search for things that are similar. Then you have an LLM build out a response based on what it finds in there. So, it treats, like, treats your database like a knowledge base, right?
You can just search through all this, like, you know, historical knowledge, get an answer, and hopefully it’s, you know, well-founded in reality and the LLM doesn’t hallucinate anything along the way. All things that we hope for. So, the application basically, like, basically, again, the workflow is the user asks, like, how do I do this thing?
How do I undo my last commit in Git? The app embeds the question. We use AI generate embeddings. I guess you don’t need to know it’s in Video 20.
That’s stupid. You could also call an external embedding, but I don’t make anyone do that because it costs extra money. The app calls SQL Server to execute some store procedure and get the results. Then the app builds the LLM prompt, right?
So, like, in this case, I had the store procedure do it because I don’t have an app, right? I’m appless. But you could have an application do that and then, you know, answer the question down here. The LLM generates an answer using all the, like, context from the links that you provided it.
And then the app shows the answer with source links for verification, right? Wouldn’t it be nice if LLM’s always cited sources? So, SQL Server’s job is to find relevant content quickly.
And the LLM’s job is to then synthesize a coherent answer from that content. So, that’s sort of in a nutshell how one would approach RAG with SQL Server. I hope you enjoyed yourselves.
I hope you learned something. And today is Friday, so I won’t see you in tomorrow’s video, but I will see you in Monday’s video, assuming that we all survive, which will be office hours. And then, what am I going to do after office hours?
Well, that week is wide open. Wide open. Who knows what will happen? All right. Thank you for watching.
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. Blog readers get 25% off the Everything Bundle — over 100 hours of performance tuning content. Need hands-on help? I offer consulting engagements from targeted investigations to ongoing retainers. Want a quick sanity check before committing to a full engagement? Schedule a call — no commitment required.




