Published on: 2024-06-22
π Introduction
When building AI apps that integrate with external data or tools, two popular libraries emerge: LangChain and LlamaIndex. Both help connect LLMs to your data, but they serve slightly different goals.
This blog explains what each library does, includes setup and sample codes, and shows how they differ visually to help you choose wisely.
π Overview Comparison
| Feature | LangChain | LlamaIndex |
|---|---|---|
| Focus | Chains & agents orchestration | Indexing, retrieval, RAG pipelines |
| Design | Modular workflows with tools & agents | Data indexes + query engines |
| Use Cases | Chatbots, AI agents, tool calls | Knowledge retrieval, RAG apps |
| Learning Curve | Steeper, more flexible | Simpler for retrieval tasks |
| Integration | Models, tools, databases, APIs | Data + LLM retrieval integrations |
ποΈ How They Work (Architecture Diagram)
β Explanation:
- LangChain orchestrates prompts, chains, and agents calling tools for complex workflows.
- LlamaIndex indexes your data and retrieves context chunks to enhance LLM responses.
π» Setup Instructions
- Create a new folder for testing:
mkdir ai-tools-test
cd ai-tools-test
- Create and activate a virtual environment (uv preferred for speed, or use venv/pip if uv not available):
Using uv (recommended if installed):
uv venv
source .venv/bin/activate
Or using python venv:
python -m venv .venv
source .venv/bin/activate
- Create a
requirements.txt:
openai
langchain
llama-index
π Note: Adjust packages to match your local or cloud environment versions.
- Install requirements:
Using uv:
uv pip install -r requirements.txt
Or using pip:
pip install -r requirements.txt
π» Sample Code: LangChain
from langchain import OpenAI, LLMChain
from langchain.prompts import PromptTemplate
# Initialize LLM (set your OPENAI_API_KEY in env)
llm = OpenAI()
prompt = PromptTemplate(
input_variables=['name'],
template='Hello {name}, how can I assist you today?'
)
chain = LLMChain(llm=llm, prompt=prompt)
print(chain.run('Alice'))
β Explanation:
- Uses a prompt template with an LLM chain to generate output.
π» Sample Code: LlamaIndex (Updated)
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.settings import Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
# β
Updated usage with working settings
Settings.llm = Ollama(model="llama3:latest", request_timeout=9999.0)
Settings.embed_model = OllamaEmbedding(model_name="mxbai-embed-large:latest", request_timeout=9999.0)
reader = SimpleDirectoryReader(input_dir="./data")
documents = reader.load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What is this dataset about?")
print(response)
β Explanation:
- Uses updated
llama_index.coreimport paths. - Configures Ollama as LLM with embedding model.
- Loads data from
./data, builds the index, and queries context.
ποΈ Folder Structure Example
ai-tools-test/
βββ .venv/
βββ requirements.txt
βββ langchain_test.py
βββ llamaindex_test.py
βββ data/
βββ file1.txt
βββ file2.txt
- langchain_test.py: Contains LangChain example code
- llamaindex_test.py: Contains LlamaIndex example code
- data/sample.txt: Test data file for indexing
π TL;DR
If your AI app needs:
- Complex workflows with agents and tool use β LangChain is ideal.
- Knowledge retrieval with RAG-style context integration β LlamaIndex shines.
Both can integrate with each other too β itβs not always a strict choice.