logasanjeev commited on
Commit
2e07c12
·
verified ·
1 Parent(s): 88ec884

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -8,6 +8,7 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
9
  from langchain.chains import ConversationalRetrievalChain
10
  from langchain.memory import ConversationBufferMemory
 
11
  from pptx import Presentation
12
  from io import BytesIO
13
  import shutil
@@ -37,6 +38,29 @@ chat_history = []
37
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
38
  PERSIST_DIRECTORY = tempfile.mkdtemp() # Use temporary directory for ChromaDB
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Custom PPTX loader
41
  class PPTXLoader:
42
  def __init__(self, file_path):
@@ -164,7 +188,9 @@ def initialize_qa_chain(temperature):
164
  qa_chain = ConversationalRetrievalChain.from_llm(
165
  llm=llm,
166
  retriever=vector_store.as_retriever(search_kwargs={"k": k}),
167
- memory=memory
 
 
168
  )
169
  logger.info(f"Initialized QA chain with {LLM_MODEL} and k={k}.")
170
  return "QA Doctor: QA chain initialized successfully.", None
@@ -261,7 +287,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DocTalk: Document Q&A Chatbot") as
261
  status = gr.Textbox(label="Status", interactive=False)
262
 
263
  with gr.Column(scale=1):
264
- temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.7, label="Temperature")
265
  chunk_size = gr.Slider(minimum=500, maximum=2000, step=100, value=1000, label="Chunk Size")
266
  chunk_overlap = gr.Slider(minimum=0, maximum=500, step=50, value=100, label="Chunk Overlap")
267
  init_button = gr.Button("Initialize QA Chain")
 
8
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
9
  from langchain.chains import ConversationalRetrievalChain
10
  from langchain.memory import ConversationBufferMemory
11
+ from langchain.prompts import PromptTemplate
12
  from pptx import Presentation
13
  from io import BytesIO
14
  import shutil
 
38
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
39
  PERSIST_DIRECTORY = tempfile.mkdtemp() # Use temporary directory for ChromaDB
40
 
41
+ # Custom prompt template
42
+ CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(
43
+ """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question, incorporating relevant context from the conversation.
44
+
45
+ Chat History:
46
+ {chat_history}
47
+
48
+ Follow-up Question: {question}
49
+
50
+ Standalone Question:"""
51
+ )
52
+
53
+ QA_PROMPT = PromptTemplate.from_template(
54
+ """You are a precise and factual assistant. Using the provided context, answer the question by checking if the exact word or phrase asked about is present in the context. If the question asks if a word is mentioned, include cases where the word appears as part of a larger word or phrase (e.g., "hugging" in "hugging face"). Do not make assumptions beyond the context. If the word is not present, say so clearly.
55
+
56
+ Context:
57
+ {context}
58
+
59
+ Question: {question}
60
+
61
+ Answer:"""
62
+ )
63
+
64
  # Custom PPTX loader
65
  class PPTXLoader:
66
  def __init__(self, file_path):
 
188
  qa_chain = ConversationalRetrievalChain.from_llm(
189
  llm=llm,
190
  retriever=vector_store.as_retriever(search_kwargs={"k": k}),
191
+ memory=memory,
192
+ condense_question_prompt=CONDENSE_QUESTION_PROMPT,
193
+ combine_docs_chain_kwargs={"prompt": QA_PROMPT}
194
  )
195
  logger.info(f"Initialized QA chain with {LLM_MODEL} and k={k}.")
196
  return "QA Doctor: QA chain initialized successfully.", None
 
287
  status = gr.Textbox(label="Status", interactive=False)
288
 
289
  with gr.Column(scale=1):
290
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.1, label="Temperature")
291
  chunk_size = gr.Slider(minimum=500, maximum=2000, step=100, value=1000, label="Chunk Size")
292
  chunk_overlap = gr.Slider(minimum=0, maximum=500, step=50, value=100, label="Chunk Overlap")
293
  init_button = gr.Button("Initialize QA Chain")