I was trying to build a RAG LLM model using opensource models. but while generating the response the llm is attaching the entire prompt and relevant document at the output. can anyone please tell me how can I remove the prompt and the Question section and get only the Answer in response ?

Code:

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader(โEM_Theory.pdfโ)

pages = loader.load_and_split()

from langchain.text_splitter import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)

text_chunks = text_splitter.split_documents(pages)

from langchain_community.embeddings import HuggingFaceEmbeddings

embeddings = HuggingFaceEmbeddings(model_name = โsentence-transformers/all-mpnet-base-v2โ)

vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)

from langchain.prompts import PromptTemplate

from langchain.chains import ConversationalRetrievalChain

from langchain.memory import ConversationBufferMemory

prompt_template = PromptTemplate(input_variables=[โchat_historyโ, โquestionโ],

template=โโโGiven the following conversation and a follow up question,

rephrase the follow up question to be a standalone question,

in its original language. Only generate the answer of the asked question.

Donโt generate the contexts and questions in output

\n\nChat History:\n{chat_history}\nFollow Up Input: {question}โโโ)

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

chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type=โstuffโ,condense_question_prompt = prompt_template,

retriever=vector_store.as_retriever(search_kwargs={โkโ: 2}),

memory=memory)

query = โwhat is the Maxwellโs equation?โ

history =

result = chain({โquestionโ: query, โchat_historyโ: history})

history.append((query, result[โanswerโ]))

print(result)

Output:

{โquestionโ: โwhat is the Maxwellโs equation?โ,

โchat_historyโ: [HumanMessage(content=โwhat is the Maxwellโs equation?โ),

AIMessage(content=โUse the following pieces of context to answer the question at the end. If you donโt know the answer, just say that you donโt know, donโt try to make up an answer.\n\nLetโs play physics 9681634157 \n10 \n \n \n \n \nWAVE EQUATION IN FREE SPACE \n Write down Maxwellโs equation in free space. Obtain the wave equation for electric \nfield intensity from them. CU 1010, 09 ,06, 01 \n OR \nShow that Maxwellโs equations suggest propagation of electromagnetic wave in a linear \nhomogeneous dielectric medium having no free charge. CU 2014 \n OR \n \nDerive the expression of speed of light from Maxwellโs equations. CU 2015 \n \n Show that for a plane em wave in free space, the unit vector in the direction of \npropagation the electric and magnetic fields are mutually perpendi cular. 4 \n OR CU2011 ,06 , 05\n\nLetโs play physics 9681634157 \n16 \n ๐ปโ ร๐ปโโ =๐ฝ +๐๐ทโโ \n๐๐ก \n โด ๐ปโ โ(๐ธโ ร๐ปโโ )=๐ปโโ โ(๐ปโ ร๐ธโ )โ๐ธโ โ(๐ปโ ร๐ปโโ ) \n \n=โ๐ปโโ โ๐๐ตโ \n๐๐กโ๐ธโ โ(๐ฝ +๐๐ทโโ \n๐๐ก) \n=โ๐ปโโ โ๐๐ตโ \n๐๐กโ๐ธโ โ๐ฝ โ๐ธโ โ๐๐ทโโ \n๐๐ก \n For a linear medium ๐ทโโ =โ๐ธโ & ๐ตโ =๐๐ปโโ \nโด ๐ปโ โ(๐ธโ ร๐ปโโ )=โ1\n2๐\n๐๐ก(๐ปโโ โ๐ตโ )โ1\n2๐\n๐๐ก(๐ธโ โ๐ทโโ )โ๐ธโ โ๐ฝ \n=โ๐\n๐๐ก(1\n2๐ปโโ โ๐ตโ +1\n2๐ธโ โ๐ทโโ )โ๐ธโ โ๐ฝ \nIntegrating above equations over a volume ๐ bounded by closed surface ๐ and \napplying divergence theorem, \nโฎ(๐ธโ ร๐ปโโ ) \n๐โ๐๐ =โ๐\n๐๐กโซ1\n2(๐ธโ โ๐ทโโ +๐ตโ โ๐ปโโ )๐๐โโซ๐ธโ โ๐ฝ \n๐ฃ \n๐ฃ ๐๐ \n \n๐๐,โฎ(๐ธโ ร๐ปโโ ) \n๐โ๐๐ +โซ๐ธโ โ๐ฝ \n๐ฃ ๐๐=โ๐\n๐๐กโซ1\n2 \n๐ฃ(๐ธโ โ๐ทโโ +๐ตโ โ๐ปโโ )๐๐ \n It is the mathematical form of Poyntingโs theorem. \nLet us now find a physical meaning of this equation. \na. The rate of work done by E.M. force on an element charge ๐๐ (=๐ ๐๐) is given \nby,\n\nQuestion: what is the Maxwellโs equation?\nHelpful Answer: I do not have enough information about Maxellโs equation therefore I cannot provide an answer.โ)],

โanswerโ: โUse the following pieces of context to answer the question at the end. If you donโt know the answer, just say that you donโt know, donโt try to make up an answer.\n\nLetโs play physics 9681634157 \n10 \n \n \n \n \nWAVE EQUATION IN FREE SPACE \n Write down Maxwellโs equation in free space. Obtain the wave equation for electric \nfield intensity from them. CU 1010, 09 ,06, 01 \n OR \nShow that Maxwellโs equations suggest propagation of electromagnetic wave in a linear \nhomogeneous dielectric medium having no free charge. CU 2014 \n OR \n \nDerive the expression of speed of light from Maxwellโs equations. CU 2015 \n \n Show that for a plane em wave in free space, the unit vector in the direction of \npropagation the electric and magnetic fields are mutually perpendi cular. 4 \n OR CU2011 ,06 , 05\n\nLetโs play physics 9681634157 \n16 \n ๐ปโ ร๐ปโโ =๐ฝ +๐๐ทโโ \n๐๐ก \n โด ๐ปโ โ(๐ธโ ร๐ปโโ )=๐ปโโ โ(๐ปโ ร๐ธโ )โ๐ธโ โ(๐ปโ ร๐ปโโ ) \n \n=โ๐ปโโ โ๐๐ตโ \n๐๐กโ๐ธโ โ(๐ฝ +๐๐ทโโ \n๐๐ก) \n=โ๐ปโโ โ๐๐ตโ \n๐๐กโ๐ธโ โ๐ฝ โ๐ธโ โ๐๐ทโโ \n๐๐ก \n For a linear medium ๐ทโโ =โ๐ธโ & ๐ตโ =๐๐ปโโ \nโด ๐ปโ โ(๐ธโ ร๐ปโโ )=โ1\n2๐\n๐๐ก(๐ปโโ โ๐ตโ )โ1\n2๐\n๐๐ก(๐ธโ โ๐ทโโ )โ๐ธโ โ๐ฝ \n=โ๐\n๐๐ก(1\n2๐ปโโ โ๐ตโ +1\n2๐ธโ โ๐ทโโ )โ๐ธโ โ๐ฝ \nIntegrating above equations over a volume ๐ bounded by closed surface ๐ and \napplying divergence theorem, \nโฎ(๐ธโ ร๐ปโโ ) \n๐โ๐๐ =โ๐\n๐๐กโซ1\n2(๐ธโ โ๐ทโโ +๐ตโ โ๐ปโโ )๐๐โโซ๐ธโ โ๐ฝ \n๐ฃ \n๐ฃ ๐๐ \n \n๐๐,โฎ(๐ธโ ร๐ปโโ ) \n๐โ๐๐ +โซ๐ธโ โ๐ฝ \n๐ฃ ๐๐=โ๐\n๐๐กโซ1\n2 \n๐ฃ(๐ธโ โ๐ทโโ +๐ตโ โ๐ปโโ )๐๐ \n It is the mathematical form of Poyntingโs theorem. \nLet us now find a physical meaning of this equation. \na. The rate of work done by E.M. force on an element charge ๐๐ (=๐ ๐๐) is given \nby,\n\nQuestion: what is the Maxwellโs equation?\nHelpful Answer: I do not have enough information about Maxellโs equation therefore I cannot provide an answer.โ}

I have also tried with mistralai/Mistral-7B-Instruct-v0.2 , NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO and mistralai/Mixtral-8x7B-Instruct-v0.1 . but got same kind of result.

langchain Version: 0.1.9

Can anyone solve this issue ?