Update app.py
Browse files621 unmodified lines6226236246256266276283 unmodified lines6326336346356366376386396406416426436449 unmodified lines654655656657658659660661662663664665621 unmodified linesexcept ValueError as e:print(f"Error parsing relevance score: {e}")return 0.00def ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss):global conversation_history3 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:database = Noneif web_search:search_results = google_search(question)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:9 unmodified linesAnswer the question based on the following web search results:Web Search Results:{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)elif google_news_rss:if database is None:621 unmodified lines6226236246256266276286296306316326336346356366376386396406416426436443 unmodified lines6486496506516526536546556566576586596606616626636646656669 unmodified lines676677678679680681682683684685686687688621 unmodified linesexcept ValueError as e:print(f"Error parsing relevance score: {e}")return 0.00def rephrase_for_search(query, model):rephrase_prompt = PromptTemplate(input_variables=["query"],template="""Rephrase the following conversational query into a concise, search-engine-friendly format.Remove any conversational elements and focus on the core information need.Conversational query: {query}Search query:""")chain = LLMChain(llm=model, prompt=rephrase_prompt)rephrased_query = chain.run(query=query).strip()return rephrased_querydef ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss):global conversation_history3 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()# Check if the FAISS database existsif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:database = Noneif web_search:# Rephrase the query for web searchrephrased_query = rephrase_for_search(question, model)print(f"Original query: {question}")print(f"Rephrased query: {rephrased_query}")search_results = google_search(rephrased_query)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:9 unmodified linesAnswer the question based on the following web search results:Web Search Results:{context}Original Question: {original_question}Rephrased Search Query: {rephrased_query}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 original question without mentioning the web search or these instructions:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)elif google_news_rss:if database is None:
app.py+27-4