How to build chatbot with LangChain
Quick answer
Use
LangChain with the ChatOpenAI class to build a chatbot by combining an LLM model like gpt-4o and optionally a retriever for context. Initialize the chain, pass user messages, and get responses programmatically with clean, modular code.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain-openai langchain-community faiss-cpu
Setup
Install the required packages and set your OpenAI API key as an environment variable.
pip install langchain-openai langchain-community faiss-cpu Step by step
This example shows a simple chatbot using LangChain with ChatOpenAI and an in-memory vector store for retrieval-augmented generation (RAG).
import os
from langchain_openai import ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_core.prompts import ChatPromptTemplate
# Set your OpenAI API key in environment variable OPENAI_API_KEY
# Load documents for context
loader = TextLoader("example_docs.txt")
docs = loader.load()
# Create embeddings and vector store
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(docs, embeddings)
# Initialize chat model
chat = ChatOpenAI(model="gpt-4o", temperature=0)
# Define a retrieval-augmented chat function
def chatbot(query: str) -> str:
# Retrieve relevant docs
relevant_docs = vectorstore.similarity_search(query, k=3)
context = "\n".join([doc.page_content for doc in relevant_docs])
# Prepare prompt with context
prompt_template = "You are a helpful assistant. Use the following context to answer the question.\nContext:\n{context}\nQuestion: {question}\nAnswer:"
prompt = prompt_template.format(context=context, question=query)
# Call chat model
response = chat.invoke([{"role": "user", "content": prompt}])
return response.content
# Example usage
if __name__ == "__main__":
question = "What is LangChain?"
answer = chatbot(question)
print("Q:", question)
print("A:", answer) output
Q: What is LangChain? A: LangChain is a framework for building applications with large language models, enabling chaining of calls, retrieval, and memory.
Common variations
- Use
ChatOpenAIwith different models likegpt-4o-minifor faster, cheaper responses. - Use async calls with
await chat.acall([...])in async functions. - Integrate other retrievers like Pinecone or Chroma for scalable vector search.
- Use streaming responses by setting
stream=Trueinchat.completions.create()calls.
from langchain_openai import ChatOpenAI
import asyncio
async def async_chatbot():
chat = ChatOpenAI(model="gpt-4o-mini", temperature=0)
response = await chat.acall([{"role": "user", "content": "Hello from async LangChain!"}])
print(response.content)
if __name__ == "__main__":
asyncio.run(async_chatbot()) output
Hello from async LangChain!
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If vector search returns no relevant documents, check your document loader and embedding model.
- For slow responses, try smaller models like
gpt-4o-minior reducemax_tokens. - Ensure all imports are from
langchain_openaiandlangchain_communityto avoid deprecated modules.
Key Takeaways
- Use
ChatOpenAIfromlangchain_openaifor clean LLM integration. - Combine vector stores like
FAISSwith LangChain for context-aware chatbots. - Async and streaming support enable responsive and scalable chatbot applications.