Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -572,15 +572,10 @@ class NutritionBot:
|
|
572 |
"""
|
573 |
Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
|
574 |
"""
|
575 |
-
|
576 |
-
st.write(f"MEM0_api_key: {MEM0_api_key}")
|
577 |
# Initialize a memory client to store and retrieve customer interactions
|
578 |
self.memory = MemoryClient(api_key=MEM0_api_key) # userdata.get("mem0")) # Complete the code to define the memory client API key
|
579 |
|
580 |
-
st.write("Got memory...")
|
581 |
-
st.write(f"api_key: {api_key}")
|
582 |
-
st.write(f"my_api_key: {my_api_key}")
|
583 |
-
|
584 |
# Initialize the OpenAI client using the provided credentials
|
585 |
self.client = ChatOpenAI(
|
586 |
model_name="gpt-4o", # Used gpt-4o to get improved results; Specify the model to use (e.g., GPT-4 optimized version)
|
@@ -589,11 +584,9 @@ class NutritionBot:
|
|
589 |
api_key="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",
|
590 |
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
|
591 |
)
|
592 |
-
st.write("Got client...")
|
593 |
|
594 |
# Define tools available to the chatbot, such as web search
|
595 |
tools = [agentic_rag]
|
596 |
-
st.write("Got tools...")
|
597 |
|
598 |
# Define the system prompt to set the behavior of the chatbot
|
599 |
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.
|
@@ -609,7 +602,6 @@ class NutritionBot:
|
|
609 |
Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
|
610 |
|
611 |
"""
|
612 |
-
st.write("Got system prompt...")
|
613 |
|
614 |
# Build the prompt template for the agent
|
615 |
prompt = ChatPromptTemplate.from_messages([
|
@@ -617,16 +609,12 @@ class NutritionBot:
|
|
617 |
("human", "{input}"), # Placeholder for human input
|
618 |
("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
|
619 |
])
|
620 |
-
st.write("Got user prompt...")
|
621 |
|
622 |
# Create an agent capable of interacting with tools and executing tasks
|
623 |
agent = create_tool_calling_agent(self.client, tools, prompt)
|
624 |
-
st.write("Got agent...")
|
625 |
|
626 |
# Wrap the agent in an executor to manage tool interactions and execution flow
|
627 |
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
628 |
-
st.write("Got executor...")
|
629 |
-
|
630 |
|
631 |
def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
|
632 |
"""
|
@@ -691,39 +679,26 @@ class NutritionBot:
|
|
691 |
|
692 |
# Retrieve relevant past interactions for context
|
693 |
relevant_history = self.get_relevant_history(user_id, query)
|
694 |
-
st.write("Got history...")
|
695 |
|
696 |
# Build a context string from the relevant history
|
697 |
context = "Previous relevant interactions:\n"
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
#context += f"""{context}"""
|
704 |
-
st.write("Got context...")
|
705 |
|
706 |
# Print context for debugging purposes
|
707 |
-
st.write("Context: ", context)
|
708 |
|
709 |
# Prepare a prompt combining past context and the current query
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
#"""
|
716 |
-
#prompt = f"""
|
717 |
-
#Context: {context}
|
718 |
-
#Current customer query: {query}
|
719 |
-
#Provide a helpful response that takes into account any relevant past interactions.
|
720 |
-
#"""
|
721 |
-
#print(f"Prepare for invoke...{prompt_str}")
|
722 |
-
print(f"Prepare for invoke...{context}")
|
723 |
|
724 |
# Generate a response using the agent
|
725 |
-
response = self.agent_executor.invoke({"input":
|
726 |
-
st.write("Got response...")
|
727 |
|
728 |
# Store the current interaction for future reference
|
729 |
self.store_customer_interaction(
|
@@ -732,8 +707,6 @@ class NutritionBot:
|
|
732 |
response=response["output"],
|
733 |
metadata={"type": "support_query"}
|
734 |
)
|
735 |
-
st.write("Got output...")
|
736 |
-
|
737 |
|
738 |
# Return the chatbot's response
|
739 |
return response['output']
|
|
|
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 |
# Initialize the OpenAI client using the provided credentials
|
580 |
self.client = ChatOpenAI(
|
581 |
model_name="gpt-4o", # Used gpt-4o to get improved results; Specify the model to use (e.g., GPT-4 optimized version)
|
|
|
584 |
api_key="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",
|
585 |
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
|
586 |
)
|
|
|
587 |
|
588 |
# Define tools available to the chatbot, such as web search
|
589 |
tools = [agentic_rag]
|
|
|
590 |
|
591 |
# Define the system prompt to set the behavior of the chatbot
|
592 |
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.
|
|
|
602 |
Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
|
603 |
|
604 |
"""
|
|
|
605 |
|
606 |
# Build the prompt template for the agent
|
607 |
prompt = ChatPromptTemplate.from_messages([
|
|
|
609 |
("human", "{input}"), # Placeholder for human input
|
610 |
("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
|
611 |
])
|
|
|
612 |
|
613 |
# Create an agent capable of interacting with tools and executing tasks
|
614 |
agent = create_tool_calling_agent(self.client, tools, prompt)
|
|
|
615 |
|
616 |
# Wrap the agent in an executor to manage tool interactions and execution flow
|
617 |
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
|
|
|
|
618 |
|
619 |
def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
|
620 |
"""
|
|
|
679 |
|
680 |
# Retrieve relevant past interactions for context
|
681 |
relevant_history = self.get_relevant_history(user_id, query)
|
|
|
682 |
|
683 |
# Build a context string from the relevant history
|
684 |
context = "Previous relevant interactions:\n"
|
685 |
+
for memory in relevant_history:
|
686 |
+
context += f"Customer: {memory['memory']}\n" # Customer's past messages
|
687 |
+
context += f"Support: {memory['memory']}\n" # Chatbot's past responses
|
688 |
+
context += "---\n"
|
|
|
|
|
|
|
689 |
|
690 |
# Print context for debugging purposes
|
691 |
+
#st.write("Context: ", context)
|
692 |
|
693 |
# Prepare a prompt combining past context and the current query
|
694 |
+
prompt = f"""
|
695 |
+
Context: {context}
|
696 |
+
Current customer query: {query}
|
697 |
+
Provide a helpful response that takes into account any relevant past interactions.
|
698 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
699 |
|
700 |
# Generate a response using the agent
|
701 |
+
response = self.agent_executor.invoke({"input": prompt})
|
|
|
702 |
|
703 |
# Store the current interaction for future reference
|
704 |
self.store_customer_interaction(
|
|
|
707 |
response=response["output"],
|
708 |
metadata={"type": "support_query"}
|
709 |
)
|
|
|
|
|
710 |
|
711 |
# Return the chatbot's response
|
712 |
return response['output']
|