Update app.py
Browse files232 unmodified lines233234235236237238107 unmodified lines3463473483493503513523534 unmodified lines3583593603613623633647 unmodified lines3723733743753763773783793803813823833843853863873883893903911 unmodified line393394395396397398399400401402403404405406232 unmodified linesfull_response += chunkexcept Exception as e:print(f"Error in generate_chunked_response: {e}")breakreturn full_response.strip()107 unmodified linesdatabase = Nonemax_attempts = 5context_reduction_factor = 0.7max_estimated_tokens = 30000 # Leave some room for the model's 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"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:database = FAISS.from_documents(web_docs, embed)7 unmodified linesinstruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""prompt_template = f"""Answer the question based on the following web search results, conversation context, entity information, and user instructions:Web Search Results:{{context}}Conversation Context: {{conv_context}}Current Question: {{question}}Topics: {{topics}}Entity Information: {{entities}}{instruction_prompt}Provide a concise and relevant answer to the question."""prompt_val = ChatPromptTemplate.from_template(prompt_template)# Start with full context and progressively reduce if necessarycurrent_context = context_strcurrent_conv_context = chatbot.get_context()current_topics = topics1 unmodified linewhile True: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 countestimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_estimated_tokens:232 unmodified lines233234235236237238239240241107 unmodified lines3493503513523533543553564 unmodified lines3613623633643653663677 unmodified lines3753763773783793803813823833843853863873883893901 unmodified line392393394395396397398399400401402403404232 unmodified linesfull_response += chunkexcept Exception as e:print(f"Error in generate_chunked_response: {e}")if "Input validation error" in str(e):# If we hit the token limit, return what we have so farreturn full_response if full_response else "The input was too long to process. Please try a shorter query."breakreturn full_response.strip()107 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)7 unmodified linesinstruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""prompt_template = f"""Answer based on: Web Results: {{context}}Context: {{conv_context}}Question: {{question}}Topics: {{topics}}Entities: {{entities}}{instruction_prompt}"""prompt_val = ChatPromptTemplate.from_template(prompt_template)current_context = context_strcurrent_conv_context = chatbot.get_context()current_topics = topics1 unmodified linewhile 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)if estimated_tokens <= max_estimated_tokens:
app.py+14-16