Is there a way I can handle context and general questions in Langchain QA Retrieval? – Langchain

by
Alexei Petrov
azure-openai chatgpt-api langchain llama-cpp-python openai-api

Quick Fix: Yes, You can store history by default. If there is no answer in the document store, it fetches the result from the large language model (LLM).

The Problem:

In a chatbot system utilizing Langchain QARetrieval, how can we enable the chatbot to handle context-specific questions and general knowledge queries that might not be directly present in the provided context or vector database?

The Solutions:

Solution 1: Storing the Chat History by Default and Fetching Results from LLM if Not in Doc Store

To handle context and general questions in Langchain QA Retrieval, you can use the following approach:

  1. Implement a Conversation Buffer Memory:

    • Create a conversation buffer memory to store the chat history. This memory will be used to keep track of the conversation and provide context for answering questions.
  2. Configure QA Retrieval:

    • Configure your QA retrieval system to use the conversation buffer memory. This ensures that the system can access the chat history and use it to answer questions.
  3. Set Up the Retrieval Chain:

    • Create a retrieval chain that combines a document retriever and a language model (LLM).
    • The document retriever will search for relevant documents in the vector database.
    • The LLM will be used to generate answers for questions that cannot be answered using the documents.
  4. Specify the Prompt Template:

    • Define a prompt template that instructs the LLM to answer questions from the given context.
    • Include placeholders for the context and the question in the template.
  5. Handle Contextual and General Questions:

    • When a question is asked, the QA retrieval system will first search for relevant documents in the vector database.
    • If no relevant documents are found, the system will use the LLM to generate an answer based on its knowledge and the information in the chat history.
  6. Use the Results:

    • The system will return the answer generated by the LLM as the response to the question.

This approach allows you to answer both contextual questions (using the vector database) and general questions (using the LLM), providing a more comprehensive and informative chatbot experience.

Here is an example code demonstrating the implementation of this approach:

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import ConversationalRetrievalChain,RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.memory import ConversationBufferMemory
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain.prompts import PromptTemplate

loader = TextLoader("fatherofnation.txt")
documents = loader.load()

template = """Answer the question in your own words from the
context given to you.
If questions are asked where there is no relevant context available, please answer from
what you know.

Context: {context}

Human: {question}
Assistant:"""

prompt = PromptTemplate(
    input_variables=["context",  "question"], template=template
)

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(documents)

embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

vectorstore = Chroma.from_documents(documents, embedding_function)

llm = "your llm model here"

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

memory.save_context(
    {"input": "Who is the founder of India?"}, {"output": "Gandhi"}
)

qa = RetrievalQA.from_chain_type(
    llm, retriever=vectorstore.as_retriever(), memory=memory, chain_type_kwargs={'prompt': prompt}
)

question = "What did I ask about India?"
result = qa({"query": question})
print(result)

question1 = "Tell me about google in short ?"
result1 = qa({"query": question1})
print(result1)

This code demonstrates how to use a conversation buffer memory and a retrieval chain to handle both contextual and general questions in a chatbot.

Q&A

How to handle general questions ?

Use LLMs to lookup domain knowledge.

How to get question from context?

Record all the conversation and lookup questions from it.

How to answer questions not in context ?

Use pretrained LLM model.

Video Explanation:

The following video, titled "Development with Large Language Models Tutorial – OpenAI ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Throughout this course, you will complete hands-on projects will help you learn how ... The LangChain Cookbook - Beginner Guide To 7 Essential ...