Artificial Intelligence 21 min read

Introduction to LangChain: Building LLM Applications with Chains, Agents, and Memory

LangChain is an innovative framework that simplifies building language‑model‑driven applications by providing modular components such as models, prompts, memory, chains, and agents, with examples of asynchronous LLM calls, custom prompt templates, vector stores, token handling, and a simple Gradio chatbot implementation.

Architect
Architect
Architect
Introduction to LangChain: Building LLM Applications with Chains, Agents, and Memory

LangChain is a framework that enables developers to create end‑to‑end applications powered by large language models (LLMs). It connects modular components—Model, Prompt, Memory, Chain, and Agent—into a chainable structure, allowing asynchronous calls, custom prompt templates, and integration with vector stores.

Model module covers various LLMs such as OpenAI GPT‑3, GPT‑4, Google BERT, LaMDA, PaLM, and Meta LLaMA. LangChain provides async support for OpenAI, PromptLayerOpenAI, ChatOpenAI, and Anthropic, and lets users wrap custom LLMs.

Prompt module lets users define # Importing modules from langchain.llms import OpenAI # Here we are using text-ada-001 but you can change it llm = OpenAI(model_name="text-ada-001", n=2, best_of=2) # Ask anything llm("Tell me a joke") and use PromptTemplate to create reusable prompt blueprints.

Memory module provides stateless default behavior but offers tools like from langchain.memory import ChatMessageHistory history = ChatMessageHistory() history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages to retain conversation history for chatbots.

Chain module combines components; for example, an LLMChain formats input with a PromptTemplate and sends it to an LLM. A more complex chain can chain multiple LLM calls, as shown in the example that builds a naming‑consultant prompt and runs it through ChatOpenAI :

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)

human_message_prompt = HumanMessagePromptTemplate(
    prompt=PromptTemplate(
        template="What is a good name for a company that makes {product}?",
        input_variables=["product"],
    )
)
chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])
chat = ChatOpenAI(temperature=0.9)
chain = LLMChain(llm=chat, prompt=chat_prompt_template)
print(chain.run("colorful socks"))

Agent module enables dynamic tool usage based on user input. An example loads tools and creates a zero‑shot REACT agent:

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 Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")

The article also explains token concepts: tokens are sub‑word units used by LLMs; token counting affects cost and model limits. It provides a Python function using tiktoken to count tokens and shows how to compute total token usage.

Model selection and pricing are discussed, comparing GPT‑3, GPT‑3.5‑turbo, and GPT‑4. Example calculations illustrate that 5,000 words (~6,667 tokens) cost roughly $0.0133 with the gpt-3.5-turbo pricing of $0.002 per 1,000 tokens.

Embedding creation is covered: text is split, embedded via OpenAIEmbeddings , and stored in a vector database such as FAISS. A QA chain is built using ConversationalRetrievalChain with a custom prompt template, enabling question answering over custom documents.

Finally, a simple Gradio web UI is presented to interact with the QA chain:

# Front end web app
import gradio as gr
with gr.Blocks() as demo:
    gr.Markdown("## Grounding DINO ChatBot")
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("Clear")
    chat_history = []

    def user(user_message, history):
        response = qa({"question": user_message, "chat_history": history})
        history.append((user_message, response["answer"]))
        return "", history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
    clear.click(lambda: None, None, chatbot, queue=False)
    if __name__ == "__main__":
        demo.launch(debug=True)

The article concludes that understanding LangChain’s modules, token management, and model trade‑offs is essential for building cost‑effective LLM pipelines.

PythonAILLMPrompt EngineeringLangChainchatbot
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.