Update app.py
Browse files7 unmodified lines891011121314152 unmodified lines18192021222324252627282930313233343536379 unmodified lines4748495051525354555657584 unmodified lines6364656667686970717273747576777879808182838485868788899091929394959697989 unmodified lines10810911011111211311411511611711811918 unmodified lines13813914014114214314414 unmodified lines15916016116216316413 unmodified lines17817918018118218336 unmodified lines2202212222232242252262277 unmodified linesfrom langchain.schema import Documentfrom duckduckgo_search import DDGS# Environment variables and configurationshuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")MODELS = ["mistralai/Mistral-7B-Instruct-v0.3",2 unmodified lines"meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]def get_embeddings():return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")def duckduckgo_search(query):with DDGS() as ddgs:results = ddgs.text(query, max_results=10)return resultsasync def rephrase_query(query, history, model):system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries. Your goal is to determine if a query is unique or related to the previous conversation, and then rephrase it appropriately for web search. Keep the rephrased query succinct and in a web search query format.If the query is unique, rephrase it to be more specific and searchable.If the query is related to the previous conversation, incorporate relevant context from the previous response.Provide your analysis in the following format:<analysis>Your reasoning about whether the query is unique or related</analysis>9 unmodified linesclient = InferenceClient(model, token=huggingface_token)try:response = await asyncio.to_thread(client.text_generation,prompt=f"{system_message}\n\n{user_message}",max_new_tokens=150,temperature=0.2,)# Extract the rephrased query from the responseanalysis_start = response.find("<analysis>")4 unmodified linesif analysis_start != -1 and analysis_end != -1 and rephrased_start != -1 and rephrased_end != -1:analysis = response[analysis_start + 10:analysis_end].strip()rephrased_query = response[rephrased_start + 17:rephrased_end].strip()return analysis, rephrased_queryelse:logging.error("Failed to parse the rephrase response")return None, queryexcept Exception as e:logging.error(f"Error in rephrase_query: {str(e)}")return None, querydef create_web_search_vectors(search_results):embed = get_embeddings()documents = []for result in search_results:if 'body' in result:content = f"{result['title']}\n{result['body']}\nSource: {result['href']}"documents.append(Document(page_content=content, metadata={"source": result['href']}))return FAISS.from_documents(documents, embed)async def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2):search_results = duckduckgo_search(query)if not search_results:yield "No web search results available. Please try again.", ""returnif use_embeddings:web_search_database = create_web_search_vectors(search_results)retriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])else:context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])system_message = """ You are a world-class AI system, capable of complex reasoning and reflection.9 unmodified linesWrite a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)full_response = ""try:for _ in range(num_calls):for response in client.chat_completion(messages=[{"role": "system", "content": system_message},18 unmodified linesyield f"An error occurred while processing your request: {str(e)}", ""if not full_response:logging.warning("No response generated from the model")yield "No response generated from the model.", ""async def respond(message, history, model, temperature, num_calls, use_embeddings):14 unmodified linesresponse = f"{main_content}\n\n{sources}"yield responseexcept asyncio.CancelledError:yield "The operation was cancelled. Please try again."except Exception as e:logging.error(f"Error in respond function: {str(e)}")13 unmodified lines# Gradio interface setupdef create_gradio_interface():custom_placeholder = "Enter your question here for web search."demo = gr.ChatInterface(36 unmodified lines7. Use the provided examples or ask your own questions.""")return demoif __name__ == "__main__":demo = create_gradio_interface()demo.launch(share=True)7 unmodified lines89101112131415161718192 unmodified lines222324252627282930313233343536373839404142434445464748495051529 unmodified lines62636465666768697071727374754 unmodified lines80818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211229 unmodified lines13213313413513613713813914014114214318 unmodified lines16216316416516616716814 unmodified lines18318418518618718818913 unmodified lines20320420520620720820936 unmodified lines2462472482492502512522532542557 unmodified linesfrom langchain.schema import Documentfrom duckduckgo_search import DDGS# Configure logginglogging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")# Environment variables and configurationshuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")logging.info("Environment variable for HuggingFace token retrieved.")MODELS = ["mistralai/Mistral-7B-Instruct-v0.3",2 unmodified lines"meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]logging.info(f"Models list initialized with {len(MODELS)} models.")def get_embeddings():logging.info("Loading HuggingFace embeddings model.")return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")def duckduckgo_search(query):logging.info(f"Initiating DuckDuckGo search for query: {query}")try:with DDGS() as ddgs:results = ddgs.text(query, max_results=10)logging.info(f"Search completed, found {len(results)} results.")return resultsexcept Exception as e:logging.error(f"Error during DuckDuckGo search: {str(e)}")return []async def rephrase_query(query, history, model):logging.info("Rephrasing query based on history and model.")system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries.Your goal is to determine whether the query is unique or related to the previous conversation, and then rephrase it appropriately for web search.The rephrased query should be succinct and optimized for search engines.If the query is unique (i.e., unrelated to the conversation), rephrase it to make it more specific and searchable.If the query is related to the previous conversation, incorporate relevant context from the conversation to enhance search relevance.Provide your analysis in the following format:<analysis>Your reasoning about whether the query is unique or related</analysis>9 unmodified linesclient = InferenceClient(model, token=huggingface_token)try:logging.info(f"Sending rephrase request to model {model}.")response = await asyncio.to_thread(client.text_generation,prompt=f"{system_message}\n\n{user_message}",max_new_tokens=150,temperature=0.2,)logging.info("Rephrase response received.")# Extract the rephrased query from the responseanalysis_start = response.find("<analysis>")4 unmodified linesif analysis_start != -1 and analysis_end != -1 and rephrased_start != -1 and rephrased_end != -1:analysis = response[analysis_start + 10:analysis_end].strip()rephrased_query = response[rephrased_start + 17:rephrased_end].strip()logging.info(f"Rephrased query: {rephrased_query}")return analysis, rephrased_queryelse:logging.error("Failed to parse the rephrase response.")return None, queryexcept Exception as e:logging.error(f"Error in rephrase_query: {str(e)}")return None, querydef create_web_search_vectors(search_results):logging.info(f"Creating web search vectors from {len(search_results)} search results.")embed = get_embeddings()documents = []for result in search_results:if 'body' in result:content = f"{result['title']}\n{result['body']}\nSource: {result['href']}"documents.append(Document(page_content=content, metadata={"source": result['href']}))logging.info(f"{len(documents)} documents created for FAISS vectorization.")return FAISS.from_documents(documents, embed)async def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2):logging.info(f"Performing web search for query: {query}")search_results = duckduckgo_search(query)if not search_results:logging.warning("No web search results found.")yield "No web search results available. Please try again.", ""returnif use_embeddings:logging.info("Using embeddings to retrieve relevant documents.")web_search_database = create_web_search_vectors(search_results)retriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])else:logging.info("Using raw search results for context.")context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])system_message = """ You are a world-class AI system, capable of complex reasoning and reflection.9 unmodified linesWrite a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""client = InferenceClient(model, token=huggingface_token)full_response = ""try:for _ in range(num_calls):logging.info(f"Sending request to model with {num_calls} API calls and temperature {temperature}.")for response in client.chat_completion(messages=[{"role": "system", "content": system_message},18 unmodified linesyield f"An error occurred while processing your request: {str(e)}", ""if not full_response:logging.warning("No response generated from the model.")yield "No response generated from the model.", ""async def respond(message, history, model, temperature, num_calls, use_embeddings):14 unmodified linesresponse = f"{main_content}\n\n{sources}"yield responseexcept asyncio.CancelledError:logging.warning("Operation cancelled by user.")yield "The operation was cancelled. Please try again."except Exception as e:logging.error(f"Error in respond function: {str(e)}")13 unmodified lines# Gradio interface setupdef create_gradio_interface():logging.info("Setting up Gradio interface.")custom_placeholder = "Enter your question here for web search."demo = gr.ChatInterface(36 unmodified lines7. Use the provided examples or ask your own questions.""")logging.info("Gradio interface ready.")return demoif __name__ == "__main__":logging.info("Launching Gradio application.")demo = create_gradio_interface()demo.launch(share=True)
app.py+37-9