Update app.py
Browse files74 unmodified lines7576777879808133 unmodified lines1151161171181191201212 unmodified lines1241251261271281291308 unmodified lines139140141142143144145146147148209 unmodified lines35835936036136236336436574 unmodified linesdef is_follow_up_question(self, question):doc = nlp(question.lower())follow_up_indicators = set(['it', 'this', 'that', 'these', 'those', 'he', 'she', 'they', 'them'])return any(token.text in follow_up_indicators for token in doc)def extract_topics(self, text):doc = nlp(text)33 unmodified lines# Otherwise, it might be a new topicreturn questiondef rephrase_query(self, question, instructions=None):if not self.model:2 unmodified linesinstruction_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}8 unmodified linesdef 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)209 unmodified linesif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)search_results = google_search(contextualized_question, num_results=3)context_chunks = []for result in search_results:if result["text"]:74 unmodified lines7576777879808133 unmodified lines1151161171181191202 unmodified lines1231241251261271281298 unmodified lines138139140141142143144145146147148209 unmodified lines35835936036136236336436536636736836974 unmodified linesdef is_follow_up_question(self, question):doc = nlp(question.lower())follow_up_indicators = set(['it', 'this', 'that', 'these', 'those', 'he', 'she', 'they', 'them'])return any(token.text in follow_up_indicators for token in doc) or question.strip().startswith("What about")def extract_topics(self, text):doc = nlp(text)33 unmodified lines# Otherwise, it might be a new topicreturn questiondef rephrase_query(self, question, instructions=None):if not self.model:2 unmodified linesinstruction_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 and rephrase it to more search-engine-friendly query:Conversation context: {self.get_context()}Current question: {question}8 unmodified linesdef process_question(self, question):core_question, instructions = self.extract_instructions(question)if self.is_follow_up_question(core_question):contextualized_question = self.get_most_relevant_context(core_question)contextualized_question = self.rephrase_query(contextualized_question, instructions)else:contextualized_question = core_questiontopics = self.extract_topics(contextualized_question)209 unmodified linesif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)# Log the contextualized question for debuggingprint(f"Contextualized question: {contextualized_question}")search_results = google_search(contextualized_question, num_results=3)context_chunks = []for result in search_results:if result["text"]:
app.py+10-6