LangChain
Overview
LangChain is the most popular Python/JS framework for building AI applications: chains, memory, vector stores, and tools.
What It Provides
LangChain connects LLM to the external world: it allows building call chains, connecting RAG, memory, and tools. Ideal for linear scenarios: input → processing → LLM → result.
Typical Examples
- Corporate knowledge base chatbot: documents → chunks → vector DB → search → response.
- Report generator: SQL query → LLM summary → formatting to HTML.
Key Feature
Deterministic logic: if step 1 succeeds, always proceed to step 2. For complex scenarios with loops and self-checks, use LangGraph.
Getting Started
pip install langchain
Minimal chain:
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_template(
"Explain the term {term} in simple words"
)
chain = prompt | llm
print(chain.invoke({"term": "RAG"}))
The | operator (LCEL) combines the prompt and model into a chain that can be invoked with a single command.