LangChain is an open-source framework for building LLM applications. It wraps common patterns for LLM development and provides standardized interfaces and components on top of various APIs.

Based on DeepLearning.AI’s “LangChain for LLM Application Development” course.

LangChain 架构概览

Models, Prompts, and Parsers

LLMs

LangChain provides a standard interface across LLM providers (OpenAI, Cohere, Hugging Face, etc.).

Prompt Templates

Prompt templates make prompts reusable. Instead of rewriting the same prompt structure, you parameterize the variable parts:

from langchain.prompts import ChatPromptTemplate

template_string = """Translate the text to {style} style:
Text: ```{text}```
"""

prompt_template = ChatPromptTemplate.from_template(template_string)

Why Prompt Templates?

When you ask an LLM similar questions repeatedly, you only need to change the specific portions. This is especially valuable for complex prompts — like building a resume where sections are both independent and reusable.

Memory

Since LLM models have no built-in memory, we need external mechanisms to maintain context:

  • Token buffer memory: Load specific portions of chat history based on token limits
  • Vector storage: Store information as embeddings for semantic retrieval

Tokens

Tokens are the chunks text gets split into for processing. Each model has a context window limit: input + completion = max tokens. Different models encode tokens differently, affecting the token count calculation.

Chains

Chains connect tasks together like links:

  • Simple chain: A linear sequence of LLM interactions
  • Sequential chain: Multiple chains coordinated with different ordering
  • Router chain: Matches tasks to appropriate handlers based on context
  • QA chain: Uses vector data retrieval and storage (RAG pattern)

LangChain QA Chain 工作原理

Evaluation

Evaluating LLM outputs requires visibility into the prompt-response process:

  • Manual evaluation with debug mode
  • LLM-based evaluation (using natural language to assess results)
  • Custom datasets for consistency testing

Agents

Agents extend LLM capabilities like plugins:

  • Built-in agents: Math calculation, Wikipedia access, Python REPL
  • Custom agents: Define your own tool, and LLM decides when to use it

Agents dramatically expand LLM preprocessing power by connecting to external APIs and tools.

Summary

LangChain provides a complete development framework for LLM applications with flexible extensibility. It’s essentially a “Swiss Army knife” for LLM application development — worth learning and adopting.