Working through this module?
Module 02 · Page 3 of 20 · 5 sections
LangChain Architecture and Core Components
How LangChain is structured as a bridge between LLMs, data and tools, what each core component does and a simple example that combines them.
Architecture Overview
- LangChain provides a modular architecture to build LLM applications.
- It acts as a bridge between LLMs, external data sources, tools and application logic.
- It provides standard interfaces and abstractions to work with different LLM providers and components.
- We can mix and match components as per our use case (RAG, agents, etc.).
How a Request Flows
- 1User provides a query to the application.
- 2LangChain processes the query using chains, agents or retrievers.
- 3It interacts with LLMs, external data sources, tools and vector stores.
- 4The LLM generates a response using the provided context and tools.
- 5LangChain returns the final response to the user.
Architecture Diagram
User Query
LangChain ApplicationChains / Agents
LLMsOpenAI, Claude, etc.
Vector DatabasesFAISS, Chroma, etc.
External APIsSerpAPI, DB, etc.
ToolsCalculators, search, etc.
Inside the application sit the LangChain components: prompts, chains, agents, memory, document loaders, vector stores and tools.
Core Components and Their Purpose
| Component | Purpose |
|---|---|
| Models | Interface to different LLMs (OpenAI, Anthropic, etc.). |
| Prompts | Manage and create prompts, prompt templates. |
| Chains | Combine multiple steps to create a workflow. |
| Agents | Use tools to take actions and solve complex tasks. |
| Memory | Maintain conversation history and context. |
| Document Loaders | Load data from various sources (PDF, web, DB, etc.). |
| Text Splitters | Split large documents into smaller chunks. |
| Vector Stores | Store and search embeddings for similarity search. |
| Retrievers | Fetch relevant documents from a vector store. |
| Tools | Integrate external tools and APIs. |
| Callbacks | Track and monitor execution (logs, token usage, etc.). |
Simple Example: Using Core Components
python
from langchain_openai import ChatOpenAI from langchain_core.prompts import PromptTemplate from langchain.chains import LLMChain # 1. Define LLM llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7) # 2. Define prompt template template = PromptTemplate( input_variables=["topic"], template="Explain {topic} in simple words." ) # 3. Create chain chain = LLMChain(llm=llm, prompt=template) # 4. Run chain response = chain.invoke({"topic": "LangChain"}) print(response)
- We create an LLM instance (OpenAI).
- Define a prompt template with a variable.
- Create an LLMChain with the model and prompt.
- Invoke the chain with input to get the response.
- We can also use memory, tools, retrievers, etc., for advanced applications.