Update app.py
Browse files621 unmodified lines6226236246256266276286296306316326336346356366376386396406416426436443 unmodified lines6486496506516526536546556566576586596606616626416636646656669 unmodified lines676677678657679680681659682683684662685686687688621 unmodified lines except ValueError as e: print(f"Error parsing relevance score: {e}") return 0.00
def 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_query
def ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss): global conversation_history
3 unmodified lines model = get_model(temperature, top_p, repetition_penalty) embed = get_embeddings()
# Check if the FAISS database exists if os.path.exists("faiss_database"): database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True) else: database = None
if web_search: # Rephrase the query for web search rephrased_query = rephrase_for_search(question, model) print(f"Original query: {question}") print(f"Rephrased query: {rephrased_query}") search_results = google_search(question) 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 lines Answer the question based on the following web search results: Web Search Results: {context} Current Question: {question} 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 question without mentioning the web search or these instructions: 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, question=question) 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