LangChain

LangChain is a framework designed to simplify the development of applications that use Large Language Models (LLMs). It provides tools to integrate LLMs with external data sources, memory, retrieval-augmented generation (RAG), agents, and chains of operations.
Author

Benedict Thekkel

1. What is LangChain?

LangChain enables developers to build powerful applications that leverage LLMs like OpenAI’s GPT, Cohere, Mistral, DeepSeek, and more.

Why Use LangChain?

  • Simplifies LLM integration with APIs, documents, and databases.
  • Provides modular components for easy prototyping and scaling.
  • Supports memory and state management for multi-turn conversations.
  • Built-in retrieval and knowledge augmentation for reducing hallucinations.
  • Enables reasoning and action using LLM Agents.

2. Core Components of LangChain

LangChain consists of six primary components:

Component Description
LLMs Interface for calling language models (GPT-4, LLaMA, Claude, etc.)
Chains Sequences of operations that process inputs and outputs
Agents AI decision-making system that dynamically selects tools
Memory Stores conversation history and user state
Retrieval Enables RAG with vector databases
Tools & Utilities Functions like Google Search, APIs, or code execution

3. Setting Up LangChain

Install LangChain and Dependencies

pip install langchain openai chromadb faiss-cpu tiktoken

Set Up API Keys (Example: OpenAI)

import os
os.environ["OPENAI_API_KEY"] = "your-api-key"

4. Using LLMs in LangChain

Basic Example with OpenAI’s GPT-4

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(model_name="gpt-4", temperature=0.7)
response = llm.predict("What is LangChain?")
print(response)

Using LLaMA or Other Local Models

from langchain.llms import Ollama

llm = Ollama(model="llama2")
response = llm("Tell me about AI applications.")
print(response)

5. Chains: Creating Pipelines

A Chain is a sequence of operations (e.g., input → LLM → output).

Basic LLM Chain

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

prompt = PromptTemplate.from_template("What are the top 3 applications of {technology}?")
chain = LLMChain(llm=llm, prompt=prompt)

response = chain.run(technology="Machine Learning")
print(response)

Sequential Chains (Multiple Steps)

from langchain.chains import SimpleSequentialChain

chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Explain {concept}"))
chain2 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize in one line: {text}"))

pipeline = SimpleSequentialChain(chains=[chain1, chain2])
response = pipeline.run("Quantum Computing")
print(response)

6. Agents: Dynamic AI with Tool Use

Agents allow an LLM to choose tools dynamically rather than following a fixed flow.

Basic Agent with OpenAI Functions

from langchain.agents import AgentType, initialize_agent
from langchain.tools import Tool
from langchain.chat_models import ChatOpenAI

def search_tool(query):
    return f"Searching for {query} on the web..."

tools = [Tool(name="WebSearch", func=search_tool, description="Searches the web")]

agent = initialize_agent(
    tools=tools,
    llm=ChatOpenAI(model="gpt-4"),
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

response = agent.run("What is the latest AI research?")
print(response)

Self-Reflecting AI Agent

from langchain.agents import OpenAIFunctionsAgent
from langchain.agents.agent import AgentExecutor

agent = OpenAIFunctionsAgent.from_llm(llm=ChatOpenAI(model="gpt-4"))
executor = AgentExecutor(agent=agent, tools=tools)

response = executor.invoke("Find the latest AI papers.")
print(response)

7. Memory and State Management

By default, LLMs do not remember past interactions. Memory in LangChain enables persistence.

Adding Memory to Conversations

from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)

print(conversation.run("Hello, who are you?"))
print(conversation.run("Can you summarize our conversation?"))

Different Memory Types

Memory Type Use Case
ConversationBufferMemory Stores complete conversation history
ConversationSummaryMemory Summarizes past interactions
VectorStoreRetrieverMemory Uses embeddings to store long-term memory

8. Retrieval-Augmented Generation (RAG)

RAG combines vector search with LLMs to reduce hallucinations.

Loading a Document and Storing Embeddings

from langchain.document_loaders import TextLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

# Load and split documents
loader = TextLoader("docs.txt")
documents = loader.load()

# Store as vector embeddings
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(documents, embeddings)

Retrieving and Passing to LLM

retriever = vectorstore.as_retriever()
retrieved_docs = retriever.get_relevant_documents("What is AI?")

9. LangChain Tools and Integrations

LangChain connects with APIs, databases, and search engines.

Google Search API

from langchain.tools import Tool
from langchain.utilities import GoogleSearchAPIWrapper

search = GoogleSearchAPIWrapper()
tool = Tool(name="Google Search", func=search.run)

result = tool.run("Latest AI news")
print(result)

Code Execution

from langchain.tools import PythonREPLTool

python_tool = PythonREPLTool()
print(python_tool.run("import math; math.sqrt(16)"))

10. Scaling and Optimization

Optimizing Token Usage

import tiktoken

encoder = tiktoken.get_encoding("cl100k_base")
print(len(encoder.encode("This is a test message.")))  # Token count

Using Local LLMs Instead of API Calls

For privacy or cost efficiency, use models like LLaMA or Mistral:

from langchain.llms import Ollama
local_llm = Ollama(model="mistral")

Batch Processing for Efficiency

responses = llm.batch(["Explain AI", "Define RAG", "What is LangChain?"])
print(responses)

11. Example Architectures and Use Cases

Use Case LangChain Components Used
AI-Powered Chatbots Memory, Chains, LLMs
Retrieval-Augmented QA Systems Retrieval, Vector Stores, LLMs
AI Agents for Research Agents, Tools, API Integrations
Automated Code Generation LLMs, Python Execution Tools
Personalized Assistants Memory, State Tracking

Final Thoughts

  • LangChain simplifies LLM development by abstracting complex workflows.
  • It integrates retrieval, memory, and agents for dynamic applications.
  • It supports local and cloud-based LLMs, ensuring flexibility.
  • Optimizing context size, retrieval methods, and caching improves efficiency.

LangChain is the foundation for building advanced, production-ready AI applications with modular, scalable components.

Back to top