tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > LangChain > LangChain Conversational RAG with Chat History

LangChain Conversational RAG with Chat History

Author: Venkata Sudhakar

ShopMax India's customer support chatbot needs to handle multi-turn conversations where follow-up questions reference earlier context. For example, after asking 'What are the specs of the Samsung Galaxy S24?', the customer might ask 'Does it support 5G?' - the second question only makes sense in the context of the first. LangChain's Conversational RAG chain solves this by reformulating follow-up questions into standalone questions before retrieval.

Conversational RAG combines a history-aware retriever with a question-answer chain. The history-aware retriever uses the LLM to rewrite the current question using prior chat messages into a standalone question that can be answered without conversation context. This rewritten question is then used to retrieve documents, which are passed to the QA chain along with the original chat history to generate the final answer.

The example below builds a conversational product support bot for ShopMax India that answers product questions and handles follow-up queries using chat history.


It gives the following output,

Q1: The Samsung Galaxy S24 has a 6.2-inch display, Exynos 2400 chip, 8GB RAM, 128GB storage, 4000mAh battery with 25W charging, and a 50MP main camera with 3x optical zoom.
Q2: Yes, the Samsung Galaxy S24 supports 5G connectivity.

In production, store chat history in a session-based store like Redis keyed by session ID rather than in-memory lists. Limit history length by trimming older messages to avoid exceeding the context window. The contextualize step adds one extra LLM call per turn, so monitor latency in high-volume scenarios. For ShopMax India support, partition knowledge bases by product category so retrieval is faster and more accurate.


 
  


  
bl  br