Ir para o conteúdo

Lição 23 · Projects and Reference

Project: Private Document Q&A with Llama

Build a document question-answering app with chunking, search, access rules, grounded prompts, citation checks and an evaluation set you can run locally.

Here you combine the retrieval and safety lessons into an app that answers questions from a set of documents, with sources. If every component runs locally, embeddings included, your documents do not need to leave your machine.

Plan

  1. Load documents and attach metadata (source and allowed groups).
  2. Split them into chunks.
  3. Search only the chunks the user may read.
  4. Refuse early when nothing relevant is found.
  5. Ask Llama to answer from the sources and cite them.
  6. Check the citations before showing the answer.
  7. Evaluate retrieval and answers.

The pipeline with stand-ins

Here, keyword overlap replaces embeddings and a hand-written function replaces the model. The control flow is what you keep.

Python Stand-in: permission-aware document Q&A

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same finance question gives different results for different users, because filtering happens before retrieval returns anything. The model never sees text that the user may not read.

Make it real

  • Embeddings: call a local embedding model (with Ollama, /api/embed) and store vectors with chunk metadata. A small project can keep them in memory or in SQLite. Compare vectors with cosine similarity. See retrieval with Llama.
  • Generation: replace fake_model with a chat call to your local Llama, using a low temperature and a system message with the grounding rules.
  • Escaping: documents may contain text that looks like your source tags. Escape or strip such markers before building the prompt.
  • Updates: when a document changes or is deleted, update or remove its chunks and vectors. Old copies can leak removed information.

Evaluate in two parts

First retrieval: for each test question, record which chunk IDs should appear. Then answers: check correctness, citations and refusal when the answer is missing.

Python Stand-in: score retrieval recall

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data above is invented to show the method. In your project, retrieved comes from your real search. The false alarm shows why you test unanswerable questions too.

Security review

  • Are permissions enforced in code for every query?
  • Can a document inject instructions, and what could the model do if it obeyed? Keep this app read-only.
  • Where are chunks, vectors, logs and chat history stored, and who can read them?
  • Is anything sent to a remote service? Check each component, including any cloud features in your tools.

A local model alone does not make an app private. A remote embedding service, telemetry, backups or logs can still copy your data elsewhere.

Extensions

  • Combine keyword search with vector search, and compare recall.
  • Show the cited passages next to the answer so users can verify them.
  • Add a "was this helpful?" button that records feedback without storing full documents.
  • Rebuild the index only for files whose content changed.

Start with a small, known document set

Pick five to ten short documents that you know well, with no sensitive content. Because you know the right answers, you can judge the results quickly. Write fifteen test questions: some easy, some phrased with different words from the text, some that need two passages and some with no answer at all. Only after this set behaves well should you add more documents.

Make failures visible

During development, print the retrieved chunk IDs and scores next to every answer. Then each wrong answer falls into one of a few groups. Either the right chunk was never found, or it was found but ranked too low, or it reached the model and the model ignored or misread it. Each group needs a different fix: better chunking or embeddings, a different number of passages or a reranker, or a clearer prompt or a stronger model.

Keep the index fresh

Store a content fingerprint, such as a hash, with each document. On the next run, compare fingerprints, rebuild only the changed documents and delete chunks whose source no longer exists. Test deletion on purpose: remove a document, rebuild and confirm that no answer can quote it anymore. Apply the same idea to permission changes, so that a user who loses access also loses it in search.

Acceptance criteria

Decide what "good enough" means before you show the app to others. For example: the right passage is retrieved for nearly all answerable test questions, no restricted chunk ever appears for the wrong user, every displayed citation was supplied to the model and unanswerable questions get an honest "I don't know". The permission rule is different from the others: treat a single violation as a failure, not as a score to average.

Tell users the limits

On the page where people ask questions, say which documents are included, when the index was last updated and that answers can be wrong. Show the sources and invite users to check them before they act. For decisions about money, health, law or employment, direct people to the responsible person and the original document. Provide a simple way to report a wrong answer, and review those reports to extend your test set.

What a good result looks like. A good version of this project is modest. It answers a defined set of questions, shows where each answer came from, admits gaps and never crosses a permission boundary.

Before sharing it. Ask a colleague to try questions you did not write, including some that try to reach restricted content. New testers usually find wording and mistakes that the author never imagined.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. When should access filtering happen?

    Text that reaches the model can appear in its reply.
  2. 2. What does the citation check confirm?

    It catches made-up IDs, but not every unsupported claim.
  3. 3. Why include unanswerable questions in tests?

    A good system does not invent answers.

Summary

  • Filter by permission first, then retrieve.
  • Refuse when nothing relevant is found, and check citations.
  • Evaluate retrieval and answers separately.
  • Confirm that every component and log keeps data where you intend.

Next: Llama Glossary and Cheat Sheet

Seu progresso fica salvo neste navegador. Não precisa de conta.