prithvi1029 commited on
Commit
d047a07
·
verified ·
1 Parent(s): d3a1534

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -4,14 +4,14 @@ from langchain_community.document_loaders import PyPDFLoader
4
  from langchain_text_splitters import RecursiveCharacterTextSplitter
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import FAISS
7
- from langchain.chains import RetrievalQA
8
  from langchain_openai import ChatOpenAI
 
 
 
9
 
10
 
11
- def run_qa(pdf_path, question):
12
- if pdf_path is None or question.strip() == "":
13
- return "Please upload a PDF and enter a question."
14
-
15
  loader = PyPDFLoader(pdf_path)
16
  docs = loader.load()
17
 
@@ -20,20 +20,38 @@ def run_qa(pdf_path, question):
20
 
21
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
22
  vectordb = FAISS.from_documents(chunks, embeddings)
 
23
 
24
  llm = ChatOpenAI(temperature=0)
25
 
26
- qa = RetrievalQA.from_chain_type(
27
- llm=llm,
28
- retriever=vectordb.as_retriever(),
29
- return_source_documents=True
 
 
 
 
 
 
30
  )
31
 
32
- result = qa.invoke({"query": question})
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- answer_text = result.get("result", "")
35
- source_docs = result.get("source_documents", [])
36
- sources = "\n\n".join([d.page_content[:500] for d in source_docs[:2]])
37
 
38
  return f"### Answer\n{answer_text}\n\n---\n### Sources\n{sources}"
39
 
 
4
  from langchain_text_splitters import RecursiveCharacterTextSplitter
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import FAISS
7
+
8
  from langchain_openai import ChatOpenAI
9
+ from langchain_core.prompts import ChatPromptTemplate
10
+ from langchain.chains.combine_documents import create_stuff_documents_chain
11
+ from langchain.chains import create_retrieval_chain
12
 
13
 
14
+ def build_chain(pdf_path: str):
 
 
 
15
  loader = PyPDFLoader(pdf_path)
16
  docs = loader.load()
17
 
 
20
 
21
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
22
  vectordb = FAISS.from_documents(chunks, embeddings)
23
+ retriever = vectordb.as_retriever(search_kwargs={"k": 4})
24
 
25
  llm = ChatOpenAI(temperature=0)
26
 
27
+ prompt = ChatPromptTemplate.from_template(
28
+ """You are a helpful assistant. Answer the question using ONLY the provided context.
29
+ If the answer is not in the context, say you don't know.
30
+
31
+ <context>
32
+ {context}
33
+ </context>
34
+
35
+ Question: {input}
36
+ """
37
  )
38
 
39
+ doc_chain = create_stuff_documents_chain(llm, prompt)
40
+ retrieval_chain = create_retrieval_chain(retriever, doc_chain)
41
+ return retrieval_chain
42
+
43
+
44
+ def run_qa(pdf_path, question):
45
+ if pdf_path is None or question.strip() == "":
46
+ return "Please upload a PDF and enter a question."
47
+
48
+ chain = build_chain(pdf_path)
49
+ result = chain.invoke({"input": question})
50
+
51
+ answer_text = result.get("answer", "")
52
+ ctx_docs = result.get("context", []) or []
53
 
54
+ sources = "\n\n".join([d.page_content[:500] for d in ctx_docs[:2]])
 
 
55
 
56
  return f"### Answer\n{answer_text}\n\n---\n### Sources\n{sources}"
57