Update app.py
Browse files348 unmodified lines3493503513523533523533543553564 unmodified lines36136236336436436536636724 unmodified lines3923933943953963953963973983993983994004014021 unmodified line40440540640740740840938 unmodified lines448449450452453454451452453454455456457461458459460461462463464468465466467348 unmodified lines database = None
max_attempts = 5 context_reduction_factor = 0.5 # More aggressive reduction max_estimated_tokens = 25000 # Further reduced to leave more room for response context_reduction_factor = 0.5 max_estimated_tokens = 25000
if web_search: contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)4 unmodified lines
for 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 characters 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 lines
while True: formatted_prompt = prompt_val.format( context=current_context[:3000], # Limit context to 3000 characters conv_context=current_conv_context[:500], # Limit conversation context to 500 characters context=current_context[:3000], conv_context=current_conv_context[:500], question=question, topics=", ".join(current_topics[:5]), # Limit to 5 topics entities=json.dumps({k: v[:2] for k, v in current_entities.items()}) # Limit to 2 entities per type 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 line 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))]38 unmodified lines context_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} 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, question=question) formatted_prompt = prompt_val.format(context=context_str[:3000], 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:app.py+9-13