Update app.py
Browse files21 unmodified lines22232425252627282930313233349 unmodified lines3833843853863873883893903913923933943953963973983994004014024034044054064077 unmodified lines41541641739841841942042142242340440540642440842542642742814 unmodified lines4434444454294304464474484499 unmodified lines4594604614464474484494504624634644654524664674684695 unmodified lines47547647747847948048146448248348448548648746948848949049117 unmodified lines5095105114935125135144964975155165175186 unmodified lines52552652751051152852953053153253353453553653751753853954054154252154354454554666 unmodified lines61361461561661761861962062162260062362462562621 unmodified linesfrom langchain_community.llms import HuggingFaceHubfrom langchain_core.documents import Documentfrom sentence_transformers import SentenceTransformerfrom llama_parse import LlamaParsefrom llama_parse import from llama_cpp import Llamafrom llama_cpp_agent.llm_agent import LlamaCppAgentfrom llama_cpp_agent.messages_formatter import MessagesFormatterTypefrom llama_cpp_agent.providers.llama_cpp_endpoint_provider import LlamaCppEndpointSettings
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")349 unmodified lines return truncate_text(context, max_tokens)
# Initialize LlamaCppAgentdef initialize_llama_cpp_agent(): main_model = LlamaCppEndpointSettings( completions_endpoint_url="http://127.0.0.1:8080/completion" ) llama_cpp_agent = LlamaCppAgent( main_model, debug_output=False, system_prompt="You are an AI assistant designed to help with RAG tasks.", predefined_messages_formatter_type=MessagesFormatterType.CHATML ) return llama_cpp_agent
# Modify the ask_question function to use LlamaCppAgentdef ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot, user_instructions): if not question: return "Please enter a question."
llama_cpp_agent = initialize_llama_cpp_agent() model = get_model(temperature, top_p, repetition_penalty) # Update the chatbot's model7 unmodified lines database = None
max_attempts = 3 max_input_tokens = 20000 # Leave room for the model's response max_input_tokens = 20000 max_output_tokens = 800
if web_search: contextualized_question, topics, entity_tracker, _ = chatbot.process_question(question) print(f"Contextualized question: {contextualized_question}") print(f"User Instructions: {user_instructions}") try: search_results = google_search(contextualized_question, num_results=5) # Increased from 3 to 5 search_results = google_search(contextualized_question, num_results=5) 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)}"14 unmodified lines
database.save_local("faiss_database")
# Prepare context using reranking context_str = prepare_context(contextualized_question, web_docs, max_input_tokens // 2) # Use half of max_input_tokens for context context_str = prepare_context(contextualized_question, web_docs, max_input_tokens // 2)
instruction_prompt = f"User Instructions: {user_instructions}\n" if user_instructions else ""
9 unmodified lines Provide a concise and relevant answer to the question. """
prompt_val = ChatPromptTemplate.from_template(prompt_template) current_conv_context = truncate_text(chatbot.get_context(), max_input_tokens // 4) # Use quarter of max_input_tokens for conversation context current_topics = topics[:5] # Limit to top 5 topics current_entities = {k: list(v)[:3] for k, v in entity_tracker.items()} # Limit to top 3 entities per type current_conv_context = truncate_text(chatbot.get_context(), max_input_tokens // 4) current_topics = topics[:5] current_entities = {k: list(v)[:3] for k, v in entity_tracker.items()}
formatted_prompt = prompt_val.format( formatted_prompt = prompt_template.format( context=context_str, conv_context=current_conv_context, question=question,5 unmodified lines formatted_prompt = truncate_text(formatted_prompt, max_input_tokens)
try: # Use LlamaCppAgent for initial response generation initial_response = llama_cpp_agent.get_chat_response(formatted_prompt, temperature=temperature) # Use generate_chunked_response for further refinement if needed full_response = generate_chunked_response(model, formatted_prompt, max_tokens=max_output_tokens) full_response = generate_chunked_response(model, initial_response, max_tokens=max_output_tokens) answer = extract_answer(full_response, user_instructions) all_answers.append(answer) break except Exception as e: print(f"Error in generate_chunked_response: {e}") print(f"Error in response generation: {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.")
17 unmodified lines if database is None: return "No documents available. Please upload PDF documents to answer questions."
retriever = database.as_retriever(search_kwargs={"k": 10}) # Retrieve more documents for reranking retriever = database.as_retriever(search_kwargs={"k": 5}) relevant_docs = retriever.get_relevant_documents(question) # Prepare context using reranking context_str = prepare_context(question, relevant_docs, max_input_tokens // 2) # Use half of max_input_tokens for context context_str = prepare_context(question, relevant_docs, max_input_tokens // 2)
instruction_prompt = f"User Instructions: {user_instructions}\n" if user_instructions else ""
6 unmodified lines Provide a summarized and direct answer to the question. """
prompt_val = ChatPromptTemplate.from_template(prompt_template) formatted_prompt = prompt_val.format(context=context_str, question=question) formatted_prompt = prompt_template.format(context=context_str, question=question)
if estimate_tokens(formatted_prompt) > max_input_tokens: formatted_prompt = truncate_text(formatted_prompt, max_input_tokens)
try: # Use LlamaCppAgent for initial response generation initial_response = llama_cpp_agent.get_chat_response(formatted_prompt, temperature=temperature) # Use generate_chunked_response for further refinement if needed full_response = generate_chunked_response(model, formatted_prompt, max_tokens=max_output_tokens) full_response = generate_chunked_response(model, initial_response, max_tokens=max_output_tokens) answer = extract_answer(full_response, user_instructions) return answer except Exception as e: print(f"Error in generate_chunked_response: {e}") print(f"Error in response generation: {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."
66 unmodified lines
enhanced_context_driven_chatbot = EnhancedContextDrivenChatbot()
# Update the chat function to use the modified ask_question function def chat(question, history, temperature, top_p, repetition_penalty, web_search, user_instructions): answer = ask_question(question, temperature, top_p, repetition_penalty, web_search, enhanced_context_driven_chatbot, user_instructions) history.append((question, answer)) return "", history submit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox, instructions_input], outputs=[question_input, chatbot])
clear_button = gr.Button("Clear Cache") clear_output = gr.Textbox(label="Cache Status") clear_button.click(clear_cache, inputs=[], outputs=clear_output)app.py+47-24