Srinivasulu kethanaboina commited on
Commit
d7e2267
·
verified ·
1 Parent(s): 5d2f342

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -36,7 +36,7 @@ def data_ingestion_from_directory():
36
  index = VectorStoreIndex.from_documents(documents)
37
  index.storage_context.persist(persist_dir=PERSIST_DIR)
38
 
39
- def handle_query(query):
40
  chat_text_qa_msgs = [
41
  (
42
  "user",
@@ -55,7 +55,13 @@ def handle_query(query):
55
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
56
  index = load_index_from_storage(storage_context)
57
 
58
- query_engine = index.as_query_engine(text_qa_template=text_qa_template)
 
 
 
 
 
 
59
  answer = query_engine.query(query)
60
 
61
  if hasattr(answer, 'response'):
@@ -84,7 +90,7 @@ output_component = gr.Textbox()
84
 
85
  # Function to add chat history to output
86
  def chat_with_history(query):
87
- response = handle_query(query)
88
  chat_history.append((query, response))
89
  return response
90
 
 
36
  index = VectorStoreIndex.from_documents(documents)
37
  index.storage_context.persist(persist_dir=PERSIST_DIR)
38
 
39
+ def handle_query(query, chat_history):
40
  chat_text_qa_msgs = [
41
  (
42
  "user",
 
55
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
56
  index = load_index_from_storage(storage_context)
57
 
58
+ # Use chat history to enhance response
59
+ context_str = ""
60
+ for past_query, response in reversed(chat_history):
61
+ if past_query.strip():
62
+ context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
63
+
64
+ query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
65
  answer = query_engine.query(query)
66
 
67
  if hasattr(answer, 'response'):
 
90
 
91
  # Function to add chat history to output
92
  def chat_with_history(query):
93
+ response = handle_query(query, chat_history)
94
  chat_history.append((query, response))
95
  return response
96