nitind1 commited on
Commit
ba291a6
·
verified ·
1 Parent(s): d32c27f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +1193 -0
app.py ADDED
@@ -0,0 +1,1193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ######################## WRITE YOUR CODE HERE #########################
3
+ # Import necessary libraries
4
+ import os # Interacting with the operating system (reading/writing files)
5
+ import chromadb # High-performance vector database for storing/querying dense vectors
6
+ from dotenv import load_dotenv # Loading environment variables from a .env file
7
+ import json # Parsing and handling JSON data
8
+
9
+ # LangChain imports
10
+ from langchain_core.documents import Document # Document data structures
11
+ from langchain_core.runnables import RunnablePassthrough # LangChain core library for running pipelines
12
+ from langchain_core.output_parsers import StrOutputParser # String output parser
13
+ from langchain.prompts import ChatPromptTemplate # Template for chat prompts
14
+ from langchain.chains.query_constructor.base import AttributeInfo # Base classes for query construction
15
+ from langchain.retrievers.self_query.base import SelfQueryRetriever # Base classes for self-querying retrievers
16
+ from langchain.retrievers.document_compressors import LLMChainExtractor, CrossEncoderReranker # Document compressors
17
+ from langchain.retrievers import ContextualCompressionRetriever # Contextual compression retrievers
18
+
19
+ # LangChain community & experimental imports
20
+ from langchain_community.vectorstores import Chroma # Implementations of vector stores like Chroma
21
+ from langchain_community.document_loaders import PyPDFDirectoryLoader, PyPDFLoader # Document loaders for PDFs
22
+ from langchain_community.cross_encoders import HuggingFaceCrossEncoder # Cross-encoders from HuggingFace
23
+ from langchain_experimental.text_splitter import SemanticChunker # Experimental text splitting methods
24
+ from langchain.text_splitter import (
25
+ CharacterTextSplitter, # Splitting text by characters
26
+ RecursiveCharacterTextSplitter # Recursive splitting of text by characters
27
+ )
28
+ from langchain_core.tools import tool
29
+ from langchain.agents import create_tool_calling_agent, AgentExecutor
30
+ from langchain_core.prompts import ChatPromptTemplate
31
+
32
+ # LangChain OpenAI imports
33
+ from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI # OpenAI embeddings and models
34
+ from langchain.embeddings.openai import OpenAIEmbeddings # OpenAI embeddings for text vectors
35
+
36
+ # LlamaParse & LlamaIndex imports
37
+ from llama_parse import LlamaParse # Document parsing library
38
+ from llama_index.core import Settings, SimpleDirectoryReader # Core functionalities of the LlamaIndex
39
+
40
+ # LangGraph import
41
+ from langgraph.graph import StateGraph, END, START # State graph for managing states in LangChain
42
+
43
+ # Pydantic import
44
+ from pydantic import BaseModel # Pydantic for data validation
45
+
46
+ # Typing imports
47
+ from typing import Dict, List, Tuple, Any, TypedDict # Python typing for function annotations
48
+
49
+ # Other utilities
50
+ import numpy as np # Numpy for numerical operations
51
+ from groq import Groq
52
+ from mem0 import MemoryClient
53
+ import streamlit as st
54
+ from datetime import datetime
55
+
56
+
57
+ #====================================SETUP=====================================#
58
+ # Fetch secrets from Hugging Face Spaces
59
+ api_key = config.get("API_KEY")
60
+ endpoint = config.get("OPENAI_API_BASE")
61
+ llama_api_key = os.environ['GROQ_API_KEY']
62
+ MEM0_api_key = os.environ['mem0']
63
+
64
+ # Initialize the OpenAI embedding function for Chroma
65
+ embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
66
+ api_base=endpoint, # Complete the code to define the API base endpoint
67
+ api_key=api_key, # Complete the code to define the API key
68
+ model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
69
+ )
70
+
71
+ # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key.
72
+
73
+ # Initialize the OpenAI Embeddings
74
+ embedding_model = OpenAIEmbeddings(
75
+ openai_api_base=endpoint,
76
+ openai_api_key=api_key,
77
+ model='text-embedding-ada-002'
78
+ )
79
+
80
+
81
+ # Initialize the Chat OpenAI model
82
+ llm = ChatOpenAI(
83
+ openai_api_base=endpoint,
84
+ openai_api_key=api_key,
85
+ model="gpt-4o-mini",
86
+ streaming=False
87
+ )
88
+ # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
89
+
90
+ # set the LLM and embedding model in the LlamaIndex settings.
91
+ Settings.llm = llm # Complete the code to define the LLM model
92
+ Settings.embedding = embedding_model # Complete the code to define the embedding model
93
+
94
+ #================================Creating Langgraph agent======================#
95
+
96
+ class AgentState(TypedDict):
97
+ query: str # The current user query
98
+ expanded_query: str # The expanded version of the user query
99
+ context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
100
+ response: str # The generated response to the user query
101
+ precision_score: float # The precision score of the response
102
+ groundedness_score: float # The groundedness score of the response
103
+ groundedness_loop_count: int # Counter for groundedness refinement loops
104
+ precision_loop_count: int # Counter for precision refinement loops
105
+ feedback: str
106
+ query_feedback: str
107
+ groundedness_check: bool
108
+ loop_max_iter: int
109
+
110
+ def expand_query(state):
111
+ """
112
+ Expands the user query to improve retrieval of nutrition disorder-related information.
113
+
114
+ Args:
115
+ state (Dict): The current state of the workflow, containing the user query.
116
+
117
+ Returns:
118
+ Dict: The updated state with the expanded query.
119
+ """
120
+ print("---------Expanding Query---------")
121
+ system_message = '''You are a domain expert assisting in answering questions related to nutrition disorder-related information.
122
+ Convert the user query into something that a healthcare professional would understand. Use domain related words.
123
+ Perform query expansion on the question received. If there are multiple common ways of phrasing a user question \
124
+ or common synonyms for key words in the question, make sure to return multiple versions \
125
+ of the query with the different phrasings.
126
+
127
+ If the query has multiple parts, split them into separate simpler queries. This is the only case where you can generate more than 3 queries.
128
+
129
+ If there are acronyms or words you are not familiar with, do not try to rephrase them.
130
+
131
+ Return only 3 versions of the question as a list.
132
+ Generate only a list of questions. Do not mention anything before or after the list.
133
+
134
+ Question:
135
+ {query}'''
136
+
137
+
138
+ expand_prompt = ChatPromptTemplate.from_messages([
139
+ ("system", system_message),
140
+ ("user", "Expand this query: {query} using the feedback: {query_feedback}")
141
+
142
+ ])
143
+
144
+ chain = expand_prompt | llm | StrOutputParser()
145
+ expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]})
146
+ print("expanded_query", expanded_query)
147
+ state["expanded_query"] = expanded_query
148
+ return state
149
+
150
+
151
+ # Initialize the Chroma vector store for retrieving documents
152
+ vector_store = Chroma(
153
+ collection_name="nutritional_research",
154
+ persist_directory="./nutrition_db",
155
+ embedding_function=embedding_model
156
+
157
+ )
158
+
159
+ # Create a retriever from the vector store
160
+ retriever = vector_store.as_retriever(
161
+ search_type='similarity',
162
+ search_kwargs={'k': 3}
163
+ )
164
+
165
+ def retrieve_context(state):
166
+ """
167
+ Retrieves context from the vector store using the expanded or original query.
168
+
169
+ Args:
170
+ state (Dict): The current state of the workflow, containing the query and expanded query.
171
+
172
+ Returns:
173
+ Dict: The updated state with the retrieved context.
174
+ """
175
+ print("---------retrieve_context---------")
176
+ query = state['expanded_query'] # Complete the code to define the key for the expanded query
177
+ #print("Query used for retrieval:", query) # Debugging: Print the query
178
+
179
+ # Retrieve documents from the vector store
180
+ docs = retriever.invoke(query)
181
+ print("Retrieved documents:", docs) # Debugging: Print the raw docs object
182
+
183
+ # Extract both page_content and metadata from each document
184
+ context= [
185
+ {
186
+ "content": doc.page_content, # The actual content of the document
187
+ "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
188
+ }
189
+ for doc in docs
190
+ ]
191
+ state['context'] = context # Complete the code to define the key for storing the context
192
+ print("Extracted context with metadata:", context)
193
+
194
+ return state
195
+
196
+
197
+
198
+ def craft_response(state: Dict) -> Dict:
199
+ """
200
+ Generates a response using the retrieved context, focusing on nutrition disorders.
201
+
202
+ Args:
203
+ state (Dict): The current state of the workflow, containing the query and retrieved context.
204
+
205
+ Returns:
206
+ Dict: The updated state with the generated response.
207
+ """
208
+ print("---------craft_response---------")
209
+ system_message = '''you are a smart nutrition disorder specialist. Use the context and feedback to respond to the query.
210
+ The answer you provide must come from the context and feedback provided.
211
+ If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
212
+
213
+ response_prompt = ChatPromptTemplate.from_messages([
214
+ ("system", system_message),
215
+ ("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")
216
+ ])
217
+
218
+ chain = response_prompt | llm
219
+ response = chain.invoke({
220
+ "query": state['query'],
221
+ "context": "\n".join([doc["content"] for doc in state['context']]),
222
+ "feedback": state['feedback'] # add feedback to the prompt
223
+ })
224
+ state['response'] = response
225
+ print("intermediate response: ", response)
226
+
227
+ return state
228
+
229
+
230
+
231
+ def score_groundedness(state: Dict) -> Dict:
232
+ """
233
+ Checks whether the response is grounded in the retrieved context.
234
+
235
+ Args:
236
+ state (Dict): The current state of the workflow, containing the response and context.
237
+
238
+ Returns:
239
+ Dict: The updated state with the groundedness score.
240
+ """
241
+ print("---------check_groundedness---------")
242
+ system_message = '''Your task is to judge the groundedness of the response based on the context.
243
+ For each statement you must return verdict as 1 if the response is completely grounded in the context and 0 if the response is completely hallucinated.'''
244
+
245
+ groundedness_prompt = ChatPromptTemplate.from_messages([
246
+ ("system", system_message),
247
+ ("user", "Context: {context}\nResponse: {response}\n\nGroundedness score:")
248
+ ])
249
+
250
+ chain = groundedness_prompt | llm | StrOutputParser()
251
+ groundedness_score = float(chain.invoke({
252
+ "context": "\n".join([doc["content"] for doc in state['context']]),
253
+ "response": state['response'] # Complete the code to define the response
254
+ }))
255
+ print("groundedness_score: ", groundedness_score)
256
+ state['groundedness_loop_count'] += 1
257
+ print("#########Groundedness Incremented###########")
258
+ state['groundedness_score'] = groundedness_score
259
+
260
+ return state
261
+
262
+
263
+
264
+ def check_precision(state: Dict) -> Dict:
265
+ """
266
+ Checks whether the response precisely addresses the user’s query.
267
+
268
+ Args:
269
+ state (Dict): The current state of the workflow, containing the query and response.
270
+
271
+ Returns:
272
+ Dict: The updated state with the precision score.
273
+ """
274
+ print("---------check_precision---------")
275
+ system_message = '''Given the query, response and context verify if the context was useful in arriving at the given answer.
276
+ Give precision score of "1" if useful and "0" if it was not useful.'''
277
+
278
+ precision_prompt = ChatPromptTemplate.from_messages([
279
+ ("system", system_message),
280
+ ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
281
+ ])
282
+
283
+ chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing
284
+ precision_score = float(chain.invoke({
285
+ "query": state['query'],
286
+ "response":state['response'] # Complete the code to access the response from the state
287
+ }))
288
+ state['precision_score'] = precision_score
289
+ print("precision_score:", precision_score)
290
+ state['precision_loop_count'] +=1
291
+ print("#########Precision Incremented###########")
292
+ return state
293
+
294
+
295
+
296
+ def refine_response(state: Dict) -> Dict:
297
+ """
298
+ Suggests improvements for the generated response.
299
+
300
+ Args:
301
+ state (Dict): The current state of the workflow, containing the query and response.
302
+
303
+ Returns:
304
+ Dict: The updated state with response refinement suggestions.
305
+ """
306
+ print("---------refine_response---------")
307
+
308
+ system_message = '''Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
309
+
310
+ refine_response_prompt = ChatPromptTemplate.from_messages([
311
+ ("system", system_message),
312
+ ("user", "Query: {query}\nResponse: {response}\n\n"
313
+ "What improvements can be made to enhance accuracy and completeness?")
314
+ ])
315
+
316
+ chain = refine_response_prompt | llm| StrOutputParser()
317
+
318
+ # Store response suggestions in a structured format
319
+ feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'response': state['response']})}"
320
+ print("feedback: ", feedback)
321
+ print(f"State: {state}")
322
+ state['feedback'] = feedback
323
+ return state
324
+
325
+
326
+
327
+ def refine_query(state: Dict) -> Dict:
328
+ """
329
+ Suggests improvements for the expanded query.
330
+
331
+ Args:
332
+ state (Dict): The current state of the workflow, containing the query and expanded query.
333
+
334
+ Returns:
335
+ Dict: The updated state with query refinement suggestions.
336
+ """
337
+ print("---------refine_query---------")
338
+ system_message = '''Your task is to refine the expanded query to improve the precision of the AI generated response.'''
339
+
340
+ refine_query_prompt = ChatPromptTemplate.from_messages([
341
+ ("system", system_message),
342
+ ("user", "Original Query: {query}\nExpanded Query: {expanded_query}\n\n"
343
+ "What improvements can be made for a better search?")
344
+ ])
345
+
346
+ chain = refine_query_prompt | llm | StrOutputParser()
347
+
348
+ # Store refinement suggestions without modifying the original expanded query
349
+ query_feedback = f"Previous Expanded Query: {state['expanded_query']}\nSuggestions: {chain.invoke({'query': state['query'], 'expanded_query': state['expanded_query']})}"
350
+ print("query_feedback: ", query_feedback)
351
+ print(f"Groundedness loop count: {state['groundedness_loop_count']}")
352
+ state['query_feedback'] = query_feedback
353
+ return state
354
+
355
+
356
+
357
+ def should_continue_groundedness(state):
358
+ """Decides if groundedness is sufficient or needs improvement."""
359
+ print("---------should_continue_groundedness---------")
360
+ print("groundedness loop count: ", state['groundedness_loop_count'])
361
+ if state['groundedness_score'] >= 1: # Complete the code to define the threshold for groundedness
362
+ print("Moving to precision")
363
+ return "check_precision"
364
+ else:
365
+ if state["groundedness_loop_count"] > state['loop_max_iter']:
366
+ return "max_iterations_reached"
367
+ else:
368
+ print(f"---------Groundedness Score Threshold Not met. Refining Response-----------")
369
+ return "refine_response"
370
+
371
+
372
+ def should_continue_precision(state: Dict) -> str:
373
+ """Decides if precision is sufficient or needs improvement."""
374
+ print("---------should_continue_precision---------")
375
+ print("precision loop count: ", state['precision_loop_count'])
376
+ if state['precision_score']>=1: # Threshold for precision
377
+ return "pass" # Complete the workflow
378
+ else:
379
+ if state['precision_loop_count']> state['loop_max_iter']: # Maximum allowed loops
380
+ return "max_iterations_reached"
381
+ else:
382
+ print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging
383
+ return "refine_query" # Refine the query
384
+
385
+
386
+
387
+
388
+ def max_iterations_reached(state: Dict) -> Dict:
389
+ """Handles the case when the maximum number of iterations is reached."""
390
+ print("---------max_iterations_reached---------")
391
+ """Handles the case when the maximum number of iterations is reached."""
392
+ response = "I'm unable to refine the response further. Please provide more context or clarify your question."
393
+ state['response'] = response
394
+ return state
395
+
396
+
397
+
398
+ from langgraph.graph import END, StateGraph, START
399
+
400
+ def create_workflow() -> StateGraph:
401
+ """Creates the updated workflow for the AI nutrition agent."""
402
+
403
+ workflow = StateGraph(AgentState)
404
+
405
+ # Add processing nodes
406
+ workflow.add_node("expand_query", expand_query) # Step 1: Expand user query.
407
+ workflow.add_node("retrieve_context", retrieve_context) # Step 2: Retrieve relevant documents.
408
+ workflow.add_node("craft_response", craft_response) # Step 3: Generate a response based on retrieved data.
409
+ workflow.add_node("score_groundedness", score_groundedness) # Step 4: Evaluate response grounding.
410
+ workflow.add_node("refine_response", refine_response) # Step 5: Improve response if it's weakly grounded.
411
+ workflow.add_node("check_precision", check_precision) # Step 6: Evaluate response precision.
412
+ workflow.add_node("refine_query",refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
413
+ workflow.add_node("max_iterations_reached", max_iterations_reached) # Step 8: Handle max iterations.
414
+
415
+ # Main flow edges
416
+ workflow.add_edge(START, "expand_query")
417
+ workflow.add_edge("expand_query", "retrieve_context")
418
+ workflow.add_edge("retrieve_context", "craft_response")
419
+ workflow.add_edge("craft_response", "score_groundedness")
420
+
421
+ # Conditional edges based on groundedness check
422
+ workflow.add_conditional_edges(
423
+ "score_groundedness",
424
+ should_continue_groundedness, # Use the conditional function
425
+ {
426
+ "check_precision": "check_precision", # If well-grounded, proceed to precision check.
427
+ "refine_response": "refine_response", # If not, refine the response.
428
+ "max_iterations_reached": END # If max loops reached, exit.
429
+ }
430
+ )
431
+
432
+ workflow.add_edge("refine_response", "craft_response") # Refined responses are reprocessed.
433
+
434
+ # Conditional edges based on precision check
435
+ workflow.add_conditional_edges(
436
+ "check_precision",
437
+ should_continue_precision, # Use the conditional function
438
+ {
439
+ "pass": END, # If precise, complete the workflow.
440
+ "refine_query": "refine_query", # If imprecise, refine the query.
441
+ "max_iterations_reached": END # If max loops reached, exit.
442
+ }
443
+ )
444
+
445
+ workflow.add_edge("refine_query", "expand_query") # Refined queries go through expansion again.
446
+ workflow.add_edge("max_iterations_reached", END)
447
+
448
+ return workflow
449
+
450
+
451
+
452
+ #=========================== Defining the agentic rag tool ====================#
453
+ WORKFLOW_APP = create_workflow().compile()
454
+ @tool
455
+ def agentic_rag(query: str):
456
+ """
457
+ Runs the RAG-based agent with conversation history for context-aware responses.
458
+
459
+ Args:
460
+ query (str): The current user query.
461
+
462
+ Returns:
463
+ Dict[str, Any]: The updated state with the generated response and conversation history.
464
+ """
465
+ # Initialize state with necessary parameters
466
+ inputs = {
467
+ "query": query,
468
+ "expanded_query": "",
469
+ "context": [],
470
+ "response": "",
471
+ "precision_score": 0,
472
+ "groundedness_score":0,
473
+ "groundedness_loop_count": 5,
474
+ "precision_loop_count": 5,
475
+ "feedback": "",
476
+ "query_feedback": "",
477
+ "loop_max_iter": 5
478
+ }
479
+
480
+ output = WORKFLOW_APP.invoke(inputs)
481
+
482
+ return output
483
+
484
+
485
+ #================================ Guardrails ===========================#
486
+ llama_guard_client = Groq(api_key=llama_api_key)
487
+ # Function to filter user input with Llama Guard
488
+ def filter_input_with_llama_guard(user_input, model="llama-guard-3-8b"):
489
+ """
490
+ Filters user input using Llama Guard to ensure it is safe.
491
+
492
+ Parameters:
493
+ - user_input: The input provided by the user.
494
+ - model: The Llama Guard model to be used for filtering (default is "llama-guard-3-8b").
495
+
496
+ Returns:
497
+ - The filtered and safe input.
498
+ """
499
+ try:
500
+ # Create a request to Llama Guard to filter the user input
501
+ response = llama_guard_client.chat.completions.create(
502
+ messages=[{"role": "user", "content": user_input}],
503
+ model=model,
504
+ )
505
+ # Return the filtered input
506
+ return response.choices[0].message.content.strip()
507
+ except Exception as e:
508
+ print(f"Error with Llama Guard: {e}")
509
+ return None
510
+
511
+
512
+ #============================= Adding Memory to the agent using mem0 ===============================#
513
+
514
+ class NutritionBot:
515
+ def __init__(self):
516
+ """
517
+ Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
518
+ """
519
+
520
+
521
+ #====================================SETUP=====================================#
522
+ # Fetch secrets from Hugging Face Spaces
523
+ api_key = config.get("API_KEY")
524
+ endpoint = config.get("OPENAI_API_BASE")
525
+ llama_api_key = os.environ['GROQ_API_KEY']
526
+ MEM0_api_key = os.environ['mem0']
527
+
528
+ # Initialize the OpenAI embedding function for Chroma
529
+ embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
530
+ api_base=endpoint, # Complete the code to define the API base endpoint
531
+ api_key=api_key, # Complete the code to define the API key
532
+ model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
533
+ )
534
+
535
+ # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key.
536
+
537
+ # Initialize the OpenAI Embeddings
538
+ embedding_model = OpenAIEmbeddings(
539
+ openai_api_base=endpoint,
540
+ openai_api_key=api_key,
541
+ model='text-embedding-ada-002'
542
+ )
543
+
544
+
545
+ # Initialize the Chat OpenAI model
546
+ llm = ChatOpenAI(
547
+ openai_api_base=endpoint,
548
+ openai_api_key=api_key,
549
+ model="gpt-4o-mini",
550
+ streaming=False
551
+ )
552
+ # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
553
+
554
+ # set the LLM and embedding model in the LlamaIndex settings.
555
+ Settings.llm = llm # Complete the code to define the LLM model
556
+ Settings.embedding = embedding_model # Complete the code to define the embedding model
557
+
558
+ #================================Creating Langgraph agent======================#
559
+
560
+ class AgentState(TypedDict):
561
+ query: str # The current user query
562
+ expanded_query: str # The expanded version of the user query
563
+ context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
564
+ response: str # The generated response to the user query
565
+ precision_score: float # The precision score of the response
566
+ groundedness_score: float # The groundedness score of the response
567
+ groundedness_loop_count: int # Counter for groundedness refinement loops
568
+ precision_loop_count: int # Counter for precision refinement loops
569
+ feedback: str
570
+ query_feedback: str
571
+ groundedness_check: bool
572
+ loop_max_iter: int
573
+
574
+ def expand_query(state):
575
+ """
576
+ Expands the user query to improve retrieval of nutrition disorder-related information.
577
+
578
+ Args:
579
+ state (Dict): The current state of the workflow, containing the user query.
580
+
581
+ Returns:
582
+ Dict: The updated state with the expanded query.
583
+ """
584
+ print("---------Expanding Query---------")
585
+ system_message = '''________________________'''
586
+
587
+
588
+ expand_prompt = ChatPromptTemplate.from_messages([
589
+ ("system", system_message),
590
+ ("user", "Expand this query: {query} using the feedback: {query_feedback}")
591
+
592
+ ])
593
+
594
+ chain = expand_prompt | llm | StrOutputParser()
595
+ expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]})
596
+ print("expanded_query", expanded_query)
597
+ state["expanded_query"] = expanded_query
598
+ return state
599
+
600
+
601
+ # Initialize the Chroma vector store for retrieving documents
602
+ vector_store = Chroma(
603
+ collection_name="nutritional_research",
604
+ persist_directory="./nutrition_db",
605
+ embedding_function=embedding_model
606
+
607
+ )
608
+
609
+ # Create a retriever from the vector store
610
+ retriever = vector_store.as_retriever(
611
+ search_type='similarity',
612
+ search_kwargs={'k': 3}
613
+ )
614
+
615
+ def retrieve_context(state):
616
+ """
617
+ Retrieves context from the vector store using the expanded or original query.
618
+
619
+ Args:
620
+ state (Dict): The current state of the workflow, containing the query and expanded query.
621
+
622
+ Returns:
623
+ Dict: The updated state with the retrieved context.
624
+ """
625
+ print("---------retrieve_context---------")
626
+
627
+ query = state['expanded_query'] # Complete the code to define the key for the expanded query
628
+
629
+
630
+ # Retrieve documents from the vector store
631
+ docs = retriever.invoke(query)
632
+ print("Retrieved documents:", docs) # Debugging: Print the raw docs object
633
+
634
+ # Extract both page_content and metadata from each document
635
+ context= [
636
+ {
637
+ "content": doc.page_content, # The actual content of the document
638
+ "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
639
+ }
640
+ for doc in docs
641
+ ]
642
+ state['context'] = context # Complete the code to define the key for storing the context
643
+ print("Extracted context with metadata:", context) # Debugging: Print the extracted context
644
+
645
+ return state
646
+
647
+
648
+
649
+ def craft_response(state: Dict) -> Dict:
650
+ """
651
+ Generates a response using the retrieved context, focusing on nutrition disorders.
652
+
653
+ Args:
654
+ state (Dict): The current state of the workflow, containing the query and retrieved context.
655
+
656
+ Returns:
657
+ Dict: The updated state with the generated response.
658
+ """
659
+ print("---------craft_response---------")
660
+ system_message = '''you are a smart nutrition disorder specialist. Use the context and feedback to respond to the query.
661
+ The answer you provide must come from the context and feedback provided.
662
+ If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
663
+
664
+ response_prompt = ChatPromptTemplate.from_messages([
665
+ ("system", system_message),
666
+ ("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")
667
+ ])
668
+
669
+ chain = response_prompt | llm
670
+ response = chain.invoke({
671
+ "query": state['query'],
672
+ "context": "\n".join([doc["content"] for doc in state['context']]),
673
+ "feedback": state['feedback'] # add feedback to the prompt
674
+ })
675
+ state['response'] = response
676
+ print("intermediate response: ", response)
677
+
678
+ return state
679
+
680
+
681
+
682
+ def score_groundedness(state: Dict) -> Dict:
683
+ """
684
+ Checks whether the response is grounded in the retrieved context.
685
+
686
+ Args:
687
+ state (Dict): The current state of the workflow, containing the response and context.
688
+
689
+ Returns:
690
+ Dict: The updated state with the groundedness score.
691
+ """
692
+ print("---------check_groundedness---------")
693
+ system_message = '''Your task is to judge the groundedness of the response based on the context.
694
+ For each statement you must return verdict as 1 if the response is completely grounded in the context and 0 if the response is completely hallucinated.'''
695
+
696
+ groundedness_prompt = ChatPromptTemplate.from_messages([
697
+ ("system", system_message),
698
+ ("user", "Context: {context}\nResponse: {response}\n\nGroundedness score:")
699
+ ])
700
+
701
+ chain = groundedness_prompt | llm | StrOutputParser()
702
+ groundedness_score = float(chain.invoke({
703
+ "context": "\n".join([doc["content"] for doc in state['context']]),
704
+ "response": state['response'] # Complete the code to define the response
705
+ }))
706
+ print("groundedness_score: ", groundedness_score)
707
+ state['groundedness_loop_count'] += 1
708
+ print("#########Groundedness Incremented###########")
709
+ state['groundedness_score'] = groundedness_score
710
+
711
+ return state
712
+
713
+
714
+
715
+ def check_precision(state: Dict) -> Dict:
716
+ """
717
+ Checks whether the response precisely addresses the user’s query.
718
+
719
+ Args:
720
+ state (Dict): The current state of the workflow, containing the query and response.
721
+
722
+ Returns:
723
+ Dict: The updated state with the precision score.
724
+ """
725
+ print("---------check_precision---------")
726
+ system_message = '''Given the query, response and context verify if the context was useful in arriving at the given answer.
727
+ Give precision score of "1" if useful and "0" if it was not useful.'''
728
+
729
+ precision_prompt = ChatPromptTemplate.from_messages([
730
+ ("system", system_message),
731
+ ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
732
+ ])
733
+
734
+ chain = _____________ | llm | StrOutputParser() # Complete the code to define the chain of processing
735
+ precision_score = float(chain.invoke({
736
+ "query": state['query'],
737
+ "response":state['response'] # Complete the code to access the response from the state
738
+ }))
739
+ state['precision_score'] = precision_score
740
+ print("precision_score:", precision_score)
741
+ state['precision_loop_count'] +=1
742
+ print("#########Precision Incremented###########")
743
+ return state
744
+
745
+
746
+
747
+ def refine_response(state: Dict) -> Dict:
748
+ """
749
+ Suggests improvements for the generated response.
750
+
751
+ Args:
752
+ state (Dict): The current state of the workflow, containing the query and response.
753
+
754
+ Returns:
755
+ Dict: The updated state with response refinement suggestions.
756
+ """
757
+ print("---------refine_response---------")
758
+
759
+ system_message = '''Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
760
+
761
+ refine_response_prompt = ChatPromptTemplate.from_messages([
762
+ ("system", system_message),
763
+ ("user", "Query: {query}\nResponse: {response}\n\n"
764
+ "What improvements can be made to enhance accuracy and completeness?")
765
+ ])
766
+
767
+ chain = refine_response_prompt | llm| StrOutputParser()
768
+
769
+ # Store response suggestions in a structured format
770
+ feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'response': state['response']})}"
771
+ print("feedback: ", feedback)
772
+ print(f"State: {state}")
773
+ state['feedback'] = feedback
774
+ return state
775
+
776
+
777
+
778
+ def refine_query(state: Dict) -> Dict:
779
+ """
780
+ Suggests improvements for the expanded query.
781
+
782
+ Args:
783
+ state (Dict): The current state of the workflow, containing the query and expanded query.
784
+
785
+ Returns:
786
+ Dict: The updated state with query refinement suggestions.
787
+ """
788
+ print("---------refine_query---------")
789
+ system_message = '''Your task is to refine the expanded query to improve the precision of the response.'''
790
+
791
+ refine_query_prompt = ChatPromptTemplate.from_messages([
792
+ ("system", system_message),
793
+ ("user", "Original Query: {query}\nExpanded Query: {expanded_query}\n\n"
794
+ "What improvements can be made for a better search?")
795
+ ])
796
+
797
+ chain = refine_query_prompt | llm | StrOutputParser()
798
+
799
+ # Store refinement suggestions without modifying the original expanded query
800
+ query_feedback = f"Previous Expanded Query: {state['expanded_query']}\nSuggestions: {chain.invoke({'query': state['query'], 'expanded_query': state['expanded_query']})}"
801
+ print("query_feedback: ", query_feedback)
802
+ print(f"Groundedness loop count: {state['groundedness_loop_count']}")
803
+ state['query_feedback'] = query_feedback
804
+ return state
805
+
806
+
807
+
808
+ def should_continue_groundedness(state):
809
+ """Decides if groundedness is sufficient or needs improvement."""
810
+ print("---------should_continue_groundedness---------")
811
+ print("groundedness loop count: ", state['groundedness_loop_count'])
812
+ if state['groundedness_score'] >= 1: # Complete the code to define the threshold for groundedness
813
+ print("Moving to precision")
814
+ return "check_precision"
815
+ else:
816
+ if state["groundedness_loop_count"] > state['loop_max_iter']:
817
+ return "max_iterations_reached"
818
+ else:
819
+ print(f"---------Groundedness Score Threshold Not met. Refining Response-----------")
820
+ return "refine_response"
821
+
822
+
823
+ def should_continue_precision(state: Dict) -> str:
824
+ """Decides if precision is sufficient or needs improvement."""
825
+ print("---------should_continue_precision---------")
826
+ print("precision loop count: ", state['precision_loop_count'])
827
+ if state['precision_score']>=1: # Threshold for precision
828
+ return "pass" # Complete the workflow
829
+ else:
830
+ if state['precision_loop_count']> state['loop_max_iter']: # Maximum allowed loops
831
+ return "max_iterations_reached"
832
+ else:
833
+ print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging
834
+ return "refine_query" # Refine the query
835
+
836
+
837
+
838
+
839
+ def max_iterations_reached(state: Dict) -> Dict:
840
+ """Handles the case when the maximum number of iterations is reached."""
841
+ print("---------max_iterations_reached---------")
842
+ """Handles the case when the maximum number of iterations is reached."""
843
+ response = "I'm unable to refine the response further. Please provide more context or clarify your question."
844
+ state['response'] = response
845
+ return state
846
+
847
+
848
+
849
+ from langgraph.graph import END, StateGraph, START
850
+
851
+ def create_workflow() -> StateGraph:
852
+ """Creates the updated workflow for the AI nutrition agent."""
853
+ workflow = StateGraph(AgentState)
854
+
855
+ # Add processing nodes
856
+ workflow.add_node("expand_query", expand_query) # Step 1: Expand user query.
857
+ workflow.add_node("retrieve_context", retrieve_context) # Step 2: Retrieve relevant documents.
858
+ workflow.add_node("craft_response", craft_response) # Step 3: Generate a response based on retrieved data.
859
+ workflow.add_node("score_groundedness", score_groundedness) # Step 4: Evaluate response grounding.
860
+ workflow.add_node("refine_response", refine_response) # Step 5: Improve response if it's weakly grounded.
861
+ workflow.add_node("check_precision", check_precision) # Step 6: Evaluate response precision.
862
+ workflow.add_node("refine_query",refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
863
+ workflow.add_node("max_iterations_reached", max_iterations_reached) # Step 8: Handle max iterations.
864
+
865
+ # Main flow edges
866
+ workflow.add_edge(START, "expand_query")
867
+ workflow.add_edge("expand_query", "retrieve_context")
868
+ workflow.add_edge("retrieve_context", "craft_response")
869
+ workflow.add_edge("craft_response", "score_groundedness")
870
+
871
+ # Conditional edges based on groundedness check
872
+ workflow.add_conditional_edges(
873
+ "score_groundedness",
874
+ should_continue_groundedness, # Use the conditional function
875
+ {
876
+ "check_precision": "check_precision", # If well-grounded, proceed to precision check.
877
+ "refine_response": "refine_response", # If not, refine the response.
878
+ "max_iterations_reached": END # If max loops reached, exit.
879
+ }
880
+ )
881
+
882
+ workflow.add_edge("refine_response", "craft_response") # Refined responses are reprocessed.
883
+
884
+ # Conditional edges based on precision check
885
+ workflow.add_conditional_edges(
886
+ "check_precision",
887
+ should_continue_precision, # Use the conditional function
888
+ {
889
+ "pass": END, # If precise, complete the workflow.
890
+ "refine_query": "refine_query", # If imprecise, refine the query.
891
+ "max_iterations_reached": END # If max loops reached, exit.
892
+ }
893
+ )
894
+
895
+ workflow.add_edge("refine_query", "expand_query") # Refined queries go through expansion again.
896
+ workflow.add_edge("max_iterations_reached", END)
897
+
898
+ return workflow
899
+
900
+
901
+
902
+
903
+ #=========================== Defining the agentic rag tool ====================#
904
+ WORKFLOW_APP = create_workflow().compile()
905
+
906
+ @tool
907
+ def agentic_rag(query: str):
908
+ """
909
+ Runs the RAG-based agent with conversation history for context-aware responses.
910
+
911
+ Args:
912
+ query (str): The current user query.
913
+
914
+ Returns:
915
+ Dict[str, Any]: The updated state with the generated response and conversation history.
916
+ """
917
+ # Initialize state with necessary parameters
918
+ inputs = {
919
+ "query": query,
920
+ "expanded_query": "",
921
+ "context": [],
922
+ "response": "",
923
+ "precision_score": 0,
924
+ "groundedness_score":0,
925
+ "groundedness_loop_count": 5,
926
+ "precision_loop_count": 5,
927
+ "feedback": "",
928
+ "query_feedback": "",
929
+ "loop_max_iter": 5
930
+ }
931
+
932
+ output = WORKFLOW_APP.invoke(inputs)
933
+
934
+ return output
935
+
936
+
937
+ #================================ Guardrails ===========================#
938
+ llama_guard_client = Groq(api_key=llama_api_key)
939
+ # Function to filter user input with Llama Guard
940
+ def filter_input_with_llama_guard(user_input, model="llama-guard-3-8b"):
941
+ """
942
+ Filters user input using Llama Guard to ensure it is safe.
943
+
944
+ Parameters:
945
+ - user_input: The input provided by the user.
946
+ - model: The Llama Guard model to be used for filtering (default is "llama-guard-3-8b").
947
+
948
+ Returns:
949
+ - The filtered and safe input.
950
+ """
951
+ try:
952
+ # Create a request to Llama Guard to filter the user input
953
+ response = llama_guard_client.chat.completions.create(
954
+ messages=[{"role": "user", "content": user_input}],
955
+ model=model,
956
+ )
957
+ # Return the filtered input
958
+ return response.choices[0].message.content.strip()
959
+ except Exception as e:
960
+ print(f"Error with Llama Guard: {e}")
961
+ return None
962
+
963
+
964
+ #============================= Adding Memory to the agent using mem0 ===============================#
965
+
966
+ class NutritionBot:
967
+ def __init__(self):
968
+ """
969
+ Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
970
+ """
971
+
972
+ # Initialize a memory client to store and retrieve customer interactions
973
+ self.memory = MemoryClient(api_key=userdata.get("mem0_api_key")) # Complete the code to define the memory client API key
974
+
975
+ # Initialize the OpenAI client using the provided credentials
976
+ self.client = ChatOpenAI(
977
+ model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
978
+ api_key=config.get("API_KEY"), # API key for authentication
979
+ openai_api_base = config.get("OPENAI_API_BASE"),
980
+ temperature=0 # Controls randomness in responses; 0 ensures deterministic results
981
+ )
982
+
983
+ # Define tools available to the chatbot, such as web search
984
+ tools = [agentic_rag]
985
+
986
+ # Define the system prompt to set the behavior of the chatbot
987
+ 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.
988
+ Guidelines for Interaction:
989
+ Maintain a polite, professional, and reassuring tone.
990
+ Show genuine empathy for customer concerns and health challenges.
991
+ Reference past interactions to provide personalized and consistent advice.
992
+ Engage with the customer by asking about their food preferences, dietary restrictions, and lifestyle before offering recommendations.
993
+ Ensure consistent and accurate information across conversations.
994
+ If any detail is unclear or missing, proactively ask for clarification.
995
+ Always use the agentic_rag tool to retrieve up-to-date and evidence-based nutrition insights.
996
+ Keep track of ongoing issues and follow-ups to ensure continuity in support.
997
+ Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
998
+
999
+ """
1000
+
1001
+ # Build the prompt template for the agent
1002
+ prompt = ChatPromptTemplate.from_messages([
1003
+ ("system", system_prompt), # System instructions
1004
+ ("human", "{input}"), # Placeholder for human input
1005
+ ("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
1006
+ ])
1007
+
1008
+ # Create an agent capable of interacting with tools and executing tasks
1009
+ agent = create_tool_calling_agent(self.client, tools, prompt)
1010
+
1011
+ # Wrap the agent in an executor to manage tool interactions and execution flow
1012
+ self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
1013
+
1014
+
1015
+ def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
1016
+ """
1017
+ Store customer interaction in memory for future reference.
1018
+
1019
+ Args:
1020
+ user_id (str): Unique identifier for the customer.
1021
+ message (str): Customer's query or message.
1022
+ response (str): Chatbot's response.
1023
+ metadata (Dict, optional): Additional metadata for the interaction.
1024
+ """
1025
+ if metadata is None:
1026
+ metadata = {}
1027
+
1028
+ # Add a timestamp to the metadata for tracking purposes
1029
+ metadata["timestamp"] = datetime.now().isoformat()
1030
+
1031
+ # Format the conversation for storage
1032
+ conversation = [
1033
+ {"role": "user", "content": message},
1034
+ {"role": "assistant", "content": response}
1035
+ ]
1036
+
1037
+ # Store the interaction in the memory client
1038
+ self.memory.add(
1039
+ conversation,
1040
+ user_id=user_id,
1041
+ output_format="v1.1",
1042
+ metadata=metadata
1043
+ )
1044
+
1045
+
1046
+ def get_relevant_history(self, user_id: str, query: str) -> List[Dict]:
1047
+ """
1048
+ Retrieve past interactions relevant to the current query.
1049
+
1050
+ Args:
1051
+ user_id (str): Unique identifier for the customer.
1052
+ query (str): The customer's current query.
1053
+
1054
+ Returns:
1055
+ List[Dict]: A list of relevant past interactions.
1056
+ """
1057
+ return self.memory.search(
1058
+ query=query, # Search for interactions related to the query
1059
+ user_id=user_id, # Restrict search to the specific user
1060
+ limit=5 # Complete the code to define the limit for retrieved interactions
1061
+ )
1062
+
1063
+
1064
+ def handle_customer_query(self, user_id: str, query: str) -> str:
1065
+ """
1066
+ Process a customer's query and provide a response, taking into account past interactions.
1067
+
1068
+ Args:
1069
+ user_id (str): Unique identifier for the customer.
1070
+ query (str): Customer's query.
1071
+
1072
+ Returns:
1073
+ str: Chatbot's response.
1074
+ """
1075
+
1076
+ # Retrieve relevant past interactions for context
1077
+ relevant_history = self.get_relevant_history(user_id, query)
1078
+
1079
+ # Build a context string from the relevant history
1080
+ context = "Previous relevant interactions:\n"
1081
+ for memory in relevant_history:
1082
+ context += f"Customer: {memory['memory']}\n" # Customer's past messages
1083
+ context += f"Support: {memory['memory']}\n" # Chatbot's past responses
1084
+ context += "---\n"
1085
+
1086
+ # Print context for debugging purposes
1087
+ print("Context: ", context)
1088
+
1089
+ # Prepare a prompt combining past context and the current query
1090
+ prompt = f"""
1091
+ Context:
1092
+ {context}
1093
+
1094
+ Current customer query: {query}
1095
+
1096
+ Provide a helpful response that takes into account any relevant past interactions.
1097
+ """
1098
+
1099
+ # Generate a response using the agent
1100
+ response = self.agent_executor.invoke({"input": prompt})
1101
+
1102
+ # Store the current interaction for future reference
1103
+ self.store_customer_interaction(
1104
+ user_id=user_id,
1105
+ message=query,
1106
+ response=response["output"],
1107
+ metadata={"type": "support_query"}
1108
+ )
1109
+
1110
+ # Return the chatbot's response
1111
+ return response['output']
1112
+
1113
+
1114
+ #=====================User Interface using streamlit ===========================#
1115
+ def nutrition_disorder_streamlit():
1116
+ """
1117
+ A Streamlit-based UI for the Nutrition Disorder Specialist Agent.
1118
+ """
1119
+ st.title("Nutrition Disorder Specialist")
1120
+ st.write("Ask me anything about nutrition disorders, symptoms, causes, treatments, and more.")
1121
+ st.write("Type 'exit' to end the conversation.")
1122
+
1123
+ # Initialize session state for chat history and user_id if they don't exist
1124
+ if 'chat_history' not in st.session_state:
1125
+ st.session_state.chat_history = []
1126
+ if 'user_id' not in st.session_state:
1127
+ st.session_state.user_id = None
1128
+
1129
+ # Login form: Only if user is not logged in
1130
+ if st.session_state.user_id is None:
1131
+ with st.form("login_form", clear_on_submit=True):
1132
+ user_id = st.text_input("Please enter your name to begin:")
1133
+ submit_button = st.form_submit_button("Login")
1134
+ if submit_button and user_id:
1135
+ st.session_state.user_id = user_id
1136
+ st.session_state.chat_history.append({
1137
+ "role": "assistant",
1138
+ "content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
1139
+ })
1140
+ st.session_state.login_submitted = True # Set flag to trigger rerun
1141
+ if st.session_state.get("login_submitted", False):
1142
+ st.session_state.pop("login_submitted")
1143
+ st.rerun()
1144
+ else:
1145
+ # Display chat history
1146
+ for message in st.session_state.chat_history:
1147
+ with st.chat_message(message["role"]):
1148
+ st.write(message["content"])
1149
+
1150
+ # Chat input with custom placeholder text
1151
+ user_query = st.chat_input("Type your question here (or exit to end): ", key="chat_input") # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...")
1152
+ if user_query:
1153
+ if user_query.lower() == "exit":
1154
+ st.session_state.chat_history.append({"role": "user", "content": "exit"})
1155
+ with st.chat_message("user"):
1156
+ st.write("exit")
1157
+ goodbye_msg = "Goodbye! Feel free to return if you have more questions about nutrition disorders."
1158
+ st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg})
1159
+ with st.chat_message("assistant"):
1160
+ st.write(goodbye_msg)
1161
+ st.session_state.user_id = None
1162
+ st.rerun()
1163
+ return
1164
+
1165
+ st.session_state.chat_history.append({"role": "user", "content": user_query})
1166
+ with st.chat_message("user"):
1167
+ st.write(user_query)
1168
+
1169
+ # Filter input using Llama Guard
1170
+ filtered_result = filter_input_with_llama_guard(user_query) # Blank #2: Fill in with the function name for filtering input (e.g., filter_input_with_llama_guard)
1171
+ filtered_result = filtered_result.replace("\n", " ") # Normalize the result
1172
+
1173
+ # Check if input is safe based on allowed statuses
1174
+ if filtered_result in ["safe", "unsafe S6", "unsafe S7"]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
1175
+ try:
1176
+ if 'chatbot' not in st.session_state:
1177
+ st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
1178
+ response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
1179
+ # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
1180
+ st.write(response)
1181
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
1182
+ except Exception as e:
1183
+ error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
1184
+ st.write(error_msg)
1185
+ st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
1186
+ else:
1187
+ inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
1188
+ st.write(inappropriate_msg)
1189
+ st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
1190
+
1191
+ if __name__ == "__main__":
1192
+ nutrition_disorder_streamlit()
1193
+