Are you still troubled by the uneven quality and poor performance of AI in China?
Then let’s take a look at Developer Cat AI (3in1).
This is an integrated AI assistant that combines GPT-4, Claude3, and Gemini into one.
It covers all models of the three AI tools.
Including GPT-4o and Gemini flash
Now you can have them for only ¥68.
The official value is ¥420+.
Send “Developer Cat” to the backend to start using.
Become a member now to enjoy one-on-one personal service, ensuring your usage is well-supported.
Welcome back to delve deeper into LlamaIndex and Ollama! In Part 1, we introduced the basics of setting up and using these powerful tools for efficient information retrieval. Now, it’s time to explore advanced indexing techniques that will elevate your document processing and querying capabilities.
1. Introduction
Before we continue, let’s quickly review the key points from Part 1:
-
Setting up LlamaIndex and Ollama
-
Creating basic indexes
-
Executing simple queries
In this part, we will dive into different types of indexes, learn how to customize index settings, manage multiple documents, and explore advanced querying techniques. By the end, you will have a deeper understanding of how to leverage LlamaIndex and Ollama for complex information retrieval tasks.
If you haven’t set up the environment yet, be sure to refer to Part 1 for detailed instructions on installing and configuring LlamaIndex and Ollama.
2. Exploring Different Index Types
LlamaIndex offers various types of indexes, each tailored for different use cases. Let’s explore four main types:
2.1 List Index
The list index is the simplest form of index in LlamaIndex. It is an ordered list of text blocks, making it ideal for simple use cases.
from llama_index.core import ListIndex, SimpleDirectoryReader, VectorStoreIndex
from dotenv import load_dotenv
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings
from IPython.display import Markdown, display
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
from llama_index.embeddings.ollama import OllamaEmbedding
import chromadb
from IPython.display import HTML
# make markdown display text color green for all cells
# Apply green color to all Markdown output
def display_green_markdown(text):
green_style = """
<style>
.green-output {
color: green;
}
</style>
"""
green_markdown = f'<div class="green-output">{text}</div>'
display(HTML(green_style + green_markdown))
# set the llm to ollama
Settings.llm = Ollama(model='phi3', base_url='http://localhost:11434',temperature=0.1)
load_dotenv()
documents = SimpleDirectoryReader('data').load_data()
index = ListIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What is llama index used for?")
display_green_markdown(response)
Advantages:
-
Simple and quick to create.
-
Best suited for small document sets.
Disadvantages:
-
Less efficient when handling large datasets.
-
Limited semantic understanding.
2.2 Vector Store Index
The vector store index uses embeddings to create semantic representations of documents, enabling more complex searches.
# Create Chroma client
chroma_client = chromadb.EphemeralClient()
# Define collection name
collection_name = "quickstart"
# Check if the collection already exists
existing_collections = chroma_client.list_collections()
if collection_name in [collection.name for collection in existing_collections]:
chroma_collection = chroma_client.get_collection(collection_name)
print(f"Using existing collection '{collection_name}'.")
else:
chroma_collection = chroma_client.create_collection(collection_name)
print(f"Created new collection '{collection_name}'.")
# Set up embedding model
embed_model = OllamaEmbedding(
model_name="snowflake-arctic-embed",
base_url="http://localhost:11434",
ollama_additional_kwargs={"prostatic": 0},
)
# Load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
# Set up ChromaVectorStore and load in data
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context, embed_model=embed_model
)
# Create query engine and perform query
query_engine = index.as_query_engine()
response = query_engine.query("What is llama index best suited for?")
display_green_markdown(response)
This index type excels in semantic search and scalability, making it ideal for large datasets.
2.3 Tree Index
The tree index organizes information hierarchically, which is beneficial for structured data.
from llama_index.core import TreeIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader('data').load_data()
tree_index = TreeIndex.from_documents(documents)
query_engine = tree_index.as_query_engine()
response = query_engine.query("Explain the tree index structure.")
display_green_markdown(response)
Tree indexes are particularly effective for data with a natural hierarchy, such as organizational structures or taxonomies.
2.4 Keyword Table Index
The keyword table index is optimized for efficient keyword-based retrieval.
from llama_index.core import KeywordTableIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader('data/paul_graham').load_data()
keyword_index = KeywordTableIndex.from_documents(documents)
query_engine = keyword_index.as_query_engine()
response = query_engine.query("What is the keyword table index in llama index?")
display_green_markdown(response)
This index type is very suitable for scenarios where quick lookups based on specific keywords are needed.
3. Customizing Index Settings
3.1 Chunking Strategies
Effective text chunking is crucial for index performance. LlamaIndex offers various chunking methods:
from llama_index.core.node_parser import SimpleNodeParser
parser = SimpleNodeParser.from_defaults(chunk_size=1024)
documents = SimpleDirectoryReader('data').load_data()
nodes = parser.get_nodes_from_documents(documents)
print(nodes[0])
Experiment with different chunking strategies to find the best balance between context retention and query performance.
3.2 Embedding Models
LlamaIndex supports various embedding models. Here’s how to use Ollama for embeddings:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.embeddings.ollama import OllamaEmbedding
embed_model = OllamaEmbedding(
model_name="snowflake-arctic-embed",
base_url="http://localhost:11434",
ollama_additional_kwargs={"mirostat": 0},
)
index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
query_engine = index.as_query_engine()
response = query_engine.query("What is an embedding model used for in LlamaIndex?")
display_green_markdown(response)
Try different Ollama models and adjust parameters to optimize embedding quality for specific use cases.
4. Handling Multiple Documents
4.1 Creating Multi-Document Indexes
LlamaIndex simplifies the process of creating indexes from various types of multiple documents:
txt_docs = SimpleDirectoryReader('data/paul_graham').load_data()
web_docs = SimpleDirectoryReader('web_pages').load_data()
data = txt_docs + web_docs
all_docs = txt_docs + web_docs
index = VectorStoreIndex.from_documents(all_docs)
query_engine = index.as_query_engine()
response = query_engine.query("How do you create a multi-document index in LlamaIndex?")
display_green_markdown(response)
4.2 Cross-Document Queries
To effectively query across multiple documents, you can implement relevance scoring and manage context boundaries:
from llama_index.core import QueryBundle
from llama_index.core.query_engine import RetrieverQueryEngine
retriever = index.as_retriever(similarity_top_k=5)
query_engine = RetrieverQueryEngine.from_args(retriever, response_mode="compact")
query = QueryBundle("How do you query across multiple documents?")
response = query_engine.query(query)
display_green_markdown(response)
5. Conclusion and Next Steps
In the second part of our LlamaIndex and Ollama series, we explored advanced indexing techniques, including:
-
Different index types and their use cases
-
Customizing index settings for optimal performance
-
Handling multiple documents and cross-document queries

If this was helpful to you, don’t rush 😝 to click “Share and Look” before you leave.🫦