RAG

RAG means the answer comes from a note you saved. The course has three steps: cut the note into sections, find the section that shares words with the question, then answer from that section.

The note can be kept in the script, or in SQLite, Chroma, mem0, or MongoDB. The last page runs the script and SQLite. Chroma, mem0, and MongoDB are shown without a Try it button, because they need an extra install or a server. Start after Agents.

The line the question does not contain

Three lines are saved. The question never says brass or key. Press Try it Yourself, then Run. The result is one line: The prize behind the blue door is a brass key.

Example

from praisonaiagents.knowledge import KeywordIndex

lines = [
    "The library closes at six.",
    "The prize behind the blue door is a brass key.",
    "Apples grow in the orchard.",
]
index = KeywordIndex()
index.add_documents(lines)
print(index.query("What is behind the blue door?", top_k=1)[0]["text"].strip())

Try it Yourself

Example explained

  • lines — The note. Only one line answers the question.
  • KeywordIndex — Picks the line that shares words with the question. It does not call the model.

Next

Exercises · Quiz · Reference