⛓️ Choosing the Right Framework: LangChain's Chain Composability vs. LlamaIndex's Data Indexing Power
Building production-grade Generative AI applications requires robust orchestration frameworks that abstract away low-level API connection logic, prompt template management, and vector store interfacing. LangChain excels at general-purpose chain composability through LCEL (LangChain Expression Language), making it the premier choice for complex multi-step agents, sequential reasoning tasks, and tool-calling loops. Conversely, LlamaIndex (formerly GPT Index) specializes in advanced data ingestion, custom index structures (Vector Store, Summary, Tree, and Knowledge Graph indices), and specialized retrieval mechanisms tailored for large-scale document Q&A. This manual details 10 real-world production engineering workflows combining both frameworks.
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, load_index_from_storage
import os
persist_dir = "./storage_index"
if not os.path.exists(persist_dir):
# 1. Load documents from directory
documents = SimpleDirectoryReader("./enterprise_docs").load_data()
# 2. Build Vector Store Index
index = VectorStoreIndex.from_documents(documents)
# 3. Persist index to disk for production reuse
index.storage_context.persist(persist_dir=persist_dir)
else:
# Load persisted index
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
index = load_index_from_storage(storage_context)
# 4. Create query engine and execute search
query_engine = index.as_query_engine(similarity_top_k=3)
response = query_engine.query("What is the company policy on remote work expense reimbursements?")
print(str(response))
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
# 1. Define Prompt Templates
sentiment_prompt = ChatPromptTemplate.from_messages([
("system", "Analyze the sentiment of the following customer review: Positive, Neutral, or Negative."),
("user", "{review}")
])
response_prompt = ChatPromptTemplate.from_messages([
("system", "Draft a professional customer support email addressing a {sentiment} review."),
("user", "Customer Review: {review}")
])
model = ChatOpenAI(model="gpt-4o", temperature=0.0)
parser = StrOutputParser()
# 2. Construct LCEL Composable Pipeline
# Note: In production, intermediate variables are passed using RunnablePassthrough / dict mappings.
chain = (
{"sentiment": sentiment_prompt | model | parser, "review": lambda x: x["review"]}
| response_prompt
| model
| parser
)
output = chain.invoke({"review": "The product arrived damaged and customer support was unhelpful."})
print(output)
from sqlalchemy import create_engine
from llama_index.core import SQLDatabase, NLSQLTableQueryEngine
from llama_index.llms.openai import OpenAI
# 1. Connect to PostgreSQL database
engine = create_engine("postgresql+psycopg2://user:pass@localhost:5432/enterprise_db")
sql_database = SQLDatabase(engine, include_tables=["orders", "customers"])
# 2. Initialize Text-to-SQL Query Engine
llm = OpenAI(model="gpt-4o", temperature=0.0)
sql_query_engine = NLSQLTableQueryEngine(
sql_database=sql_database,
llm=llm,
tables=["orders", "customers"]
)
# 3. Query relational data in plain English
response = sql_query_engine.query("What was the total revenue generated by customers in California last month?")
print(str(response))
from typing import TypedDict
from langgraph.graph import StateGraph, END
class State(TypedDict):
draft_email: str
approved: bool
def draft_node(state: State):
return {"draft_email": "Dear Customer, your refund of $500 has been processed."}
def human_approval_node(state: State):
# Pauses graph execution until human supervisor approves via UI/API
return {"approved": True}
workflow = StateGraph(State)
workflow.add_node("drafter", draft_node)
workflow.add_node("approver", human_approval_node)
workflow.set_entry_point("drafter")
workflow.add_edge("drafter", "approver")
workflow.add_edge("approver", END)
app = workflow.compile()
print("[✔] LangGraph stateful approval graph compiled.")
from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader
from llama_index.graph_stores.neo4j import Neo4jGraphStore
from llama_index.core import StorageContext
# Connect to Neo4j graph database backend
graph_store = Neo4jGraphStore(
username="neo4j",
password="password",
url="bolt://localhost:7687"
)
storage_context = StorageContext.from_defaults(graph_store=graph_store)
documents = SimpleDirectoryReader("./tech_specs").load_data()
kg_index = KnowledgeGraphIndex.from_documents(
documents,
storage_context=storage_context,
max_triplets_per_chunk=3,
include_embeddings=True
)
query_engine = kg_index.as_query_engine(include_text=True)
response = query_engine.query("How are microservice A and service B connected in the payment pipeline?")
print(str(response))
from langchain_core.tools import tool
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
@tool
def restart_kubernetes_pod(pod_name: str, namespace: str = "default") -> str:
"""Restarts a specified Kubernetes pod in the given namespace."""
# Production subprocess or Kubernetes client call here
return f"Pod '{pod_name}' in namespace '{namespace}' successfully restarted."
tools = [restart_kubernetes_pod]
llm = ChatOpenAI(model="gpt-4o", temperature=0.0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert SRE assistant."),
("user", "{input}"),
("placeholder", "{agent_scratchpad}")
])
agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({"input": "Please restart the payment-api pod in production namespace."})
print(result["output"])
from llama_index.core.selectors import LLMSingleSelector
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core import SummaryIndex, VectorStoreIndex
# Define specialized index backends
summary_index = SummaryIndex.from_documents(documents)
vector_index = VectorStoreIndex.from_documents(documents)
summary_engine = summary_index.as_query_engine(response_mode="tree_summarize")
vector_engine = vector_index.as_query_engine(similarity_top_k=2)
query_engine = RouterQueryEngine(
selector=LLMSingleSelector.from_defaults(),
query_engine_tools=[
Tool(query_engine=summary_engine, description="Useful for summarization questions about the entire corpus."),
Tool(query_engine=vector_engine, description="Useful for specific factual lookups and details.")
]
)
import os
# Enable LangSmith tracing automatically for all LangChain/LCEL invocations
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "lsv2_pt_..."
os.environ["LANGCHAIN_PROJECT"] = "enterprise-rag-production"
# Any subsequent chain.invoke() or agent_executor.invoke() call
# will stream complete telemetry traces, token counts, and latency metrics to LangSmith dashboard.
from llama_index.multi_modal_llms.openai import OpenAIMultiModal
from llama_index.core import SimpleDirectoryReader
# Load documents containing mixed text and embedded figures
image_documents = SimpleDirectoryReader("./architecture_diagrams", required_exts=[".pdf", ".png"]).load_data()
# Query multi-modal index combining text and image embeddings
# Enables users to query architectural diagrams alongside text specifications.
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI(title="Enterprise GenAI Orchestration API", version="1.0.0")
class QueryRequest(BaseModel):
query: str
tenant_id: str
class QueryResponse(BaseModel):
answer: str
sources: list[str]
@app.post("/v1/query", response_model=QueryResponse)
async def query_endpoint(req: QueryRequest):
try:
# Invoke LlamaIndex or LangChain pipeline
# response = query_engine.query(req.query)
return QueryResponse(answer="Mock orchestrated response", sources=["doc_1.pdf"])
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))