Tutorials / Prompting Meta AI and Llama Models / Retrieval-augmented prompting: answering from your own documents

Retrieval-augmented prompting: answering from your own documents

12 min read text
Course contents (20 lessons)
  1. Getting to know Meta AI and Llama
  2. What Meta AI is and where you can use it
  3. Meta AI versus Llama: the assistant and the models
  4. Your first chat with Meta AI
  5. Prompt basics that work
  6. The anatomy of a good prompt
  7. Context, tone, length and follow-ups
  8. Everyday uses
  9. Writing and replying to messages
  10. Captions, posts and bios
  11. Planning and study help
  12. Customer replies for a small business
  13. Images, groups and privacy
  14. Writing image generation prompts
  15. Group chats, etiquette and privacy
  16. Working with Llama models directly
  17. Running a Llama model on your own computer
  18. System prompts and chat templates
  19. Parameters: temperature, top_p and max tokens
  20. Few-shot examples and structured JSON output
  21. Advanced prompting
  22. Prompting Llama for code
  23. Retrieval-augmented prompting: answering from your own documents
  24. Evaluating and comparing answers
  25. Safety, limits and a final project
  26. Safety, limits and responsible use
  27. Final project: build a study buddy assistant

The problem

A model knows what it learned in training. It does not know your company handbook, your shop policies, last week's meeting notes or the PDF you got this morning. If you ask about them, it may guess. Guessing about your own documents is exactly where you cannot afford errors.

The idea in one paragraph

Retrieval-augmented generation, usually shortened to RAG, works in two steps. First, a search step finds the few passages from your documents that are most related to the question. Second, those passages are placed in the prompt together with the question, and the model is told to answer from them. The model is no longer remembering. It is reading.

The pieces

  1. Split your documents into chunks, such as a few paragraphs each.
  2. Index the chunks so they can be searched. Many systems turn each chunk into a list of numbers called an embedding, which allows search by meaning. Simple keyword search also works for small collections.
  3. Retrieve the top few chunks for each question.
  4. Prompt the model with the chunks and the question.

You do not have to build all of this for a first test. For a short document, you can just paste the relevant section by hand. That is already retrieval-augmented prompting, done manually.

The prompt pattern

Answer the question using only the context below.
If the context does not contain the answer, reply exactly: "Not found in the provided documents."
After your answer, quote the sentence from the context that supports it.

Context:
[1] Staff may carry over up to 5 unused leave days into the next year. Days above 5 are lost on 31 January.
[2] Leave requests of more than 3 days must be submitted two weeks in advance.

Question: Can I carry over 8 unused leave days?

Why it works

"Using only the context" restricts the source. The exact fallback sentence gives a safe, easy-to-detect response. Asking for the supporting quote makes the model show its evidence, so you can check it in seconds. The numbers on the chunks let you ask for citations such as "[1]".

The expected answer here is that up to five days carry over, and the rest are lost. If the model says eight, the prompt or the model needs work.

Test the fallback

Question: How many days of paternity leave do I get?

The context says nothing about it. A good setup answers "Not found in the provided documents." If it invents a number, strengthen the instruction, lower the temperature, or use a larger model. Always test questions whose answers are not in the documents, as this is where RAG systems quietly fail.

Practical problems

  • Bad retrieval: if the wrong chunks are found, the model cannot answer well. When results are poor, look at what was retrieved before blaming the model.
  • Chunks too small or too large: small chunks lose context, large ones waste space. Try paragraphs of a few hundred words.
  • Too much context: more is not better. A model can miss details buried in a long prompt. Send a few good chunks, not twenty.
  • Out-of-date documents: the answer is only as fresh as your files.
  • Prompt injection: a document can contain text such as "ignore your instructions". If the documents come from outside sources, treat them as untrusted and never let the model take actions only because a document told it to.

Meta AI and your documents

In an assistant app, you can do the manual version. Paste the relevant text and use the same prompt. Do not paste confidential documents into a service unless you are allowed to. For private documents, a local model with a local search is the safer design.

The common mistake

The common mistake is believing that giving the model documents guarantees correct answers. It improves them a lot, but the model can still misread a passage or mix two. That is why the supporting quote is part of the prompt. It turns a trust problem into a checking problem, and checking is faster.

Try it yourself

Take one real page, such as a school policy, a product manual or a recipe. Copy three paragraphs. Write five questions, three whose answers are in the text, one that is only partly covered, and one that is not covered at all. Run the pattern above for each. Mark every answer right, wrong or "honest not found". Aim for no invented answers.

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account