Update app.py
Browse files81 unmodified lines8283848586878586878889909192939394959596979837 unmodified lines136137138139139140141142143144145146147141142143144145146147148149150153151152153154192 unmodified lines34734834935035135235335410 unmodified lines3653663673683683693703713713723733742 unmodified lines37737837938038138038138238338438538638738838838939039181 unmodified lines
def 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*\?)?$", 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(), True return match.group(1).strip(), match.group(2).strip()
return text, False return text, None
def get_most_relevant_context(self, question): if not self.history:37 unmodified lines return rephrased_question.strip()
def process_question(self, question): question, has_instructions = self.extract_instructions(question) core_question, instructions = self.extract_instructions(question) if has_instructions: self.last_instructions = question contextualized_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) 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 = instructions return contextualized_question, topics, self.entity_tracker, self.last_instructions return contextualized_question, topics, self.entity_tracker, instructions # Initialize LlamaParsellama_parser = LlamaParse(192 unmodified lines if 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 search search_results = google_search(contextualized_question) all_answers = []
10 unmodified lines
context_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 "" 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 any provided instructions: 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 lines Entity 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. 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=contextualized_question, question=question, # Use the original question here topics=", ".join(topics), entities=json.dumps(serializable_entity_tracker) )app.py+19-19