Update app.py
Browse files222 unmodified lines2232242252262262272282292292302312322332342352352362372382392392402412422422432442451 unmodified line24724824925025025125225310 unmodified lines264265266267267268269269270271272273274275276276277278279279280281282282283284285286286287288289289290291292292293294295222 unmodified lines
def ask_question(question, temperature, top_p, repetition_penalty, web_search): global conversation_history
if 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 format web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
# Check if the FAISS database exists if os.path.exists("faiss_database"): database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)1 unmodified line else: 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 lines database = 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 answer answer_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] = answer
if not web_search: conversation_history = manage_conversation_history(question, answer, conversation_history)
return answer
def update_vectors(files, use_recursive_splitter):app.py+14-14