Update app.py
Browse files324 unmodified lines32532632732832933033133233333410 unmodified lines3453463473443483493463503513523533543553523533563573583593603583613623633643 unmodified lines36836937036837137237310 unmodified lines38438538638738838939039139239339438538638738838939039139539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943040143143243343415 unmodified lines4504514524234244254264274284534544552 unmodified lines458459460461437438462463464465466467468469470471472473474475476477478479480481482483484485486487488448489490491492324 unmodified lines
return all_results
def estimate_tokens(text): # Rough estimate: 1 token ~= 4 characters return len(text) // 4
def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot): if not question: return "Please enter a question."10 unmodified lines else: database = None
max_attempts = 3 max_attempts = 5 context_reduction_factor = 0.7 max_context_chars = 8000 # Adjust this value as needed max_estimated_tokens = 30000 # Leave some room for the model's response
if web_search: contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question) serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()} # Use only the core question for the search search_results = google_search(contextualized_question, num_results=3) # Reduced number of results search_results = google_search(contextualized_question, num_results=3) all_answers = []
for attempt in range(max_attempts): try: web_docs = [Document(page_content=result["text"][:2000], metadata={"source": result["link"]}) for result in search_results if result["text"]] # Limit each result to 2000 characters web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
if database is None: database = FAISS.from_documents(web_docs, embed)3 unmodified lines database.save_local("faiss_database")
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs]) context_str = context_str[:max_context_chars]
instruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""
10 unmodified lines """
prompt_val = ChatPromptTemplate.from_template(prompt_template) # Start with full context and progressively reduce if necessary current_context = context_str current_conv_context = chatbot.get_context() current_topics = topics current_entities = serializable_entity_tracker
while True: formatted_prompt = prompt_val.format( context=context_str, conv_context=chatbot.get_context()[:1000], # Limit conversation context question=question, topics=", ".join(topics[:5]), # Limit number of topics entities=json.dumps({k: v[:3] for k, v in serializable_entity_tracker.items()}) # Limit number of entities ) formatted_prompt = prompt_val.format( context=current_context, conv_context=current_conv_context, question=question, topics=", ".join(current_topics), entities=json.dumps(current_entities) )
# Estimate token count estimated_tokens = estimate_tokens(formatted_prompt) if estimated_tokens <= max_estimated_tokens: break # Reduce context if estimated token count is too high current_context = current_context[:int(len(current_context) * context_reduction_factor)] current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)] current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))] current_entities = {k: v[:max(1, int(len(v) * context_reduction_factor))] for k, v in current_entities.items()}
if len(current_context) + len(current_conv_context) + len(str(current_topics)) + len(str(current_entities)) < 100: raise ValueError("Context reduced too much. Unable to process the query.")
full_response = generate_chunked_response(model, formatted_prompt) answer = extract_answer(full_response, instructions) all_answers.append(answer) break
except ValueError as ve: print(f"Error in ask_question (attempt {attempt + 1}): {ve}") if attempt == max_attempts - 1: all_answers.append(f"I apologize, but I'm having trouble processing the query due to its length or complexity. Could you please try asking a more specific or shorter question?")
except Exception as e: print(f"Error in ask_question (attempt {attempt + 1}): {e}") if attempt == max_attempts - 1: all_answers.append(f"I apologize, but I'm having trouble processing the query due to its length or complexity.") all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.")
answer = "\n\n".join(all_answers) sources = set(doc.metadata['source'] for doc in web_docs)15 unmodified lines relevant_docs = retriever.get_relevant_documents(question) context_str = "\n".join([doc.page_content for doc in relevant_docs])
if attempt > 0: words = context_str.split() context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
context_str = context_str[:max_context_chars]
prompt_template = """ Answer the question based on the following context from the PDF document: Context:2 unmodified lines Provide a summarized and direct answer to the question. """
while True: prompt_val = ChatPromptTemplate.from_template(prompt_template) formatted_prompt = prompt_val.format(context=context_str, question=question) prompt_val = ChatPromptTemplate.from_template(prompt_template) formatted_prompt = prompt_val.format(context=context_str, question=question)
estimated_tokens = estimate_tokens(formatted_prompt) if estimated_tokens <= max_estimated_tokens: break # Reduce context if estimated token count is too high context_str = context_str[:int(len(context_str) * context_reduction_factor)]
if len(context_str) < 100: raise ValueError("Context reduced too much. Unable to process the query.")
full_response = generate_chunked_response(model, formatted_prompt) answer = extract_answer(full_response)
return answer
except ValueError as ve: print(f"Error in ask_question (attempt {attempt + 1}): {ve}") if attempt == max_attempts - 1: return f"I apologize, but I'm having trouble processing your question due to the complexity of the document. Could you please try asking a more specific or shorter question?"
except Exception as e: print(f"Error in ask_question (attempt {attempt + 1}): {e}") if attempt == max_attempts - 1: return f"I apologize, but I'm having trouble processing your question. Could you please try rephrasing it more concisely?" return f"I apologize, but an unexpected error occurred. Please try again with a different question."
return "An unexpected error occurred. Please try again later."
app.py+64-23