Update app.py
Browse files
app.py
CHANGED
|
@@ -75,7 +75,7 @@ class EnhancedContextDrivenChatbot:
|
|
| 75 |
def is_follow_up_question(self, question):
|
| 76 |
doc = nlp(question.lower())
|
| 77 |
follow_up_indicators = set(['it', 'this', 'that', 'these', 'those', 'he', 'she', 'they', 'them'])
|
| 78 |
-
return any(token.text in follow_up_indicators for token in doc)
|
| 79 |
|
| 80 |
def extract_topics(self, text):
|
| 81 |
doc = nlp(text)
|
|
@@ -115,7 +115,6 @@ class EnhancedContextDrivenChatbot:
|
|
| 115 |
|
| 116 |
# Otherwise, it might be a new topic
|
| 117 |
return question
|
| 118 |
-
|
| 119 |
|
| 120 |
def rephrase_query(self, question, instructions=None):
|
| 121 |
if not self.model:
|
|
@@ -124,7 +123,7 @@ class EnhancedContextDrivenChatbot:
|
|
| 124 |
instruction_prompt = f"Instructions: {instructions}\n" if instructions else ""
|
| 125 |
|
| 126 |
prompt = f"""
|
| 127 |
-
Given the conversation context, the current question, and any provided instructions, rephrase the question to include relevant context:
|
| 128 |
|
| 129 |
Conversation context: {self.get_context()}
|
| 130 |
Current question: {question}
|
|
@@ -139,10 +138,11 @@ class EnhancedContextDrivenChatbot:
|
|
| 139 |
def process_question(self, question):
|
| 140 |
core_question, instructions = self.extract_instructions(question)
|
| 141 |
|
| 142 |
-
contextualized_question = self.get_most_relevant_context(core_question)
|
| 143 |
-
|
| 144 |
if self.is_follow_up_question(core_question):
|
|
|
|
| 145 |
contextualized_question = self.rephrase_query(contextualized_question, instructions)
|
|
|
|
|
|
|
| 146 |
|
| 147 |
topics = self.extract_topics(contextualized_question)
|
| 148 |
|
|
@@ -358,8 +358,12 @@ def ask_question(question: str, temperature: float, top_p: float, repetition_pen
|
|
| 358 |
|
| 359 |
if web_search:
|
| 360 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
| 361 |
-
search_results = google_search(contextualized_question, num_results=3)
|
| 362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
context_chunks = []
|
| 364 |
for result in search_results:
|
| 365 |
if result["text"]:
|
|
|
|
| 75 |
def is_follow_up_question(self, question):
|
| 76 |
doc = nlp(question.lower())
|
| 77 |
follow_up_indicators = set(['it', 'this', 'that', 'these', 'those', 'he', 'she', 'they', 'them'])
|
| 78 |
+
return any(token.text in follow_up_indicators for token in doc) or question.strip().startswith("What about")
|
| 79 |
|
| 80 |
def extract_topics(self, text):
|
| 81 |
doc = nlp(text)
|
|
|
|
| 115 |
|
| 116 |
# Otherwise, it might be a new topic
|
| 117 |
return question
|
|
|
|
| 118 |
|
| 119 |
def rephrase_query(self, question, instructions=None):
|
| 120 |
if not self.model:
|
|
|
|
| 123 |
instruction_prompt = f"Instructions: {instructions}\n" if instructions else ""
|
| 124 |
|
| 125 |
prompt = f"""
|
| 126 |
+
Given the conversation context, the current question, and any provided instructions, rephrase the question to include relevant context and rephrase it to more search-engine-friendly query:
|
| 127 |
|
| 128 |
Conversation context: {self.get_context()}
|
| 129 |
Current question: {question}
|
|
|
|
| 138 |
def process_question(self, question):
|
| 139 |
core_question, instructions = self.extract_instructions(question)
|
| 140 |
|
|
|
|
|
|
|
| 141 |
if self.is_follow_up_question(core_question):
|
| 142 |
+
contextualized_question = self.get_most_relevant_context(core_question)
|
| 143 |
contextualized_question = self.rephrase_query(contextualized_question, instructions)
|
| 144 |
+
else:
|
| 145 |
+
contextualized_question = core_question
|
| 146 |
|
| 147 |
topics = self.extract_topics(contextualized_question)
|
| 148 |
|
|
|
|
| 358 |
|
| 359 |
if web_search:
|
| 360 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
|
|
|
| 361 |
|
| 362 |
+
# Log the contextualized question for debugging
|
| 363 |
+
print(f"Contextualized question: {contextualized_question}")
|
| 364 |
+
|
| 365 |
+
search_results = google_search(contextualized_question, num_results=3)
|
| 366 |
+
|
| 367 |
context_chunks = []
|
| 368 |
for result in search_results:
|
| 369 |
if result["text"]:
|