Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -572,10 +572,10 @@ class NutritionBot:
|
|
572 |
"""
|
573 |
Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
|
574 |
"""
|
575 |
-
|
576 |
# Initialize a memory client to store and retrieve customer interactions
|
577 |
self.memory = MemoryClient(api_key=MEM0_api_key) # userdata.get("mem0")) # Complete the code to define the memory client API key
|
578 |
-
|
579 |
|
580 |
# Initialize the OpenAI client using the provided credentials
|
581 |
self.client = ChatOpenAI(
|
@@ -583,11 +583,11 @@ class NutritionBot:
|
|
583 |
api_key=api_key, # API key for authentication
|
584 |
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
|
585 |
)
|
586 |
-
|
587 |
|
588 |
# Define tools available to the chatbot, such as web search
|
589 |
tools = [agentic_rag]
|
590 |
-
|
591 |
|
592 |
# Define the system prompt to set the behavior of the chatbot
|
593 |
system_prompt = """You are a caring and knowledgeable Medical Support Agent, specializing in nutrition disorder-related guidance. Your goal is to provide accurate, empathetic, and tailored nutritional recommendations while ensuring a seamless customer experience.
|
@@ -603,7 +603,7 @@ class NutritionBot:
|
|
603 |
Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
|
604 |
|
605 |
"""
|
606 |
-
|
607 |
|
608 |
# Build the prompt template for the agent
|
609 |
prompt = ChatPromptTemplate.from_messages([
|
@@ -611,15 +611,15 @@ class NutritionBot:
|
|
611 |
("human", "{input}"), # Placeholder for human input
|
612 |
("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
|
613 |
])
|
614 |
-
|
615 |
|
616 |
# Create an agent capable of interacting with tools and executing tasks
|
617 |
agent = create_tool_calling_agent(self.client, tools, prompt)
|
618 |
-
|
619 |
|
620 |
# Wrap the agent in an executor to manage tool interactions and execution flow
|
621 |
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
622 |
-
|
623 |
|
624 |
|
625 |
def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
|
@@ -685,7 +685,7 @@ class NutritionBot:
|
|
685 |
|
686 |
# Retrieve relevant past interactions for context
|
687 |
relevant_history = self.get_relevant_history(user_id, query)
|
688 |
-
|
689 |
|
690 |
# Build a context string from the relevant history
|
691 |
context = "Previous relevant interactions:\n"
|
@@ -695,7 +695,7 @@ class NutritionBot:
|
|
695 |
context += "---\n"
|
696 |
|
697 |
# Print context for debugging purposes
|
698 |
-
|
699 |
|
700 |
# Prepare a prompt combining past context and the current query
|
701 |
prompt = f"""
|
@@ -710,7 +710,7 @@ class NutritionBot:
|
|
710 |
|
711 |
# Generate a response using the agent
|
712 |
response = self.agent_executor.invoke({"input": prompt})
|
713 |
-
|
714 |
|
715 |
# Store the current interaction for future reference
|
716 |
self.store_customer_interaction(
|
@@ -719,7 +719,7 @@ class NutritionBot:
|
|
719 |
response=response["output"],
|
720 |
metadata={"type": "support_query"}
|
721 |
)
|
722 |
-
|
723 |
|
724 |
|
725 |
# Return the chatbot's response
|
@@ -787,17 +787,17 @@ def nutrition_disorder_streamlit():
|
|
787 |
|
788 |
# Check if input is safe based on allowed statuses
|
789 |
if filtered_result in ["safe", "safe S7", "safe S6"]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
|
790 |
-
|
791 |
try:
|
792 |
if 'chatbot' not in st.session_state:
|
793 |
-
|
794 |
st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
|
795 |
else:
|
796 |
-
|
797 |
|
798 |
-
|
799 |
response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
|
800 |
-
|
801 |
# Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
|
802 |
st.write(response)
|
803 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
|
|
572 |
"""
|
573 |
Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
|
574 |
"""
|
575 |
+
st.write("Entered NutritionBot...")
|
576 |
# Initialize a memory client to store and retrieve customer interactions
|
577 |
self.memory = MemoryClient(api_key=MEM0_api_key) # userdata.get("mem0")) # Complete the code to define the memory client API key
|
578 |
+
st.write("Got memory...")
|
579 |
|
580 |
# Initialize the OpenAI client using the provided credentials
|
581 |
self.client = ChatOpenAI(
|
|
|
583 |
api_key=api_key, # API key for authentication
|
584 |
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
|
585 |
)
|
586 |
+
st.write("Got client...")
|
587 |
|
588 |
# Define tools available to the chatbot, such as web search
|
589 |
tools = [agentic_rag]
|
590 |
+
st.write("Got tools...")
|
591 |
|
592 |
# Define the system prompt to set the behavior of the chatbot
|
593 |
system_prompt = """You are a caring and knowledgeable Medical Support Agent, specializing in nutrition disorder-related guidance. Your goal is to provide accurate, empathetic, and tailored nutritional recommendations while ensuring a seamless customer experience.
|
|
|
603 |
Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
|
604 |
|
605 |
"""
|
606 |
+
st.write("Got system prompt...")
|
607 |
|
608 |
# Build the prompt template for the agent
|
609 |
prompt = ChatPromptTemplate.from_messages([
|
|
|
611 |
("human", "{input}"), # Placeholder for human input
|
612 |
("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
|
613 |
])
|
614 |
+
st.write("Got user prompt...")
|
615 |
|
616 |
# Create an agent capable of interacting with tools and executing tasks
|
617 |
agent = create_tool_calling_agent(self.client, tools, prompt)
|
618 |
+
st.write("Got agent...")
|
619 |
|
620 |
# Wrap the agent in an executor to manage tool interactions and execution flow
|
621 |
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
622 |
+
st.write("Got executor...")
|
623 |
|
624 |
|
625 |
def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
|
|
|
685 |
|
686 |
# Retrieve relevant past interactions for context
|
687 |
relevant_history = self.get_relevant_history(user_id, query)
|
688 |
+
st.write("Got history...")
|
689 |
|
690 |
# Build a context string from the relevant history
|
691 |
context = "Previous relevant interactions:\n"
|
|
|
695 |
context += "---\n"
|
696 |
|
697 |
# Print context for debugging purposes
|
698 |
+
st.write("Context: ", context)
|
699 |
|
700 |
# Prepare a prompt combining past context and the current query
|
701 |
prompt = f"""
|
|
|
710 |
|
711 |
# Generate a response using the agent
|
712 |
response = self.agent_executor.invoke({"input": prompt})
|
713 |
+
st.write("Got response...")
|
714 |
|
715 |
# Store the current interaction for future reference
|
716 |
self.store_customer_interaction(
|
|
|
719 |
response=response["output"],
|
720 |
metadata={"type": "support_query"}
|
721 |
)
|
722 |
+
st.write("Got output...")
|
723 |
|
724 |
|
725 |
# Return the chatbot's response
|
|
|
787 |
|
788 |
# Check if input is safe based on allowed statuses
|
789 |
if filtered_result in ["safe", "safe S7", "safe S6"]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
|
790 |
+
st.write("Got a safe input...")
|
791 |
try:
|
792 |
if 'chatbot' not in st.session_state:
|
793 |
+
st.write("In not in st.session_state...")
|
794 |
st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
|
795 |
else:
|
796 |
+
st.write("In st.session_state...")
|
797 |
|
798 |
+
st.write("Getting response...")
|
799 |
response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
|
800 |
+
st.write(f"Got response: {response}")
|
801 |
# Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
|
802 |
st.write(response)
|
803 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|