Update app.py
Browse files287 unmodified lines28828929029129229329429529629729828 unmodified lines327328329330331332333153 unmodified lines487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556287 unmodified lineshistory = history + [(message, "")]try:if use_web_search:history[-1] = (message, "Generating response... (This may take a moment)")yield historyfor response in respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, instruction_key):history[-1] = (message, response)yield historyexcept gr.CancelledError:28 unmodified linesuse_web_search = False # Ensure we use PDF search for summariesif use_web_search:for response, _ in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):yield responseelse:embed = get_embeddings()153 unmodified linesretriever = web_search_database.as_retriever(search_kwargs={"k": 10})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs[:5]])initial_prompt = f"""Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'Stick closely to the information provided in the context and avoid making unsupported claims."""try:client = InferenceClient(model, token=huggingface_token)# Generate initial responseinitial_response = client.text_generation(initial_prompt, max_new_tokens=1000, temperature=temperature)# Generate critiquecritique = critique_response(initial_response, context, query, model)final_prompt = f"""Given the following initial response, context, critique, and original query, provide a revised response that addresses the identified issues and sticks closely to the information provided in the context while fully answering the user's query in a detailed and complete research document, after writing the document, please provide a list of sources used in your response.User Query: {query}Initial Response:{initial_response}Context:{context}Critique:{critique}Revised Response:"""# Generate final responsefull_response = ""for chunk in client.text_generation(final_prompt, max_new_tokens=1500, temperature=temperature, stream=True):full_response += chunkyield full_response, ""# Add a disclaimerdisclaimer = ("\nNote: This response was generated by an AI model based on web search results. ""While efforts have been made to ensure accuracy, please verify important information from authoritative sources.")full_response += disclaimeryield full_response, ""except Exception as e:logging.error(f"Error in multi-step generation process: {str(e)}")yield f"An error occurred during the response generation process: {str(e)}", ""def critique_response(response, context, query, model):critique_prompt = f"""Given the following response, original context, and user query, identify any statements that might be inaccurate, unsupported by the context, or irrelevant to the query. Be specific about which parts may be hallucinations or extrapolations beyond the given information.User Query: {query}Response:{response}Original Context:{context}Critique:"""client = InferenceClient(model, token=huggingface_token)critique = client.text_generation(critique_prompt, max_new_tokens=500, temperature=0.2)return critiqueINSTRUCTION_PROMPTS = {287 unmodified lines28828929029129229329428 unmodified lines323324325326327328329330331332153 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521287 unmodified lineshistory = history + [(message, "")]try:for response in respond(message, history, model, temperature, num_calls, use_web_search):history[-1] = (message, response)yield historyexcept gr.CancelledError:28 unmodified linesuse_web_search = False # Ensure we use PDF search for summariesif use_web_search:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''# logging.info(f"Generated Response (first line): {first_line}")yield responseelse:embed = get_embeddings()153 unmodified linesretriever = web_search_database.as_retriever(search_kwargs={"k": 10})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])prompt = f"""Using the following context from web search results:{context}Write 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.Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context."""After writing the document, please provide a list of sources used in your response."""if model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):yield response, "" # Yield streaming response without sourceselse:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)main_content = ""for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=10000,temperature=temperature,stream=True,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourcesINSTRUCTION_PROMPTS = {
app.py+29-64