AI Ustoz UZ: learn artificial intelligence in Uzbek

The idea of RAG: find first, then answer

Advanced level · lesson 3 · about 15 min

A model does not know everything and sometimes makes things up. The idea of RAG (retrieval-augmented generation): first a document piece that fits the question is searched for, then it is added to the request and the model is told “answer only from this text”. This reduces mistakes but does not remove them completely: you still need to check the source and the answer.

Real systems use methods such as embeddings for the search. We simplify: we count the words shared by the question and the document. A set is a collection without repeats, and & gives the common part of two sets.

a = "python dasturlash tili"
b = "python nima"
umumiy = set(a.split()) & set(b.split())
print(len(umumiy))
Output1

The next step: put the found text into a prompt template. The template clearly separates the source and the question for the model. The search in this lesson is very simple: in real life a good search is a separate job.

Remember

  • RAG: search first, then answer from the found text.
  • It reduces mistakes but does not remove them: keep checking.
  • A prompt template separates the source from the question.

Exercise

What is the RAG approach for?

Answer and explanation
  1. To ground the model’s answer in a found source text and lower the risk of made-up content. (correct answer)before answering, a fitting text is added to the request.
  2. To make the model completely free of mistakes.there is no guarantee: the source can be wrong or the search poor.
  3. To run a program without the internet.RAG does not depend on the internet; that is something else.

Code exercises

  1. eng_mos(savol, hujjatlar) should return the document that shares the most words with the question. Use the ready-made umumiy(a, b); on a tie the first one stays.
    Hint: Start with the first document as the best and replace it in a loop when umumiy is larger.
  2. rag_prompt(savol, hujjat) should return three lines of text: Faqat quyidagi matn asosida javob ber., Matn: ..., Savol: ....
    Hint: Put \n between the lines and use an f-string.

Do the exercise in the app.

Practical task

Write 3 short texts and decide by hand which one fits two different questions best; then compare with the function’s result.

  • I wrote 3 texts
  • I decided by hand
  • I compared with the function

Glossary

Interactive mode: search, progress and Python exercises.