Introduction
Building an AI chatbot that actually works for your business takes more than calling the OpenAI API. You need conversation memory, grounding in your own data, and a sensible way to handle edge cases. LangChain gives you the scaffolding for all of that — though you'll still write the parts that matter most yourself.
In this guide, we'll build a production-ready chatbot using LangChain, OpenAI GPT-4, and Next.js with Retrieval-Augmented Generation (RAG) so your bot answers from your business's actual documents — not just whatever the model picked up in training.
What You'll Build
- A Next.js API route that handles chat messages
- A LangChain conversation chain with memory
- A RAG pipeline using Pinecone as a vector store
- A simple React chat UI
Prerequisites
- Node.js 18+
- OpenAI API key
- Pinecone account (free tier works)
Step 1: Install Dependencies
npm install langchain @langchain/openai @langchain/pinecone @pinecone-database/pinecone
Step 2: Set Up the RAG Pipeline
Create src/lib/chatbot.ts to initialise your LangChain chain with Pinecone retrieval and conversation memory.
The chain uses ConversationalRetrievalQAChain, which retrieves the top 4 relevant document chunks from Pinecone, injects them into the prompt, and passes conversation history through BufferMemory.
Step 3: Create the API Route
Create src/app/api/chat/route.ts as a Next.js API route that accepts POST requests with a message field and returns the AI response.
Step 4: Ingest Your Documents
Before users can chat, embed your documents into Pinecone: use RecursiveCharacterTextSplitter to chunk the text, OpenAIEmbeddings to create vectors, and PineconeStore.fromDocuments to store them. Tune the chunk size to your content — there's no universal right answer, and we've ended up adjusting it on almost every project.
Key Takeaways
- RAG grounds your chatbot in real business data, which is the single biggest defence against hallucinated answers
BufferMemorygives the chatbot conversation history within a session — fine for dev, not enough for production- For production, store conversation history in Redis or a database rather than in-memory, or you'll lose context every time the process restarts
- Woyce Technologies builds custom LangChain chatbots — contact us if you'd like to talk through what's right for your use case