Update app.py
Browse files81 unmodified lines828384858687888990919293949596979837 unmodified lines136137138139140141142143144145146147148149150151152153154155156192 unmodified lines34935035135235335410 unmodified lines3653663673683693703713723733742 unmodified lines37737837938038138238338438538638738838939039181 unmodified linesdef extract_instructions(self, text):instruction_patterns = [r"^(?:please\s+)?(?:can\s+you\s+)?(?:could\s+you\s+)?(.*?)\s*(?:for\s+me|for\s+this\s+response|in\s+your\s+response|in\s+your\s+answer)(?:\s*\?)?$",r"^(?:I\s+want\s+you\s+to\s+)?(.*?)\s*(?:for\s+me|for\s+this\s+response|in\s+your\s+response|in\s+your\s+answer)(?:\s*\?)?$",r"^(?:make\s+sure\s+to\s+)?(.*?)\s*(?:for\s+me|for\s+this\s+response|in\s+your\s+response|in\s+your\s+answer)(?:\s*\?)?$",]for pattern in instruction_patterns:match = re.match(pattern, text, re.IGNORECASE)if match:return match.group(1).strip(), Truereturn text, Falsedef get_most_relevant_context(self, question):if not self.history:37 unmodified linesreturn rephrased_question.strip()def process_question(self, question):question, has_instructions = self.extract_instructions(question)if has_instructions:self.last_instructions = questioncontextualized_question = self.get_most_relevant_context(question)else:contextualized_question = self.get_most_relevant_context(question)if self.is_follow_up_question(question):contextualized_question = self.rephrase_query(contextualized_question, self.last_instructions)topics = self.extract_topics(contextualized_question)self.add_to_history(question)return contextualized_question, topics, self.entity_tracker, self.last_instructions# Initialize LlamaParsellama_parser = LlamaParse(192 unmodified linesif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}search_results = google_search(contextualized_question)all_answers = []10 unmodified linescontext_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])instruction_prompt = f"Instructions: {instructions}\n" if instructions else ""prompt_template = f"""Answer the question based on the following web search results, conversation context, entity information, and any provided instructions:Web Search Results:{{context}}Conversation Context: {{conv_context}}2 unmodified linesEntity Information: {{entities}}{instruction_prompt}If the web search results don't contain relevant information, state that the information is not available in the search results.Provide a summarized and direct answer to the question without mentioning the web search or these instructions.Do not include any source information in your answer."""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str,conv_context=chatbot.get_context(),question=contextualized_question,topics=", ".join(topics),entities=json.dumps(serializable_entity_tracker))81 unmodified lines828384858687888990919293949596979837 unmodified lines136137138139140141142143144145146147148149150151152153154192 unmodified lines34734834935035135235335410 unmodified lines3653663673683693703713723733742 unmodified lines37737837938038138238338438538638738838939039181 unmodified linesdef extract_instructions(self, text):instruction_patterns = [r"(.*?),?\s*(?:please\s+)?(provide\s+(?:me\s+)?a\s+.*?|give\s+(?:me\s+)?a\s+.*?|create\s+a\s+.*?)$",r"(.*?),?\s*(?:please\s+)?(summarize|analyze|explain|describe|elaborate\s+on).*$",r"(.*?),?\s*(?:please\s+)?(in\s+detail|briefly|concisely).*$",]for pattern in instruction_patterns:match = re.match(pattern, text, re.IGNORECASE)if match:return match.group(1).strip(), match.group(2).strip()return text, Nonedef get_most_relevant_context(self, question):if not self.history:37 unmodified linesreturn rephrased_question.strip()def process_question(self, question):core_question, instructions = self.extract_instructions(question)contextualized_question = self.get_most_relevant_context(core_question)if self.is_follow_up_question(core_question):contextualized_question = self.rephrase_query(contextualized_question, instructions)topics = self.extract_topics(contextualized_question)self.add_to_history(question)self.last_instructions = instructionsreturn contextualized_question, topics, self.entity_tracker, instructions# Initialize LlamaParsellama_parser = LlamaParse(192 unmodified linesif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}# Use only the core question for the searchsearch_results = google_search(contextualized_question)all_answers = []10 unmodified linescontext_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])instruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""prompt_template = f"""Answer the question based on the following web search results, conversation context, entity information, and user instructions:Web Search Results:{{context}}Conversation Context: {{conv_context}}2 unmodified linesEntity Information: {{entities}}{instruction_prompt}If the web search results don't contain relevant information, state that the information is not available in the search results.Provide a response that addresses the question and follows the user's instructions.Do not mention these instructions or the web search process in your answer."""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str,conv_context=chatbot.get_context(),question=question, # Use the original question heretopics=", ".join(topics),entities=json.dumps(serializable_entity_tracker))
app.py+19-19