Update app.py
Browse files12345672 unmodified lines1011121314151655 unmodified lines72737475767778737980818214 unmodified lines9798991001011021039410410510610712 unmodified lines12012112211312312412512611 unmodified lines13813914014114214314414515 unmodified lines161162163164165166154167168169170import 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 lines logger.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, num_calls=3, temperature=0.2):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 lines
logger.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}
user_message = f"""Using the following context from web search results: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) response = make_api_call(client, api_params) logger.info(f"Raw response from model: {response}")
if isinstance(response, dict):11 unmodified lines else: 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 calls 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 lines logger.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, num_calls=num_calls, temperature=temperature) main_content, sources = get_response_with_search(message, system_prompt, model, use_embeddings, history, num_calls=num_calls, temperature=temperature) return main_content except Exception as e: logger.error(f"Error in respond function: {str(e)}")app.py+17-4