Update app.py
Browse files
app.py
CHANGED
|
@@ -82,17 +82,17 @@ class EnhancedContextDrivenChatbot:
|
|
| 82 |
|
| 83 |
def extract_instructions(self, text):
|
| 84 |
instruction_patterns = [
|
| 85 |
-
r"
|
| 86 |
-
r"
|
| 87 |
-
r"
|
| 88 |
]
|
| 89 |
|
| 90 |
for pattern in instruction_patterns:
|
| 91 |
match = re.match(pattern, text, re.IGNORECASE)
|
| 92 |
if match:
|
| 93 |
-
return match.group(1).strip(),
|
| 94 |
|
| 95 |
-
return text,
|
| 96 |
|
| 97 |
def get_most_relevant_context(self, question):
|
| 98 |
if not self.history:
|
|
@@ -136,21 +136,19 @@ class EnhancedContextDrivenChatbot:
|
|
| 136 |
return rephrased_question.strip()
|
| 137 |
|
| 138 |
def process_question(self, question):
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
contextualized_question = self.get_most_relevant_context(question)
|
| 146 |
-
if self.is_follow_up_question(question):
|
| 147 |
-
contextualized_question = self.rephrase_query(contextualized_question, self.last_instructions)
|
| 148 |
|
| 149 |
topics = self.extract_topics(contextualized_question)
|
| 150 |
|
| 151 |
self.add_to_history(question)
|
|
|
|
| 152 |
|
| 153 |
-
return contextualized_question, topics, self.entity_tracker,
|
| 154 |
|
| 155 |
# Initialize LlamaParse
|
| 156 |
llama_parser = LlamaParse(
|
|
@@ -349,6 +347,8 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
| 349 |
if web_search:
|
| 350 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
| 351 |
serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}
|
|
|
|
|
|
|
| 352 |
search_results = google_search(contextualized_question)
|
| 353 |
all_answers = []
|
| 354 |
|
|
@@ -365,10 +365,10 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
| 365 |
|
| 366 |
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
| 367 |
|
| 368 |
-
instruction_prompt = f"Instructions: {instructions}\n" if instructions else ""
|
| 369 |
|
| 370 |
prompt_template = f"""
|
| 371 |
-
Answer the question based on the following web search results, conversation context, entity information, and
|
| 372 |
Web Search Results:
|
| 373 |
{{context}}
|
| 374 |
Conversation Context: {{conv_context}}
|
|
@@ -377,15 +377,15 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
| 377 |
Entity Information: {{entities}}
|
| 378 |
{instruction_prompt}
|
| 379 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
| 380 |
-
Provide a
|
| 381 |
-
Do not
|
| 382 |
"""
|
| 383 |
|
| 384 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
| 385 |
formatted_prompt = prompt_val.format(
|
| 386 |
context=context_str,
|
| 387 |
conv_context=chatbot.get_context(),
|
| 388 |
-
question=
|
| 389 |
topics=", ".join(topics),
|
| 390 |
entities=json.dumps(serializable_entity_tracker)
|
| 391 |
)
|
|
|
|
| 82 |
|
| 83 |
def extract_instructions(self, text):
|
| 84 |
instruction_patterns = [
|
| 85 |
+
r"(.*?),?\s*(?:please\s+)?(provide\s+(?:me\s+)?a\s+.*?|give\s+(?:me\s+)?a\s+.*?|create\s+a\s+.*?)$",
|
| 86 |
+
r"(.*?),?\s*(?:please\s+)?(summarize|analyze|explain|describe|elaborate\s+on).*$",
|
| 87 |
+
r"(.*?),?\s*(?:please\s+)?(in\s+detail|briefly|concisely).*$",
|
| 88 |
]
|
| 89 |
|
| 90 |
for pattern in instruction_patterns:
|
| 91 |
match = re.match(pattern, text, re.IGNORECASE)
|
| 92 |
if match:
|
| 93 |
+
return match.group(1).strip(), match.group(2).strip()
|
| 94 |
|
| 95 |
+
return text, None
|
| 96 |
|
| 97 |
def get_most_relevant_context(self, question):
|
| 98 |
if not self.history:
|
|
|
|
| 136 |
return rephrased_question.strip()
|
| 137 |
|
| 138 |
def process_question(self, question):
|
| 139 |
+
core_question, instructions = self.extract_instructions(question)
|
| 140 |
|
| 141 |
+
contextualized_question = self.get_most_relevant_context(core_question)
|
| 142 |
+
|
| 143 |
+
if self.is_follow_up_question(core_question):
|
| 144 |
+
contextualized_question = self.rephrase_query(contextualized_question, instructions)
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
topics = self.extract_topics(contextualized_question)
|
| 147 |
|
| 148 |
self.add_to_history(question)
|
| 149 |
+
self.last_instructions = instructions
|
| 150 |
|
| 151 |
+
return contextualized_question, topics, self.entity_tracker, instructions
|
| 152 |
|
| 153 |
# Initialize LlamaParse
|
| 154 |
llama_parser = LlamaParse(
|
|
|
|
| 347 |
if web_search:
|
| 348 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
| 349 |
serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}
|
| 350 |
+
|
| 351 |
+
# Use only the core question for the search
|
| 352 |
search_results = google_search(contextualized_question)
|
| 353 |
all_answers = []
|
| 354 |
|
|
|
|
| 365 |
|
| 366 |
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
| 367 |
|
| 368 |
+
instruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""
|
| 369 |
|
| 370 |
prompt_template = f"""
|
| 371 |
+
Answer the question based on the following web search results, conversation context, entity information, and user instructions:
|
| 372 |
Web Search Results:
|
| 373 |
{{context}}
|
| 374 |
Conversation Context: {{conv_context}}
|
|
|
|
| 377 |
Entity Information: {{entities}}
|
| 378 |
{instruction_prompt}
|
| 379 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
| 380 |
+
Provide a response that addresses the question and follows the user's instructions.
|
| 381 |
+
Do not mention these instructions or the web search process in your answer.
|
| 382 |
"""
|
| 383 |
|
| 384 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
| 385 |
formatted_prompt = prompt_val.format(
|
| 386 |
context=context_str,
|
| 387 |
conv_context=chatbot.get_context(),
|
| 388 |
+
question=question, # Use the original question here
|
| 389 |
topics=", ".join(topics),
|
| 390 |
entities=json.dumps(serializable_entity_tracker)
|
| 391 |
)
|