Update app.py
Browse files1234562 unmodified lines9101112131455 unmodified lines7071727374757614 unmodified lines9192939495969712 unmodified lines11011111211311411511611 unmodified lines12812913013113213315 unmodified lines149150151152153154155156157import osimport loggingimport jsonimport gradio as grfrom huggingface_hub import InferenceClientfrom langchain.embeddings import HuggingFaceEmbeddings2 unmodified linesfrom duckduckgo_search import DDGSfrom dotenv import load_dotenvfrom functools import lru_cache# Load environment variablesload_dotenv()55 unmodified lineslogger.info(f"Created vectors for {len(documents)} search results.")return FAISS.from_documents(documents, embed)def get_response_with_search(query, system_prompt, model, use_embeddings, num_calls=3, temperature=0.2):searcher = WebSearcher()search_results = searcher.search(query)14 unmodified lineslogger.info(f"Context created for query: {query}")user_message = f"""Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'."""12 unmodified lines"top_p": 0.8,}logger.info(f"Sending request to API with params: {json.dumps(api_params, indent=2, default=str)}")response = client.chat_completion(**api_params)logger.info(f"Raw response from model: {response}")if isinstance(response, dict):11 unmodified lineselse:logger.error(f"Unexpected response format from the model: {type(response)}")return "Unexpected response format from the model. Please try again.", ""except Exception as e:logger.error(f"Error in get_response_with_search: {str(e)}")logger.info(f"Attempting fallback to {FALLBACK_MODEL}")15 unmodified lineslogger.info(f"Number of API Calls: {num_calls}")logger.info(f"Use Embeddings: {use_embeddings}")logger.info(f"System Prompt: {system_prompt}")try:main_content, sources = get_response_with_search(message, system_prompt, model, use_embeddings, num_calls=num_calls, temperature=temperature)return main_contentexcept Exception as e:logger.error(f"Error in respond function: {str(e)}")12345672 unmodified lines1011121314151655 unmodified lines727374757677787980818214 unmodified lines97989910010110210310410510610712 unmodified lines12012112212312412512611 unmodified lines13813914014114214314414515 unmodified lines161162163164165166167168169170import osimport loggingimport jsonimport timeimport gradio as grfrom huggingface_hub import InferenceClientfrom langchain.embeddings import HuggingFaceEmbeddings2 unmodified linesfrom duckduckgo_search import DDGSfrom dotenv import load_dotenvfrom functools import lru_cachefrom tenacity import retry, stop_after_attempt, wait_fixed# Load environment variablesload_dotenv()55 unmodified lineslogger.info(f"Created vectors for {len(documents)} search results.")return FAISS.from_documents(documents, embed)@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))def make_api_call(client, api_params):return client.chat_completion(**api_params)def get_response_with_search(query, system_prompt, model, use_embeddings, history, num_calls=3, temperature=0.2):searcher = WebSearcher()search_results = searcher.search(query)14 unmodified lineslogger.info(f"Context created for query: {query}")chat_history = "\n".join([f"Human: {h[0]}\nAI: {h[1]}" for h in history])user_message = f"""Chat history:{chat_history}Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'."""12 unmodified lines"top_p": 0.8,}logger.info(f"Sending request to API with params: {json.dumps(api_params, indent=2, default=str)}")response = make_api_call(client, api_params)logger.info(f"Raw response from model: {response}")if isinstance(response, dict):11 unmodified lineselse:logger.error(f"Unexpected response format from the model: {type(response)}")return "Unexpected response format from the model. Please try again.", ""time.sleep(1) # Add a 1-second delay between API callsexcept Exception as e:logger.error(f"Error in get_response_with_search: {str(e)}")logger.info(f"Attempting fallback to {FALLBACK_MODEL}")15 unmodified lineslogger.info(f"Number of API Calls: {num_calls}")logger.info(f"Use Embeddings: {use_embeddings}")logger.info(f"System Prompt: {system_prompt}")logger.info(f"History: {history}")try:main_content, sources = get_response_with_search(message, system_prompt, model, use_embeddings, history, num_calls=num_calls, temperature=temperature)return main_contentexcept Exception as e:logger.error(f"Error in respond function: {str(e)}")
app.py+17-4