Memory Chatbot
This page may contain outdated information. It will be updated as soon as possible.
This flow extends the basic prompting flow to include chat memory for unique SessionIDs.
Prerequisites
Create the memory chatbot project
- From the Langflow dashboard, click New Project.
- Select Memory Chatbot.
- The Memory Chatbot flow is created.
![Docusaurus themed image](/img/memory-chatbot.png)
![Docusaurus themed image](/img/memory-chatbot.png)
This flow creates a basic chatbot with the Chat Input, Prompt, and OpenAI components.
This chatbot is augmented with the Chat Memory component, which stores messages submitted via Chat Input and prepends them to subsequent prompts to OpenAI via {context}
.
The Chat History component gives the OpenAI component a memory of previous questions.
- To create an environment variable for the OpenAI component, in the OpenAI API Key field, click the Globe button, and then click Add New Variable.
- In the Variable Name field, enter
openai_api_key
. - In the Value field, paste your OpenAI API Key (
sk-...
). - Click Save Variable.
- In the Variable Name field, enter
Run the memory chatbot flow
- Click the Run button. The Interaction Panel opens, where you can converse with your bot.
- Type a message and press Enter. The bot will respond according to the template in the Prompt component.
- Type more questions. In the Outputs log, your queries are logged in order. Up to 5 queries are stored by default. Try asking
What is the first subject I asked you about?
to see where the LLM's memory disappears.
Modify the Session ID field to have multiple conversations
SessionID
is a unique identifier in Langchain for a conversation session between a chatbot and a client.
A SessionID
is created when a conversation is initiated, and then associated with all subsequent messages during that session.
In the Memory Chatbot flow you created, the Chat Memory component references past interactions with Chat Input by Session ID. You can demonstrate this by modifying the Session ID value to switch between conversation histories.
- In the Session ID field of the Chat Memory and Chat Input components, change the Session ID value from
MySessionID
toAnotherSessionID
. - Click the Run button to run your flow. In the Interaction Panel, you will have a new conversation. (You may need to clear the cache with the Eraser button).
- Type a few questions to your bot.
- In the Session ID field of the Chat Memory and Chat Input components, change the Session ID value back to
MySessionID
. - Run your flow.
The Outputs log of the Interaction Panel displays the history from your initial chat with
MySessionID
.
Store Session ID as a Langflow variable
To store Session ID as a Langflow variable, in the Session ID field, click the Globe button, and then click Add New Variable.
- In the Variable Name field, enter a name like
customer_chat_emea
. - In the Value field, enter a value like
1B5EBD79-6E9C-4533-B2C8-7E4FF29E983B
. - Click Save Variable.
- Apply this variable to Chat Input.