Building the First AI App in .NET – Step 2: From Grounding to Retrieval

Introduction In Step 1, we built a simple grounded LLM application. The application could answer questions using our documents, but there was a fundamental problem: we sent all documents to the model for every question. That works for a handful of documents. It doesn’t work for a real knowledge base. So the next question is: How do we give the LLM only the information it actually needs? This leads us to retrieval and the moment we learn about RAG. As usual, you can find the code for this example here: https://github.com/genoiucosmin/AIApp1/tree/Step2 From all documents to relevant documents Step 1 Step 2 Let’s explain why this is cheaper, faster and more scalable conceptually. Chunking and our first retrieval mechanism In the previous example we sent in all documents to the LLM. This is clearly not something scalable, so we need to define a way of sending only the relevant data to the LLM. This is actually RAG – and we will implement a very primitive version of it, by just splitting the documents we had into chunks of 300 characters. Then we find out how many times each word from our question appears in each chunk. We then select the top…