Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources
What is LangChain? The Core LLM Application Framework

What is LangChain? The Core LLM Application Framework

An in-depth look at LangChain, its features, and how it simplifies building LLM-powered applications.

Introduction: The Lego Kit for LLM Apps

In the world of artificial intelligence, building an application that uses a Large Language Model (LLM) is not just about writing a single prompt. Real-world applications, such as a chatbot that can answer questions about your company's documents or an agent that can book flights, require a structured way to connect LLMs to external data, APIs, and other services. This is where LangChain comes in. At its core, LangChain is an open-source framework designed to help developers build and deploy LLM applications with greater ease and flexibility. It provides the "Lego bricks"—the modular components—needed to assemble complex, data-aware, and intelligent systems.

Core Concepts and Components

LangChain is built on a few fundamental abstractions that make it incredibly powerful. By understanding these concepts, you can see how to move from a simple prompt to a sophisticated, production-ready application.

1. Models: A Universal Interface

LangChain provides a standard interface to connect to any LLM. This abstraction allows you to swap between different models—like OpenAI's GPT, Google's Gemini, or Anthropic's Claude—with minimal code changes. This is critical for experimentation, cost management, and avoiding vendor lock-in.

2. Prompts: Dynamic Input Management

Instead of hardcoding text, LangChain uses Prompt Templates to create dynamic prompts. These templates are essential for making your applications reusable and robust. They can automatically insert variables (like a user's question or retrieved data) into a pre-defined prompt structure.

3. Chains: The Building Blocks of Logic

A Chain is the central component for combining different LangChain modules into a single, linear workflow. A simple chain might take a prompt, pass it to an LLM, and then parse the output. A more complex chain could have multiple steps. The newest and most recommended way to build chains is using the LangChain Expression Language (LCEL).

4. Agents and Tools: Decision-Making Capabilities

While a chain follows a fixed sequence, an Agent is a powerful component that can dynamically decide which actions to take. An agent uses an LLM as its "brain" to reason about a task and chooses a specific Tool to use. A tool could be anything from a search engine to a custom function that accesses your company's database. This allows the application to be more flexible and responsive to user input.

5. Retrieval-Augmented Generation (RAG): Connecting LLMs to Your Data

One of LangChain's most popular use cases is RAG, which allows an LLM to answer questions using your private, external data. The process involves several key steps orchestrated by LangChain:

  • Loaders: Components to load data from various sources (e.g., PDFs, web pages, Notion, YouTube).
  • Text Splitters: Algorithms to break down large documents into smaller, more manageable chunks.
  • Vector Stores: A database that stores vector embeddings of your document chunks.
  • Embeddings: Models that convert text into numerical vectors for the vector store.
  • Retrievers: Components that query the vector store to find the most relevant document chunks for a given question.

The retriever then passes these chunks to the LLM, allowing it to generate an answer grounded in your data, preventing hallucinations and ensuring accuracy.

LangChain Expression Language (LCEL)

LCEL is a declarative syntax for composing chains. It's a key feature for building production-grade applications. It allows you to "pipe" components together in a very readable and efficient way. LCEL is designed to be highly composable, making it easy to create complex logic by combining simple building blocks.

Code Example: A Simple LCEL Chain

This example demonstrates how to create a simple chain that takes a user's question, formats it with a prompt, and then passes it to an LLM, all using the intuitive pipe (`|`) syntax.

# Import necessary components from LangChain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# 1. Instantiate the LLM, prompt template, and output parser
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_template("Tell me a short, funny story about a {animal}.")
output_parser = StrOutputParser()

# 2. Build the chain using LCEL's pipe operator
# The user input (e.g., 'dog') flows through the components in order.
chain = prompt | llm | output_parser

# 3. Invoke the chain to get a response
response = chain.invoke({"animal": "penguin"})
print(response)

# Example of a more complex chain with a custom function
def my_custom_function(text):
    return text.upper()

# This chain will capitalize the final output from the LLM
complex_chain = prompt | llm | output_parser | my_custom_function
response_upper = complex_chain.invoke({"animal": "cat"})
print(response_upper)

Putting It All Together: A RAG Application

This full code example shows how LangChain components work together to build a functional Retrieval-Augmented Generation application. It demonstrates the entire process, from data loading to generating a grounded answer.

import os
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate

# Set up your environment variables
# Note: In a real application, you would load these securely.
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"

# 1. Load and process documents from a web page
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
docs = loader.load()

# 2. Split documents into smaller chunks
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)

# 3. Create vector embeddings and a vector store
embeddings = OpenAIEmbeddings()
vector = FAISS.from_documents(documents, embeddings)

# 4. Create a retriever to search the vector store
retriever = vector.as_retriever()

# 5. Define the LLM and the prompt template
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
prompt = ChatPromptTemplate.from_template("""Answer the following question based only on the provided context:

{context}

Question: {input}
""")

# 6. Build the document chain to handle the retrieved context
document_chain = create_stuff_documents_chain(llm, prompt)

# 7. Build the final retrieval chain that connects the retriever and the document chain
retrieval_chain = create_retrieval_chain(retriever, document_chain)

# 8. Invoke the chain with a question
response = retrieval_chain.invoke({"input": "What is ReAct?"})
print(response["answer"])

# The output will be an answer about ReAct based only on the retrieved document,
# demonstrating how LangChain combines retrieval and generation.

Why Use LangChain?

The LangChain framework provides significant advantages for developers:

  • Modularity: Components are interchangeable, so you can easily swap out an embedding model or a vector store without rewriting your entire application.
  • Interoperability: It acts as a bridge, allowing you to connect various LLMs, data sources, and tools in a consistent way.
  • Standardization: It establishes a common language and pattern for building LLM applications, which is essential for collaborative and production-grade development.
  • Abstraction: It simplifies complex processes like RAG and agentic workflows, abstracting away much of the boilerplate code and logic.

In essence, LangChain is the scaffolding that allows developers to build robust, scalable, and intelligent LLM-powered applications that can interact with the outside world, going far beyond a simple API call.

← Back to Articles