Menu

AI & Machine Learning Topic

LangChain / LlamaIndex for App Development

Why These Frameworks Exist

Building with LLMs is more than just making API calls. Real applications require:

  • Chaining multiple steps together (retrieve → process → generate)

  • Managing context windows intelligently

  • Connecting to various data sources and tools

  • Maintaining memory across conversations

LangChain and LlamaIndex provide the abstractions and building blocks to do this efficiently.


LangChain: The General-Purpose LLM Framework

Philosophy: "Chains" as the fundamental building block. LangChain is a comprehensive, Swiss Army knife-like framework for building any type of LLM application, with a strong focus on workflow orchestration.

Core Concepts:

1. Components & Abstraction
LangChain provides standardized interfaces for:

  • LLMs (OpenAI, Anthropic, open-source models)

  • Prompt Templates (reusable prompts with variables)

  • Document Loaders (PDFs, websites, databases)

  • Vector Stores (Pinecone, Chroma, etc.)

  • Tools (Google Search, APIs, calculators)

  • Memory (conversation history)

2. Chains
The heart of LangChain. A chain sequences multiple components together.

  • Simple Chain: Prompt → LLM → Output

  • Sequential Chain: Chain 1 → Chain 2 → Chain 3

  • Router Chains: Choose different sub-chains based on input

3. Agents
An AI that can use tools. This is one of LangChain's most powerful concepts.

  • The LLM becomes a reasoning engine that can decide what action to take and in what order.

  • ReAct Framework: Reason → Act → Observe → Repeat

Practical LangChain Examples:

1. Basic Chatbot with Memory

python

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)

# This maintains conversation history automatically
response = chain.run("Hi, I'm John")
response = chain.run("What's my name?") # Remembers name from previous message

2. Tool-Using Agent

python

from langchain.agents import initialize_agent, Tool
from langchain.tools import DuckDuckGoSearchRun

tools = [
    Tool(
        name="Web Search",
        func=DuckDuckGoSearchRun(),
        description="Useful for finding current information"
    )
]

agent = initialize_agent(tools, llm, agent="zero-shot-react-description")
result = agent.run("What's the current price of Bitcoin and what was its price one year ago?")

The agent will reason: "I need to search for current Bitcoin price, then search for Bitcoin price one year ago, then compare them."

3. Multi-Step Workflow Chain

python

# Summarize a document, then write an email based on the summary
from langchain.chains import SimpleSequentialChain

summary_chain = load_summarize_chain(llm, chain_type="map_reduce")
email_chain = LLMChain(llm=llm, prompt=email_prompt)

overall_chain = SimpleSequentialChain(
    chains=[summary_chain, email_chain],
    verbose=True
)

# Takes document → generates summary → writes email from summary
overall_chain.run(long_document)

LlamaIndex: The Data Framework for LLMs

Philosophy: "Your data → LLM context." LlamaIndex is specialized for connecting your private data to LLMs. It excels at RAG and data ingestion pipelines.

Core Concepts:

1. Data Connectors ("LlamaHub")

  • Huge library of connectors for PDFs, Slack, Notion, databases, APIs

  • SimpleDirectoryReader for local files

2. Indexes
Different ways to structure your data for efficient retrieval:

  • Vector Store Index: Standard semantic search (what we discussed in RAG)

  • Tree Index: Hierarchical summary for drilling down into details

  • Keyword Table Index: Traditional keyword-based search

  • Graph Index: For complex relationships

3. Query Engines
Intelligent interfaces for asking questions of your data:

  • Simple Query: Basic question → answer

  • Sub Question Query Engine: Breaks complex questions into simpler sub-questions

  • Router Query Engine: Chooses the best index to query from

Practical LlamaIndex Examples:

1. Basic Knowledge Assistant

python

from llama_index import VectorStoreIndex, SimpleDirectoryReader

# Load and index documents
documents = SimpleDirectoryReader("data/").load_data()
index = VectorStoreIndex.from_documents(documents)

# Create query engine
query_engine = index.as_query_engine()

# Query your data
response = query_engine.query("What are the main safety guidelines in the manual?")
print(response)

2. Complex Query with Reasoning

python

from llama_index import TreeIndex
from llama_index.query_engine import SubQuestionQueryEngine

# Create an index that can handle complex queries
index = TreeIndex.from_documents(documents)
query_engine = SubQuestionQueryEngine.from_defaults(index=index)

# This will be broken down into sub-questions automatically
response = query_engine.query(
    "Compare and contrast the marketing strategy from Q1 with the actual results in Q2"
)

LangChain vs. LlamaIndex: When to Use Which

Aspect LangChain LlamaIndex
Primary Focus Workflow orchestration, tool usage, complex chains Data ingestion, indexing, querying for RAG
Strength Building agents, multi-step reasoning, applications requiring action Getting your data into LLMs, sophisticated RAG, data pipelines
Learning Curve Steeper due to broader scope More focused, easier for data-centric tasks
Best For "Doing things" - chatbots that act, complex workflows "Knowing things" - knowledge bases, document Q&A

How They Work Together

In practice, many applications use both frameworks:

python

# LlamaIndex for data loading and indexing
from llama_index import VectorStoreIndex
documents = SimpleDirectoryReader("data/").load_data()
index = VectorStoreIndex.from_documents(documents)

# LangChain for orchestration and tools
from langchain.agents import Tool

query_engine = index.as_query_engine()
tools = [
    Tool(
        name="Company Knowledge Base",
        func=query_engine.query,
        description="Useful for answering questions about company policies and documents"
    )
]

# Build a LangChain agent that can use the LlamaIndex query engine
agent = initialize_agent(tools, llm, agent="zero-shot-react-description")
result = agent.run(
    "Search our knowledge base for the remote work policy and tell me the main requirements for employees"
)

Real-World Application Patterns

1. Customer Support Chatbot

  • LlamaIndex: Indexes product documentation, FAQs, support tickets

  • LangChain: Orchestrates the conversation flow, handles user authentication, escalates to human agent when confidence is low

2. Research Assistant

  • LlamaIndex: Connects to academic databases, arXiv, internal research papers

  • LangChain: Creates multi-step research workflows: search → summarize → compare → generate report

3. Enterprise Knowledge Worker

  • LlamaIndex: Indexes SharePoint, Slack, Confluence, emails

  • LangChain: Builds agents that can both retrieve information and take actions (schedule meetings, update CRM)

Key Takeaways

  • LangChain is your orchestrator - it's about building the "brain" that decides what to do and when.

  • LlamaIndex is your data specialist - it's about getting your information ready and accessible for the LLM.

  • RAG is the foundational architecture that both frameworks enable beautifully.

  • For most serious applications, you'll likely end up using both.

These frameworks abstract away the enormous complexity of building reliable LLM applications, letting you focus on creating value rather than wrestling with low-level details. They represent the current state-of-the-art in production LLM application development.

Comments (0)

No comments yet. Be the first to comment!

AlphaAI Assistant

Hello! I'm AlphaAI

What would you like to learn today?