This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Create a retrieval-augmented generation system that answers questions
using your documents as context.
Problem
You want an LLM to answer questions using your specific documents—not
just its training data. You need to retrieve relevant context and
include it in the prompt.
Solution
What’s in this recipe:
- Embed and index documents for retrieval
- Create a query function that retrieves context
- Generate answers grounded in your documents
You build a pipeline that: (1) embeds documents, (2) finds relevant
chunks for a query, and (3) generates an answer using those chunks as
context.
Setup
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘rag_demo’.
<pixeltable.catalog.dir.Dir at 0x17c878c10>
Step 1: create document store with embeddings
Created table ‘chunks’.
Step 2: load documents
Inserting rows into `chunks`: 5 rows [00:00, 345.31 rows/s]
Inserted 5 rows with 0 errors.
5 rows inserted, 15 values computed.
Step 3: create the RAG query function
retrieve_context(‘What are the key features?’)
Step 4: generate answers with context
Created table ‘qa’.
Added 0 column values with 0 errors.
No rows affected.
Added 0 column values with 0 errors.
No rows affected.
Added 0 column values with 0 errors.
Added 0 column values with 0 errors.
No rows affected.
Ask questions
Inserting rows into `qa`: 3 rows [00:00, 872.12 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 18 values computed.
Explanation
RAG pipeline flow:
Question → Embed → Retrieve similar chunks → Build prompt with context → Generate answer
Key components:
Scaling tips:
- Use
doc-chunk-for-rag recipe to split long documents
- Adjust
top_k to balance context size vs. relevance
- Consider metadata filtering for large knowledge bases
See also