Shreyas094 commited on
Commit
c21734b
·
verified ·
1 Parent(s): fe1ab08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -48,6 +48,7 @@ class EnhancedContextDrivenChatbot:
48
  self.history = []
49
  self.history_size = history_size
50
  self.entity_tracker = {}
 
51
 
52
  def add_to_history(self, text):
53
  self.history.append(text)
@@ -61,8 +62,12 @@ class EnhancedContextDrivenChatbot:
61
  self.entity_tracker[ent.label_] = set()
62
  self.entity_tracker[ent.label_].add(ent.text)
63
 
 
 
 
 
64
  def get_context(self):
65
- return " ".join(self.history)
66
 
67
  def is_follow_up_question(self, question):
68
  doc = nlp(question.lower())
@@ -87,12 +92,27 @@ class EnhancedContextDrivenChatbot:
87
  # Calculate similarity
88
  similarity = cosine_similarity([context_embedding], [question_embedding])[0][0]
89
 
90
- # If similarity is low, it might be a new topic
91
- if similarity < 0.3: # This threshold can be adjusted
92
- return question
 
 
 
 
 
 
 
93
 
94
- # Otherwise, prepend the context
95
- return f"{combined_context} {question}"
 
 
 
 
 
 
 
 
96
 
97
  def process_question(self, question):
98
  contextualized_question = self.get_most_relevant_context(question)
@@ -100,10 +120,9 @@ class EnhancedContextDrivenChatbot:
100
  # Extract topics from the question
101
  topics = self.extract_topics(question)
102
 
103
- # Check if it's a follow-up question
104
  if self.is_follow_up_question(question):
105
- # If it's a follow-up, make sure to include previous context
106
- contextualized_question = f"{self.get_context()} {question}"
107
 
108
  # Add the new question to history
109
  self.add_to_history(question)
@@ -336,7 +355,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
336
  formatted_prompt = prompt_val.format(
337
  context=context_str,
338
  conv_context=chatbot.get_context(),
339
- question=question,
340
  topics=", ".join(topics),
341
  entities=json.dumps(serializable_entity_tracker)
342
  )
@@ -356,6 +375,9 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
356
  sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
357
  answer += sources_section
358
 
 
 
 
359
  return answer
360
 
361
  else: # PDF document chat
 
48
  self.history = []
49
  self.history_size = history_size
50
  self.entity_tracker = {}
51
+ self.conversation_context = ""
52
 
53
  def add_to_history(self, text):
54
  self.history.append(text)
 
62
  self.entity_tracker[ent.label_] = set()
63
  self.entity_tracker[ent.label_].add(ent.text)
64
 
65
+ # Update conversation context
66
+ self.conversation_context += f" {text}"
67
+ self.conversation_context = ' '.join(self.conversation_context.split()[-100:]) # Keep last 100 words
68
+
69
  def get_context(self):
70
+ return self.conversation_context
71
 
72
  def is_follow_up_question(self, question):
73
  doc = nlp(question.lower())
 
92
  # Calculate similarity
93
  similarity = cosine_similarity([context_embedding], [question_embedding])[0][0]
94
 
95
+ # If similarity is high, it's likely a follow-up question
96
+ if similarity > 0.5: # This threshold can be adjusted
97
+ return f"{combined_context} {question}"
98
+
99
+ # Otherwise, it might be a new topic
100
+ return question
101
+
102
+ def rephrase_query(self, question):
103
+ prompt = f"""
104
+ Given the conversation context and the current question, rephrase the question to include relevant context:
105
 
106
+ Conversation context: {self.get_context()}
107
+ Current question: {question}
108
+
109
+ Rephrased question:
110
+ """
111
+
112
+ # Use your language model to generate the rephrased question
113
+ rephrased_question = generate_chunked_response(model, prompt)
114
+
115
+ return rephrased_question.strip()
116
 
117
  def process_question(self, question):
118
  contextualized_question = self.get_most_relevant_context(question)
 
120
  # Extract topics from the question
121
  topics = self.extract_topics(question)
122
 
123
+ # Check if it's a follow-up question and rephrase if necessary
124
  if self.is_follow_up_question(question):
125
+ contextualized_question = self.rephrase_query(contextualized_question)
 
126
 
127
  # Add the new question to history
128
  self.add_to_history(question)
 
355
  formatted_prompt = prompt_val.format(
356
  context=context_str,
357
  conv_context=chatbot.get_context(),
358
+ question=contextualized_question,
359
  topics=", ".join(topics),
360
  entities=json.dumps(serializable_entity_tracker)
361
  )
 
375
  sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
376
  answer += sources_section
377
 
378
+ # Update chatbot context with the answer
379
+ chatbot.add_to_history(answer)
380
+
381
  return answer
382
 
383
  else: # PDF document chat