Getting Started with LangChain: A Practical Guide

Learn how to build your first LLM-powered application using LangChain. From chains to agents, this hands-on guide covers everything you need to get started.

·8 min read

Why LangChain?

LangChain has become the de facto standard for building LLM-powered applications in Python. It provides a modular framework that abstracts away the complexity of working with language models, allowing you to focus on building your application logic.

In this guide, we'll build a simple Q&A bot that can answer questions about your own documents.

Setting Up Your Environment

First, create a new Python project and install the dependencies:

pip install langchain langchain-openai chromadb python-dotenv

Create a .env file with your OpenAI API key:

OPENAI_API_KEY=your-key-here

Building Your First Chain

The simplest LangChain application is a chain — a sequence of operations that process input and produce output.

from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o", temperature=0)

prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful AI assistant."), ("user", "{input}") ])

chain = prompt | llm response = chain.invoke({"input": "What is LangChain?"}) print(response.content)

Working with Documents

LangChain's real power comes from its document handling capabilities. Let's load a PDF and ask questions about it:

from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma

# Load and split the document loader = PyPDFLoader("your-document.pdf") documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200 ) chunks = text_splitter.split_documents(documents)

# Create vector store embeddings = OpenAIEmbeddings() vectorstore = Chroma.from_documents(chunks, embeddings)

Building a RAG Pipeline

Now let's combine everything into a Retrieval-Augmented Generation (RAG) pipeline:

from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

retriever = vectorstore.as_retriever()

qa_prompt = ChatPromptTemplate.from_messages([ ("system", "Answer based on the provided context."), ("user", "Context: {context}\n\nQuestion: {input}") ])

document_chain = create_stuff_documents_chain(llm, qa_prompt) retrieval_chain = create_retrieval_chain(retriever, document_chain)

response = retrieval_chain.invoke({ "input": "What are the key findings?" })

Next Steps

Now that you've built your first RAG application, try:

- Adding memory for multi-turn conversations - Using different document loaders (web pages, databases, APIs) - Implementing streaming responses for better UX - Adding error handling and fallbacks

LangChain's ecosystem is vast, but starting with these fundamentals will give you a solid foundation for building more complex applications.

Ad Unit Placeholder

Related Articles