A Retrieval-Augmented Generation (RAG) system can be built entirely in R using local LLMs (like Ollama with Llama 3.2) and vector embeddings, enabling semantic search and grounded answer generation from documents without requiring Python, API keys, or cloud costs. The system works by chunking documents into smaller pieces, converting them to numerical vectors (embeddings), storing these vectors, and when a question is asked, converting it to a vector and finding similar vectors to retrieve relevant context for the LLM to generate accurate answers.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Build a RAG System in R | Easy Setup, No API Key, Free & Local
Added:I have a folder on my computer with many files that contain university policies.
And suppose I ask this question for offering a course in a blended format, whom should the faculty member provide rational. Let's hit enter.
So it looks at those documents and then I get this answer. The faculty member should submit the pedagogical rational to the department chair as part of the department's scheduling process and also share it with the dean.
So if you look on the internet, every course, every tutorial, every blog post, everywhere you'll see that for creating a rack system, you need Python. But this is totally made in R. And also there's no API, no cost involved.
So how this system works is very simple.
So you may have some documents like PDFs on your computer maybe in a folder. So in the next step basically it creates chunks. It splits documents into smaller pieces and then converts chunks into dense vectors of some numbers and then that's what is stored. So stores embeddings for efficient retrieval. So that is done offline and then when you're online and you're asking a question it converts a question into vector and then searches for vector store for similar vectors and then combines stop chunks and the question and generate answer using context and question using LLMs. So that gives you the final answer. Basically, LLM doesn't know your documents which could be reports, it could be policies or it could be just notes and rag basically fixes that in three steps. First, it splits the document into chunks and then converts each chunk into embeddings which is basically a vector of numbers that captures the meaning or the essence. And then second step is when you ask a question, we embed the question as well. and also find chunks whose vectors are most similar. So that's the retrieval part of the rack system. In step three, we paste those chunks in the prompt and ask LLM to answer only using the context. So that's the generation part of the rack system.
That's it. So retrieval plus generation and every step in between is just math plus API calls and R is really good at those.
So first we need Olama which is a free app that helps to run LLMs on the local machine. I'm going to provide a link below and it will take you over some steps that help to install Olama. Very straightforward steps and once you do that you can run library or when you run test connection to check if everything is working. So you get a response status. It says okay. And one of the models that we'll use for LLM which is freely available is lama 3.2.
So I'm going to pull that.
And we also need nomic-bed.ext.
Now if you run list models, it will also list you like what models are available.
So these are the models that I have downloaded already. And llama 3.2 2 is a very small model just 2 GB and parameter size is about 3.2 billion parameters. So note that this runs on a normal laptop you don't really need any GPUs. So this nomic embed- text so basically turns text into vectors and lama 3.2 will basically generate answers based on the question we ask. We also need two other libraries HTTR2. So you need to install that. This package basically helps to talk to Olama's local web API and PDF tools helps to extract text from PDF files.
So in my downloads folder I created this folder called docs. And for this example I have added three university policy related PDF files. So approved blended teaching policy, faculty instructional responsibilities and guidelines for faculty personal actions. So we are going to work with this data. So you can see that in this fourth window in files currently I'm in downloads folder where this docs folder exists.
So next I'm going to use a function called source and within quotes I'm going to provide a link which is also going to be available below this video.
So basically this is the file that has all the R codes. I have divided this code into four groups chunks embeddings retrieval and generation. So you'll notice that the docs folder appears here in this first line. So here we are reading the files that are available within docs folder and then we break down the text into different chunks. So note that LLM context usually is limited. So retrieval works best with smaller and more focused pieces. So we break each document into chunks of about 500 characters with little overlap so that we don't cut sentences into half.
So in this part we create the embeddings that I talked about and then we have retrieval based on cosign similarity.
Basically whatever context is retrieved that is handed over to LLMs in the generation phase so that a answer can be generated. So note that we also tell here that if the answer is not in the context say you don't know. So basically it stops the rack system from hallucinations and it is not forced to provide some wrong answer. What I'm going to do is uh I will click on raw and then copy this link paste it here and run.
So it runs that part and now it's ready.
So let's see what is the length of the chunks.
So it says 61. So let's also see dimension of the embeddings. So 61 by 728 that's the matrix that we get based on text files that we have provided. Now if we use the retrieve function and say for promotion to full professor when it goes to FC to chair. So let's see what it gives. So it exactly pulls those paragraphs that are meaningful and related to this question. So there's no keyword matching here. We we did not provide any keyword and then it is looking for the keyword but it totally looks at the context. So basically it is a semantic kind of search.
Now using ask_rag let's ask this question for offering a course in blended format to whom should a faculty member provide rational. So if I run that, it provides this answer. A faculty member should submit the rational to the department chair as part of the department's scheduling process and it shall be shared with the dean.
Now let me also ask this question. What is our policy on office pets? Note that uh in the policy documents there is no such policy. But let's ask this question.
So we ask this question and it says I don't know there is no mention of office pets in the provided context. So basically it refuses to make things up.
So what we have got is a working rack system. What we just now created is a real architecture. Same pattern that's behind enterprise level rack systems.
Now you can easily extend this from PDF files to maybe HTML or word documents.
So doing modern AI in R is as easy as this. If you use this and create a rack system for example a document question answer tool for your team or maybe a research assistant. So please share that in the comments. If you find this useful, don't forget to like and subscribe to the channel so that I can continue to provide more modern AI related R videos. Thanks for watching.
I'll see you soon.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

we're almost finished the house (ep.125)
JennaPhipps
347K views•2026-07-22

We Finally Know Where Saturn’s Rings Came From
astrumspace
79K views•2026-07-22

BIG BET: Cathie Wood goes ALL IN on Elon Musk
FoxBusiness
89K views•2026-07-22

MIC DROP: Smithsonian Director Called Out For Woke Propaganda
TheAmalaEkpunobi
37K views•2026-07-23