- Description: I have added an example showing how to pass a custom
template to ConversationRetrievalChain. Instead of
CONDENSE_QUESTION_PROMPT we can pass any prompt in the argument
condense_question_prompt. Look in Use cases -> QA over Documents -> How
to -> Store and reference chat history,
- Issue: #8864,
- Dependencies: NA,
- Tag maintainer: @hinthornw,
- Twitter handle:
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
query = "What did the president say about Ketanji Brown Jackson"
result = qa({"question": query})
```
```python
result["answer"]
```
@ -78,13 +71,11 @@ result["answer"]
</CodeOutputBlock>
```python
query = "Did he mention who she succeeded"
result = qa({"question": query})
```
```python
result['answer']
```
@ -101,21 +92,18 @@ result['answer']
In the above example, we used a Memory object to track chat history. We can also just pass it in explicitly. In order to do this, we need to initialize a chain without any memory object.
Here's an example of asking a question with no chat history
```python
chat_history = []
query = "What did the president say about Ketanji Brown Jackson"
result = qa({"question": query, "chat_history": chat_history})
```
```python
result["answer"]
```
@ -130,14 +118,12 @@ result["answer"]
Here's an example of asking a question with some chat history
```python
chat_history = [(query, result["answer"])]
query = "Did he mention who she succeeded"
result = qa({"question": query, "chat_history": chat_history})
```
```python
result['answer']
```
@ -154,12 +140,10 @@ result['answer']
This chain has two steps. First, it condenses the current question and the chat history into a standalone question. This is necessary to create a standanlone vector to use for retrieval. After that, it does retrieval and then answers the question using retrieval augmented generation with a separate model. Part of the power of the declarative nature of LangChain is that you can easily use a separate language model for each call. This can be useful to use a cheaper and faster model for the simpler task of condensing the question, and then a more expensive model for answering the question. Here is an example of doing so.
query = "What did the president say about Ketanji Brown Jackson"
result = qa({"question": query, "chat_history": chat_history})
```
```python
chat_history = [(query, result["answer"])]
query = "Did he mention who she succeeded"
result = qa({"question": query, "chat_history": chat_history})
```
## Using a custom prompt for condensing the question
By default, ConversationalRetrievalQA uses CONDENSE_QUESTION_PROMPT to condense a question. Here is the implementation of this in the docs
```python
from langchain.prompts.prompt import PromptTemplate
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.
But instead of this any custom template can be used to further augment information in the question or instruct the LLM to do something. Here is an example
```python
from langchain.prompts.prompt import PromptTemplate
```
```python
custom_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. At the end of standalone question add this 'Answer the question in German language.' If you do not know the answer reply with 'I am sorry'.
query = "What did the president say about Ketanji Brown Jackson"
result = qa({"question": query})
```
```python
query = "Did he mention who she succeeded"
result = qa({"question": query})
```
## Return Source Documents
You can also easily return source documents from the ConversationalRetrievalChain. This is useful for when you want to inspect what documents were returned.
You can also easily return source documents from the ConversationalRetrievalChain. This is useful for when you want to inspect what documents were returned.