Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
customization.rst 6.27 KiB

Customization Tutorial

Tip

If you haven't, install, complete starter tutorial, and learn the high-level concepts before you read this. It will make a lot more sense!

In this tutorial, we show the most common customizations with the starter example:

from llama_index import VectorStoreIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader('data').load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)

"I want to parse my documents into smaller chunks"

from llama_index import ServiceContext
service_context = ServiceContext.from_defaults(chunk_size=1000)

Tip

ServiceContext is a bundle of services and configurations used across a LlamaIndex pipeline, Learn more here.


"I want to use a different vector store"

import chromadb
from llama_index.vector_stores import ChromaVectorStore
from llama_index import StorageContext

chroma_client = chromadb.PersistentClient()
chroma_collection = chroma_client.create_collection("quickstart")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

Tip

StorageContext defines the storage backend for where the documents, embeddings, and indexes are stored. Learn more here.


"I want to retrieve more context when I query"

Tip

as_query_engine builds a default retriever and query engine on top of the index. You can configure the retriever and query engine by passing in keyword arguments. Here, we configure the retriever to return the top 5 most similar documents (instead of the default of 2). Learn more about vector index here.


"I want to use a different LLM"

from llama_index import ServiceContext
from llama_index.llms import PaLM
service_context = ServiceContext.from_defaults(llm=PaLM())

Tip

Learn more about customizing LLMs here.


"I want to use a different response mode"

Tip

Learn more about query engine usage pattern here and available response modes here.


"I want to stream the response back"

Tip

Learn more about streaming here.


"I want a chatbot instead of Q&A"

Tip

Learn more about chat engine usage pattern here.


Next Steps

  • want a thorough walkthrough of (almost) everything you can configure? Try the end-to-end tutorial on basic usage pattern.
  • want more in-depth understanding of specific modules? Check out the module guides 👈