LangChain Memory combined with LLMs: let your chatbot have memory

1. Introduction

In the rapidly evolving field of artificial intelligence, chatbots have become indispensable tools, offering a full range of services from answering customer queries to providing personalized assistance. However, traditional chatbots often fail to remember the context of a conversation, resulting in it sometimes appearing disjointed and impersonal. This is because most chatbots are stateless, treating each user query as an independent interaction without reference to previous exchanges.

In order to solve this limitation and improve the conversation experience, the concept of Langchain conversation memory was introduced. This innovative solution enables chatbots to remember past interactions and generate more relevant responses based on context, creating smoother, more human-like conversations.

In this article we will delve into the memory world of LangChain and explore its various types and functions. We will also introduce how to combine it with Streamlit and OpenAI GPT API to build a smarter and more responsive chatbot.

2. LangChain

LangChain is an open source framework that allows AI developers to combine large language models (LLM) like GPT-4 with external data. It provides packages for Python or JavaScript (TypeScript). With LangChain, you can easily build a chatbot that generates document summaries, saving you time and effort.

LangChain provides support for several main modules, such as models, hints, indexes, etc., which can be used in various ways for different use cases.

Models : Various model types and model integrations supported by LangChain.

Indexing : Language models are often more powerful when combined with your own text data - this module covers best practices for doing so.

Chain : A chain is not just a single LLM call, but a series of calls (whether to LLM or other tools). LangChain provides a standard link interface, many integrations with other tools, and end-to-end chains for common applications.

3. LangChain Memory

LangChain Memory is a tool for managing and manipulating previous chat messages, designed to enable the memory of previous interactions. It provides modular and useful tools that can be easily incorporated into chatbots and other conversational agents.

The main functions of LangChain Memory include:

  • Manage and access different types of Memory : LangChain provides a unified interface that can manage and access different types of memory, including conversation buffer memory, conversation summary memory, etc. This allows developers to easily store and retrieve information from previous conversations.

  • Learn and adapt to new information : LangChain Memory can learn and adapt to new information and leverage previous context to generate more accurate responses. It leverages stored conversation history to provide a more coherent and personalized conversation experience.

  • Integrate with different types of language models : LangChain Memory can be integrated with various types of language models, including pre-trained models such as GPT-3, ChatGPT, and custom models. In this way, developers can choose a suitable language model based on their needs and seamlessly integrate it with LangChain Memory.

4. LangChain Memory Type

This section takes an in-depth look at the various types of memory available in the Langchain library. We'll dive into how each type works by using it with ConversationChain and comparing their tips. We'll also evaluate the pros and cons of each.

This section will delve into the different Memory types available in the LangChain library. We'll break down how each type works and evaluate their respective pros and cons by using tips with ConversationChain and comparing them.

First install and import the following libraries and modules, which will be used in later cases.

pip install langchain
pip install openai
import os 
os.environ['OPENAI_API_KEY'] = "your-openai-api-key"
from langchain.llms import OpenAI
from langchain.chains import ConversationChain

4.1、ConversationBufferMemory

The ConversationBufferMemory mechanism in the LangChain library is a simple and intuitive approach that involves storing each chat interaction directly in a buffer. This allows the LangChain Language Model (LLM) to easily recall conversation history. However, this approach also has some drawbacks. One of them is that high token usage can lead to slower response times and increased computational costs. Additionally, due to LLM token limitations, it is not always possible to store everything as expected.

In LangChain, chains are usually used to decompose tasks and are composed of links. Lang Chain provides ConversationChain, which is specially created for scenarios with some memory concepts. When creating an instance of a class ConversationChain, three parameters must be provided:

llm, specifying the language model used to generate the response;

memory, which determines the type of memory used to store conversation history;

verbose, control whether to print prompts and other information during a call.

After initializing ConversationChainthe instance, we use this predictmethod to simulate a conversation based on the provided user input. For example, here I have three conversations.

Another type of memory is ConversationBufferWindowMemorythat it keeps a list of conversations over time. It only uses the last K interactions. This can be used to keep a sliding window of recent interactions so that the buffer doesn't get too large.

from langchain.memory import ConversationBufferMemory
conversation_with_memory = ConversationChain(
    llm=OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY")), 
    memory=ConversationBufferMemory(), 
    verbose=True
)

conversation_with_memory.predict(input="你好,我是Kevin") 
conversation_with_memory.predict(input="我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识") 
conversation_with_memory.predict(input="我希望你能用我的名字为我的公众号设计一个专业名称") 
conversation_with_memory.predict(input="你还可以给出更多选项吗")

Given below is a sample output of the above simulated conversation. We observed that the responses generated by the AI ​​and user input for each round were stored in memory, and these conversations were stored in memory. The next time it is queried, the in-memory conversation is passed to the AI ​​as context to help generate a more coherent and accurate response.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: 你好,我是Kevin
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:

> Finished chain.


> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:  哇,你真的很棒!我也很喜欢人工智能,我可以为你提供一些有关人工智能的信息,比如最新的研究成果,最新的技术发展,以及有关人工智能的最新新闻。
Human: 我希望你能用我的名字为我的公众号设计一个专业名称
AI:

> Finished chain.


> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:  哇,你真的很棒!我也很喜欢人工智能,我可以为你提供一些有关人工智能的信息,比如最新的研究成果,最新的技术发展,以及有关人工智能的最新新闻。
Human: 我希望你能用我的名字为我的公众号设计一个专业名称
AI:  好的,我可以为你的公众号设计一个专业名称。你可以考虑一些以你的名字开头的名字,比如“Kevin的人工智能之旅”,或者“Kevin的AI世界”。你可以根据你的喜好来选择一个最适合你的名字。
Human: 你还可以给出更多选项吗
AI:  当然可以!你可以考虑一些以你的名字开头的名字,比如“Kevin的AI探索”,“Kevin的AI探究”,“Kevin的AI探索者”,“Kevin的AI探索家”,“Kevin的AI探索之旅”,“Kevin的AI探索世界”,“Kevin的AI探索者之旅”,“Kevin的AI探索家之旅”,“Kevin的AI探索之路”,“Kevin的AI探索之道”等等

> Finished chain.

4.2、ConversationBufferWindowMemory

The ConversationBufferWindowMemory class is a subclass of ConversationMemory that maintains a window memory, stores only the most recent specified number of interactions, and discards the rest. This can help limit the size of memory to suit specific application scenarios.

Using ConversationBufferWindowMemory you can keep only the most recent interactions in the conversation to control the memory size. This is useful in resource-constrained environments or scenarios where old interactions need to be discarded quickly.

from langchain.memory import ConversationBufferWindowMemory
conversation_with_memory = ConversationChain(
    llm=OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY")), 
    memory=ConversationBufferMemory(k=2), 
    verbose=True
)
conversation_with_memory.predict(input="你好,我是Kevin") 
conversation_with_memory.predict(input="我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识") 
conversation_with_memory.predict(input="我希望你能用我的名字为我的公众号设计一个专业名称") 
conversation_with_memory.predict(input="你还可以给出更多选项吗")

Here, we specify the parameter k=2 to indicate that we want to store the last two turns of the conversation in memory. We can see the effective change in memory compared to above in the example prompt below

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: 你好,我是Kevin
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:  哇,你真的很棒!我也很喜欢人工智能,它可以帮助我们解决很多有趣的问题。你最喜欢的人工智能领域是什么?
Human: 我希望你能用我的名字为我的公众号设计一个专业名称
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: 你好,我是Kevin
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:  哇,你真的很棒!我也很喜欢人工智能,它可以帮助我们解决很多有趣的问题。你最喜欢的人工智能领域是什么?
Human: 我希望你能用我的名字为我的公众号设计一个专业名称
AI:  好的,我可以帮你设计一个专业的名称,你想要什么样的名称?
Human: 你还可以给出更多选项吗
AI:  当然可以!我可以给你提供更多的选项,你有什么特别的要求吗?

> Finished chain.

However, this approach is not suitable for retaining long-term memory, but can help us limit the number of tokens used.

4.3、ConversationTokenBufferMemory

The ConversationTokenBufferMemory class is another subclass of ConversationMemory that uses token limits to limit the length of stored messages. Unlike ConversationBufferWindowMemory, ConversationTokenBufferMemory discards interactions based on the number of tokens in the message.

from langchain.llms import OpenAI
from langchain.memory import ConversationTokenBufferMemory
from langchain.chains import ConversationChain
llm=OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY"))  
conversation_with_memory = ConversationChain(
    llm=llm,
    memory=ConversationTokenBufferMemory(llm=llm,max_token_limit=60), 
    verbose=True
)
conversation_with_memory.predict(input="你好,我是Kevin") 
conversation_with_memory.predict(input="我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识") 
conversation_with_memory.predict(input="我希望你能用我的名字为我的公众号设计一个专业名称") 
conversation_with_memory.predict(input="你还可以给出更多选项吗")

Sample output is as follows

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: 你好,我是Kevin
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
AI:  你好Kevin,我是一个AI,很高兴认识你!我可以为你做什么?
Human: 我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: 我希望你能用我的名字为我的公众号设计一个专业名称
AI:

> Finished chain.

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: 你还可以给出更多选项吗
AI:  当然可以!我可以提供更多的选项,以便您可以更好地做出决定。我可以提供更多的信息,以便您可以更好地了解每个选项的优缺点。

> Finished chain.

4.4、ConversationSummaryMemory

ConversationSummaryMemory is a solution to the problem of storing and tracking long conversations. It builds a "running summary" of past interactions by summarizing the interactions between the user and the AI. Compared with ConversationBufferMemory, ConversationSummaryMemory has the following advantages: it avoids the problems of token limit errors and increased computational costs, and it can better remember the original target of the question. However, it is important to note that ConversationSummaryMemory is highly dependent on the summary capabilities of the LLM model passed to it.

from langchain.llms import OpenAI
from langchain.memory import ConversationSummaryMemory
from langchain.chains import ConversationChain
llm=OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY"))  
conversation_with_memory = ConversationChain(
    llm=llm,
    memory=ConversationSummaryMemory(llm=llm), 
    verbose=True
)
conversation_with_memory.predict(input="你好,我是Kevin") 
conversation_with_memory.predict(input="我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识") 
conversation_with_memory.predict(input="我希望你能用我的名字为我的公众号设计一个专业名称") 
conversation_with_memory.predict(input="你还可以给出更多选项吗")

Sample output is as follows

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
The human introduces himself as Kevin and the AI introduces itself as an AI and expresses pleasure in meeting Kevin. The AI then asks what it can do for Kevin, to which Kevin responds that he is an AI enthusiast who enjoys sharing knowledge about AI through public channels. Kevin then requests that the AI design a professional name for his public channel, to which the AI agrees and asks if Kevin has any ideas or specific keywords or themes he would like to include in the name.
Human: 你还可以给出更多选项吗
AI:  当然可以!我可以根据你的关键词和主题来提供更多的选项。你有什么想法或者关键词或主题想要包含在你的公共频道名称中吗

> Finished chain.

4.5、ConversationSummaryBufferMemory

ConversationSummaryBufferMemory is an idea that combines the idea of ​​maintaining a buffer and summarizing conversations. It works by storing recent conversations in a buffer instead of discarding past conversations. It then summarizes these conversations and uses token restrictions to clear them.

By storing the conversation in a buffer, ConversationSummaryBufferMemory preserves the integrity of the conversation without losing any important information. This is useful for scenarios where you need to review and reference past conversations.

By summarizing the conversation, ConversationSummaryBufferMemory can extract and record the main points and information of the conversation, thereby managing and utilizing conversation history more effectively. Summarized conversation information can be more compact and easier to understand, avoiding storing too much information or causing token limit errors.

Finally, ConversationSummaryBufferMemory uses token limiting to clear conversations, meaning only the most recent conversations are kept, allowing for more efficient computation with limited resources. By clearing old conversations, you free up memory and computing resources while still retaining summary information for the conversations.

In general, ConversationSummaryBufferMemory is a method of comprehensively utilizing buffers and summarizing conversation ideas. It can maintain the integrity of the conversation, extract the key points of the conversation, and perform efficient calculation and management under limited resources.

from langchain.llms import OpenAI
from langchain.memory import ConversationSummaryBufferMemory
from langchain.chains import ConversationChain
llm=OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY"))  
conversation_with_memory = ConversationChain(
    llm=llm,
    memory=ConversationSummaryBufferMemory(llm=llm,max_token_limit=60), 
    verbose=True
)
conversation_with_memory.predict(input="你好,我是Kevin") 
conversation_with_memory.predict(input="我是一个人工智能爱好者,喜欢通过公众号分享人工智能领域相关的知识") 
conversation_with_memory.predict(input="我希望你能用我的名字为我的公众号设计一个专业名称") 
conversation_with_memory.predict(input="你还可以给出更多选项吗")

Sample output is as follows

> Entering new  chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential. Kevin introduces himself and the AI expresses its enthusiasm for artificial intelligence and its willingness to listen to Kevin's knowledge. The AI then offers to help Kevin design a professional name for his public account and suggests some ideas or allows Kevin to come up with his own name.
Human: 你还可以给出更多选项吗
AI:  当然可以!我可以给你一些关于你的公共账户名称的想法,比如“Kevin的科技”,“Kevin的创新”,“Kevin的技术”,“Kevin的AI”,或者你可以自己想一个名字。

> Finished chain.

4.6、Entity Memory

Entity Memory is a memory used to store specific entity information. It helps extract entities and their related information by passing LLM as parameter. As the conversation proceeds, Entity Memory gradually accumulates knowledge about these entities. Here we also use ENTITY MEMORY CONVERSATION_TEMPLATE to avoid overriding the ConversationChain's default prompt template. In this way, Entity Memory can effectively store and manage entity information involved in conversations, providing more accurate and rich context for conversations.

from langchain.llms import OpenAI
from langchain.memory import ConversationEntityMemory
from langchain.chains import ConversationChain
from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
llm = OpenAI(temperature=0,openai_api_key=os.getenv("OPENAI_API_KEY"))
conversation_with_memory = ConversationChain(
    llm=llm, 
    verbose=True,
    prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
    memory=ConversationEntityMemory(llm=llm)
)
conversation_with_memory.predict(input="Kevin和QinFen是好朋友,但他们不在同一家公司工作")
conversation_with_memory.predict(input="他们都很喜欢学习,QinFen最近做了一个减肥计划")
conversation_with_memory.predict(input="最近QinFen正在准备公司的人工智能创新案例计划,拿到名字希望能跟Kevin出去庆祝")
conversation_with_memory.predict(input="你对Kevin和QinFen了解多少呢?")

Sample output is as follows

> Entering new  chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.

> Entering new  chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

Context:
{'Kevin': 'Kevin是一个好朋友,他和QinFen经常一起出去玩,但是由于他们在不同的公司工作,他们没有太多机会一起工作。他们都很喜欢学习,而且QinFen最近正在准备公司的人工智能创新案例计划,希望他能拿到名字,然后和Kevin一起出去庆祝,一起享受这个成功的时刻。', 'QinFen': 'QinFen是Kevin的好朋友,他们经常一起出去玩,但是由于他们在不同的公司工作,他们没有太多机会一起工作。QinFen很喜欢学习,并且最近也开始了一个减肥计划,希望他能够成功减肥,变得更健康,同时也正在准备公司的人工智能创新案例计划,希望拿到名字后能和Kevin一起出去庆'}

Current conversation:
Human: Kevin和QinFen是好朋友,但他们不在同一家公司工作
AI:  是的,Kevin和QinFen是很好的朋友,他们经常一起出去玩,但是由于他们在不同的公司工作,他们没有太多机会一起工作。
Human: 他们都很喜欢学习,QinFen最近做了一个减肥计划
AI:  是的,Kevin和QinFen都很喜欢学习,而且QinFen最近也开始了一个减肥计划,希望他能够成功减肥,变得更健康。
Human: 最近QinFen正在准备公司的人工智能创新案例计划,拿到名字希望能跟Kevin出去庆祝
AI:  是的,QinFen最近正在准备公司的人工智能创新案例计划,希望他能拿到名字,然后和Kevin一起出去庆祝,一起享受这个成功的时刻。
Last line:
Human: 你对Kevin和QinFen了解多少呢?
You:  我知道Kevin和QinFen是很好的朋友,他们经常一起出去玩,但是由于他们在不同的公司工作,他们没有太多机会一起工作。我也知道他们都很喜欢学习,而且QinFen最近也开始了一个减肥计划,希望他能够成功减肥,变得更健康,同时也正在准备公司的人工智能创新案例计划,希望

> Finished chain.

5. Build intelligent robots with memory capabilities

Earlier we introduced how to use Streamlit and ChatGPT to build a chatbot. Today we will enhance the memory function of our robot by accessing LangChain Memory to make it closer to the level of human intelligence.

"LangChain Artifact: In just five steps, you can build a custom knowledge chatbot"

"Building interactive chatbots using Langchain, ChatGPT, Pinecone and Streamlit"

5.1. Streamlit interactive robot

First install and import the necessary libraries

from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
import streamlit as st
from streamlit_chat import message
  • langchain.chat_models : This is the module that contains language models that can be used to generate responses. We will use OpenAI's GPT-3.5 Turbo model in our chatbot.

  • langchain.chains : This is a module containing different types of chains that can be used to build session proxies. In this example we will use toConversationChaintrack conversation history and generate responses.

  • langchain.chains.conversation.memory : This module contains different types of memory objects that can be used to store conversation history. In this example, we will useConversationBufferWindowMemorya fixed-length window, which stores conversation history.

  • Streamlit : Streamlit is a Python framework for building interactive data science web applications, while Streamlit Chat is a Python package that provides a chatbot interface for Streamlit applications

Here st.title()a function is used to give the chatbot a name. Use st.text_input()a function to create a text area for user input queries. Initialize the session state variables "past" and "generated" and use them st.session_stateto store queries and generated responses from the ChatGPT API.

This way, even if the user refreshes the page or leaves the application and returns later, the chat history can be retained and displayed to the user in the chat interface.

st.title("Smart ChatBot")
query = st.text_input("Query: ", key="input")

if 'responses' not in st.session_state:
    st.session_state['responses'] = []

if 'requests' not in st.session_state:
    st.session_state['requests'] = []

Next, we need to set up the OpenAI API key and start our LLM using the ChatOpenAI class and ChatGPT (currently using the GPT-3.5 Turbo model)

llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=os.getenv("OPENAI_API_KEY"))

In this code, we create an ConversationBufferWindowMemoryobject and assign it to the session state variable 'buffer_memory'if it does not already exist. ConversationBufferWindowMemory is a memory used to store conversation history in a limited-sized buffer window. The parameter "k" specifies the maximum number of rounds (i.e. exchanges back and forth between the user and the chatbot) that can be stored in the memory buffer window. When the maximum number of rounds is reached, the oldest rounds are removed from memory to make room for new rounds.

if 'buffer_memory' not in st.session_state:
            st.session_state.buffer_memory= ConversationBufferWindowMemory(k=3)

In the next step, we initialize the dialog chain by passing in the necessary parameters. The parameter " llm " specifies the language model used to generate the response, in this case the ChatOpenAI object. The parameter " verbose " is set to True to print session details while debugging.

However, the most critical parameter is " memory ". We pass the object stored in st.session state.buffer memory ConversationBufferWindowMemoryto the memory parameter of the conversation chain ConversationChain. This is done so that the chatbot can store and retrieve previous conversations in its memory buffer.

conversation = ConversationChain(
    llm=llm,
    memory=st.session_state.buffer_memory,
    verbose=True
)

If a user provides input, we will use chain.runthe method to get the response from llm. We then add the user's query to the session state variable st.session_state.requestsand the generated response to st.session_state.responsesso that it is recorded in the chat history.

if query:
    response = conversation.run(query)
    st.session_state.requests.append(query)
    st.session_state.responses.append(response)

Now we come to the final step of displaying the dialog. If st.session_statethere is a generated response in the session state variable, then we will start a for loop. The loop will iterate through the list in order from newest response to oldest response.

For each generated response, we call message()the function twice to display the query asked by the user and llmthe response generated by. Parameters keyare used to uniquely identify each message.

if st.session_state['responses']:

    for i in range(len(st.session_state['responses'])-1, -1, -1):
        message(st.session_state['requests'][i], is_user=True, key=str(i) + '_user')
        message(st.session_state["responses"][i], key=str(i))

At this point, we have successfully built a custom chatbot with session memory and can run the application using the following command.

streamlit run file_name.py

5.2. CLI command line interactive robot

In order to facilitate running a robot with memory capabilities directly in the Google Colab environment, use the following code to run it directly, and enter quit to end the conversation.

from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
import os
import sys

def message(msg, is_user=False, key=None):
    if is_user:
        print(f"User: {msg}")
    else:
        print(f"Bot: {msg}")

print("Smart ChatBot")

if 'responses' not in globals():
    responses = []

if 'requests' not in globals():
    requests = []

llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=os.getenv("OPENAI_API_KEY"))
if 'buffer_memory' not in globals():
    buffer_memory = ConversationBufferWindowMemory(k=3)

conversation = ConversationChain(
    llm=llm,
    memory=buffer_memory,
    verbose=True
)

while True:
    query = input("Query: ")
    if query.lower() == "quit":
        break
    response = conversation.run(query)
    requests.append(query)
    responses.append(response)
    message(query, is_user=True)
    message(response)

Complete code for Jupyter Notebook :

https://github.com/Crossme0809/langchain-tutorials/blob/main/LangChainMemoryChatGPT_ChatBot.ipynb

6. Summary

This article explains the importance of chatbots in the rapidly evolving field of artificial intelligence and the limitations of traditional chatbots. To address these issues, the concept of LangChain conversational memory is introduced, enabling chatbots to remember past interactions and generate more relevant responses.

The article details the memory world and various types and functions of LangChain, and explores how to combine LangChain with Streamlit and OpenAI GPT API to build a smarter and more responsive chatbot.

Finally, the article proposes a method of using LangChain Memory to enhance the memory function of the chatbot, making it closer to the level of human intelligence.

If you are interested in this article and want to learn more about practical skills in the field of AI, you can follow the "Technology Frenzy AI" public account . Here, you can see the latest and hottest articles and practical case tutorials in the AIGC field.

Guess you like

Origin blog.csdn.net/FrenzyTechAI/article/details/132696428