10 KiB
RAG
The RAG pipeline (aka Extractor) joins a prompt, context data store and generative model together to extract knowledge.
The data store can be an embeddings database or a similarity instance with associated input text. The generative model can be a prompt-driven large language model (LLM), an extractive question-answering model or a custom pipeline. This is known as retrieval augmented generation (RAG).
Example
The following shows a simple example using this pipeline.
from txtai import Embeddings, RAG
# Input data
data = [
"US tops 5 million confirmed virus cases",
"Canada's last fully intact ice shelf has suddenly collapsed, " +
"forming a Manhattan-sized iceberg",
"Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
"The National Park Service warns against sacrificing slower friends " +
"in a bear attack",
"Maine man wins $1M from $25 lottery ticket",
"Make huge profits without work, earn up to $100,000 a day"
]
# Build embeddings index
embeddings = Embeddings(content=True)
embeddings.index(data)
# Create and run pipeline
rag = RAG(embeddings, "google/flan-t5-base", template="""
Answer the following question using the provided context.
Question:
{question}
Context:
{context}
""")
rag("What was won?")
See the Embeddings and LLM pages for additional configuration options.
See the links below for more detailed examples.
| Notebook | Description | |
|---|---|---|
| Prompt-driven search with LLMs | Embeddings-guided and Prompt-driven search with Large Language Models (LLMs) | |
| Prompt templates and task chains | Build model prompts and connect tasks together with workflows | |
| Build RAG pipelines with txtai | Guide on retrieval augmented generation including how to create citations | |
| Integrate LLM frameworks | Integrate llama.cpp, LiteLLM and custom generation frameworks | |
| Generate knowledge with Semantic Graphs and RAG | Knowledge exploration and discovery with Semantic Graphs and RAG | |
| Build knowledge graphs with LLMs | Build knowledge graphs with LLM-driven entity extraction | |
| Advanced RAG with graph path traversal | Graph path traversal to collect complex sets of data for advanced RAG | |
| Advanced RAG with guided generation | Retrieval Augmented and Guided Generation | |
| RAG with llama.cpp and external API services | RAG with additional vector and LLM frameworks | |
| How RAG with txtai works | Create RAG processes, API services and Docker instances | |
| Speech to Speech RAG ▶️ | Full cycle speech to speech workflow with RAG | |
| Generative Audio | Storytelling with generative audio workflows | |
| Analyzing Hugging Face Posts with Graphs and Agents | Explore a rich dataset with Graph Analysis and Agents | |
| Granting autonomy to agents | Agents that iteratively solve problems as they see fit | |
| Getting started with LLM APIs | Generate embeddings and run LLMs with OpenAI, Claude, Gemini, Bedrock and more | |
| Extractive QA with txtai | Introduction to extractive question-answering with txtai | |
| Extractive QA with Elasticsearch | Run extractive question-answering queries with Elasticsearch | |
| Extractive QA to build structured data | Build structured datasets using extractive question-answering |
Configuration-driven example
Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.
config.yml
# Allow documents to be indexed
writable: True
# Content is required for extractor pipeline
embeddings:
content: True
rag:
path: google/flan-t5-base
template: |
Answer the following question using the provided context.
Question:
{question}
Context:
{context}
workflow:
search:
tasks:
- action: rag
Run with Workflows
Built in tasks make using the extractor pipeline easier.
from txtai import Application
# Create and run pipeline with workflow
app = Application("config.yml")
app.add([
"US tops 5 million confirmed virus cases",
"Canada's last fully intact ice shelf has suddenly collapsed, " +
"forming a Manhattan-sized iceberg",
"Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
"The National Park Service warns against sacrificing slower friends " +
"in a bear attack",
"Maine man wins $1M from $25 lottery ticket",
"Make huge profits without work, earn up to $100,000 a day"
])
app.index()
list(app.workflow("search", ["What was won?"]))
Run with API
CONFIG=config.yml uvicorn "txtai.api:app" &
curl \
-X POST "http://localhost:8000/workflow" \
-H "Content-Type: application/json" \
-d '{"name": "search", "elements": ["What was won"]}'
Methods
Python documentation for the pipeline.

