Skip to main content

Data sources and RAG

A model doesn't know your handbook, your product or your policies. Data sources fix that. Attach your files, and the agent looks up the relevant parts before it answers. This technique is called retrieval-augmented generation (RAG).

An open-book exam​

Think of RAG as letting the agent sit an open-book exam. It doesn't memorize your files. For each question, it flips to the most relevant pages, reads them, and answers from what it just read.

That has three big benefits:

  • Answers come from your content, not from the model's general knowledge.
  • You can update knowledge by uploading a new file. No retraining.
  • You can check its work. Every reply tells you which passages it used.

What happens behind the scenes​

When you deploy, Custodian prepares your files once:

  1. Extract the text from each file.
  2. Split it into short passages, roughly a paragraph each. Neighbouring passages overlap slightly so sentences aren't cut in half.
  3. Index each passage by its meaning, using an embedding. That's a list of numbers that captures what the text is about, so "refund window" and "how long to return an item" end up close together.

On every message, the agent:

  1. Finds the 4 passages whose meaning is closest to the question.
  2. Adds them to the prompt with the question.
  3. Writes an answer based on them.
"How long do I have to return a laptop?"
↓ find the closest passages
"Returns are accepted within 30 days..." (handbook.pdf)
"Electronics must be unopened..." (handbook.pdf)
↓ answer from them
"You have 30 days, as long as the laptop is unopened."

You don't set up a database or write any of this. You just attach the file.

Attach files​

from custodian_labs import Custodian

app = (
Custodian(model="gpt-6-luna", system_prompt="Answer from the provided docs.")
.add_data_source_file("./support-handbook.pdf") # call again for more files
.deploy()
)

reply = app.chat("What is the refund policy?")
print(reply.retrieved_contexts) # the passages used for this answer

Supported files​

KindExtensions
Text.txt, .md, .csv, .json, .yaml, .yml, .log
PDF.pdf
Word.docx, .odt
Excel.xls, .xlsx, .xlsm, .xltx, .xltm

Text files must be UTF-8. PDFs need selectable text.

What RAG is good at, and what it isn't​

Works wellWorks less well
"What does the policy say about X?""Summarize the whole 300-page manual"
"How do I configure Y?""How many rows in this spreadsheet have Z?"
Questions answered by one or two passagesQuestions that need every part of a file at once

The agent only reads a few passages per question. Answers that depend on the whole file at once, like totals or full summaries, may miss things.

Getting better answers​

  • Tell the agent what to do when the answer isn't there. "If the documents don't cover it, say so" stops it guessing.
  • Use clear headings. They help each passage make sense on its own.
  • Keep files focused. Several small, on-topic files beat one giant one.
  • Remove outdated content. Old passages can be retrieved just as easily as new ones.
  • Check retrieved_contexts. If the right passage isn't there, reword the file or the question.
  • Mask sensitive files first. See RAG on sensitive data.

Files in a squad​

A file belongs to the agent you attach it to. Other agents in the squad don't search it. That lets each specialist have its own knowledge.

Files or tools?​

Use a data source whenUse a built-in tool when
You have the knowledge as filesThe answer depends on current or public information
The content is fairly stableThe source changes often, or you don't know it in advance

You can use both on the same agent.

See it in code: RAG agent template.