LangChain Memory: Enhancing AI Conversational Capabilities
Tags: llm, machinelearning, ai, vectordatabase
Published: 2024-03-19 07:00:00

Chatbots have existed since the [1960s](https://onlim.com/en/the-history-of-chatbots/) but mostly relied on [basic natural language processing](https://zilliz.com/learn/A-Beginner-Guide-to-Natural-Language-Processing) (NLP). The early chatbots relied on fixed input statements and responses and used probability and fuzzy matching to connect inputs and responses.

The recent advent of LLM ([Large Language Model](https://zilliz.com/glossary/large-language-models-\(llms\))) has been a game changer in the chatbot industry. Modern bots can generate real-time, human-like responses. They also offer advanced capabilities like following instructions and memory buffers for remembering conversation history.

AI memory is a major concept in chatbot development. It allows the model to remember past conversations, maintain context, and deliver present responses accordingly. The ability to maintain conversation memory is what makes modern chatbots more natural and human-like.

[LangChain](https://python.langchain.com/docs/get_started/introduction), a framework for developing LLM-based chatbots, has led the modern LLM revolution. It provides features like integration with state-of-the-art LLMs, prompt templating, and memory buffers and has been pivotal in developing modern LLM applications.

This article will explore the memory capabilities of modern LLMs, using LangChain modules to establish memory buffers and build conversational AI applications. 


## Understanding LangChain

[LangChain](https://zilliz.com/learn/LangChain) provides a suite of tools to develop LLM Chatbots in practical applications. It allows integration with pre-trained models like [ChatGPT](https://zilliz.com/learn/ChatGPT-Vector-Database-Prompt-as-code), external datastore integration, [prompt](https://zilliz.com/glossary/prompt-as-code-\(prompt-engineering\)) templates for response relevance, and memory buffers for developing conversational AI. LangChain treats each feature as a separate module, allowing users to chain these to build a powerful end-to-end chatbot.

One of its most prominent features is the memory modules. Unlike traditional chatbots that struggle with maintaining conversation context, LangChain allows LLMs to maintain a long context window and access AI chat history. In simpler terms, the context window determines how much conversation information the model can retain. This helps the bot answer questions regarding things discussed a few responses back. Memory also allows it to infer details not directly specified in the query.

A LangChain conversational bot can be set up using three primary modules. Let's discuss these in detail.


### ConversationChain

The `ConversationChain` module builds the premise around a conversational chatbot. It accepts crucial parameters, such as a pre-trained LLM, a prompt template, and memory buffer configuration, and sets up the chatbot according to these parameters.

```
from langchain.chains import ConversationChain

# basic initialization of ConversationChain
conversation = ConversationChain(
   llm=llm, verbose=True
)

conversation.predict(input="Hi there!")
```


### ConversationBufferMemory

The most basic type of memory configuration is LangChain. It is passed to the ConversationChain as a parameter and forces the bot to store the entire conversation history for context. When the memory buffer is initialized, the framework sends over the entire conversation history along with the present prompt to be processed. This allows the LLM to remember what was talked about previously and how that impacts the current response.

```
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

# ConversationChain initialization with memory buffer
conversation = ConversationChain(
   llm=llm, verbose=True, memory=ConversationBufferMemory()
)

conversation.predict(input="Hi there!")
```

The memory buffers progressively increase the amount of tokens processed. This leads to slower processing and higher costs as the conversation goes on. The buffer is also limited by the maximum number of tokens an LLM can process (4096 for GPT 3.5-turbo).


### ConversationalRetrievalChain

The `ConversationalRetrievalChain` (CRC) is a more advanced type of memory configuration for a LangChain chatbot. It analyzes the user query, conversation history, and external documents to generate the response. CRC uses the conversation history to rephrase the user query to be more specific to the requirement. Then, it uses the rephrased question to retrieve relevant information documents from an external source (usually a vector store like [Milvus](https://zilliz.com/what-is-milvus) or Zilliz Cloud). Finally, it uses the retrieved information to generate and send back the response.


## Integration of Langchain in Chatbots

LangChain provides all the necessary tools to take the chatbot experience to the next level. It comes packed with support for various popular models, such as GPT and Davinci from OpenAI, [Llama](http://v) from Meta, and Claude from Anthropic. The included models are both open-source and closed-source, offering versatility and function.

The framework allows users to tweak the chatbot using prompt templates to reduce [hallucinations](https://zilliz.com/glossary/ai-hallucination) and improve relevancy. Prompts drive the LLM to generate responses that better suit the user's requirements.

Moreover, LangChain provides memory buffers to improve AI conversational depth. The memory buffers allow the model to process the chat history while answering a query. This way, the model can find relevant information in the chat to formulate a response to the present question. 

Memory enables the chatbot to converse naturally rather than the user having to provide detailed context every time. It also allows users to integrate RAG ([Retrieval Augmented Generation](https://zilliz.com/learn/Retrieval-Augmented-Generation)) to capture information from external documents.


## Real-World Applications

LLM's versatility and LangChain's extensive features have introduced chatbots to real-world applications. Modern chatbots have diverse knowledge, can converse like humans, and can learn new information on the fly. All these factors make them ideal for public-dealing applications, replacing human workers while maintaining the same user experience. 

A few key industry applications include:

- **Educational Tutor**: Chatbots can be trained on subject-specific information. These can then act as teachers or tutors for school and college students. Educational chatbots can teach new topics, answer technical queries, and validate and correct students' answers.

- **Healthcare Assistant**: While many still question AI’s credibility as medical professionals, LLM chatbots can give second opinions. They can store patients' medical histories in memory and answer present health-related queries. Modern multi-modal chatbots can process medical imaging such as X-rays and MRI scans and provide diagnosis.

- **Customer Service:** LLMs can be tasked with customer support in eCommerce stores. They can be linked to vector stores with product-related information for specific knowledge. A customer service chatbot can guide users regarding various products, give recommendations, and be integrated into websites to perform actions like processing refunds.


## Future Directions

There is no stopping the LLM revolution. New LLMs are released daily, each improving upon the predecessor, focusing on better text processing and larger memory depth. The recently released [Claude-3](https://www.anthropic.com/news/claude-3-family) has a context window of 1 Million tokens.

These numbers will only improve as we move towards Artificial General Intelligence (AGI). Modern chatbots will feature end-to-end integration with digital systems, offering them abilities beyond conversation. They will be able to interact directly with the system, change settings, activate workflows, and even fix code-related bugs automatically.

However, frameworks like LangChain will further aid the development of such technologies. LangChain's APIs will be enhanced to provide a more user-friendly, low-code interface. It will also offer integrations with operating systems such as Android or Windows to develop versatile functionality.


## Final Thoughts

LangChain has played a pivotal role in developing LLM-based chatbots. It offers versatile functionality, including integration with pre-trained models, prompt templating and utilizing memory buffers.

The AI memory capabilities of LLMs allow developers to build conversational chatbots. LLMs can process entire chat histories to gain context and formulate relevant responses. It makes the conversation feel more fluid and human-like and improves user experience. Although currently LLMs are limited by the number of tokens they can process in a single query, the capabilities are improving with every update.

The details covered in this article are only the tip of the iceberg. LangChain is a continuously evolving framework that offers much more functionality for deeper development. Readers are advised to walk through the [official documentation](https://python.langchain.com/docs/get_started/introduction) to understand its full capabilities and develop real-world chatbot applications.