Update app.py
Browse files43 unmodified lines4445464748495051525354555657585960160 unmodified lines221222223224225226102 unmodified lines32933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244343 unmodified linesnlp = load_spacy_model()class EnhancedContextDrivenChatbot:def __init__(self, history_size=10, model=None):self.history = []self.history_size = history_sizeself.entity_tracker = {}self.conversation_context = ""self.model = modelself.last_instructions = Nonedef add_to_history(self, text):self.history.append(text)if len(self.history) > self.history_size:self.history.pop(0)# Update entity tracker160 unmodified lineshuggingfacehub_api_token=huggingface_token)def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):full_response = ""for i in range(max_chunks):102 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 = 5context_reduction_factor = 0.7max_tokens = 32000 # Maximum tokens allowed by the modelif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}search_results = google_search(contextualized_question, num_results=3)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 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 = serializable_entity_trackerwhile 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_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 chat43 unmodified lines444546474849505152535455565758596061160 unmodified lines222223224225226227228229230231232233234235236237238239240241242243244245246247248249102 unmodified lines35235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140243 unmodified linesnlp = load_spacy_model()class EnhancedContextDrivenChatbot:def __init__(self, history_size: int = 10, max_history_chars: int = 5000):self.history = []self.history_size = history_sizeself.max_history_chars = max_history_charsself.entity_tracker = {}self.conversation_context = ""self.model = Noneself.last_instructions = Nonedef add_to_history(self, text: str):self.history.append(text)while len(' '.join(self.history)) > self.max_history_chars or len(self.history) > self.history_size:self.history.pop(0)# Update entity tracker160 unmodified lineshuggingfacehub_api_token=huggingface_token)MAX_PROMPT_CHARS = 24000 # Adjust based on your model's limitationsdef chunk_text(text: str, max_chunk_size: int = 1000) -> List[str]:chunks = []current_chunk = ""for sentence in re.split(r'(?<=[.!?])\s+', text):if len(current_chunk) + len(sentence) > max_chunk_size:chunks.append(current_chunk.strip())current_chunk = sentenceelse:current_chunk += " " + sentenceif current_chunk:chunks.append(current_chunk.strip())return chunksdef get_most_relevant_chunks(question: str, chunks: List[str], top_k: int = 3) -> List[str]:question_embedding = sentence_model.encode([question])[0]chunk_embeddings = sentence_model.encode(chunks)similarities = cosine_similarity([question_embedding], chunk_embeddings)[0]top_indices = np.argsort(similarities)[-top_k:]return [chunks[i] for i in top_indices]def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):full_response = ""for i in range(max_chunks):102 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)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 chat
app.py+63-104