mohitbhardwaj commited on
Commit
3124891
·
verified ·
1 Parent(s): 3f65c7c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +807 -0
app.py ADDED
@@ -0,0 +1,807 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Import necessary libraries
3
+ import os # Interacting with the operating system (reading/writing files)
4
+ import chromadb # High-performance vector database for storing/querying dense vectors
5
+ from dotenv import load_dotenv # Loading environment variables from a .env file
6
+ import json # Parsing and handling JSON data
7
+
8
+ # LangChain imports
9
+ from langchain_core.documents import Document # Document data structures
10
+ from langchain_core.runnables import RunnablePassthrough # LangChain core library for running pipelines
11
+ from langchain_core.output_parsers import StrOutputParser # String output parser
12
+ from langchain.prompts import ChatPromptTemplate # Template for chat prompts
13
+ from langchain.chains.query_constructor.base import AttributeInfo # Base classes for query construction
14
+ from langchain.retrievers.self_query.base import SelfQueryRetriever # Base classes for self-querying retrievers
15
+ from langchain.retrievers.document_compressors import LLMChainExtractor, CrossEncoderReranker # Document compressors
16
+ from langchain.retrievers import ContextualCompressionRetriever # Contextual compression retrievers
17
+
18
+ # LangChain community & experimental imports
19
+ from langchain_community.vectorstores import Chroma # Implementations of vector stores like Chroma
20
+ from langchain_community.document_loaders import PyPDFDirectoryLoader, PyPDFLoader # Document loaders for PDFs
21
+ from langchain_community.cross_encoders import HuggingFaceCrossEncoder # Cross-encoders from HuggingFace
22
+ from langchain_experimental.text_splitter import SemanticChunker # Experimental text splitting methods
23
+ from langchain.text_splitter import (
24
+ CharacterTextSplitter, # Splitting text by characters
25
+ RecursiveCharacterTextSplitter # Recursive splitting of text by characters
26
+ )
27
+ from langchain_core.tools import tool
28
+ from langchain.agents import create_tool_calling_agent, AgentExecutor
29
+ from langchain_core.prompts import ChatPromptTemplate
30
+
31
+ # LangChain OpenAI imports
32
+ from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI # OpenAI embeddings and models
33
+ from langchain.embeddings.openai import OpenAIEmbeddings # OpenAI embeddings for text vectors
34
+
35
+ # LlamaParse & LlamaIndex imports
36
+ from llama_parse import LlamaParse # Document parsing library
37
+ from llama_index.core import Settings, SimpleDirectoryReader # Core functionalities of the LlamaIndex
38
+
39
+ # LangGraph import
40
+ from langgraph.graph import StateGraph, END, START # State graph for managing states in LangChain
41
+
42
+ # Pydantic import
43
+ from pydantic import BaseModel # Pydantic for data validation
44
+
45
+ # Typing imports
46
+ from typing import Dict, List, Tuple, Any, TypedDict # Python typing for function annotations
47
+
48
+ # Other utilities
49
+ import numpy as np # Numpy for numerical operations
50
+ from groq import Groq
51
+ from mem0 import MemoryClient
52
+ import streamlit as st
53
+ from datetime import datetime
54
+
55
+ #====================================SETUP=====================================#
56
+ # Fetch secrets from Hugging Face Spaces
57
+ api_key = config.get("API_KEY")
58
+ endpoint = config.get("OPENAI_API_BASE")
59
+ llama_api_key = os.environ['GROQ_API_KEY']
60
+ MEM0_api_key = os.environ['mem0']
61
+
62
+ # Initialize the OpenAI embedding function for Chroma
63
+ embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
64
+ api_base=endpoint, # Complete the code to define the API base endpoint
65
+ api_key=api_key, # Complete the code to define the API key
66
+ model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
67
+ )
68
+
69
+ # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key.
70
+
71
+ # Initialize the OpenAI Embeddings
72
+ embedding_model = OpenAIEmbeddings(
73
+ openai_api_base=endpoint,
74
+ openai_api_key=api_key,
75
+ model='text-embedding-ada-002'
76
+ )
77
+
78
+
79
+ # Initialize the Chat OpenAI model
80
+ llm = ChatOpenAI(
81
+ openai_api_base=endpoint,
82
+ openai_api_key=api_key,
83
+ model="gpt-4o-mini",
84
+ streaming=False
85
+ )
86
+ # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
87
+
88
+ # set the LLM and embedding model in the LlamaIndex settings.
89
+ Settings.llm = _____ # Complete the code to define the LLM model
90
+ Settings.embedding = _____ # Complete the code to define the embedding model
91
+
92
+ #================================Creating Langgraph agent======================#
93
+
94
+ class AgentState(TypedDict):
95
+ query: str # The current user query
96
+ expanded_query: str # The expanded version of the user query
97
+ context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
98
+ response: str # The generated response to the user query
99
+ precision_score: float # The precision score of the response
100
+ groundedness_score: float # The groundedness score of the response
101
+ groundedness_loop_count: int # Counter for groundedness refinement loops
102
+ precision_loop_count: int # Counter for precision refinement loops
103
+ feedback: str
104
+ query_feedback: str
105
+ groundedness_check: bool
106
+ loop_max_iter: int
107
+
108
+ def expand_query(state):
109
+ """
110
+ Expands the user query to improve retrieval of nutrition disorder-related information.
111
+
112
+ Args:
113
+ state (Dict): The current state of the workflow, containing the user query.
114
+
115
+ Returns:
116
+ Dict: The updated state with the expanded query.
117
+ """
118
+ print("---------Expanding Query---------")
119
+ #system_message = '''________________________'''
120
+ system_message = """You are a nutrition-focused query expander. Take the user’s original question about nutritional disorders and broaden it—adding relevant synonyms, related conditions, and subtopics—without changing its intent, so that the retrieval step can find the most useful documents."""
121
+
122
+
123
+ expand_prompt = ChatPromptTemplate.from_messages([
124
+ ("system", system_message),
125
+ ("user", "Expand this query: {query} using the feedback: {query_feedback}")
126
+
127
+ ])
128
+
129
+ chain = expand_prompt | llm | StrOutputParser()
130
+ expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]})
131
+ print("expanded_query", expanded_query)
132
+ state["expanded_query"] = expanded_query
133
+ return state
134
+
135
+
136
+ # Initialize the Chroma vector store for retrieving documents
137
+ vector_store = Chroma(
138
+ collection_name="nutritional_hypotheticals",
139
+ persist_directory="./nutritional_db",
140
+ embedding_function=embedding_model
141
+
142
+ )
143
+
144
+ # Create a retriever from the vector store
145
+ retriever = vector_store.as_retriever(
146
+ search_type='similarity',
147
+ search_kwargs={'k': 3}
148
+ )
149
+
150
+ def retrieve_context(state):
151
+ """
152
+ Retrieves context from the vector store using the expanded or original query.
153
+
154
+ Args:
155
+ state (Dict): The current state of the workflow, containing the query and expanded query.
156
+
157
+ Returns:
158
+ Dict: The updated state with the retrieved context.
159
+ """
160
+ print("---------retrieve_context---------")
161
+ #query = state['_____'] # Complete the code to define the key for the expanded query
162
+ query = state['expanded_query'] #
163
+ #print("Query used for retrieval:", query) # Debugging: Print the query
164
+
165
+ # Retrieve documents from the vector store
166
+ docs = retriever.invoke(query)
167
+
168
+ print("Retrieved documents:", docs) # Debugging: Print the raw docs object
169
+
170
+ # Extract both page_content and metadata from each document
171
+ context= [
172
+ {
173
+ "content": doc.page_content, # The actual content of the document
174
+ "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
175
+ }
176
+ for doc in docs
177
+ ]
178
+ #state['_____'] = context # Complete the code to define the key for storing the context
179
+ state['context'] = context
180
+ print("Extracted context with metadata:", context) # Debugging: Print the extracted context
181
+ #print(f"Groundedness loop count: {state['groundedness_loop_count']}")
182
+ return state
183
+
184
+
185
+
186
+ def craft_response(state: Dict) -> Dict:
187
+ """
188
+ Generates a response using the retrieved context, focusing on nutrition disorders.
189
+
190
+ Args:
191
+ state (Dict): The current state of the workflow, containing the query and retrieved context.
192
+
193
+ Returns:
194
+ Dict: The updated state with the generated response.
195
+ """
196
+ print("---------craft_response---------")
197
+ #system_message = '''________________________'''
198
+ system_message = """
199
+ You are an expert Nutrition Disorder Specialist. Use only the retrieved context to craft a clear, accurate, and empathetic answer to the user’s query about nutritional disorders.
200
+ """
201
+
202
+ response_prompt = ChatPromptTemplate.from_messages([
203
+ ("system", system_message),
204
+ ("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")
205
+ ])
206
+
207
+ chain = response_prompt | llm
208
+ response = chain.invoke({
209
+ "query": state['query'],
210
+ "context": "\n".join([doc["content"] for doc in state['context']]),
211
+ #"feedback": ________________ # add feedback to the prompt
212
+ "feedback": state['feedback'] # add
213
+ })
214
+ state['response'] = response
215
+ print("intermediate response: ", response)
216
+
217
+ return state
218
+
219
+
220
+
221
+ def score_groundedness(state: Dict) -> Dict:
222
+ """
223
+ Checks whether the response is grounded in the retrieved context.
224
+
225
+ Args:
226
+ state (Dict): The current state of the workflow, containing the response and context.
227
+
228
+ Returns:
229
+ Dict: The updated state with the groundedness score.
230
+ """
231
+ print("---------check_groundedness---------")
232
+ #system_message = '''________________________'''
233
+ system_message = """
234
+ You are a factuality evaluator. Given a piece of context and a proposed response, assign a groundedness score between 0 (no support in the context) and 1 (fully supported by the context).
235
+ """
236
+
237
+ groundedness_prompt = ChatPromptTemplate.from_messages([
238
+ ("system", system_message),
239
+ ("user", "Context: {context}\nResponse: {response}\n\nGroundedness score:")
240
+ ])
241
+
242
+ chain = groundedness_prompt | llm | StrOutputParser()
243
+ groundedness_score = float(chain.invoke({
244
+ "context": "\n".join([doc["content"] for doc in state['context']]),
245
+ #"response": __________ # Complete the code to define the response
246
+ "response": state['response'] #
247
+ }))
248
+ print("groundedness_score: ", groundedness_score)
249
+ state['groundedness_loop_count'] += 1
250
+ print("#########Groundedness Incremented###########")
251
+ state['groundedness_score'] = groundedness_score
252
+
253
+ return state
254
+
255
+
256
+
257
+ def check_precision(state: Dict) -> Dict:
258
+ """
259
+ Checks whether the response precisely addresses the user’s query.
260
+
261
+ Args:
262
+ state (Dict): The current state of the workflow, containing the query and response.
263
+
264
+ Returns:
265
+ Dict: The updated state with the precision score.
266
+ """
267
+ print("---------check_precision---------")
268
+ #system_message = '''________________________'''
269
+ system_message = """You are a precision evaluator. Given a user query and an answer, assign a precision score from 0 (does not address the query) to 1 (fully answers the query)."""
270
+
271
+ precision_prompt = ChatPromptTemplate.from_messages([
272
+ ("system", system_message),
273
+ ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
274
+ ])
275
+
276
+ chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing
277
+ precision_score = float(chain.invoke({
278
+ "query": state['query'],
279
+ "response":state['response'] # Complete the code to access the response from the state
280
+ }))
281
+ state['precision_score'] = precision_score
282
+ print("precision_score:", precision_score)
283
+ state['precision_loop_count'] +=1
284
+ print("#########Precision Incremented###########")
285
+ return state
286
+
287
+
288
+
289
+ def refine_response(state: Dict) -> Dict:
290
+ """
291
+ Suggests improvements for the generated response.
292
+
293
+ Args:
294
+ state (Dict): The current state of the workflow, containing the query and response.
295
+
296
+ Returns:
297
+ Dict: The updated state with response refinement suggestions.
298
+ """
299
+ print("---------refine_response---------")
300
+
301
+ #system_message = '''________________________'''
302
+ system_message = """
303
+ You are a response-refinement assistant. Given a user query and an existing answer, suggest concrete improvements—adding missing details, correcting errors, and clarifying wording to make it as accurate and complete as possible.
304
+ """
305
+
306
+ refine_response_prompt = ChatPromptTemplate.from_messages([
307
+ ("system", system_message),
308
+ ("user", "Query: {query}\nResponse: {response}\n\n"
309
+ "What improvements can be made to enhance accuracy and completeness?")
310
+ ])
311
+
312
+ chain = refine_response_prompt | llm| StrOutputParser()
313
+
314
+ # Store response suggestions in a structured format
315
+ feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'response': state['response']})}"
316
+ print("feedback: ", feedback)
317
+ print(f"State: {state}")
318
+ state['feedback'] = feedback
319
+ return state
320
+
321
+
322
+
323
+ def refine_query(state: Dict) -> Dict:
324
+ """
325
+ Suggests improvements for the expanded query.
326
+
327
+ Args:
328
+ state (Dict): The current state of the workflow, containing the query and expanded query.
329
+
330
+ Returns:
331
+ Dict: The updated state with query refinement suggestions.
332
+ """
333
+ print("---------refine_query---------")
334
+ #system_message = '''________________________'''
335
+ system_message = """
336
+ You are a query‐refinement assistant. Given an original user question and its expanded form, suggest concrete ways to make the search query more precise, comprehensive, and effective for retrieving nutrition‐disorder information.
337
+ """
338
+
339
+ refine_query_prompt = ChatPromptTemplate.from_messages([
340
+ ("system", system_message),
341
+ ("user", "Original Query: {query}\nExpanded Query: {expanded_query}\n\n"
342
+ "What improvements can be made for a better search?")
343
+ ])
344
+
345
+ chain = refine_query_prompt | llm | StrOutputParser()
346
+
347
+ # Store refinement suggestions without modifying the original expanded query
348
+ query_feedback = f"Previous Expanded Query: {state['expanded_query']}\nSuggestions: {chain.invoke({'query': state['query'], 'expanded_query': state['expanded_query']})}"
349
+ print("query_feedback: ", query_feedback)
350
+ print(f"Groundedness loop count: {state['groundedness_loop_count']}")
351
+ state['query_feedback'] = query_feedback
352
+ return state
353
+
354
+
355
+
356
+ def should_continue_groundedness(state):
357
+ """Decides if groundedness is sufficient or needs improvement."""
358
+ print("---------should_continue_groundedness---------")
359
+ print("groundedness loop count: ", state['groundedness_loop_count'])
360
+ if state['groundedness_score'] >= 0.8: # Complete the code to define the threshold for groundedness
361
+ print("Moving to precision")
362
+ return "check_precision"
363
+ else:
364
+ if state["groundedness_loop_count"] > state['loop_max_iter']:
365
+ return "max_iterations_reached"
366
+ else:
367
+ print(f"---------Groundedness Score Threshold Not met. Refining Response-----------")
368
+ return "refine_response"
369
+
370
+
371
+ def should_continue_precision(state: Dict) -> str:
372
+ """Decides if precision is sufficient or needs improvement."""
373
+ print("---------should_continue_precision---------")
374
+ print("precision loop count: ", state['precision_loop_count'])
375
+ if state['precision_score'] >= 0.8: # Threshold for precision
376
+ return "pass" # Complete the workflow
377
+ else:
378
+ if state['precision_loop_count'] > state['loop_max_iter']: # Maximum allowed loops
379
+ return "max_iterations_reached"
380
+ else:
381
+ print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging
382
+ return "refine_query" # Refine the query
383
+
384
+
385
+
386
+
387
+ def max_iterations_reached(state: Dict) -> Dict:
388
+ """Handles the case when the maximum number of iterations is reached."""
389
+ print("---------max_iterations_reached---------")
390
+ """Handles the case when the maximum number of iterations is reached."""
391
+ response = "I'm unable to refine the response further. Please provide more context or clarify your question."
392
+ state['response'] = response
393
+ return state
394
+
395
+
396
+
397
+ from langgraph.graph import END, StateGraph, START
398
+
399
+ # def create_workflow() -> StateGraph:
400
+ # """Creates the updated workflow for the AI nutrition agent."""
401
+ # workflow = StateGraph(_____ ) # Complete the code to define the initial state of the agent
402
+
403
+ # # Add processing nodes
404
+ # workflow.add_node("expand_query", _____ ) # Step 1: Expand user query. Complete with the function to expand the query
405
+ # workflow.add_node("retrieve_context", _____ ) # Step 2: Retrieve relevant documents. Complete with the function to retrieve context
406
+ # workflow.add_node("craft_response", _____ ) # Step 3: Generate a response based on retrieved data. Complete with the function to craft a response
407
+ # workflow.add_node("score_groundedness", _____ ) # Step 4: Evaluate response grounding. Complete with the function to score groundedness
408
+ # workflow.add_node("refine_response", _____ ) # Step 5: Improve response if it's weakly grounded. Complete with the function to refine the response
409
+ # workflow.add_node("check_precision", _____ ) # Step 6: Evaluate response precision. Complete with the function to check precision
410
+ # workflow.add_node("refine_query", _____ ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
411
+ # workflow.add_node("max_iterations_reached", _____ ) # Step 8: Handle max iterations. Complete with the function to handle max iterations
412
+
413
+ # # Main flow edges
414
+ # workflow.add_edge(START, "expand_query")
415
+ # workflow.add_edge("expand_query", "retrieve_context")
416
+ # workflow.add_edge("retrieve_context", "craft_response")
417
+ # workflow.add_edge("craft_response", "score_groundedness")
418
+
419
+ # # Conditional edges based on groundedness check
420
+ # workflow.add_conditional_edges(
421
+ # "score_groundedness",
422
+ # ___________, # Use the conditional function
423
+ # {
424
+ # "check_precision": ___________, # If well-grounded, proceed to precision check.
425
+ # "refine_response": ___________, # If not, refine the response.
426
+ # "max_iterations_reached": ___________ # If max loops reached, exit.
427
+ # }
428
+ # )
429
+
430
+ # workflow.add_edge(__________, ___________) # Refined responses are reprocessed.
431
+
432
+ # # Conditional edges based on precision check
433
+ # workflow.add_conditional_edges(
434
+ # "check_precision",
435
+ # ___________, # Use the conditional function
436
+ # {
437
+ # "pass": ___________, # If precise, complete the workflow.
438
+ # "refine_query": ___________, # If imprecise, refine the query.
439
+ # "max_iterations_reached": ___________ # If max loops reached, exit.
440
+ # }
441
+ # )
442
+
443
+ # workflow.add_edge(__________, ___________) # Refined queries go through expansion again.
444
+
445
+ # workflow.add_edge("max_iterations_reached", END)
446
+
447
+ # return workflow
448
+
449
+ def create_workflow() -> StateGraph:
450
+ """Creates the updated workflow for the AI nutrition agent."""
451
+ workflow = StateGraph(START) # Initial state of the agent
452
+
453
+ # Add processing nodes
454
+ workflow.add_node("expand_query", expand_query) # Step 1: Expand user query. Complete with the function to expand the query
455
+ workflow.add_node("retrieve_context", retrieve_context) # Step 2: Retrieve relevant documents. Complete with the function to retrieve context
456
+ workflow.add_node("craft_response", craft_response) # Step 3: Generate a response based on retrieved data. Complete with the function to craft a response
457
+ workflow.add_node("score_groundedness", score_groundedness) # Step 4: Evaluate response grounding. Complete with the function to score groundedness
458
+ workflow.add_node("refine_response", refine_response) # Step 5: Improve response if it's weakly grounded. Complete with the function to refine the response
459
+ workflow.add_node("check_precision", check_precision) # Step 6: Evaluate response precision. Complete with the function to check precision
460
+ workflow.add_node("refine_query", refine_query) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
461
+ workflow.add_node("max_iterations_reached", max_iterations_reached) # Step 8: Handle max iterations. Complete with the function to handle max iterations
462
+
463
+ # Main flow edges
464
+ workflow.add_edge(START, "expand_query")
465
+ workflow.add_edge("expand_query", "retrieve_context")
466
+ workflow.add_edge("retrieve_context", "craft_response")
467
+ workflow.add_edge("craft_response", "score_groundedness")
468
+
469
+ # Conditional edges based on groundedness check
470
+ workflow.add_conditional_edges(
471
+ "score_groundedness",
472
+ should_continue_groundedness, # Use the conditional function
473
+ {
474
+ "check_precision": "check_precision", # If well-grounded, proceed to precision check.
475
+ "refine_response": "refine_response", # If not, refine the response.
476
+ "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit.
477
+ }
478
+ )
479
+ workflow.add_edge("refine_response", "craft_response") # Refined responses are reprocessed.
480
+
481
+ # Conditional edges based on precision check
482
+ workflow.add_conditional_edges(
483
+ "check_precision",
484
+ should_continue_precision, # Use the conditional function
485
+ {
486
+ "pass": END, # If precise, complete the workflow.
487
+ "refine_query": "refine_query", # If imprecise, refine the query.
488
+ "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit.
489
+ }
490
+ )
491
+ workflow.add_edge("refine_query", "expand_query") # Refined queries go through expansion again.
492
+
493
+ workflow.add_edge("max_iterations_reached", END)
494
+
495
+ return workflow
496
+
497
+
498
+ #=========================== Defining the agentic rag tool ====================#
499
+ WORKFLOW_APP = create_workflow().compile()
500
+ @tool
501
+ def agentic_rag(query: str):
502
+ """
503
+ Runs the RAG-based agent with conversation history for context-aware responses.
504
+
505
+ Args:
506
+ query (str): The current user query.
507
+
508
+ Returns:
509
+ Dict[str, Any]: The updated state with the generated response and conversation history.
510
+ """
511
+ # Initialize state with necessary parameters
512
+ # inputs = {
513
+ # "query": query, # Current user query
514
+ # "expanded_query": "_____", # Complete the code to define the expanded version of the query
515
+ # "context": [], # Retrieved documents (initially empty)
516
+ # "response": "_____", # Complete the code to define the AI-generated response
517
+ # "precision_score": _____, # Complete the code to define the precision score of the response
518
+ # "groundedness_score": _____, # Complete the code to define the groundedness score of the response
519
+ # "groundedness_loop_count": _____, # Complete the code to define the counter for groundedness loops
520
+ # "precision_loop_count": _____, # Complete the code to define the counter for precision loops
521
+ # "feedback": "_____", # Complete the code to define the feedback
522
+ # "query_feedback": "_____", # Complete the code to define the query feedback
523
+ # "loop_max_iter": _____ # Complete the code to define the maximum number of iterations for loops
524
+ # }
525
+
526
+ inputs = {
527
+ "query": query,
528
+ "expanded_query": query,
529
+ "context": [],
530
+ "response": "",
531
+ "precision_score": 0.0,
532
+ "groundedness_score": 0.0,
533
+ "groundedness_loop_count": 0,
534
+ "precision_loop_count": 0,
535
+ "feedback": "",
536
+ "query_feedback": "",
537
+ "loop_max_iter": 3
538
+ }
539
+
540
+
541
+ output = WORKFLOW_APP.invoke(inputs)
542
+
543
+ return output
544
+
545
+
546
+ #================================ Guardrails ===========================#
547
+ llama_guard_client = Groq(api_key=llama_api_key)
548
+ # Function to filter user input with Llama Guard
549
+ def filter_input_with_llama_guard(user_input, model="llama-guard-3-8b"):
550
+ """
551
+ Filters user input using Llama Guard to ensure it is safe.
552
+
553
+ Parameters:
554
+ - user_input: The input provided by the user.
555
+ - model: The Llama Guard model to be used for filtering (default is "llama-guard-3-8b").
556
+
557
+ Returns:
558
+ - The filtered and safe input.
559
+ """
560
+ try:
561
+ # Create a request to Llama Guard to filter the user input
562
+ response = llama_guard_client.chat.completions.create(
563
+ messages=[{"role": "user", "content": user_input}],
564
+ model=model,
565
+ )
566
+ # Return the filtered input
567
+ return response.choices[0].message.content.strip()
568
+ except Exception as e:
569
+ print(f"Error with Llama Guard: {e}")
570
+ return None
571
+
572
+
573
+ #============================= Adding Memory to the agent using mem0 ===============================#
574
+
575
+ class NutritionBot:
576
+ def __init__(self):
577
+ """
578
+ Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
579
+ """
580
+
581
+ # Initialize a memory client to store and retrieve customer interactions
582
+ self.memory = MemoryClient(api_key=userdata.get("mem0")) # Complete the code to define the memory client API key
583
+
584
+ # Initialize the OpenAI client using the provided credentials
585
+ self.client = ChatOpenAI(
586
+ model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
587
+ api_key=config.get("API_KEY"), # API key for authentication
588
+ endpoint = config.get("OPENAI_API_BASE"),
589
+ temperature=0 # Controls randomness in responses; 0 ensures deterministic results
590
+ )
591
+
592
+ # Define tools available to the chatbot, such as web search
593
+ tools = [agentic_rag]
594
+
595
+ # Define the system prompt to set the behavior of the chatbot
596
+ 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.
597
+ Guidelines for Interaction:
598
+ Maintain a polite, professional, and reassuring tone.
599
+ Show genuine empathy for customer concerns and health challenges.
600
+ Reference past interactions to provide personalized and consistent advice.
601
+ Engage with the customer by asking about their food preferences, dietary restrictions, and lifestyle before offering recommendations.
602
+ Ensure consistent and accurate information across conversations.
603
+ If any detail is unclear or missing, proactively ask for clarification.
604
+ Always use the agentic_rag tool to retrieve up-to-date and evidence-based nutrition insights.
605
+ Keep track of ongoing issues and follow-ups to ensure continuity in support.
606
+ Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
607
+
608
+ """
609
+
610
+ # Build the prompt template for the agent
611
+ prompt = ChatPromptTemplate.from_messages([
612
+ ("system", system_prompt), # System instructions
613
+ ("human", "{input}"), # Placeholder for human input
614
+ ("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
615
+ ])
616
+
617
+ # Create an agent capable of interacting with tools and executing tasks
618
+ agent = create_tool_calling_agent(self.client, tools, prompt)
619
+
620
+ # Wrap the agent in an executor to manage tool interactions and execution flow
621
+ self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
622
+
623
+
624
+ def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
625
+ """
626
+ Store customer interaction in memory for future reference.
627
+
628
+ Args:
629
+ user_id (str): Unique identifier for the customer.
630
+ message (str): Customer's query or message.
631
+ response (str): Chatbot's response.
632
+ metadata (Dict, optional): Additional metadata for the interaction.
633
+ """
634
+ if metadata is None:
635
+ metadata = {}
636
+
637
+ # Add a timestamp to the metadata for tracking purposes
638
+ metadata["timestamp"] = datetime.now().isoformat()
639
+
640
+ # Format the conversation for storage
641
+ conversation = [
642
+ {"role": "user", "content": message},
643
+ {"role": "assistant", "content": response}
644
+ ]
645
+
646
+ # Store the interaction in the memory client
647
+ self.memory.add(
648
+ conversation,
649
+ user_id=user_id,
650
+ output_format="v1.1",
651
+ metadata=metadata
652
+ )
653
+
654
+
655
+ def get_relevant_history(self, user_id: str, query: str) -> List[Dict]:
656
+ """
657
+ Retrieve past interactions relevant to the current query.
658
+
659
+ Args:
660
+ user_id (str): Unique identifier for the customer.
661
+ query (str): The customer's current query.
662
+
663
+ Returns:
664
+ List[Dict]: A list of relevant past interactions.
665
+ """
666
+ return self.memory.search(
667
+ query=query, # Search for interactions related to the query
668
+ user_id=user_id, # Restrict search to the specific user
669
+ limit=5 # Complete the code to define the limit for retrieved interactions
670
+ )
671
+
672
+
673
+ def handle_customer_query(self, user_id: str, query: str) -> str:
674
+ """
675
+ Process a customer's query and provide a response, taking into account past interactions.
676
+
677
+ Args:
678
+ user_id (str): Unique identifier for the customer.
679
+ query (str): Customer's query.
680
+
681
+ Returns:
682
+ str: Chatbot's response.
683
+ """
684
+
685
+ # Retrieve relevant past interactions for context
686
+ relevant_history = self.get_relevant_history(user_id, query)
687
+
688
+ # Build a context string from the relevant history
689
+ context = "Previous relevant interactions:\n"
690
+ for memory in relevant_history:
691
+ context += f"Customer: {memory['memory']}\n" # Customer's past messages
692
+ context += f"Support: {memory['memory']}\n" # Chatbot's past responses
693
+ context += "---\n"
694
+
695
+ # Print context for debugging purposes
696
+ print("Context: ", context)
697
+
698
+ # Prepare a prompt combining past context and the current query
699
+ prompt = f"""
700
+ Context:
701
+ {context}
702
+
703
+ Current customer query: {query}
704
+
705
+ Provide a helpful response that takes into account any relevant past interactions.
706
+ """
707
+
708
+ # Generate a response using the agent
709
+ response = self.agent_executor.invoke({"input": prompt})
710
+
711
+ # Store the current interaction for future reference
712
+ self.store_customer_interaction(
713
+ user_id=user_id,
714
+ message=query,
715
+ response=response["output"],
716
+ metadata={"type": "support_query"}
717
+ )
718
+
719
+ # Return the chatbot's response
720
+ return response['output']
721
+
722
+
723
+ #=====================User Interface using streamlit ===========================#
724
+ def nutrition_disorder_streamlit():
725
+ """
726
+ A Streamlit-based UI for the Nutrition Disorder Specialist Agent.
727
+ """
728
+ st.title("Nutrition Disorder Specialist")
729
+ st.write("Ask me anything about nutrition disorders, symptoms, causes, treatments, and more.")
730
+ st.write("Type 'exit' to end the conversation.")
731
+
732
+ # Initialize session state for chat history and user_id if they don't exist
733
+ if 'chat_history' not in st.session_state:
734
+ st.session_state.chat_history = []
735
+ if 'user_id' not in st.session_state:
736
+ st.session_state.user_id = None
737
+
738
+ # Login form: Only if user is not logged in
739
+ if st.session_state.user_id is None:
740
+ with st.form("login_form", clear_on_submit=True):
741
+ user_id = st.text_input("Please enter your name to begin:")
742
+ submit_button = st.form_submit_button("Login")
743
+ if submit_button and user_id:
744
+ st.session_state.user_id = user_id
745
+ st.session_state.chat_history.append({
746
+ "role": "assistant",
747
+ "content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
748
+ })
749
+ st.session_state.login_submitted = True # Set flag to trigger rerun
750
+ if st.session_state.get("login_submitted", False):
751
+ st.session_state.pop("login_submitted")
752
+ st.rerun()
753
+ else:
754
+ # Display chat history
755
+ for message in st.session_state.chat_history:
756
+ with st.chat_message(message["role"]):
757
+ st.write(message["content"])
758
+
759
+ # Chat input with custom placeholder text
760
+ #user_query = st.chat_input(__________) # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...")
761
+ user_query = st.chat_input("Type your question here (or 'exit' to end)...") # Blank #1:
762
+ if user_query:
763
+ if user_query.lower() == "exit":
764
+ st.session_state.chat_history.append({"role": "user", "content": "exit"})
765
+ with st.chat_message("user"):
766
+ st.write("exit")
767
+ goodbye_msg = "Goodbye! Feel free to return if you have more questions about nutrition disorders."
768
+ st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg})
769
+ with st.chat_message("assistant"):
770
+ st.write(goodbye_msg)
771
+ st.session_state.user_id = None
772
+ st.rerun()
773
+ return
774
+
775
+ st.session_state.chat_history.append({"role": "user", "content": user_query})
776
+ with st.chat_message("user"):
777
+ st.write(user_query)
778
+
779
+ # Filter input using Llama Guard
780
+ #filtered_result = __________(user_query) # Blank #2: Fill in with the function name for filtering input (e.g., filter_input_with_llama_guard)
781
+ filtered_result = filter_input_with_llama_guard(user_query) # Blank #2:
782
+ filtered_result = filtered_result.replace("\n", " ") # Normalize the result
783
+
784
+ # Check if input is safe based on allowed statuses
785
+ #if filtered_result in [__________, __________, __________]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
786
+ if filtered_result in ["safe", "unsafe S7", "unsafe S6"]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
787
+
788
+ try:
789
+ if 'chatbot' not in st.session_state:
790
+ #st.session_state.chatbot = __________() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
791
+ st.session_state.chatbot = NutritionBot() # Blank #6:
792
+ response = st.session_state.chatbot.__________(st.session_state.user_id, user_query)
793
+ response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
794
+ # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
795
+ st.write(response)
796
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
797
+ except Exception as e:
798
+ error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
799
+ st.write(error_msg)
800
+ st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
801
+ else:
802
+ inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
803
+ st.write(inappropriate_msg)
804
+ st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
805
+
806
+ if __name__ == "__main__":
807
+ nutrition_disorder_streamlit()