Unlocking LangChain: Build End-to-End LLM Apps with Chains, Agents, and Memory
This article introduces LangChain—a modular framework for constructing large‑language‑model applications—covering its core components, asynchronous support, prompt engineering, memory handling, chain and agent workflows, token considerations, embedding techniques, and a step‑by‑step Python example that culminates in a Gradio‑based conversational chatbot.
What Is LangChain?
LangChain is an innovative framework that reshapes how we build applications powered by large language models (LLMs). It connects modular components—models, prompts, memory, chains, and agents—into a single pipeline.
Core Modules
Model : Interfaces to LLMs such as OpenAI GPT‑3/4, Google BERT, LLaMA, etc.
Prompt : Template‑based prompts that can be customized or built from scratch.
Memory : Stores conversation history for stateful interactions.
Chain : Chains multiple components (e.g., Prompt → LLM) into a workflow.
Agent : Dynamically selects tools based on user input and iteratively calls them.
Asynchronous Support
LangChain offers async wrappers for OpenAI, ChatOpenAI, and other models, allowing concurrent LLM calls and better resource utilization.
Example: Simple LLM Call
# Import modules
from langchain.llms import OpenAI
# Use text‑ada‑001 model
llm = OpenAI(model_name="text-ada-001", n=2, best_of=2)
# Ask anything
llm("Tell me a joke")Prompt Templates
from langchain import PromptTemplate
template = """I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product}?"""
prompt = PromptTemplate(input_variables=["product"], template=template)
print(prompt.format(product="colorful socks"))Memory Example
from langchain.memory import ChatMessageHistory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("what's up?")
print(history.messages)Building a Chain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.chains import LLMChain
from langchain import PromptTemplate
human_msg = HumanMessagePromptTemplate(
prompt=PromptTemplate(
template="What is a good name for a company that makes {product}?",
input_variables=["product"]
)
)
chat_prompt = ChatPromptTemplate.from_messages([human_msg])
chat = ChatOpenAI(temperature=0.9)
chain = LLMChain(llm=chat, prompt=chat_prompt)
print(chain.run("colorful socks"))Agent Usage
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run("Who is Leonardo DiCaprio's girlfriend? What is her age raised to the 0.43 power?")Tokens and Models
Tokens are the atomic units processed by LLMs; a typical English token is about four characters. Token limits (e.g., 2049 for GPT‑3, 4096 for GPT‑3.5‑turbo) affect prompt length and cost. The tiktoken library can compute token counts.
Embedding and Vector Stores
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_texts(texts, embeddings)Conversational Retrieval Chain
qa = ConversationalRetrievalChain.from_llm(
ChatOpenAI(temperature=0.8),
vectorstore.as_retriever(),
qa_prompt=QA_PROMPT
)Web UI with Gradio
import gradio as gr
with gr.Blocks() as demo:
gr.Markdown("## Grounding DINO ChatBot")
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
# Define user interaction function here
demo.launch(debug=True)Pricing Insight
Using $0.002 per 1 000 tokens for GPT‑3.5‑turbo, processing roughly 6 667 tokens (≈5 000 words) costs about $0.013, highlighting the importance of token‑efficient prompts and preprocessing.
Summary
LangChain lets developers stitch together LLMs, prompts, memory, chains, and agents to create sophisticated AI applications, while token management and model selection remain crucial for cost‑effective deployment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
