Update app.py
Browse files199 unmodified lines2002012022032042052062072082092102112122135 unmodified lines219220221222223224225226227228229230231232233234235236237238239240241242243244245199 unmodified linesif not question:return "Please enter a question."if question in memory_database:answer = memory_database[question]else:embed = get_embeddings()model = get_model(temperature, top_p, repetition_penalty)history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])if web_search:search_results = google_search(question)context_str = "\n".join([result["text"] for result in search_results if result["text"]])5 unmodified linesCurrent Question: {question}Provide a concise and direct answer to the question:"""else:database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)if is_related_to_history(question, conversation_history):context_str = "No additional context needed. Please refer to the conversation history."else:retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(question)context_str = "\n".join([doc.page_content for doc in relevant_docs])prompt_template = prompt # Use the original prompt for document-based answersprompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)answer = generate_chunked_response(model, formatted_prompt)answer = re.split(r'Question:|Current Question:', answer)[-1].strip()memory_database[question] = answerconversation_history = manage_conversation_history(question, answer, conversation_history)return answer199 unmodified lines2002012022032042052062072082092105 unmodified lines216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249199 unmodified linesif not question:return "Please enter a question."if question in memory_database and not web_search:answer = memory_database[question]else:model = get_model(temperature, top_p, repetition_penalty)if web_search:search_results = google_search(question)context_str = "\n".join([result["text"] for result in search_results if result["text"]])5 unmodified linesCurrent Question: {question}If the web search results don't contain relevant information, state that the information is not available in the search results.Provide a concise and direct answer to the question:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)else:embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])if is_related_to_history(question, conversation_history):context_str = "No additional context needed. Please refer to the conversation history."else:retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(question)context_str = "\n".join([doc.page_content for doc in relevant_docs])prompt_val = ChatPromptTemplate.from_template(prompt)formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)answer = generate_chunked_response(model, formatted_prompt)answer = re.split(r'Question:|Current Question:', answer)[-1].strip()if not web_search:memory_database[question] = answerif not web_search:conversation_history = manage_conversation_history(question, answer, conversation_history)return answer
app.py+14-10