Skip to content
← Inforjota
Cursos Inforjota Português

Lesson 4 of 6 · 14 min · Concept content, does not go stale

How it works inside: training, embeddings and RAG

By the end of this lesson you will be able to:

  • Explain in simple words how a model is trained and fine-tuned
  • Describe embeddings with the image of a map of sentences
  • Explain what "projects" and "knowledge bases" do when they answer with your documents
  • Prepare documents so that the tool finds them and makes less up

The counter at the junta

Mr Jorge has been at the counter of the junta de freguesia (parish council) for thirty years. He has read so much paper in his life that, if you start a sentence, he guesses how it ends. But when you ask him something specific ("how much is the certificate?"), he does not answer from memory: he opens the drawer, takes out the table of fees, reads it, and only then answers.

The AI tools you use work in a very similar way. Understanding how helps you know why they get things right, why they get things wrong and how to put your own documents to work for you.

Training: reading almost everything and guessing the next word

In Level 1 you saw that the AI guesses the next word. That is what it learns in training. The model reads an enormous amount of text (web pages, books, articles) and, sentence by sentence, tries to guess the word that comes next. When it gets it wrong, it adjusts a little. This is repeated a number of times that is hard to imagine.

It is like the proverb game: "A stitch in time saves…". You say "nine" because you have heard it many times. The model did the same with almost everything that is written. That is why it knows grammar, common facts and writing styles. And that is also why it repeats the mistakes and prejudices it found in the texts.

Fine-tuning: extra classes

After training, the model knows how to continue a text, but it is not yet a good assistant. Then comes fine-tuning: extra classes with examples of questions and good answers, written by people. After that, people compare two answers and choose the better one, and the model learns from those choices.

Fine-tuning is what makes it polite, makes it answer instead of just continuing your sentence, and leads it to refuse some requests. But notice: neither training nor fine-tuning teaches it your documents. Your junta's opening hours are not in there.

Embeddings: a map of sentences

To work with your documents, the tool needs to find things in them fast. For that it uses embeddings.

Imagine an enormous map. Each sentence becomes a point on that map, and sentences with the same meaning sit close together, even with different words:

  • "What time does the bakery open?" sits right next to "What are the bakery's opening hours?".
  • "Bread went up in price" sits in the same neighbourhood, a little further away.
  • "The boat to Pico was cancelled" sits at the other end of the map.

When you ask a question, it becomes a point too. The tool looks for the points of your documents that are closest. It is a search by meaning, not by exact words.

Tip

This is why you do not need to use the exact words of the document. But it helps to use the names that are in it: if the regulations talk about the "annual fee", asking about the "annual fee" brings you closer to the right piece.

RAG: going to the drawer before answering

Put it all together and you have RAG, short for "retrieval-augmented generation". It works in three steps:

  1. Prepare: the tool cuts your documents into pieces and puts each piece on the map.
  2. Fetch: when you ask, it finds the pieces closest to your question.
  3. Read and answer: it adds those pieces to your question and only then writes the answer.

It is Mr Jorge opening the drawer. And it is what the projects and knowledge bases you used in Level 2 do: you upload the regulations, the frequently asked questions or the manual, and the tool goes there before answering. When the documents are few and small, some tools read them whole each time; when there are many, they fetch only the pieces.

Why it still makes things up (less)

RAG reduces made-up answers a lot, because the AI answers with the paper in front of it. But it does not remove them:

  • It brings the wrong piece. If there are two versions of the regulations, it may read the old one.
  • It does not find it. The answer is in a table scanned as a photograph, or written very differently from the question.
  • It mixes. It joins what it read with what it "knows" from training, without saying which is which.
  • It reads badly. It summarises in a hurry and swaps a number.

Watch out

An answer taken from your documents sounds even more sure than the others. For numbers, dates, deadlines and amounts, always ask for the document and the part it came from, and open it to confirm.

Tip

Tidy the drawer: remove old versions, give files clear names ("Internal regulations 2026"), prefer text to photographs of paper and, in PDFs, check that you can select the text.

Exercise: putting the drawer to the test

Use the projects or documents feature you learned in Level 2. Upload a document you know well (the association's regulations, the library rules, an appliance manual) and paste these instructions:

Answer only on the basis of the documents in this project. For each answer, say which document and which part you took the information from. If the answer is not in the documents, say "I did not find it in the documents" and do not fill in with what you know.

Exercise

How I know it worked right answers in steps 2 and 3, each with the part of the document indicated (open it and confirm), and an "I did not find it in the documents" in step 4.

If it didn't work

  • Did it fail in step 3? Try again with a word that is in the document. If it gets it right then, you saw the search at work: you moved closer to the right point on the map.
  • Did it make something up in step 4? Repeat it in the question itself: "if it is not in the documents, say you did not find it". If it still makes things up, take note: with this tool, checking is a must.
  • Did it find nothing? The document may be a photograph of paper. Try selecting the text in the PDF: if you cannot, the tool may not read it well either.

Videos

RAG Explicado: Como Dar Conhecimento aos Seus Agentes de IA
Attekita Dev · 13 min · PT-BR · subtitles · Watch on YouTube
Explains RAG: document extraction, chunking, embeddings, vector databases, semantic search, reranking and answer generation.
What is Retrieval-Augmented Generation (RAG)?
IBM Technology · 6 min · EN · subtitles · Watch on YouTube
IBM: what RAG is and how it fixes the outdated or wrong answers of a model that only knows its training data. It is from 2023, but the idea still holds.

Test what you learned

1. You put the association's regulations into a knowledge base. The AI answered about the membership fees with the figure from the old regulations. What is the most likely cause and the best reaction?
2. In the junta's knowledge base it says "public service hours are from 9:00 to 12:30". A resident asks "what time can I go there to deal with paperwork?". Why can the tool find the answer?
3. Which of these requests makes the most of a project with the school's documents?