Update app.py
Browse files348 unmodified lines3493503513523533543553564 unmodified lines36136236336436536636724 unmodified lines3923933943953963973983994004014021 unmodified line40440540640740840941038 unmodified lines449450451452453454455456457458459460461462463464465466467468469470471348 unmodified linesdatabase = Nonemax_attempts = 5context_reduction_factor = 0.5 # More aggressive reductionmax_estimated_tokens = 25000 # Further reduced to leave more room for responseif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)4 unmodified linesfor attempt in range(max_attempts):try:web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]] # Limit each result to 1000 charactersif database is None:database = FAISS.from_documents(web_docs, embed)24 unmodified lineswhile True:formatted_prompt = prompt_val.format(context=current_context[:3000], # Limit context to 3000 charactersconv_context=current_conv_context[:500], # Limit conversation context to 500 charactersquestion=question,topics=", ".join(current_topics[:5]), # Limit to 5 topicsentities=json.dumps({k: v[:2] for k, v in current_entities.items()}) # Limit to 2 entities per type)estimated_tokens = estimate_tokens(formatted_prompt)1 unmodified lineif estimated_tokens <= max_estimated_tokens:break# Reduce context if estimated token count is too highcurrent_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))]38 unmodified linescontext_str = "\n".join([doc.page_content for doc in relevant_docs])prompt_template = """Answer the question based on the following context from the PDF document:Context:{context}Question: {question}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)estimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_estimated_tokens:break# Reduce context if estimated token count is too highcontext_str = context_str[:int(len(context_str) * context_reduction_factor)]if len(context_str) < 100:348 unmodified lines3493503513523533543553564 unmodified lines36136236336436536636724 unmodified lines3923933943953963973983994004014021 unmodified line40440540640740840938 unmodified lines448449450451452453454455456457458459460461462463464465466467348 unmodified linesdatabase = Nonemax_attempts = 5context_reduction_factor = 0.5max_estimated_tokens = 25000if web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)4 unmodified linesfor attempt in range(max_attempts):try:web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:database = FAISS.from_documents(web_docs, embed)24 unmodified lineswhile True:formatted_prompt = prompt_val.format(context=current_context[:3000],conv_context=current_conv_context[:500],question=question,topics=", ".join(current_topics[:5]),entities=json.dumps({k: v[:2] for k, v in current_entities.items()}))estimated_tokens = estimate_tokens(formatted_prompt)1 unmodified lineif estimated_tokens <= max_estimated_tokens:breakcurrent_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))]38 unmodified linescontext_str = "\n".join([doc.page_content for doc in relevant_docs])prompt_template = """Answer based on: PDF Context: {context}Question: {question}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[:3000], question=question)estimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_estimated_tokens:breakcontext_str = context_str[:int(len(context_str) * context_reduction_factor)]if len(context_str) < 100:
app.py+9-13