Update app.py
Browse files49 unmodified lines5051525354555622 unmodified lines79808182838415 unmodified lines1001011021031041051061071081091101111121131141152 unmodified lines118119120121122123124125126127128129130131132133190 unmodified lines32432532632732832933011 unmodified lines34234334434534634734834935035135235335435549 unmodified linesself.entity_tracker = {}self.conversation_context = ""self.model = modeldef add_to_history(self, text):self.history.append(text)if len(self.history) > self.history_size:22 unmodified linesdoc = nlp(text)return [chunk.text for chunk in doc.noun_chunks]def get_most_relevant_context(self, question):if not self.history:return question15 unmodified lines# Otherwise, it might be a new topicreturn questiondef rephrase_query(self, question):if not self.model:return question # Return original question if no model is availableprompt = f"""Given the conversation context and the current question, rephrase the question to include relevant context:Conversation context: {self.get_context()}Current question: {question}Rephrased question:"""2 unmodified linesreturn rephrased_question.strip()def process_question(self, question):contextualized_question = self.get_most_relevant_context(question)topics = self.extract_topics(question)if self.is_follow_up_question(question):contextualized_question = self.rephrase_query(contextualized_question)self.add_to_history(question)return contextualized_question, topics, self.entity_tracker# Initialize LlamaParsellama_parser = LlamaParse(190 unmodified linescontext_reduction_factor = 0.7if web_search:contextualized_question, topics, entity_tracker = 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 = []11 unmodified linescontext_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])prompt_template = """Answer the question based on the following web search results, conversation context, and entity information:Web Search Results:{context}Conversation Context: {conv_context}Current Question: {question}Topics: {topics}Entity Information: {entities}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.49 unmodified lines505152535455565722 unmodified lines808182838485868788899091929394959697989915 unmodified lines1151161171181191201211221231241251261271281291301311321332 unmodified lines136137138139140141142143144145146147148149150151152153154155156190 unmodified lines34734834935035135235311 unmodified lines36536636736836937037137237337437537637737837938038149 unmodified linesself.entity_tracker = {}self.conversation_context = ""self.model = modelself.last_instructions = Nonedef add_to_history(self, text):self.history.append(text)if len(self.history) > self.history_size:22 unmodified linesdoc = nlp(text)return [chunk.text for chunk in doc.noun_chunks]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*\?)?$",]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:return question15 unmodified lines# Otherwise, it might be a new topicreturn questiondef rephrase_query(self, question, instructions=None):if not self.model:return question # Return original question if no model is availableinstruction_prompt = f"Instructions: {instructions}\n" if instructions else ""prompt = f"""Given the conversation context, the current question, and any provided instructions, rephrase the question to include relevant context:Conversation context: {self.get_context()}Current question: {question}{instruction_prompt}Rephrased question:"""2 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(190 unmodified linescontext_reduction_factor = 0.7if 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 = []11 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}}Current Question: {{question}}Topics: {{topics}}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.
app.py+43-17