Update app.py
Browse files348 unmodified lines34935035135235335435535613 unmodified lines37037137237337437537655 unmodified lines43243343443543643743843944044144244344444544644744844945045145245345441 unmodified lines496497498499500501502503504505506507508509510511512513348 unmodified linesreturn all_resultsdef estimate_tokens(text):# Rough estimate: 1 token ~= 4 charactersreturn len(text) // 4def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot, user_instructions):if not question:13 unmodified linesmax_attempts = 3context_reduction_factor = 0.7max_tokens = 32000if web_search:contextualized_question, topics, entity_tracker, _ = chatbot.process_question(question)55 unmodified linesentities=json.dumps(current_entities))estimated_tokens = len(formatted_prompt) // 4if estimated_tokens <= max_tokens - 1000: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))]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, max_tokens=1000)answer = extract_answer(full_response, user_instructions)all_answers.append(answer)breakexcept ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")41 unmodified linesestimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_tokens - 1000:breakcontext_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, max_tokens=1000)answer = extract_answer(full_response, user_instructions)return answerexcept ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")348 unmodified lines34935035135235335435535635735835936036136213 unmodified lines37637737837938038138238355 unmodified lines43944044144244344444544644744844945045145245345445545645745845946046146246346446546646741 unmodified lines509510511512513514515516517518519520521522523524525526527528529530348 unmodified linesreturn all_resultsdef estimate_tokens(text):# A more accurate estimation (still an estimate, but better than 1 token = 4 chars)return len(text.split())def truncate_text(text, max_tokens):words = text.split()if len(words) <= max_tokens:return textreturn ' '.join(words[:max_tokens])def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot, user_instructions):if not question:13 unmodified linesmax_attempts = 3context_reduction_factor = 0.7max_input_tokens = 31000 # Leave room for the model's responsemax_output_tokens = 1000if web_search:contextualized_question, topics, entity_tracker, _ = chatbot.process_question(question)55 unmodified linesentities=json.dumps(current_entities))estimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_input_tokens:break# Reduce context sizescurrent_context = truncate_text(current_context, int(estimate_tokens(current_context) * context_reduction_factor))current_conv_context = truncate_text(current_conv_context, int(estimate_tokens(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 estimate_tokens(current_context) + estimate_tokens(current_conv_context) + estimate_tokens(", ".join(current_topics)) + estimate_tokens(json.dumps(current_entities)) < 100:raise ValueError("Context reduced too much. Unable to process the query.")try:full_response = generate_chunked_response(model, formatted_prompt, max_tokens=max_output_tokens)answer = extract_answer(full_response, user_instructions)all_answers.append(answer)breakexcept Exception as e:print(f"Error in generate_chunked_response: {e}")if attempt == max_attempts - 1:all_answers.append(f"I apologize, but I encountered an error while generating the response. Please try again with a simpler question.")except ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")41 unmodified linesestimated_tokens = estimate_tokens(formatted_prompt)if estimated_tokens <= max_input_tokens:breakcontext_str = truncate_text(context_str, int(estimate_tokens(context_str) * context_reduction_factor))if estimate_tokens(context_str) < 100:raise ValueError("Context reduced too much. Unable to process the query.")try:full_response = generate_chunked_response(model, formatted_prompt, max_tokens=max_output_tokens)answer = extract_answer(full_response, user_instructions)return answerexcept Exception as e:print(f"Error in generate_chunked_response: {e}")if attempt == max_attempts - 1:return f"I apologize, but I encountered an error while generating the response. Please try again with a simpler question."except ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")
app.py+36-19