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
%pip install -qU pixeltable openai
import getpassimport osif 'OPENAI_API_KEY' not in os.environ: os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')
import pixeltable as pxtfrom pixeltable.functions.openai import chat_completions, embeddings# Create a fresh directorypxt.drop_dir('rag_demo', force=True)pxt.create_dir('rag_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘rag_demo’.
<pixeltable.catalog.dir.Dir at 0x17c878c10>
# Add embedding index for semantic searchchunks.add_embedding_index( column='chunk_text', string_embed=embeddings.using(model='text-embedding-3-small'),)
Step 2: load documents
# Sample knowledge base (in production, load from files/database)documents = [ { 'doc_id': 'password-reset', 'chunk_text': 'To reset your password, go to the login page and click "Forgot Password". Enter your email address and you will receive a reset link within 5 minutes. The link expires after 24 hours.', }, { 'doc_id': 'password-reset', 'chunk_text': 'Password requirements: minimum 8 characters, at least one uppercase letter, one number, and one special character. Passwords expire every 90 days for security.', }, { 'doc_id': 'account-settings', 'chunk_text': 'To update your profile, navigate to Settings > Account. You can change your display name, email address, and notification preferences. Changes take effect immediately.', }, { 'doc_id': 'billing', 'chunk_text': 'Billing occurs on the first of each month. You can view invoices under Settings > Billing. To change your payment method, click "Update Payment" and enter your new card details.', }, { 'doc_id': 'api-access', 'chunk_text': 'API keys can be generated in Settings > Developer. Each key has configurable permissions. Rate limits are 1000 requests per minute for standard plans, 10000 for enterprise.', },]chunks.insert(documents)
# Define a query function that retrieves context@pxt.querydef retrieve_context(query: str, top_k: int = 3): """Retrieve the most relevant chunks for a query.""" sim = chunks.chunk_text.similarity(string=query) return ( chunks.where(sim > 0.5) .order_by(sim, asc=False) .limit(top_k) .select(doc_id=chunks.doc_id, text=chunks.chunk_text) )
# View retrieved context for a queryquery = 'What are the key features?'context_chunks = retrieve_context(query)context_chunks
retrieve_context(‘What are the key features?’)
Step 4: generate answers with context
# Create a table for questions/answersqa = pxt.create_table('rag_demo/qa', {'question': pxt.String})
Added 0 column values with 0 errors.
No rows affected.
# Build the RAG prompt@pxt.udfdef build_rag_prompt(question: str, context: list[dict]) -> str: context_text = '\n\n'.join( [f'[{c["doc_id"]}]: {c["text"]}' for c in context] ) return f"""Answer the question based only on the provided context. If the context doesn't contain the answer, say "I don't have information about that."Context:{context_text}Question: {question}Answer:"""qa.add_computed_column(prompt=build_rag_prompt(qa.question, qa.context))
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
# Insert questionsquestions = [ {'question': 'How do I reset my password?'}, {'question': 'What are the API rate limits?'}, {'question': 'When am I billed?'},]qa.insert(questions)