Building with llamaindex

So far we have the pieces: embeddings, vector storage, and chunking. LlamaIndex is the framework that connects them all. It handles chunking your documents, embedding them, storing in Qdrant, and retrieving relevant chunks when you ask a question.

The End-to-end RAG pipeline

A user query flows through embedding, retrieval, prompt augmentation, and generation to produce a grounded answer with sources.

Building each piece from scratch taught you how everything works under the hood. LlamaIndex gives you production-grade wiring: automatic chunking, batched embedding calls, retry logic, and a clean query interface. You now understand what the framework hides, so you can debug it when things go wrong.

05_rag_pipeline.ipynb
python
from llama_index.core import VectorStoreIndex, StorageContext, Settings
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.llms.litellm import LiteLLM
from llama_index.embeddings.litellm import LiteLLMEmbedding
from qdrant_client import QdrantClient

# Configure LlamaIndex with our models
Settings.llm = LiteLLM(model=DEFAULT_MODEL)
Settings.embed_model = LiteLLMEmbedding(model_name=EMBEDDING_MODEL)

# Connect to Qdrant
qdrant_client = QdrantClient(url=QDRANT_URL)
vector_store = QdrantVectorStore(
    client=qdrant_client,
    collection_name="green_bites_rag"
)
storage_context = StorageContext.from_defaults(
    vector_store=vector_store
)

Initialize LlamaIndex with Qdrant, LiteLLM, and embedding model

With the models and vector store connected, let's load Green Bites' knowledge base. LlamaIndex will automatically chunk each document, generate embeddings, and upload them to Qdrant in a single call.

05_rag_pipeline.ipynb
python
from llama_index.core import Document

knowledge_base = [
    "Green Bites is open Monday to Saturday, 11 AM to 10 PM. We are closed on Sundays.",
    "Our Mushroom Burger is made with portobello mushrooms, caramelized onions, and house-made vegan cheese on a brioche bun.",
    "We offer full refunds within 30 minutes of purchase if the order is incorrect or the food quality is unsatisfactory.",
    "Green Bites sources all vegetables from local organic farms within 50 miles of each restaurant location.",
    "We do not use any honey or bee products. All sweeteners are plant-based including maple syrup and agave.",
    "Gift cards are available in $25, $50, and $100 denominations and can be purchased in-store or online.",
]

documents = [Document(text=text) for text in knowledge_base]

# This single call: chunks → embeds → uploads to Qdrant
index = VectorStoreIndex.from_documents(
    documents,
    storage_context=storage_context
)
print("Knowledge base indexed!")

Load Green Bites knowledge base and create the index

The index is ready. Now we create a query engine, the interface that accepts natural language questions, retrieves relevant chunks from Qdrant, and passes them to the LLM to generate a grounded answer.

05_rag_pipeline.ipynb
python
query_engine = index.as_query_engine(
    streaming=True,
    similarity_top_k=2  # Retrieve top 2 most relevant chunks
)

def ask_green_bites(question):
    """Ask a question and get a grounded answer."""
    print(f"Q: {question}")
    response = query_engine.query(question)
    print(f"A: {response}\n")
    return response

ask_green_bites("What are your opening hours on Saturday?")
ask_green_bites("Is there honey in your food?")
ask_green_bites("Tell me about the Mushroom Burger")

Create a query engine and ask Green Bites questions

Complete RAG pipeline flow

How LlamaIndex orchestrates the full RAG pipeline.

Quiz: Quiz

Loading practice…

Flashcards: Flashcards

Loading practice…