nitind1 commited on
Commit
e2956d3
·
verified ·
1 Parent(s): 929efe2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +94 -87
app.py CHANGED
@@ -1,37 +1,36 @@
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
  from langchain.memory import ConversationSummaryBufferMemory
36
 
37
  from langchain_openai import ChatOpenAI
@@ -48,20 +47,20 @@ from langchain_core.tools import tool
48
  from langchain.agents import create_tool_calling_agent, AgentExecutor
49
 
50
  # LlamaParse & LlamaIndex imports
51
- from llama_parse import LlamaParse # Document parsing library
52
- from llama_index.core import Settings, SimpleDirectoryReader # Core functionalities of the LlamaIndex
53
 
54
  # LangGraph import
55
- from langgraph.graph import StateGraph, END, START # State graph for managing states in LangChain
56
 
57
  # Pydantic import
58
- from pydantic import BaseModel # Pydantic for data validation
59
 
60
  # Typing imports
61
- from typing import Dict, List, Tuple, Any, TypedDict # Python typing for function annotations
62
 
63
  # Other utilities
64
- import numpy as np # Numpy for numerical operations
65
  from groq import Groq
66
  from mem0 import MemoryClient
67
  import streamlit as st
@@ -74,16 +73,15 @@ endpoint = os.environ["OPENAI_API_BASE"]
74
  llama_api_key = os.environ['GROQ_API_KEY']
75
  mem0_api_key = os.environ['mem0']
76
 
77
- # Initialize the OpenAI embedding function for Chroma
78
  embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
79
  api_base=endpoint, # Complete the code to define the API base endpoint
80
  api_key=api_key, # Complete the code to define the API key
81
  model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
82
  )
83
 
84
- # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key.
85
 
86
- # Initialize the OpenAI Embeddings
87
  embedding_model = OpenAIEmbeddings(
88
  openai_api_base=endpoint,
89
  openai_api_key=api_key,
@@ -91,14 +89,13 @@ embedding_model = OpenAIEmbeddings(
91
  )
92
 
93
 
94
- # Initialize the Chat OpenAI model
95
  llm = ChatOpenAI(
96
  openai_api_base=endpoint,
97
  openai_api_key=api_key,
98
  model="gpt-4o-mini",
99
  streaming=False
100
  )
101
- # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
102
 
103
  # set the LLM and embedding model in the LlamaIndex settings.
104
  Settings.llm = llm # Complete the code to define the LLM model
@@ -107,14 +104,14 @@ Settings.embedding = embedding_model # Complete the code to define the embedding
107
  #================================Creating Langgraph agent======================#
108
 
109
  class AgentState(TypedDict):
110
- query: str # The current user query
111
- expanded_query: str # The expanded version of the user query
112
- context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
113
- response: str # The generated response to the user query
114
- precision_score: float # The precision score of the response
115
- groundedness_score: float # The groundedness score of the response
116
- groundedness_loop_count: int # Counter for groundedness refinement loops
117
- precision_loop_count: int # Counter for precision refinement loops
118
  feedback: str
119
  query_feedback: str
120
  groundedness_check: bool
@@ -186,8 +183,7 @@ def retrieve_context(state):
186
  Dict: The updated state with the retrieved context.
187
  """
188
  print("---------retrieve_context---------")
189
- query = state['expanded_query'] # Complete the code to define the key for the expanded query
190
- #print("Query used for retrieval:", query) # Debugging: Print the query
191
 
192
  # Retrieve documents from the vector store
193
  docs = retriever.invoke(query)
@@ -202,8 +198,8 @@ def retrieve_context(state):
202
  for doc in docs
203
  ]
204
  state['context'] = context # Complete the code to define the key for storing the context
205
- print("Extracted context with metadata:", context)
206
-
207
  return state
208
 
209
 
@@ -219,8 +215,8 @@ def craft_response(state: Dict) -> Dict:
219
  Dict: The updated state with the generated response.
220
  """
221
  print("---------craft_response---------")
222
- system_message = '''you are a smart nutrition disorder specialist. Use the context and feedback to respond to the query.
223
- The answer you provide must come from the context and feedback provided.
224
  If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
225
 
226
  response_prompt = ChatPromptTemplate.from_messages([
@@ -232,7 +228,7 @@ def craft_response(state: Dict) -> Dict:
232
  response = chain.invoke({
233
  "query": state['query'],
234
  "context": "\n".join([doc["content"] for doc in state['context']]),
235
- "feedback": state['feedback'] # add feedback to the prompt
236
  })
237
  state['response'] = response
238
  print("intermediate response: ", response)
@@ -252,7 +248,7 @@ def score_groundedness(state: Dict) -> Dict:
252
  Dict: The updated state with the groundedness score.
253
  """
254
  print("---------check_groundedness---------")
255
- system_message = '''Your task is to judge the groundedness of the response based on the context.
256
  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.'''
257
 
258
  groundedness_prompt = ChatPromptTemplate.from_messages([
@@ -263,7 +259,7 @@ def score_groundedness(state: Dict) -> Dict:
263
  chain = groundedness_prompt | llm | StrOutputParser()
264
  groundedness_score = float(chain.invoke({
265
  "context": "\n".join([doc["content"] for doc in state['context']]),
266
- "response": state['response'] # Complete the code to define the response
267
  }))
268
  print("groundedness_score: ", groundedness_score)
269
  state['groundedness_loop_count'] += 1
@@ -285,7 +281,7 @@ def check_precision(state: Dict) -> Dict:
285
  Dict: The updated state with the precision score.
286
  """
287
  print("---------check_precision---------")
288
- system_message = '''Given the query, response and context verify if the context was useful in arriving at the given answer.
289
  Give precision score of "1" if useful and "0" if it was not useful.'''
290
 
291
  precision_prompt = ChatPromptTemplate.from_messages([
@@ -296,7 +292,7 @@ def check_precision(state: Dict) -> Dict:
296
  chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing
297
  precision_score = float(chain.invoke({
298
  "query": state['query'],
299
- "response":state['response'] # Complete the code to access the response from the state
300
  }))
301
  state['precision_score'] = precision_score
302
  print("precision_score:", precision_score)
@@ -318,7 +314,7 @@ def refine_response(state: Dict) -> Dict:
318
  """
319
  print("---------refine_response---------")
320
 
321
- system_message = '''Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
322
 
323
  refine_response_prompt = ChatPromptTemplate.from_messages([
324
  ("system", system_message),
@@ -348,7 +344,7 @@ def refine_query(state: Dict) -> Dict:
348
  Dict: The updated state with query refinement suggestions.
349
  """
350
  print("---------refine_query---------")
351
- system_message = '''Your task is to refine the expanded query to improve the precision of the AI generated response.'''
352
 
353
  refine_query_prompt = ChatPromptTemplate.from_messages([
354
  ("system", system_message),
@@ -416,14 +412,14 @@ def create_workflow() -> StateGraph:
416
  workflow = StateGraph(AgentState)
417
 
418
  # Add processing nodes
419
- workflow.add_node("expand_query", expand_query) # Step 1: Expand user query.
420
- workflow.add_node("retrieve_context", retrieve_context) # Step 2: Retrieve relevant documents.
421
- workflow.add_node("craft_response", craft_response) # Step 3: Generate a response based on retrieved data.
422
- workflow.add_node("score_groundedness", score_groundedness) # Step 4: Evaluate response grounding.
423
- workflow.add_node("refine_response", refine_response) # Step 5: Improve response if it's weakly grounded.
424
- workflow.add_node("check_precision", check_precision) # Step 6: Evaluate response precision.
425
- workflow.add_node("refine_query",refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
426
- workflow.add_node("max_iterations_reached", max_iterations_reached) # Step 8: Handle max iterations.
427
 
428
  # Main flow edges
429
  workflow.add_edge(START, "expand_query")
@@ -434,7 +430,7 @@ def create_workflow() -> StateGraph:
434
  # Conditional edges based on groundedness check
435
  workflow.add_conditional_edges(
436
  "score_groundedness",
437
- should_continue_groundedness, # Use the conditional function
438
  {
439
  "check_precision": "check_precision", # If well-grounded, proceed to precision check.
440
  "refine_response": "refine_response", # If not, refine the response.
@@ -447,7 +443,7 @@ def create_workflow() -> StateGraph:
447
  # Conditional edges based on precision check
448
  workflow.add_conditional_edges(
449
  "check_precision",
450
- should_continue_precision, # Use the conditional function
451
  {
452
  "pass": END, # If precise, complete the workflow.
453
  "refine_query": "refine_query", # If imprecise, refine the query.
@@ -595,7 +591,21 @@ def expand_query(state):
595
  Dict: The updated state with the expanded query.
596
  """
597
  print("---------Expanding Query---------")
598
- system_message = '''________________________'''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
 
600
 
601
  expand_prompt = ChatPromptTemplate.from_messages([
@@ -638,7 +648,7 @@ def retrieve_context(state):
638
  print("---------retrieve_context---------")
639
 
640
  query = state['expanded_query'] # Complete the code to define the key for the expanded query
641
-
642
 
643
  # Retrieve documents from the vector store
644
  docs = retriever.invoke(query)
@@ -654,7 +664,7 @@ def retrieve_context(state):
654
  ]
655
  state['context'] = context # Complete the code to define the key for storing the context
656
  print("Extracted context with metadata:", context) # Debugging: Print the extracted context
657
-
658
  return state
659
 
660
 
@@ -670,8 +680,8 @@ def craft_response(state: Dict) -> Dict:
670
  Dict: The updated state with the generated response.
671
  """
672
  print("---------craft_response---------")
673
- system_message = '''you are a smart nutrition disorder specialist. Use the context and feedback to respond to the query.
674
- The answer you provide must come from the context and feedback provided.
675
  If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
676
 
677
  response_prompt = ChatPromptTemplate.from_messages([
@@ -703,7 +713,7 @@ def score_groundedness(state: Dict) -> Dict:
703
  Dict: The updated state with the groundedness score.
704
  """
705
  print("---------check_groundedness---------")
706
- system_message = '''Your task is to judge the groundedness of the response based on the context.
707
  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.'''
708
 
709
  groundedness_prompt = ChatPromptTemplate.from_messages([
@@ -736,7 +746,7 @@ def check_precision(state: Dict) -> Dict:
736
  Dict: The updated state with the precision score.
737
  """
738
  print("---------check_precision---------")
739
- system_message = '''Given the query, response and context verify if the context was useful in arriving at the given answer.
740
  Give precision score of "1" if useful and "0" if it was not useful.'''
741
 
742
  precision_prompt = ChatPromptTemplate.from_messages([
@@ -769,7 +779,7 @@ def refine_response(state: Dict) -> Dict:
769
  """
770
  print("---------refine_response---------")
771
 
772
- system_message = '''Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
773
 
774
  refine_response_prompt = ChatPromptTemplate.from_messages([
775
  ("system", system_message),
@@ -799,7 +809,7 @@ def refine_query(state: Dict) -> Dict:
799
  Dict: The updated state with query refinement suggestions.
800
  """
801
  print("---------refine_query---------")
802
- system_message = '''Your task is to refine the expanded query to improve the precision of the response.'''
803
 
804
  refine_query_prompt = ChatPromptTemplate.from_messages([
805
  ("system", system_message),
@@ -866,14 +876,14 @@ def create_workflow() -> StateGraph:
866
  workflow = StateGraph(AgentState)
867
 
868
  # Add processing nodes
869
- workflow.add_node("expand_query", expand_query) # Step 1: Expand user query.
870
- workflow.add_node("retrieve_context", retrieve_context) # Step 2: Retrieve relevant documents.
871
- workflow.add_node("craft_response", craft_response) # Step 3: Generate a response based on retrieved data.
872
- workflow.add_node("score_groundedness", score_groundedness) # Step 4: Evaluate response grounding.
873
- workflow.add_node("refine_response", refine_response) # Step 5: Improve response if it's weakly grounded.
874
- workflow.add_node("check_precision", check_precision) # Step 6: Evaluate response precision.
875
- workflow.add_node("refine_query",refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
876
- workflow.add_node("max_iterations_reached", max_iterations_reached) # Step 8: Handle max iterations.
877
 
878
  # Main flow edges
879
  workflow.add_edge(START, "expand_query")
@@ -983,7 +993,6 @@ class NutritionBot:
983
  """
984
 
985
  # Initialize a memory client to store and retrieve customer interactions
986
- #self.memory = MemoryClient(api_key=userdata.get("mem0_api_key")) # Complete the code to define the memory client API key
987
  self.memory = MemoryClient(api_key=mem0_api_key)
988
  # Initialize the OpenAI client using the provided credentials
989
  self.client = ChatOpenAI(
@@ -1161,7 +1170,7 @@ def nutrition_disorder_streamlit():
1161
  st.write(message["content"])
1162
 
1163
  # Chat input with custom placeholder text
1164
- 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)...")
1165
  if user_query:
1166
  if user_query.lower() == "exit":
1167
  st.session_state.chat_history.append({"role": "user", "content": "exit"})
@@ -1180,16 +1189,15 @@ def nutrition_disorder_streamlit():
1180
  st.write(user_query)
1181
 
1182
  # Filter input using Llama Guard
1183
- 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)
1184
  filtered_result = filtered_result.replace("\n", " ") # Normalize the result
1185
 
1186
  # Check if input is safe based on allowed statuses
1187
- 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")
1188
  try:
1189
  if 'chatbot' not in st.session_state:
1190
- st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
1191
  response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
1192
- # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
1193
  st.write(response)
1194
  st.session_state.chat_history.append({"role": "assistant", "content": response})
1195
  except Exception as e:
@@ -1201,7 +1209,6 @@ def nutrition_disorder_streamlit():
1201
  st.write(inappropriate_msg)
1202
  st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
1203
 
1204
- #def main():
1205
  if __name__ == "__main__":
1206
  nutrition_disorder_streamlit()
1207
 
 
1
 
2
  ######################## WRITE YOUR CODE HERE #########################
3
+ # Import libraries
4
+ import os
5
+ import chromadb
6
+ from dotenv import load_dotenv
7
+ import json
8
 
9
  # LangChain imports
10
+ from langchain_core.documents import Document
11
+ from langchain_core.runnables import RunnablePassthrough
12
+ from langchain_core.output_parsers import StrOutputParser
13
+ from langchain.prompts import ChatPromptTemplate
14
+ from langchain.chains.query_constructor.base import AttributeInfo
15
+ from langchain.retrievers.self_query.base import SelfQueryRetriever
16
+ from langchain.retrievers.document_compressors import LLMChainExtractor, CrossEncoderReranker
17
+ from langchain.retrievers import ContextualCompressionRetriever
18
 
19
  # LangChain community & experimental imports
20
+ from langchain_community.vectorstores import Chroma
21
+ from langchain_community.document_loaders import PyPDFDirectoryLoader, PyPDFLoader
22
+ from langchain_community.cross_encoders import HuggingFaceCrossEncoder
23
+ from langchain_experimental.text_splitter import SemanticChunker
24
  from langchain.text_splitter import (
25
+ CharacterTextSplitter,
26
+ RecursiveCharacterTextSplitter
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
 
34
  from langchain.memory import ConversationSummaryBufferMemory
35
 
36
  from langchain_openai import ChatOpenAI
 
47
  from langchain.agents import create_tool_calling_agent, AgentExecutor
48
 
49
  # LlamaParse & LlamaIndex imports
50
+ from llama_parse import LlamaParse
51
+ from llama_index.core import Settings, SimpleDirectoryReader
52
 
53
  # LangGraph import
54
+ from langgraph.graph import StateGraph, END, START
55
 
56
  # Pydantic import
57
+ from pydantic import BaseModel
58
 
59
  # Typing imports
60
+ from typing import Dict, List, Tuple, Any, TypedDict
61
 
62
  # Other utilities
63
+ import numpy as np
64
  from groq import Groq
65
  from mem0 import MemoryClient
66
  import streamlit as st
 
73
  llama_api_key = os.environ['GROQ_API_KEY']
74
  mem0_api_key = os.environ['mem0']
75
 
76
+ # Initializing the OpenAI embedding function for Chroma
77
  embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
78
  api_base=endpoint, # Complete the code to define the API base endpoint
79
  api_key=api_key, # Complete the code to define the API key
80
  model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
81
  )
82
 
 
83
 
84
+ # Initializing the OpenAI Embeddings
85
  embedding_model = OpenAIEmbeddings(
86
  openai_api_base=endpoint,
87
  openai_api_key=api_key,
 
89
  )
90
 
91
 
92
+ # Initializing the Chat OpenAI model
93
  llm = ChatOpenAI(
94
  openai_api_base=endpoint,
95
  openai_api_key=api_key,
96
  model="gpt-4o-mini",
97
  streaming=False
98
  )
 
99
 
100
  # set the LLM and embedding model in the LlamaIndex settings.
101
  Settings.llm = llm # Complete the code to define the LLM model
 
104
  #================================Creating Langgraph agent======================#
105
 
106
  class AgentState(TypedDict):
107
+ query: str
108
+ expanded_query: str
109
+ context: List[Dict[str, Any]]
110
+ response: str
111
+ precision_score: float
112
+ groundedness_score: float
113
+ groundedness_loop_count: int
114
+ precision_loop_count: int
115
  feedback: str
116
  query_feedback: str
117
  groundedness_check: bool
 
183
  Dict: The updated state with the retrieved context.
184
  """
185
  print("---------retrieve_context---------")
186
+ query = state['expanded_query']
 
187
 
188
  # Retrieve documents from the vector store
189
  docs = retriever.invoke(query)
 
198
  for doc in docs
199
  ]
200
  state['context'] = context # Complete the code to define the key for storing the context
201
+ print("Extracted context with metadata:", context)
202
+
203
  return state
204
 
205
 
 
215
  Dict: The updated state with the generated response.
216
  """
217
  print("---------craft_response---------")
218
+ system_message = '''You are a smart nutrition disorder specialist. Use the context and feedback to respond to the healthcare professionals query.
219
+ Use domain words. The answer you provide must come from the context and feedback provided.
220
  If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
221
 
222
  response_prompt = ChatPromptTemplate.from_messages([
 
228
  response = chain.invoke({
229
  "query": state['query'],
230
  "context": "\n".join([doc["content"] for doc in state['context']]),
231
+ "feedback": state['feedback']
232
  })
233
  state['response'] = response
234
  print("intermediate response: ", response)
 
248
  Dict: The updated state with the groundedness score.
249
  """
250
  print("---------check_groundedness---------")
251
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to judge the groundedness of the response based on the context.
252
  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.'''
253
 
254
  groundedness_prompt = ChatPromptTemplate.from_messages([
 
259
  chain = groundedness_prompt | llm | StrOutputParser()
260
  groundedness_score = float(chain.invoke({
261
  "context": "\n".join([doc["content"] for doc in state['context']]),
262
+ "response": state['response']
263
  }))
264
  print("groundedness_score: ", groundedness_score)
265
  state['groundedness_loop_count'] += 1
 
281
  Dict: The updated state with the precision score.
282
  """
283
  print("---------check_precision---------")
284
+ system_message = '''You are a smart nutrition disorder specialist. Given the query, response and context verify if the context was useful in arriving at the given answer.
285
  Give precision score of "1" if useful and "0" if it was not useful.'''
286
 
287
  precision_prompt = ChatPromptTemplate.from_messages([
 
292
  chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing
293
  precision_score = float(chain.invoke({
294
  "query": state['query'],
295
+ "response":state['response']
296
  }))
297
  state['precision_score'] = precision_score
298
  print("precision_score:", precision_score)
 
314
  """
315
  print("---------refine_response---------")
316
 
317
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
318
 
319
  refine_response_prompt = ChatPromptTemplate.from_messages([
320
  ("system", system_message),
 
344
  Dict: The updated state with query refinement suggestions.
345
  """
346
  print("---------refine_query---------")
347
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to refine the expanded query to improve the precision of the AI generated response.'''
348
 
349
  refine_query_prompt = ChatPromptTemplate.from_messages([
350
  ("system", system_message),
 
412
  workflow = StateGraph(AgentState)
413
 
414
  # Add processing nodes
415
+ workflow.add_node("expand_query", expand_query)
416
+ workflow.add_node("retrieve_context", retrieve_context)
417
+ workflow.add_node("craft_response", craft_response)
418
+ workflow.add_node("score_groundedness", score_groundedness)
419
+ workflow.add_node("refine_response", refine_response)
420
+ workflow.add_node("check_precision", check_precision)
421
+ workflow.add_node("refine_query",refine_query )
422
+ workflow.add_node("max_iterations_reached", max_iterations_reached)
423
 
424
  # Main flow edges
425
  workflow.add_edge(START, "expand_query")
 
430
  # Conditional edges based on groundedness check
431
  workflow.add_conditional_edges(
432
  "score_groundedness",
433
+ should_continue_groundedness,
434
  {
435
  "check_precision": "check_precision", # If well-grounded, proceed to precision check.
436
  "refine_response": "refine_response", # If not, refine the response.
 
443
  # Conditional edges based on precision check
444
  workflow.add_conditional_edges(
445
  "check_precision",
446
+ should_continue_precision,
447
  {
448
  "pass": END, # If precise, complete the workflow.
449
  "refine_query": "refine_query", # If imprecise, refine the query.
 
591
  Dict: The updated state with the expanded query.
592
  """
593
  print("---------Expanding Query---------")
594
+ system_message = '''You are a domain expert assisting in answering questions related to nutrition disorder-related information.
595
+ Convert the user query into something that a healthcare professional would understand. Use domain related words.
596
+ Perform query expansion on the question received. If there are multiple common ways of phrasing a user question \
597
+ or common synonyms for key words in the question, make sure to return multiple versions \
598
+ of the query with the different phrasings.
599
+
600
+ 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.
601
+
602
+ If there are acronyms or words you are not familiar with, do not try to rephrase them.
603
+
604
+ Return only 3 versions of the question as a list.
605
+ Generate only a list of questions. Do not mention anything before or after the list.
606
+
607
+ Question:
608
+ {query}'''
609
 
610
 
611
  expand_prompt = ChatPromptTemplate.from_messages([
 
648
  print("---------retrieve_context---------")
649
 
650
  query = state['expanded_query'] # Complete the code to define the key for the expanded query
651
+
652
 
653
  # Retrieve documents from the vector store
654
  docs = retriever.invoke(query)
 
664
  ]
665
  state['context'] = context # Complete the code to define the key for storing the context
666
  print("Extracted context with metadata:", context) # Debugging: Print the extracted context
667
+
668
  return state
669
 
670
 
 
680
  Dict: The updated state with the generated response.
681
  """
682
  print("---------craft_response---------")
683
+ system_message = '''You are a smart nutrition disorder specialist. Use the context and feedback to respond to the healthcare professionals query.
684
+ Use domain words. The answer you provide must come from the context and feedback provided.
685
  If information provided is not enough to answer the query response with 'I don't know the answer. Not in my records'''
686
 
687
  response_prompt = ChatPromptTemplate.from_messages([
 
713
  Dict: The updated state with the groundedness score.
714
  """
715
  print("---------check_groundedness---------")
716
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to judge the groundedness of the response based on the context.
717
  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.'''
718
 
719
  groundedness_prompt = ChatPromptTemplate.from_messages([
 
746
  Dict: The updated state with the precision score.
747
  """
748
  print("---------check_precision---------")
749
+ system_message = '''You are a smart nutrition disorder specialist. Given the query, response and context verify if the context was useful in arriving at the given answer.
750
  Give precision score of "1" if useful and "0" if it was not useful.'''
751
 
752
  precision_prompt = ChatPromptTemplate.from_messages([
 
779
  """
780
  print("---------refine_response---------")
781
 
782
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to refine the AI generated response by improving the accuracy and completeness of the response based on the contexxt.'''
783
 
784
  refine_response_prompt = ChatPromptTemplate.from_messages([
785
  ("system", system_message),
 
809
  Dict: The updated state with query refinement suggestions.
810
  """
811
  print("---------refine_query---------")
812
+ system_message = '''You are a smart nutrition disorder specialist. Your task is to refine the expanded query to improve the precision of the response.'''
813
 
814
  refine_query_prompt = ChatPromptTemplate.from_messages([
815
  ("system", system_message),
 
876
  workflow = StateGraph(AgentState)
877
 
878
  # Add processing nodes
879
+ workflow.add_node("expand_query", expand_query)
880
+ workflow.add_node("retrieve_context", retrieve_context)
881
+ workflow.add_node("craft_response", craft_response)
882
+ workflow.add_node("score_groundedness", score_groundedness)
883
+ workflow.add_node("refine_response", refine_response)
884
+ workflow.add_node("check_precision", check_precision)
885
+ workflow.add_node("refine_query",refine_query )
886
+ workflow.add_node("max_iterations_reached", max_iterations_reached)
887
 
888
  # Main flow edges
889
  workflow.add_edge(START, "expand_query")
 
993
  """
994
 
995
  # Initialize a memory client to store and retrieve customer interactions
 
996
  self.memory = MemoryClient(api_key=mem0_api_key)
997
  # Initialize the OpenAI client using the provided credentials
998
  self.client = ChatOpenAI(
 
1170
  st.write(message["content"])
1171
 
1172
  # Chat input with custom placeholder text
1173
+ user_query = st.chat_input("Type your question here (or exit to end): ", key="chat_input")
1174
  if user_query:
1175
  if user_query.lower() == "exit":
1176
  st.session_state.chat_history.append({"role": "user", "content": "exit"})
 
1189
  st.write(user_query)
1190
 
1191
  # Filter input using Llama Guard
1192
+ filtered_result = filter_input_with_llama_guard(user_query)
1193
  filtered_result = filtered_result.replace("\n", " ") # Normalize the result
1194
 
1195
  # Check if input is safe based on allowed statuses
1196
+ if filtered_result in ["safe", "unsafe S6", "unsafe S7"]:
1197
  try:
1198
  if 'chatbot' not in st.session_state:
1199
+ st.session_state.chatbot = NutritionBot()
1200
  response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
 
1201
  st.write(response)
1202
  st.session_state.chat_history.append({"role": "assistant", "content": response})
1203
  except Exception as e:
 
1209
  st.write(inappropriate_msg)
1210
  st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
1211
 
 
1212
  if __name__ == "__main__":
1213
  nutrition_disorder_streamlit()
1214