LangChain 01 - Getting Started: Quick Hello World Guide
LangChain Overview
LangChain is a framework for building language model applications. It simplifies LLM-based application development through modular design. The framework was developed by Harrison Chase and first released in October 2022, becoming an important tool in AI application development.
Core Features
-
Modular Components
- Provides standardized interfaces and abstraction layers
- Breaks down complex tasks into reusable components
- Includes core function modules like model calling, memory management, data retrieval
-
Chained Calls
- Supports chaining multiple LLM calls into workflows
- Can build complex multi-step reasoning processes
-
Data Augmented Generation
- Integrates external data sources and knowledge bases
- Implements context-based information retrieval and generation
Main Modules
-
Model Interaction
- Supports OpenAI, Anthropic and other mainstream APIs
- Provides unified interface specifications
-
Memory Management
- Short-term memory (conversation state maintenance)
- Long-term memory (vector storage integration)
-
Agent System
- Supports tool calling and decision making
- Configurable action plans and execution strategies
Application Scenarios
-
Intelligent Q&A Systems
- Build Q&A assistants with domain knowledge
- Examples: medical consultation, technical support robots
-
Document Processing
- Long text summarization and analysis
- Contract terms extraction and parsing
-
Data Analysis
- Natural language database queries
- Automated data report generation
Technical Advantages
-
Development Efficiency
- Reduce boilerplate code
- Rapid prototype development
-
Scalability
- Support custom components
- Flexible architecture design
-
Community Ecosystem
- Rich third-party integrations
- Active developer community
Typical Workflow
- Initialize language model
- Configure necessary components (memory, retrievers, etc.)
- Define processing chains or agents
- Execute and optimize applications
LangChain continues to update and iterate. Currently, it supports both Python and JavaScript, providing developers with powerful tools for building next-generation AI applications.
Install Dependencies
pip install --upgrade --quiet langchain-core langchain-community langchain-openai
Write Code
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# API Key
openai_api_key = "sk-xxx"
# If no local proxy, need a machine that can access to help forward
openai_api_base = "http://xxx:3000/v1"
# Template - content in {xxx} can be replaced
prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
model = ChatOpenAI(
openai_api_base=openai_api_base,
openai_api_key=openai_api_key,
model="gpt-3.5-turbo",
)
# Define output
output_parser = StrOutputParser()
# Chain: in order prompt -> model -> output_parser
chain = prompt | model | output_parser
# Execute - replace template
message = chain.invoke({"topic": "cat"})
# Print result
print(message)
LCEL (LangChain Expression Language)
Overview
LCEL (LangChain Expression Language) is a declarative programming language provided by the LangChain framework, specifically designed for building and composing language model applications. It allows developers to define complex workflows through concise expression syntax without writing large amounts of boilerplate code.
Core Features
-
Chain Composition: Supports chaining different components (like LLMs, prompt templates, output parsers, etc.)
-
Declarative Syntax: Uses Pythonic declarative syntax, making code more readable and maintainable
-
Auto Parallelization: Automatically identifies components that can be parallelized
-
Error Handling: Built-in error handling and retry mechanisms
-
Streaming Support: Native support for streaming output processing
Typical Application Scenarios
# Example: Build a simple Q&A chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import Ollama
prompt = ChatPromptTemplate.from_template(
"Answer based on the following context:\n\n{context}\n\nQuestion: {question}"
)
llm = Ollama(model="llama2")
output_parser = StrOutputParser()
chain = prompt | llm | output_parser # Use LCEL's pipe operator to compose components
Advanced Features
- Conditional Branching: Supports executing different paths based on conditions
- Dynamic Routing: Can dynamically select processing paths based on input content
- Batch Processing: Supports batch processing inputs for efficiency
- Memory Functions: Can integrate with conversation memory system
Advantages Comparison
| Feature | Traditional Way | LCEL Way |
|---|---|---|
| Code Amount | Needs lots of boilerplate code | Concise declarative syntax |
| Maintainability | Decreases as complexity increases | Maintains high readability |
| Parallel Optimization | Needs manual implementation | Auto handled |
| Error Handling | Needs separate implementation | Built-in support |
LCEL significantly simplifies LangChain application development by providing standardized component connections, allowing developers to focus on business logic rather than infrastructure code.
Execution Flow
chain = prompt | model | output_parser
The execution order is: prompt → model → output_parser
Running Result
Why was the cat sitting on the computer? Because it wanted to keep an eye on the mouse!