Update app.py
Browse files222 unmodified lines2232242252262272282292302312322332342352362372382392402412422432442451 unmodified line24724824925025125225310 unmodified lines264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295222 unmodified linesdef ask_question(question, temperature, top_p, repetition_penalty, web_search):global conversation_historyif 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)embed = get_embeddings()if web_search:search_results = google_search(question)context_str = "\n".join([result["text"] for result in search_results if result["text"]])# Convert web search results to Document formatweb_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]# Check if the FAISS database existsif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)1 unmodified lineelse:database = FAISS.from_documents(web_docs, embed)database.save_local("faiss_database")prompt_template = """Answer the question based on the following web search results:Web Search Results:10 unmodified linesdatabase = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:return "No FAISS database found. Please upload documents to create the vector store."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()# Remove any remaining prompt instructions from the answeranswer_lines = answer.split('\n')answer = '\n'.join(line for line in answer_lines if not line.startswith('If') and not line.startswith('Provide'))if not web_search:memory_database[question] = answerif not web_search:conversation_history = manage_conversation_history(question, answer, conversation_history)return answerdef update_vectors(files, use_recursive_splitter):222 unmodified lines2232242252262272282292302312322332342352362372382392402412422432442451 unmodified line24724824925025125225310 unmodified lines264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295222 unmodified linesdef ask_question(question, temperature, top_p, repetition_penalty, web_search):global conversation_historyif 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)embed = get_embeddings()if web_search:search_results = google_search(question)context_str = "\n".join([result["text"] for result in search_results if result["text"]])# Convert web search results to Document formatweb_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]# Check if the FAISS database existsif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)1 unmodified lineelse:database = FAISS.from_documents(web_docs, embed)database.save_local("faiss_database")prompt_template = """Answer the question based on the following web search results:Web Search Results:10 unmodified linesdatabase = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:return "No FAISS database found. Please upload documents to create the vector store."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()# Remove any remaining prompt instructions from the answeranswer_lines = answer.split('\n')answer = '\n'.join(line for line in answer_lines if not line.startswith('If') and not line.startswith('Provide'))if not web_search:memory_database[question] = answerif not web_search:conversation_history = manage_conversation_history(question, answer, conversation_history)return answerdef update_vectors(files, use_recursive_splitter):
app.py+14-14