pradeepsengarr commited on
Commit
6b52351
·
verified ·
1 Parent(s): 8d47fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -429,7 +429,7 @@ from langchain_community.vectorstores import Chroma
429
  from langchain_community.embeddings import SentenceTransformerEmbeddings
430
  from langchain_community.llms import HuggingFacePipeline
431
  from langchain.chains import RetrievalQA
432
- from langchain_community.document_loaders import TextLoader
433
  from langchain.docstore.document import Document
434
 
435
  # --- Streamlit Config ---
@@ -467,7 +467,6 @@ def create_vectorstore(text_chunks, embeddings):
467
  shutil.rmtree(temp_dir)
468
  os.makedirs(temp_dir, exist_ok=True)
469
 
470
- # Wrap each chunk in a Document object
471
  documents = [Document(page_content=chunk) for chunk in text_chunks]
472
  db = Chroma.from_documents(documents, embedding=embeddings, persist_directory=temp_dir)
473
  db.persist()
@@ -493,22 +492,29 @@ def process_question(question, full_text):
493
  retriever = vectorstore.as_retriever()
494
 
495
  llm = load_llm()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
  qa = RetrievalQA.from_chain_type(
497
  llm=llm,
498
  retriever=retriever,
499
  chain_type="stuff",
 
500
  return_source_documents=False,
501
- chain_type_kwargs={
502
- "prompt": f"""You are a helpful assistant. Answer the user's question based only on the provided document content.
503
-
504
- If the answer is clearly stated in the document, respond accurately and directly.
505
-
506
- If not, say "The document does not provide enough information." Do not make things up.
507
-
508
- Question: {question}
509
- Context: {{context}}
510
- Answer:"""
511
- }
512
  )
513
 
514
  return qa.run(question)
@@ -551,4 +557,3 @@ if uploaded_file:
551
  else:
552
  st.info("Upload a PDF to begin.")
553
 
554
-
 
429
  from langchain_community.embeddings import SentenceTransformerEmbeddings
430
  from langchain_community.llms import HuggingFacePipeline
431
  from langchain.chains import RetrievalQA
432
+ from langchain.prompts import PromptTemplate
433
  from langchain.docstore.document import Document
434
 
435
  # --- Streamlit Config ---
 
467
  shutil.rmtree(temp_dir)
468
  os.makedirs(temp_dir, exist_ok=True)
469
 
 
470
  documents = [Document(page_content=chunk) for chunk in text_chunks]
471
  db = Chroma.from_documents(documents, embedding=embeddings, persist_directory=temp_dir)
472
  db.persist()
 
492
  retriever = vectorstore.as_retriever()
493
 
494
  llm = load_llm()
495
+
496
+ # ✅ Custom PromptTemplate
497
+ prompt_template = PromptTemplate(
498
+ input_variables=["context", "question"],
499
+ template="""
500
+ You are a helpful assistant. Answer the user's question based only on the provided document context below.
501
+ If the answer is in the context, answer it accurately. If not, say: "The document does not provide enough information."
502
+
503
+ Context:
504
+ {context}
505
+
506
+ Question:
507
+ {question}
508
+
509
+ Answer:"""
510
+ )
511
+
512
  qa = RetrievalQA.from_chain_type(
513
  llm=llm,
514
  retriever=retriever,
515
  chain_type="stuff",
516
+ chain_type_kwargs={"prompt": prompt_template},
517
  return_source_documents=False,
 
 
 
 
 
 
 
 
 
 
 
518
  )
519
 
520
  return qa.run(question)
 
557
  else:
558
  st.info("Upload a PDF to begin.")
559