# Import necessary libraries import os # Interacting with the operating system (reading/writing files) import logging import json # Parsing and handling JSON data import numpy as np import chromadb import chromadb.utils.embedding_functions as embedding_functions from dotenv import load_dotenv # Loading environment variables from a .env file # LangChain imports from langchain_core.documents import Document # Document data structures from langchain_core.runnables import RunnablePassthrough # LangChain core library for running pipelines from langchain_core.output_parsers import StrOutputParser # String output parser from langchain.prompts import ChatPromptTemplate # Template for chat prompts from langchain.chains.query_constructor.base import AttributeInfo # Base classes for query construction from langchain.retrievers.self_query.base import SelfQueryRetriever # Base classes for self-querying retrievers from langchain.retrievers.document_compressors import LLMChainExtractor, CrossEncoderReranker # Document compressors from langchain.retrievers import ContextualCompressionRetriever # Contextual compression retrievers # LangChain community & experimental imports from langchain_community.vectorstores import Chroma # Implementations of vector stores like Chroma from langchain_community.document_loaders import PyPDFDirectoryLoader, PyPDFLoader # Document loaders for PDFs from langchain_community.cross_encoders import HuggingFaceCrossEncoder # Cross-encoders from HuggingFace from langchain_experimental.text_splitter import SemanticChunker # Experimental text splitting methods from langchain_community.vectorstores import FAISS # FAISS vector store ## Modified, added to make it same as Section 2 from langchain_experimental.text_splitter import SemanticChunker # Experimental text splitting methods from langchain.text_splitter import ( CharacterTextSplitter, # Splitting text by characters RecursiveCharacterTextSplitter # Recursive splitting of text by characters ) from langchain_core.tools import tool from langchain.agents import create_tool_calling_agent, AgentExecutor from langchain_core.prompts import ChatPromptTemplate # LangChain OpenAI imports #from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI # OpenAI embeddings and models - #modified removed from langchain_openai import OpenAIEmbeddings, ChatOpenAI ## Modified, added ##from langchain.embeddings.openai import OpenAIEmbeddings # OpenAI embeddings for text vectors # LlamaParse & LlamaIndex imports from llama_parse import LlamaParse # Document parsing library from llama_index.core import Settings, SimpleDirectoryReader # Core functionalities of the LlamaIndex # LangGraph import from langgraph.graph import StateGraph, END, START # State graph for managing states in LangChain # Pydantic import from pydantic import BaseModel # Pydantic for data validation # Typing imports from typing import Dict, List, Tuple, Any, TypedDict # Python typing for function annotations # Other utilities import numpy as np # Numpy for numerical operations from groq import Groq from mem0 import MemoryClient import streamlit as st from datetime import datetime logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) ### Kamran added this seciton to read the keys from the secrets # Replaced the original code, this is to confirm the keys are read def load_keys(): """Reads keys from config and validates them.""" try: #config = read_config(config_file) api_key = os.getenv("OPENAI_API_KEY") endpoint = os.getenv("OPENAI_API_BASE") llamaparse_api_key = os.getenv("LLAMA_GUARD_API_KEY") mem0_api_key = os.getenv("MEM0_API_KEY") if not all([api_key, endpoint, llamaparse_api_key, mem0_api_key]): raise ValueError("Missing required API keys") # log the keys — be cautious about logging sensitive keys! ##logger.info(f"OPENAI_API_KEY: {api_key[:4]}… (length={len(api_key)})") ##logger.info(f"OPENAI_API_BASE: {endpoint}") ##logger.info(f"LLAMA_GUARD_API_KEY: {llamaparse_api_key[:4]}… (length={len(llamaparse_api_key)})") ##logger.info(f"MEM0_API_KEY: {mem0_api_key[:4]}… (length={len(mem0_api_key)})") return api_key, endpoint, llamaparse_api_key, mem0_api_key except Exception as e: logger.error(f"Error retrieving API keys: {e}") raise # 🧪 MAIN api_key, endpoint, llamaparse_api_key, mem0_api_key = load_keys() # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key. # Initialize the OpenAI Embeddings embedding_model = OpenAIEmbeddings( base_url=endpoint, # Fill in the endpoint api_key=api_key, # Fill in the API key model='text-embedding-3-small' # Fill in the model name ) # This initializes the OpenAI embeddings model using the specified endpoint, API key, and model name. # Initialize the Chat OpenAI model llm = ChatOpenAI( #openai_api_base=endpoint base_url=endpoint, ## MOdified added # openai_api_key=api_key, ## Modified removed api_key=api_key, ## Modified added model="gpt-4o-mini", streaming=False ) # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability). # set the LLM and embedding model in the LlamaIndex settings. Settings.llm = llm # Complete the code to define the LLM model ## Modfied Settings.embedding = embedding_model # Complete the code to define the embedding model ## Modified #================================Creating Langgraph agent======================# class AgentState(TypedDict): query: str # The current user query expanded_query: str # The expanded version of the user query context: List[Dict[str, Any]] # Retrieved documents (content and metadata) response: str # The generated response to the user query precision_score: float # The precision score of the response groundedness_score: float # The groundedness score of the response groundedness_loop_count: int # Counter for groundedness refinement loops precision_loop_count: int # Counter for precision refinement loops feedback: str query_feedback: str groundedness_check: bool loop_max_iter: int def expand_query(state): """ Expands the user query to improve retrieval of nutrition disorder-related information. Args: state (Dict): The current state of the workflow, containing the user query. Returns: Dict: The updated state with the expanded query. """ ### print("---------Expanding Query---------") ## Modified system_message = '''You are an expert in nutrition and health disorders. Your task is to expand user queries to improve information retrieval about nutrition disorders, symptoms, treatments, and related health topics. When expanding a query: 1. Add relevant medical and nutritional terminology 2. Include related symptoms, causes, and treatment options 3. Consider different ways the same condition might be described 4. Add context about nutrition disorders, dietary interventions, and health implications 5. Include synonyms and alternative phrasings 6. Consider both clinical and common language terms Expand the query to be more comprehensive while maintaining focus on nutrition disorder-related information.''' expand_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Expand this query: {query} using the feedback: {query_feedback}") ]) chain = expand_prompt | llm | StrOutputParser() expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]}) ### print("expanded_query", expanded_query) state["expanded_query"] = expanded_query return state # Initialize the Chroma vector store for retrieving documents vector_store = Chroma( collection_name="nutritional_hypotheticals", persist_directory="./nutritional_db", embedding_function=embedding_model ) # Create a retriever from the vector store retriever = vector_store.as_retriever( search_type='similarity', search_kwargs={'k': 3} ) def retrieve_context(state): """ Retrieves context from the vector store using the expanded or original query. Args: state (Dict): The current state of the workflow, containing the query and expanded query. Returns: Dict: The updated state with the retrieved context. """ ###print("---------retrieve_context---------") query = state['expanded_query'] # Complete the code to define the key for the expanded query ## Modified #print("Query used for retrieval:", query) # Debugging: Print the query # Retrieve documents from the vector store docs = retriever.invoke(query) ###print("Retrieved documents:", docs) # Debugging: Print the raw docs object # Extract both page_content and metadata from each document context= [ { "content": doc.page_content, # The actual content of the document "metadata": doc.metadata # The metadata (e.g., source, page number, etc.) } for doc in docs ] state['context'] = context # Complete the code to define the key for storing the context ## Modified ###print("Extracted context with metadata:", context) # Debugging: Print the extracted context #print(f"Groundedness loop count: {state['groundedness_loop_count']}") return state def craft_response(state: Dict) -> Dict: """ Generates a response using the retrieved context, focusing on nutrition disorders. Args: state (Dict): The current state of the workflow, containing the query and retrieved context. Returns: Dict: The updated state with the generated response. """ ###print("---------craft_response---------") system_message = '''You are an expert assistant specializing in nutrition disorders. Your task is to provide accurate, helpful, and evidence-based responses about nutrition-related conditions, dietary interventions, and health recommendations. Guidelines: - Use only the provided context to answer the query - Focus specifically on nutrition disorders and related topics - Provide clear, actionable information when appropriate - If the context doesn't contain sufficient information to answer the query, state this clearly - Incorporate any feedback provided to improve your response - Maintain a professional and supportive tone''' response_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}") ]) chain = response_prompt | llm | StrOutputParser() # --> Modified to make the output a string response = chain.invoke({ "query": state['query'], "context": "\n".join([doc["content"] for doc in state['context']]), "feedback": state.get('feedback','') # add feedback to the prompt ## MOdified }) state['response'] = response ###print("intermediate response: ", response) return state def score_groundedness(state: Dict) -> Dict: """ Checks whether the response is grounded in the retrieved context. Args: state (Dict): The current state of the workflow, containing the response and context. Returns: Dict: The updated state with the groundedness score. """ ###print("---------check_groundedness---------") ### MOdified system_message = '''You are a grading expert tasked with evaluating whether a response is grounded in the provided context. Your task is to score how well the response is supported by the given context on a scale from 0.0 to 1.0, where: - 1.0 = Fully grounded: All claims and information in the response are directly supported by the context - 0.8-0.9 = Mostly grounded: Most information is supported, with minor unsupported details - 0.6-0.7 = Partially grounded: Some information is supported, but significant portions are not - 0.4-0.5 = Minimally grounded: Little information is supported by the context - 0.0-0.3 = Not grounded: Most or all information is not supported by the context Guidelines: - Only consider information that is explicitly stated or can be directly inferred from the context - Be strict in your evaluation - if information is not in the context, it reduces groundedness - Return only a single numeric score (e.g., 0.85) - Do not provide explanations, just the score''' groundedness_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Context: {context}\nResponse: {response}\n\nGroundedness score:") ]) chain = groundedness_prompt | llm | StrOutputParser() groundedness_score = float(chain.invoke({ "context": "\n".join([doc["content"] for doc in state['context']]), "response": state['response'] # Complete the code to define the response ## Modified })) ###print("groundedness_score: ", groundedness_score) state['groundedness_loop_count'] += 1 ###print("#########Groundedness Incremented###########") state['groundedness_score'] = groundedness_score return state def check_precision(state: Dict) -> Dict: """ Checks whether the response precisely addresses the user’s query. Args: state (Dict): The current state of the workflow, containing the query and response. Returns: Dict: The updated state with the precision score. """ ##print("---------check_precision---------") ### MOdified system_message = '''You are an evaluation expert tasked with scoring how precisely a response addresses a user's query. Your task is to score the precision of the response on a scale from 0.0 to 1.0, where: - 1.0 = Perfect precision: The response directly and completely addresses the specific query with relevant, focused information - 0.8-0.9 = High precision: The response addresses the query well with mostly relevant information and minimal off-topic content - 0.6-0.7 = Moderate precision: The response addresses the query but includes some irrelevant information or misses some aspects - 0.4-0.5 = Low precision: The response partially addresses the query but contains significant irrelevant information or misses key aspects - 0.0-0.3 = Very low precision: The response does not address the query effectively or contains mostly irrelevant information Guidelines: - Focus on how well the response answers the specific question asked - Consider whether the response is focused and relevant to the query - Evaluate if the response addresses all parts of multi-part questions - Penalize responses that are too generic or contain excessive irrelevant information - Return only a single numeric score (e.g., 0.75) - Do not provide explanations, just the score''' precision_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:") ]) chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing ## Modified precision_score = float(chain.invoke({ "query": state['query'], "response":state['response'] # Complete the code to access the response from the state ## Modified })) state['precision_score'] = precision_score ##print("precision_score:", precision_score) state['precision_loop_count'] +=1 ##print("#########Precision Incremented###########") return state def refine_response(state: Dict) -> Dict: """ Suggests improvements for the generated response. Args: state (Dict): The current state of the workflow, containing the query and response. Returns: Dict: The updated state with response refinement suggestions. """ ##print("---------refine_response---------") ## Modified system_message = '''You are an expert assistant specializing in improving responses about nutrition disorders and health topics. Your task is to analyze a given response and provide specific, actionable suggestions for enhancement. Focus on the following aspects when suggesting improvements: 1. Accuracy: Identify any potential inaccuracies or areas where more precise information could be provided 2. Completeness: Suggest additional relevant information that would make the response more comprehensive 3. Clarity: Recommend ways to make the response clearer and more understandable 4. Structure: Suggest better organization or formatting to improve readability 5. Actionability: Recommend ways to make the response more practical and useful for the user 6. Evidence: Suggest areas where more specific evidence or examples could strengthen the response 7. Safety: Identify any health-related advice that should be qualified or made more cautious Guidelines: - Be specific and constructive in your suggestions - Focus on nutrition disorders and related health topics - Consider the user's original query when making suggestions - Provide concrete recommendations rather than general feedback - If the response is already comprehensive, suggest minor refinements or additional context - Prioritize suggestions that would most improve the response's value to the user''' refine_response_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Query: {query}\nResponse: {response}\n\n" "What improvements can be made to enhance accuracy and completeness?") ]) chain = refine_response_prompt | llm| StrOutputParser() # Store response suggestions in a structured format feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'response': state['response']})}" ##print("feedback: ", feedback) ##print(f"State: {state}") state['feedback'] = feedback return state def refine_query(state: Dict) -> Dict: """ Suggests improvements for the expanded query. Args: state (Dict): The current state of the workflow, containing the query and expanded query. Returns: Dict: The updated state with query refinement suggestions. """ ##print("---------refine_query---------") ## Modified system_message = '''You are an expert in information retrieval and query optimization, specializing in nutrition disorders and health-related topics. Your task is to analyze an expanded query and suggest improvements to enhance search effectiveness and retrieval accuracy. Focus on the following aspects when suggesting query improvements: 1. Keywords: Identify more specific, relevant, or alternative keywords that could improve retrieval 2. Medical terminology: Suggest appropriate medical or scientific terms related to nutrition disorders 3. Synonyms and variations: Recommend additional terms that might capture relevant documents 4. Specificity: Balance between being too broad (retrieving irrelevant results) and too narrow (missing relevant information) 5. Search operators: Suggest logical operators or phrase combinations that could improve precision 6. Domain-specific terms: Include nutrition-specific terminology, disorder classifications, or treatment approaches 7. Context expansion: Add contextual terms that might help retrieve more comprehensive information Guidelines: - Consider the original user intent when suggesting improvements - Focus on nutrition disorders, dietary interventions, and related health topics - Suggest specific terms or phrases to add, remove, or modify - Explain why certain changes would improve retrieval effectiveness - Consider both technical/medical terminology and common language variations - Ensure suggestions maintain relevance to the original query - Prioritize improvements that would retrieve more accurate and comprehensive context''' refine_query_prompt = ChatPromptTemplate.from_messages([ ("system", system_message), ("user", "Original Query: {query}\nExpanded Query: {expanded_query}\n\n" "What improvements can be made for a better search?") ]) chain = refine_query_prompt | llm | StrOutputParser() # Store refinement suggestions without modifying the original expanded query query_feedback = f"Previous Expanded Query: {state['expanded_query']}\nSuggestions: {chain.invoke({'query': state['query'], 'expanded_query': state['expanded_query']})}" ##print("query_feedback: ", query_feedback) ## print(f"Groundedness loop count: {state['groundedness_loop_count']}") state['query_feedback'] = query_feedback return state def should_continue_groundedness(state): """Decides if groundedness is sufficient or needs improvement.""" ##print("---------should_continue_groundedness---------") ##print("groundedness loop count: ", state['groundedness_loop_count']) if state['groundedness_score'] >= 0.9: # Complete the code to define the threshold for groundedness ## MOdified ##print("Moving to precision") return "check_precision" else: if state["groundedness_loop_count"] > state['loop_max_iter']: return "max_iterations_reached" else: ##print(f"---------Groundedness Score Threshold Not met. Refining Response-----------") return "refine_response" def should_continue_precision(state: Dict) -> str: """Decides if precision is sufficient or needs improvement.""" ##print("---------should_continue_precision---------") ##print("precision loop count: ", state['precision_loop_count']) ## Modified if state['precision_score'] >= 0.9: # Threshold for precision ## Modified return "pass" # Complete the workflow else: if state['precision_loop_count'] > state['loop_max_iter']: # Maximum allowed loops ## Modified return "max_iterations_reached" else: ##print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging return "refine_query" # Refine the query def max_iterations_reached(state: Dict) -> Dict: """Handles the case when the maximum number of iterations is reached.""" ##print("---------max_iterations_reached---------") """Handles the case when the maximum number of iterations is reached.""" response = "I'm unable to refine the response further. Please provide more context or clarify your question." state['response'] = response return state from langgraph.graph import END, StateGraph, START def create_workflow() -> StateGraph: """Creates the updated workflow for the AI nutrition agent.""" workflow = StateGraph(AgentState) # Complete the code to define the initial state of the agent ## MOdified # Add processing nodes ## MOdified workflow.add_node("expand_query", expand_query ) # Step 1: Expand user query. Complete with the function to expand the query workflow.add_node("retrieve_context", retrieve_context ) # Step 2: Retrieve relevant documents. Complete with the function to retrieve context workflow.add_node("craft_response", craft_response ) # Step 3: Generate a response based on retrieved data. Complete with the function to craft a response workflow.add_node("score_groundedness", score_groundedness ) # Step 4: Evaluate response grounding. Complete with the function to score groundedness workflow.add_node("refine_response", refine_response ) # Step 5: Improve response if it's weakly grounded. Complete with the function to refine the response workflow.add_node("check_precision", check_precision ) # Step 6: Evaluate response precision. Complete with the function to check precision workflow.add_node("refine_query", refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query workflow.add_node("max_iterations_reached", lambda state: state) # Step 8: Handle max iterations. Complete with the function to handle max iterations # Main flow edges workflow.add_edge(START, "expand_query") workflow.add_edge("expand_query", "retrieve_context") workflow.add_edge("retrieve_context", "craft_response") workflow.add_edge("craft_response", "score_groundedness") # Conditional edges based on groundedness check workflow.add_conditional_edges( "score_groundedness", should_continue_groundedness, # Use the conditional function ## Modified { "check_precision": "check_precision", # If well-grounded, proceed to precision check. ## Modified "refine_response": "refine_response", # If not, refine the response. ## Modified "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit. ## MOdified } ) workflow.add_edge("refine_response", "craft_response") # Refined responses are reprocessed. ## MOdified # Conditional edges based on precision check workflow.add_conditional_edges( "check_precision", should_continue_precision, # Use the conditional function ## Modified { "pass": END, # If precise, complete the workflow. ## Modified "refine_query": "refine_query", # If imprecise, refine the query. ## Modified "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit. ## Modified } ) workflow.add_edge("refine_query", "expand_query") # Refined queries go through expansion again. ## Modified workflow.add_edge("max_iterations_reached", END) return workflow #=========================== Defining the agentic rag tool ====================# WORKFLOW_APP = create_workflow().compile() @tool def agentic_rag(query: str): """ Runs the RAG-based agent with conversation history for context-aware responses. Args: query (str): The current user query. Returns: Dict[str, Any]: The updated state with the generated response and conversation history. """ # Initialize state with necessary parameters inputs = { "query": query.strip(), # Current user query "expanded_query": "", # Complete the code to define the expanded version of the query ## Modified "context": [], # Retrieved documents (initially empty) ## Modified "response": "", # Complete the code to define the AI-generated response ## Modified "precision_score": 0.0, # Complete the code to define the precision score of the response ## Modified "groundedness_score": 0.0, # Complete the code to define the groundedness score of the response ## Modified "groundedness_loop_count": 0, # Complete the code to define the counter for groundedness loops ## Modified "precision_loop_count": 0, # Complete the code to define the counter for precision loops ## Modified "feedback": "", # Complete the code to define the feedback ## Modified "query_feedback": "", # Complete the code to define the query feedback ## Modified "loop_max_iter": 3 # Complete the code to define the maximum number of iterations for loops ## Modified } try: ## Modified, added error checking output = WORKFLOW_APP.invoke(inputs) return output except Exception as e: ## Modified, added return {"error": f"Workflow execution failed: {str(e)}"} return output #================================ Guardrails ===========================# llama_guard_client = Groq(api_key=llamaparse_api_key) ## Modified the key variable # Function to filter user input with Llama Guard def filter_input_with_llama_guard(user_input, model="meta-llama/llama-guard-4-12b"): """ Filters user input using Llama Guard to ensure it is safe. Parameters: - user_input: The input provided by the user. - model: The Llama Guard model to be used for filtering (default is "llama-guard-3-8b"). Returns: - The filtered and safe input. """ try: # Create a request to Llama Guard to filter the user input response = llama_guard_client.chat.completions.create( messages=[{"role": "user", "content": user_input}], model=model, ) # Return the filtered input return response.choices[0].message.content.strip() except Exception as e: print(f"Error with Llama Guard: {e}") return None #============================= Adding Memory to the agent using mem0 ===============================# class NutritionBot: def __init__(self): """ Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor. """ # Initialize a memory client to store and retrieve customer interactions self.memory = MemoryClient(api_key=mem0_api_key) # Complete the code to define the memory client API key # Initialize the OpenAI client using the provided credentials self.client = ChatOpenAI( model_name = "gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version) api_key = api_key, # API key for authentication ## Modified base_url = endpoint, ## MOdified temperature=0 # Controls randomness in responses; 0 ensures deterministic results ) # Define tools available to the chatbot, such as web search tools = [agentic_rag] # Define the system prompt to set the behavior of the chatbot 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. Guidelines for Interaction: Maintain a polite, professional, and reassuring tone. Show genuine empathy for customer concerns and health challenges. Reference past interactions to provide personalized and consistent advice. Engage with the customer by asking about their food preferences, dietary restrictions, and lifestyle before offering recommendations. Ensure consistent and accurate information across conversations. If any detail is unclear or missing, proactively ask for clarification. Always use the agentic_rag tool to retrieve up-to-date and evidence-based nutrition insights. Keep track of ongoing issues and follow-ups to ensure continuity in support. Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences. """ # Build the prompt template for the agent prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), # System instructions ("human", "{input}"), # Placeholder for human input ("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps ]) # Create an agent capable of interacting with tools and executing tasks agent = create_tool_calling_agent(self.client, tools, prompt) # Wrap the agent in an executor to manage tool interactions and execution flow self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None): """ Store customer interaction in memory for future reference. Args: user_id (str): Unique identifier for the customer. message (str): Customer's query or message. response (str): Chatbot's response. metadata (Dict, optional): Additional metadata for the interaction. """ if metadata is None: metadata = {} # Add a timestamp to the metadata for tracking purposes metadata["timestamp"] = datetime.now().isoformat() # Format the conversation for storage conversation = [ {"role": "user", "content": message}, {"role": "assistant", "content": response} ] # Store the interaction in the memory client self.memory.add( conversation, user_id=user_id, output_format="v1.1", metadata=metadata ) def get_relevant_history(self, user_id: str, query: str) -> List[Dict]: """ Retrieve past interactions relevant to the current query. Args: user_id (str): Unique identifier for the customer. query (str): The customer's current query. Returns: List[Dict]: A list of relevant past interactions. """ return self.memory.search( query=query, # Search for interactions related to the query user_id=user_id, # Restrict search to the specific user limit=5 # Complete the code to define the limit for retrieved interactions ## Modified ) def handle_customer_query(self, user_id: str, query: str) -> str: """ Process a customer's query and provide a response, taking into account past interactions. Args: user_id (str): Unique identifier for the customer. query (str): Customer's query. Returns: str: Chatbot's response. """ # Retrieve relevant past interactions for context relevant_history = self.get_relevant_history(user_id, query) # Build a context string from the relevant history context = "Previous relevant interactions:\n" for memory in relevant_history: context += f"Customer: {memory['memory']}\n" # Customer's past messages context += f"Support: {memory['memory']}\n" # Chatbot's past responses context += "---\n" # Print context for debugging purposes ##print("Context: ", context) # Prepare a prompt combining past context and the current query prompt = f""" Context: {context} Current customer query: {query} Provide a helpful response that takes into account any relevant past interactions. """ # Generate a response using the agent response = self.agent_executor.invoke({"input": prompt}) # Store the current interaction for future reference self.store_customer_interaction( user_id=user_id, message=query, response=response["output"], metadata={"type": "support_query"} ) # Return the chatbot's response return response['output'] #=====================User Interface using streamlit ===========================# def nutrition_disorder_streamlit(): """ A Streamlit-based UI for the Nutrition Disorder Specialist Agent. """ st.title("Nutrition Disorder Specialist") st.write("Ask me anything about nutrition disorders, symptoms, causes, treatments, and more.") st.write("Type 'exit' to end the conversation.") logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Initialize session state for chat history and user_id if they don't exist if 'chat_history' not in st.session_state: st.session_state.chat_history = [] if 'user_id' not in st.session_state: st.session_state.user_id = None # Login form: Only if user is not logged in if st.session_state.user_id is None: with st.form("login_form", clear_on_submit=True): user_id = st.text_input("Please enter your name to begin:") submit_button = st.form_submit_button("Login") if submit_button and user_id: st.session_state.user_id = user_id st.session_state.chat_history.append({ "role": "assistant", "content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?" }) st.session_state.login_submitted = True # Set flag to trigger rerun if st.session_state.get("login_submitted", False): st.session_state.pop("login_submitted") st.rerun() else: # Display chat history for message in st.session_state.chat_history: with st.chat_message(message["role"]): st.write(message["content"]) # Chat input with custom placeholder text user_query = st.chat_input("Type your question here (or 'exit' to end) ...") # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...") ## MOdified if user_query: if user_query.lower() == "exit": st.session_state.chat_history.append({"role": "user", "content": "exit"}) with st.chat_message("user"): st.write("exit") goodbye_msg = "Goodbye! Feel free to return if you have more questions about nutrition disorders." st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg}) with st.chat_message("assistant"): st.write(goodbye_msg) st.session_state.user_id = None st.rerun() return st.session_state.chat_history.append({"role": "user", "content": user_query}) with st.chat_message("user"): st.write(user_query) # Filter input using Llama Guard filtered_result = filter_input_with_llama_guard(user_query) # Call function to filter input ## Modified filtered_result = filtered_result.replace("\n", " ").strip().upper() # Normalize the result and remove whitespace ## Modified, added .string() to remove any leading or training spaces and change to upper case # Debug print to see exact output ##logger.info(f"Filter result: '{filtered_result}'") # Check if input is safe based on allowed statuses if filtered_result in ["SAFE", "S6", "S7"]: # --> Modified try: if 'chatbot' not in st.session_state: st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot) ## Modified response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query) ## Modified # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query) st.write(response) st.session_state.chat_history.append({"role": "assistant", "content": response}) except Exception as e: error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}" st.write(error_msg) st.session_state.chat_history.append({"role": "assistant", "content": error_msg}) else: inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again." st.write(inappropriate_msg) st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg}) if __name__ == "__main__": nutrition_disorder_streamlit()