Update app.py
Browse files351 unmodified lines352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406351 unmodified lines# Rough estimate: 1 token ~= 4 charactersreturn len(text) // 4def ask_question(question: str, temperature: float, top_p: float, repetition_penalty: float, web_search: bool, chatbot: EnhancedContextDrivenChatbot) -> str:model = get_model(temperature, top_p, repetition_penalty)chatbot.model = modelif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)# Log the contextualized question for debuggingprint(f"Contextualized question: {contextualized_question}")search_results = google_search(contextualized_question, num_results=3)context_chunks = []for result in search_results:if result["text"]:context_chunks.extend(chunk_text(result["text"]))relevant_chunks = get_most_relevant_chunks(question, context_chunks)prompt_parts = [f"Question: {question}",f"Conversation Context: {chatbot.get_context()[-1000:]}", # Last 1000 characters"Relevant Web Search Results:"]for chunk in relevant_chunks:if len(' '.join(prompt_parts)) + len(chunk) < MAX_PROMPT_CHARS:prompt_parts.append(chunk)else:breakif instructions:prompt_parts.append(f"User Instructions: {instructions}")prompt_template = """Answer the question based on the following information:{context}Provide a concise and relevant answer to the question."""formatted_prompt = prompt_template.format(context='\n'.join(prompt_parts))# Generate response using the modelfull_response = generate_chunked_response(model, formatted_prompt, max_tokens=1000)answer = extract_answer(full_response, instructions)# Update chatbot context with the answerchatbot.add_to_history(answer)return answerelse: # PDF document chat351 unmodified lines352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477351 unmodified lines# Rough estimate: 1 token ~= 4 charactersreturn len(text) // 4def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot):if not question:return "Please enter a question."model = get_model(temperature, top_p, repetition_penalty)# Update the chatbot's modelchatbot.model = modelembed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:database = Nonemax_attempts = 3 # Define the maximum number of attemptscontext_reduction_factor = 0.7max_tokens = 32000 # Maximum tokens allowed by the modelif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)# Log the contextualized question and instructions separately for debuggingprint(f"Contextualized question: {contextualized_question}")print(f"Instructions: {instructions}")try:search_results = google_search(contextualized_question, num_results=3)except Exception as e:print(f"Error in web search: {e}")return f"I apologize, but I encountered an error while searching for information: {str(e)}"all_answers = []for 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 not web_docs:return "I'm sorry, but I couldn't find any relevant information from the web search."if database is None:database = FAISS.from_documents(web_docs, embed)else:database.add_documents(web_docs)database.save_local("faiss_database")context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])instruction_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 = topicscurrent_entities = {k: list(v) for k, v in entity_tracker.items()}while 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 count (rough estimate)estimated_tokens = len(formatted_prompt) // 4if estimated_tokens <= max_tokens - 1000: # Leave 1000 tokens for the model's responsebreak# 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))]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, instructions)all_answers.append(answer)breakexcept 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 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)sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)answer += sources_section# Update chatbot context with the answerchatbot.add_to_history(answer)return answerelse: # PDF document chat
app.py+108-37