Update app.py
Browse files240 unmodified lines2412422432442452462472482492506 unmodified lines25725825926026126226313 unmodified lines277278279280281282283284285286287288240 unmodified linesweb_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:# Create the database with web search results if it doesn't existdatabase = FAISS.from_documents(web_docs, embed)else:# Add web search results to the existing databasedatabase.add_documents(web_docs)database.save_local("faiss_database")6 unmodified lines{context}Current 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 without mentioning the web search or these instructions:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)13 unmodified linesprompt_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] = answer240 unmodified lines2412422432442452462472486 unmodified lines25525625725825926026113 unmodified lines275276277278279280281282283284285286287288289290291292293294240 unmodified linesweb_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:database = FAISS.from_documents(web_docs, embed)else:database.add_documents(web_docs)database.save_local("faiss_database")6 unmodified lines{context}Current 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)13 unmodified linesprompt_val = ChatPromptTemplate.from_template(prompt)formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)full_response = generate_chunked_response(model, formatted_prompt)# Filter out the prompt and extract only the answeranswer_pattern = r"(?:Answer:|Provide a concise and direct answer to the question:)([\s\S]*)"match = re.search(answer_pattern, full_response, re.IGNORECASE)if match:answer = match.group(1).strip()else:# If no match is found, use the entire response but remove any instructionsanswer = re.sub(r"(?i)If the .+?instructions:", "", full_response).strip()# Remove any numbered list formattinganswer = re.sub(r'^\s*\d+\.\s*', '', answer, flags=re.MULTILINE)if not web_search:memory_database[question] = answer
app.py+15-9