Intro to LangChain for AI Apps
Build your first retrieval-augmented chatbot in under an hour with LangChain and OpenAI.
What You'll Learn
- Understand LangChain's core abstractions: chains, prompts, and retrievers
- Build a RAG (Retrieval-Augmented Generation) pipeline
- Load and chunk documents into a vector store
- Query with semantic similarity search
- Wrap your chatbot in a FastAPI REST endpoint
Prerequisites
- Python 3.10+ installed
- An OpenAI API key
- pip / virtualenv basics
Step-by-Step Guide
Step 1
1. Install LangChain & Dependencies
Create a virtual environment and install: pip install langchain langchain-openai chromadb tiktoken fastapi uvicorn python-dotenv. Store your OPENAI_API_KEY in a .env file and load it with python-dotenv. LangChain's modular design means you install only what you need — swap OpenAI for any other LLM provider without changing your chain logic.
Step 2
2. Load & Chunk Your Documents
Use LangChain's document loaders (PyPDFLoader, TextLoader, WebBaseLoader) to ingest your source data. Then split with RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50). Overlap ensures context isn't lost at chunk boundaries. The output is a list of Document objects with page_content and metadata.
Step 3
3. Embed & Store in Chroma
Convert chunks to vector embeddings using OpenAIEmbeddings() and persist to Chroma (a local vector database). vectorstore = Chroma.from_documents(docs, embedding=OpenAIEmbeddings(), persist_directory='./chroma_db'). On subsequent runs, load with Chroma(persist_directory='./chroma_db', embedding_function=...) instead of re-embedding.
Step 4
4. Build the RAG Chain
Create a retriever from the vector store: retriever = vectorstore.as_retriever(search_kwargs={'k': 4}). Build a RetrievalQA chain: qa = RetrievalQA.from_chain_type(llm=ChatOpenAI(model='gpt-4o-mini'), chain_type='stuff', retriever=retriever). 'stuff' puts all retrieved chunks into one prompt — ideal for short contexts.
Step 5
5. Expose via FastAPI
Create a FastAPI app with a POST /chat endpoint that accepts a query string and returns the answer. Run with uvicorn main:app --reload. Add streaming with StreamingResponse and LangChain's streaming callback handlers for a ChatGPT-like typing experience. This is production-ready with a simple gunicorn front in front.
Further Reading & Resources
Ready to put this into practice?
Apply for a real AI & Automation internship and ship production projects with mentorship.
