Hi guys, in the previous video, we used the retrieval QA chain to ask questions
against a vector store, that works well, but has one disadvantage, it fails to
preserve conversational history.
A common requirement for reg or retrieval
-augmented generation apps is support for follow -up questions.
Requirements can contain references to past chat history.
In this video, I ll show you how to save the chat history so that you can ask
follow -up questions.
I am importing chatOpenAI to instantiate
the LLM object.
Instead of using the retrieval QA chain,
we ll use another chain called conversationalRetrievalChain.
I am importing it from langChain .chains
import conversationalRetrievalChain.
This chain is used to have a conversation
based on the retrieved documents.
I will also import the
ConversationBufferMemory class, which acts as a buffer for storing conversation memory.
From langChain .memory import ConversationBufferMemory.
I am creating the LLM object.
I ll use gpt4 -turbo, but you can also use gpt3 .5 -turbo if you are using a
free OpenAI account.
gpt4 -turbo -preview and the temperature
equals 0.
In REG systems, external data is
retrieved and then passed to the LLM when doing the generation step.
I am creating the retriever.
Retriever equals vector store as
retriever and the arguments are search type equals similarity and the search
keywords equals and the key is k and the value lets say 5.
A retriever is a crucial component that helps LLMs find and access relevant information.
It does this by searching for relevant data and retrieving the information.
In this example, the retriever will search by similarity and will retrieve
the top k most similar chunks of data.
I am creating a memory object that will
be passed to the conversational retrieval chain as an argument.
The memory will be automatically updated with the questions and the answers memory
equals conversation memory buffer of memory key equals chat history and the
return messages equals true.
Memory is specifically designed to store
and manage conversation history within the link chain application.
Memory key equals chat history gives your memory a label.
When retrieving or interacting with the stored conversation, you'll use the key
chat history.
I am creating the conversational
retrieval chain.
Equals retriever memory equals memory
chain type equals stuff and the verbose equals true.
Chain type equals stuff means use all of the text from the documents.
I am running the code.
Next, I will define a function called ask
question to make it easier to send the questions.
The parameters will be queue for the
question and the chain.
I am running the chain by calling chain .invoke.
It takes a dict that contains the question as an argument and returns a result.
Just to ensure that data is loaded, splitted into chunks and embedded, I will
copy and paste the code that does that from a previous cell.
I am sending the first question.
It is the same question we asked before.
How many pairs of questions and answers had the stack overflow dataset?
And the result equals ask question of queue and crc, the chain.
And I am printing the result.
I am running the code.
It is running the chain.
Very well.
The answer is in the document and will be returned.
The stack overflow dataset had 8 million
pairs of questions and answers.
Note that the result is a dictionary that
contains 3 key value pairs, the question sent by the user, the chat history and
the answer.
If you only want the answer, use result
of answer.
Let's test if it remembers the last question.
Q equals, multiply that number by 10 and the result equals ask question of queue
and crc.
I am running it.
I am also printing the result.
Note that it knew exactly what I was
referring to.
The result of multiplying the number of
pairs of questions and answers in the dataset which is 8 million by 10 would be
80 million.
Very well.
Let's try another one.
Divide the result by 80 and I am running it.
The result of dividing 80 million by 80 is 1 million.
To display the chat history which contains all the questions and their
answers, iterate over the content of the chat history key like this for item in
result of chat history, print item.
You will see all the questions and the
answers from the conversation.
That's it.
In this video, you learned how to use the conversational retrieval chain and the
conversation buffer memory classes to add memory to your rack system.
In the next video, we will dive deeper into it and see how to use the custom
prompt with prompt templates.