What RAG is

Three steps

A question should be answered from your note, not from what the model happens to remember.

Keep the lines. Search picks the line that shares words with the question. The next pages ask the model to use only that line.

Example

from praisonai.knowledge import KeywordIndex

lines = [
    "Apples grow on trees in the orchard.",
    "The secret colour of the boat is blue.",
    "Tickets for the ferry cost four pounds.",
]
index = KeywordIndex()
index.add_documents(lines)
print(index.query("What colour is the boat?", top_k=1)[0]["text"])

Try it Yourself

Example explained

  • KeywordIndex — A search that matches words. It does not call the model.
  • query — The question. The first result is the closest line.

Generate from the hit

Search then generate — the next lessons split these steps. Here both run in one script.

Example

from praisonai import Agent
from praisonai.knowledge import KeywordIndex

lines = ["Tickets for the ferry cost four pounds."]
index = KeywordIndex()
index.add_documents(lines)
question = "How much is the ferry?"
line = index.query(question, top_k=1)[0]["text"]
agent = Agent(instructions="Answer using only the Note.")
print(agent.start(question + "\nNote: " + line))

Try it Yourself

Example explained

  • from praisonai import Agent — Imports Agent needed for generate from the hit.
  • from praisonai.knowledge import KeywordIndex — Imports KeywordIndex needed for generate from the hit.
  • Agent(…) — Instantiates the Agent configured for generate from the hit.
  • .start(…) — Executes the configured run for generate from the hit and returns the result.

Reference: documentation