i come here from one the course deeplearning offered using the RAG method to chat with the pdf file uploading, it was working fine and giving the response okay with the content i provided , but the thing is if i upload multiple documents i asked the query by mentioning file names it was not giving response. i am struck here any help is grateful. Please look for below code for reference
from langchain.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from langchain.document_loaders import PyPDFLoader
from langchain.prompts import PromptTemplate
persist_directory = ‘docs/chroma/’
embedding = OpenAIEmbeddings(openai_api_key=api_key)
documents =
files_list = [‘Downloads/emaple1.pdf’, ‘Downloads/emaple2.pdf’]
for file_path in files_list:
loader = PyPDFLoader(file_path)
docs = loader.load()
documents.extend(docs)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=250)
docs = text_splitter.split_documents(documents)
vectordb = Chroma.from_documents(documents, persist_directory=persist_directory, embedding=embedding)
llm = ChatOpenAI(openai_api_key=api_key, model_name=llm_name, temperature=0)
file_names = [“emaple1.pdf”, “emaple2.pdf”]
template = “”"Use the following pieces of context from the {file_names} 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. Use three sentences maximum. Keep the answer as concise as possible. Always say “thanks for asking!” at the end of the answer.
Question: {{question}}
Context: {{context}}
Helpful Answer:“”"
template = template.format(
file_names=", ".join(file_names),
)
QA_CHAIN_PROMPT = PromptTemplate(input_variables=[“context”, “question”, “file_names”], template=template)
qa_chain = RetrievalQA.from_chain_type(llm,
retriever=vectordb.as_retriever(),
return_source_documents=True,
chain_type=‘stuff’,
verbose=True,
chain_type_kwargs={“prompt”: QA_CHAIN_PROMPT}
)
question = “What is the name of the files i uploaded ?”
result = qa_chain({“query”: question})
print(result[“result”])