Getting Started with LangChain: Building LLM‑Powered Applications
This article introduces LangChain, explains why it’s useful for building applications with large language models, walks through installation, API‑key setup, model and embedding selection, prompt engineering, chaining, memory, agents, and vector‑store indexing, and provides runnable Python code examples throughout.
Since the release of ChatGPT, large language models (LLMs) have become extremely popular, and developers can now leverage pre‑trained LLMs to create personal assistants, custom chatbots, and code or document analysis tools.
LangChain is an open‑source Python framework that simplifies building LLM‑driven applications by providing a unified interface to multiple models, prompt management, memory, indexing, and agent capabilities.
Installation
First, install the package (Python ≥ 3.8.1, < 4.0):
pip install langchainThen import the library:
import langchainAPI Keys
To use proprietary models (e.g., OpenAI, Anthropic) you need an API key. Example for OpenAI:
import os
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"For open‑source models hosted on Hugging Face, set the HuggingFace token similarly:
import os
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "YOUR_TOKEN"Model and Embedding Selection
LangChain supports both proprietary and open‑source models. Example of loading a proprietary LLM:
# Proprietary LLM from e.g. OpenAI
# pip install openai
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")Example of loading an open‑source model from Hugging Face:
# Alternatively, open‑source LLM hosted on Hugging Face
# pip install huggingface_hub
from langchain import HuggingFaceHub
llm = HuggingFaceHub(repo_id="google/flan-t5-xl")Embedding models work similarly:
# Proprietary text embedding model from OpenAI
# pip install tiktoken
from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
# Open‑source embedding model
# pip install sentence_transformers
from langchain.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")Prompt Engineering
Create reusable prompt templates:
from langchain import PromptTemplate
template = "What is a good name for a company that makes {product}?"
prompt = PromptTemplate(input_variables=["product"], template=template)
prompt.format(product="colorful socks")Few‑shot prompts can be built by providing example dictionaries and an example template, then combining them with FewShotPromptTemplate .
Chains
Combine an LLM with a prompt using LLMChain :
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
chain.run("colorful socks")Sequentially chain multiple LLM calls with SimpleSequentialChain :
from langchain.chains import SimpleSequentialChain
second_prompt = PromptTemplate(input_variables=["company_name"],
template="Write a catchphrase for the following company: {company_name}")
chain_two = LLMChain(llm=llm, prompt=second_prompt)
overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)
overall_chain.run("colorful socks")Memory
Maintain conversation history with ConversationChain :
from langchain import ConversationChain
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(input="Alice has a parrot.")
conversation.predict(input="Bob has two cats.")
conversation.predict(input="How many pets do Alice and Bob have?")Agents
Agents can call external tools (e.g., Wikipedia, calculator) based on LLM output:
# pip install wikipedia
from langchain.agents import load_tools, initialize_agent, AgentType
tools = load_tools(["wikipedia", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("When was Barack Obama born? How old was he in 2022?")Indexes and Retrieval
Load external data (e.g., YouTube transcript) and index it with a vector store such as FAISS:
# pip install youtube-transcript-api pytube
from langchain.document_loaders import YoutubeLoader
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
documents = loader.load()
# pip install faiss-cpu
from langchain.vectorstores import FAISS
db = FAISS.from_documents(documents, embeddings)Perform retrieval‑augmented QA:
from langchain.chains import RetrievalQA
retriever = db.as_retriever()
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)
result = qa({"query": "What am I never going to do?"})
print(result["result"])Conclusion
LangChain provides a comprehensive set of building blocks—model wrappers, prompt templates, chains, memory, agents, and indexes—that enable developers to prototype sophisticated LLM applications quickly, though the ecosystem evolves rapidly and examples may become outdated.
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.
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.