AdaptAI-appdev1 commited on
Commit
2d30a34
·
verified ·
1 Parent(s): 4429c73

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +722 -0
app.py ADDED
@@ -0,0 +1,722 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from langchain_openai import ChatOpenAI
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
+ #====================================SETUP=====================================#
57
+ # Fetch secrets from Hugging Face Spaces
58
+ api_key = config.get("API_KEY")
59
+ endpoint = config.get("OPENAI_API_BASE")
60
+ llama_api_key = os.environ['GROQ_API_KEY']
61
+ MEM0_api_key = os.environ['mem0']
62
+
63
+ # Initialize the OpenAI embedding function for Chroma
64
+ embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
65
+ api_base=endpoint, # Complete the code to define the API base endpoint
66
+ api_key=api_key, # Complete the code to define the API key
67
+ model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
68
+ )
69
+
70
+ # This initializes the OpenAI embedding function for the Chroma vectorstore, using the provided endpoint and API key.
71
+
72
+ # Initialize the OpenAI Embeddings
73
+ embedding_model = OpenAIEmbeddings(
74
+ openai_api_base=endpoint,
75
+ openai_api_key=api_key,
76
+ model='text-embedding-ada-002'
77
+ )
78
+
79
+
80
+ # Initialize the Chat OpenAI model
81
+ llm = ChatOpenAI(
82
+ base_url=endpoint,
83
+ openai_api_key=api_key,
84
+ model="gpt-4o-mini",
85
+ streaming=False
86
+ )
87
+ # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
88
+
89
+ # set the LLM and embedding model in the LlamaIndex settings.
90
+ Settings.llm = _____ # Complete the code to define the LLM model
91
+ Settings.embedding = _____ # Complete the code to define the embedding model
92
+
93
+ #================================Creating Langgraph agent======================#
94
+
95
+ class AgentState(TypedDict):
96
+ query: str # The current user query
97
+ expanded_query: str # The expanded version of the user query
98
+ context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
99
+ response: str # The generated response to the user query
100
+ precision_score: float # The precision score of the response
101
+ groundedness_score: float # The groundedness score of the response
102
+ groundedness_loop_count: int # Counter for groundedness refinement loops
103
+ precision_loop_count: int # Counter for precision refinement loops
104
+ feedback: str
105
+ query_feedback: str
106
+ groundedness_check: bool
107
+ loop_max_iter: int
108
+
109
+ def expand_query(state):
110
+ """
111
+ Expands the user query to improve retrieval of nutrition disorder-related information.
112
+
113
+ Args:
114
+ state (Dict): The current state of the workflow, containing the user query.
115
+
116
+ Returns:
117
+ Dict: The updated state with the expanded query.
118
+ """
119
+ print("---------Expanding Query---------")
120
+ system_message = '''________________________'''
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
+ #print("Query used for retrieval:", query) # Debugging: Print the query
163
+
164
+ # Retrieve documents from the vector store
165
+ docs = retriever.invoke(query)
166
+ print("Retrieved documents:", docs) # Debugging: Print the raw docs object
167
+
168
+ # Extract both page_content and metadata from each document
169
+ context= [
170
+ {
171
+ "content": doc.page_content, # The actual content of the document
172
+ "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
173
+ }
174
+ for doc in docs
175
+ ]
176
+ state['_____'] = context # Complete the code to define the key for storing the context
177
+ print("Extracted context with metadata:", context) # Debugging: Print the extracted context
178
+ #print(f"Groundedness loop count: {state['groundedness_loop_count']}")
179
+ return state
180
+
181
+
182
+
183
+ def craft_response(state: Dict) -> Dict:
184
+ """
185
+ Generates a response using the retrieved context, focusing on nutrition disorders.
186
+
187
+ Args:
188
+ state (Dict): The current state of the workflow, containing the query and retrieved context.
189
+
190
+ Returns:
191
+ Dict: The updated state with the generated response.
192
+ """
193
+ print("---------craft_response---------")
194
+ system_message = '''________________________'''
195
+
196
+ response_prompt = ChatPromptTemplate.from_messages([
197
+ ("system", system_message),
198
+ ("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")
199
+ ])
200
+
201
+ chain = response_prompt | llm
202
+ response = chain.invoke({
203
+ "query": state['query'],
204
+ "context": "\n".join([doc["content"] for doc in state['context']]),
205
+ "feedback": ________________ # add feedback to the prompt
206
+ })
207
+ state['response'] = response
208
+ print("intermediate response: ", response)
209
+
210
+ return state
211
+
212
+
213
+
214
+ def score_groundedness(state: Dict) -> Dict:
215
+ """
216
+ Checks whether the response is grounded in the retrieved context.
217
+
218
+ Args:
219
+ state (Dict): The current state of the workflow, containing the response and context.
220
+
221
+ Returns:
222
+ Dict: The updated state with the groundedness score.
223
+ """
224
+ print("---------check_groundedness---------")
225
+ system_message = '''________________________'''
226
+
227
+ groundedness_prompt = ChatPromptTemplate.from_messages([
228
+ ("system", system_message),
229
+ ("user", "Context: {context}\nResponse: {response}\n\nGroundedness score:")
230
+ ])
231
+
232
+ chain = groundedness_prompt | llm | StrOutputParser()
233
+ groundedness_score = float(chain.invoke({
234
+ "context": "\n".join([doc["content"] for doc in state['context']]),
235
+ "response": __________ # Complete the code to define the response
236
+ }))
237
+ print("groundedness_score: ", groundedness_score)
238
+ state['groundedness_loop_count'] += 1
239
+ print("#########Groundedness Incremented###########")
240
+ state['groundedness_score'] = groundedness_score
241
+
242
+ return state
243
+
244
+
245
+
246
+ def check_precision(state: Dict) -> Dict:
247
+ """
248
+ Checks whether the response precisely addresses the user’s query.
249
+
250
+ Args:
251
+ state (Dict): The current state of the workflow, containing the query and response.
252
+
253
+ Returns:
254
+ Dict: The updated state with the precision score.
255
+ """
256
+ print("---------check_precision---------")
257
+ system_message = '''________________________'''
258
+
259
+ precision_prompt = ChatPromptTemplate.from_messages([
260
+ ("system", system_message),
261
+ ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
262
+ ])
263
+
264
+ chain = _____________ | llm | StrOutputParser() # Complete the code to define the chain of processing
265
+ precision_score = float(chain.invoke({
266
+ "query": state['query'],
267
+ "response":______________ # Complete the code to access the response from the state
268
+ }))
269
+ state['precision_score'] = precision_score
270
+ print("precision_score:", precision_score)
271
+ state['precision_loop_count'] +=1
272
+ print("#########Precision Incremented###########")
273
+ return state
274
+
275
+
276
+
277
+ def refine_response(state: Dict) -> Dict:
278
+ """
279
+ Suggests improvements for the generated response.
280
+
281
+ Args:
282
+ state (Dict): The current state of the workflow, containing the query and response.
283
+
284
+ Returns:
285
+ Dict: The updated state with response refinement suggestions.
286
+ """
287
+ print("---------refine_response---------")
288
+
289
+ system_message = '''________________________'''
290
+
291
+ refine_response_prompt = ChatPromptTemplate.from_messages([
292
+ ("system", system_message),
293
+ ("user", "Query: {query}\nResponse: {response}\n\n"
294
+ "What improvements can be made to enhance accuracy and completeness?")
295
+ ])
296
+
297
+ chain = refine_response_prompt | llm| StrOutputParser()
298
+
299
+ # Store response suggestions in a structured format
300
+ feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'response': state['response']})}"
301
+ print("feedback: ", feedback)
302
+ print(f"State: {state}")
303
+ state['feedback'] = feedback
304
+ return state
305
+
306
+
307
+
308
+ def refine_query(state: Dict) -> Dict:
309
+ """
310
+ Suggests improvements for the expanded query.
311
+
312
+ Args:
313
+ state (Dict): The current state of the workflow, containing the query and expanded query.
314
+
315
+ Returns:
316
+ Dict: The updated state with query refinement suggestions.
317
+ """
318
+ print("---------refine_query---------")
319
+ system_message = '''________________________'''
320
+
321
+ refine_query_prompt = ChatPromptTemplate.from_messages([
322
+ ("system", system_message),
323
+ ("user", "Original Query: {query}\nExpanded Query: {expanded_query}\n\n"
324
+ "What improvements can be made for a better search?")
325
+ ])
326
+
327
+ chain = refine_query_prompt | llm | StrOutputParser()
328
+
329
+ # Store refinement suggestions without modifying the original expanded query
330
+ query_feedback = f"Previous Expanded Query: {state['expanded_query']}\nSuggestions: {chain.invoke({'query': state['query'], 'expanded_query': state['expanded_query']})}"
331
+ print("query_feedback: ", query_feedback)
332
+ print(f"Groundedness loop count: {state['groundedness_loop_count']}")
333
+ state['query_feedback'] = query_feedback
334
+ return state
335
+
336
+
337
+
338
+ def should_continue_groundedness(state):
339
+ """Decides if groundedness is sufficient or needs improvement."""
340
+ print("---------should_continue_groundedness---------")
341
+ print("groundedness loop count: ", state['groundedness_loop_count'])
342
+ if state['groundedness_score'] >= _____: # Complete the code to define the threshold for groundedness
343
+ print("Moving to precision")
344
+ return "check_precision"
345
+ else:
346
+ if state["groundedness_loop_count"] > state['loop_max_iter']:
347
+ return "max_iterations_reached"
348
+ else:
349
+ print(f"---------Groundedness Score Threshold Not met. Refining Response-----------")
350
+ return "refine_response"
351
+
352
+
353
+ def should_continue_precision(state: Dict) -> str:
354
+ """Decides if precision is sufficient or needs improvement."""
355
+ print("---------should_continue_precision---------")
356
+ print("precision loop count: ", ___________)
357
+ if ___________: # Threshold for precision
358
+ return "pass" # Complete the workflow
359
+ else:
360
+ if ___________: # Maximum allowed loops
361
+ return "max_iterations_reached"
362
+ else:
363
+ print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging
364
+ return ____________ # Refine the query
365
+
366
+
367
+
368
+
369
+ def max_iterations_reached(state: Dict) -> Dict:
370
+ """Handles the case when the maximum number of iterations is reached."""
371
+ print("---------max_iterations_reached---------")
372
+ """Handles the case when the maximum number of iterations is reached."""
373
+ response = "I'm unable to refine the response further. Please provide more context or clarify your question."
374
+ state['response'] = response
375
+ return state
376
+
377
+
378
+
379
+ from langgraph.graph import END, StateGraph, START
380
+
381
+ def create_workflow() -> StateGraph:
382
+ """Creates the updated workflow for the AI nutrition agent."""
383
+ workflow = StateGraph(_____ ) # Complete the code to define the initial state of the agent
384
+
385
+ # Add processing nodes
386
+ workflow.add_node("expand_query", _____ ) # Step 1: Expand user query. Complete with the function to expand the query
387
+ workflow.add_node("retrieve_context", _____ ) # Step 2: Retrieve relevant documents. Complete with the function to retrieve context
388
+ workflow.add_node("craft_response", _____ ) # Step 3: Generate a response based on retrieved data. Complete with the function to craft a response
389
+ workflow.add_node("score_groundedness", _____ ) # Step 4: Evaluate response grounding. Complete with the function to score groundedness
390
+ workflow.add_node("refine_response", _____ ) # Step 5: Improve response if it's weakly grounded. Complete with the function to refine the response
391
+ workflow.add_node("check_precision", _____ ) # Step 6: Evaluate response precision. Complete with the function to check precision
392
+ workflow.add_node("refine_query", _____ ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
393
+ workflow.add_node("max_iterations_reached", _____ ) # Step 8: Handle max iterations. Complete with the function to handle max iterations
394
+
395
+ # Main flow edges
396
+ workflow.add_edge(START, "expand_query")
397
+ workflow.add_edge("expand_query", "retrieve_context")
398
+ workflow.add_edge("retrieve_context", "craft_response")
399
+ workflow.add_edge("craft_response", "score_groundedness")
400
+
401
+ # Conditional edges based on groundedness check
402
+ workflow.add_conditional_edges(
403
+ "score_groundedness",
404
+ ___________, # Use the conditional function
405
+ {
406
+ "check_precision": ___________, # If well-grounded, proceed to precision check.
407
+ "refine_response": ___________, # If not, refine the response.
408
+ "max_iterations_reached": ___________ # If max loops reached, exit.
409
+ }
410
+ )
411
+
412
+ workflow.add_edge(__________, ___________) # Refined responses are reprocessed.
413
+
414
+ # Conditional edges based on precision check
415
+ workflow.add_conditional_edges(
416
+ "check_precision",
417
+ ___________, # Use the conditional function
418
+ {
419
+ "pass": ___________, # If precise, complete the workflow.
420
+ "refine_query": ___________, # If imprecise, refine the query.
421
+ "max_iterations_reached": ___________ # If max loops reached, exit.
422
+ }
423
+ )
424
+
425
+ workflow.add_edge(__________, ___________) # Refined queries go through expansion again.
426
+
427
+ workflow.add_edge("max_iterations_reached", END)
428
+
429
+ return workflow
430
+
431
+
432
+
433
+
434
+ #=========================== Defining the agentic rag tool ====================#
435
+ WORKFLOW_APP = create_workflow().compile()
436
+ @tool
437
+ def agentic_rag(query: str):
438
+ """
439
+ Runs the RAG-based agent with conversation history for context-aware responses.
440
+
441
+ Args:
442
+ query (str): The current user query.
443
+
444
+ Returns:
445
+ Dict[str, Any]: The updated state with the generated response and conversation history.
446
+ """
447
+ # Initialize state with necessary parameters
448
+ inputs = {
449
+ "query": query, # Current user query
450
+ "expanded_query": "_____", # Complete the code to define the expanded version of the query
451
+ "context": [], # Retrieved documents (initially empty)
452
+ "response": "_____", # Complete the code to define the AI-generated response
453
+ "precision_score": _____, # Complete the code to define the precision score of the response
454
+ "groundedness_score": _____, # Complete the code to define the groundedness score of the response
455
+ "groundedness_loop_count": _____, # Complete the code to define the counter for groundedness loops
456
+ "precision_loop_count": _____, # Complete the code to define the counter for precision loops
457
+ "feedback": "_____", # Complete the code to define the feedback
458
+ "query_feedback": "_____", # Complete the code to define the query feedback
459
+ "loop_max_iter": _____ # Complete the code to define the maximum number of iterations for loops
460
+ }
461
+
462
+ output = WORKFLOW_APP.invoke(inputs)
463
+
464
+ return output
465
+
466
+
467
+ #================================ Guardrails ===========================#
468
+ llama_guard_client = Groq(api_key=llama_api_key)
469
+ # Function to filter user input with Llama Guard
470
+ def filter_input_with_llama_guard(user_input, model="meta-llama/llama-guard-4-12b"):
471
+ """
472
+ Filters user input using Llama Guard to ensure it is safe.
473
+
474
+ Parameters:
475
+ - user_input: The input provided by the user.
476
+ - model: The Llama Guard model to be used for filtering (default is "llama-guard-3-8b").
477
+
478
+ Returns:
479
+ - The filtered and safe input.
480
+ """
481
+ try:
482
+ # Create a request to Llama Guard to filter the user input
483
+ response = llama_guard_client.chat.completions.create(
484
+ messages=[{"role": "user", "content": user_input}],
485
+ model=model,
486
+ )
487
+ # Return the filtered input
488
+ return response.choices[0].message.content.strip()
489
+ except Exception as e:
490
+ print(f"Error with Llama Guard: {e}")
491
+ return None
492
+
493
+
494
+ #============================= Adding Memory to the agent using mem0 ===============================#
495
+
496
+ class NutritionBot:
497
+ def __init__(self):
498
+ """
499
+ Initialize the NutritionBot class, setting up memory, the LLM client, tools, and the agent executor.
500
+ """
501
+
502
+ # Initialize a memory client to store and retrieve customer interactions
503
+ self.memory = MemoryClient(api_key= MEM0_api_key ) # Complete the code to define the memory client API key
504
+
505
+ # Initialize the OpenAI client using the provided credentials
506
+ self.client = ChatOpenAI(
507
+ model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
508
+ api_key=config.get("API_KEY"), # API key for authentication
509
+ base_url = config.get("OPENAI_API_BASE"),
510
+ temperature=0 # Controls randomness in responses; 0 ensures deterministic results
511
+ )
512
+
513
+ # Define tools available to the chatbot, such as web search
514
+ tools = [agentic_rag]
515
+
516
+ # Define the system prompt to set the behavior of the chatbot
517
+ 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.
518
+ Guidelines for Interaction:
519
+ Maintain a polite, professional, and reassuring tone.
520
+ Show genuine empathy for customer concerns and health challenges.
521
+ Reference past interactions to provide personalized and consistent advice.
522
+ Engage with the customer by asking about their food preferences, dietary restrictions, and lifestyle before offering recommendations.
523
+ Ensure consistent and accurate information across conversations.
524
+ If any detail is unclear or missing, proactively ask for clarification.
525
+ Always use the agentic_rag tool to retrieve up-to-date and evidence-based nutrition insights.
526
+ Keep track of ongoing issues and follow-ups to ensure continuity in support.
527
+ Your primary goal is to help customers make informed nutrition decisions that align with their health conditions and personal preferences.
528
+
529
+ """
530
+
531
+ # Build the prompt template for the agent
532
+ prompt = ChatPromptTemplate.from_messages([
533
+ ("system", system_prompt), # System instructions
534
+ ("human", "{input}"), # Placeholder for human input
535
+ ("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
536
+ ])
537
+
538
+ # Create an agent capable of interacting with tools and executing tasks
539
+ agent = create_tool_calling_agent(self.client, tools, prompt)
540
+
541
+ # Wrap the agent in an executor to manage tool interactions and execution flow
542
+ self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
543
+
544
+
545
+ def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
546
+ """
547
+ Store customer interaction in memory for future reference.
548
+
549
+ Args:
550
+ user_id (str): Unique identifier for the customer.
551
+ message (str): Customer's query or message.
552
+ response (str): Chatbot's response.
553
+ metadata (Dict, optional): Additional metadata for the interaction.
554
+ """
555
+ if metadata is None:
556
+ metadata = {}
557
+
558
+ # Add a timestamp to the metadata for tracking purposes
559
+ metadata["timestamp"] = datetime.now().isoformat()
560
+
561
+ # Format the conversation for storage
562
+ conversation = [
563
+ {"role": "user", "content": message},
564
+ {"role": "assistant", "content": response}
565
+ ]
566
+
567
+ # Store the interaction in the memory client
568
+ self.memory.add(
569
+ conversation,
570
+ user_id=user_id,
571
+ output_format="v1.1",
572
+ metadata=metadata
573
+ )
574
+
575
+
576
+ def get_relevant_history(self, user_id: str, query: str) -> List[Dict]:
577
+ """
578
+ Retrieve past interactions relevant to the current query.
579
+
580
+ Args:
581
+ user_id (str): Unique identifier for the customer.
582
+ query (str): The customer's current query.
583
+
584
+ Returns:
585
+ List[Dict]: A list of relevant past interactions.
586
+ """
587
+ return self.memory.search(
588
+ query=query, # Search for interactions related to the query
589
+ user_id=user_id, # Restrict search to the specific user
590
+ limit=_____ # Complete the code to define the limit for retrieved interactions
591
+ )
592
+
593
+
594
+ def handle_customer_query(self, user_id: str, query: str) -> str:
595
+ """
596
+ Process a customer's query and provide a response, taking into account past interactions.
597
+
598
+ Args:
599
+ user_id (str): Unique identifier for the customer.
600
+ query (str): Customer's query.
601
+
602
+ Returns:
603
+ str: Chatbot's response.
604
+ """
605
+
606
+ # Retrieve relevant past interactions for context
607
+ relevant_history = self.get_relevant_history(user_id, query)
608
+
609
+ # Build a context string from the relevant history
610
+ context = "Previous relevant interactions:\n"
611
+ for memory in relevant_history:
612
+ context += f"Customer: {memory['memory']}\n" # Customer's past messages
613
+ context += f"Support: {memory['memory']}\n" # Chatbot's past responses
614
+ context += "---\n"
615
+
616
+ # Print context for debugging purposes
617
+ print("Context: ", context)
618
+
619
+ # Prepare a prompt combining past context and the current query
620
+ prompt = f"""
621
+ Context:
622
+ {context}
623
+
624
+ Current customer query: {query}
625
+
626
+ Provide a helpful response that takes into account any relevant past interactions.
627
+ """
628
+
629
+ # Generate a response using the agent
630
+ response = self.agent_executor.invoke({"input": prompt})
631
+
632
+ # Store the current interaction for future reference
633
+ self.store_customer_interaction(
634
+ user_id=user_id,
635
+ message=query,
636
+ response=response["output"],
637
+ metadata={"type": "support_query"}
638
+ )
639
+
640
+ # Return the chatbot's response
641
+ return response['output']
642
+
643
+
644
+ #=====================User Interface using streamlit ===========================#
645
+ def nutrition_disorder_streamlit():
646
+ """
647
+ A Streamlit-based UI for the Nutrition Disorder Specialist Agent.
648
+ """
649
+ st.title("Nutrition Disorder Specialist")
650
+ st.write("Ask me anything about nutrition disorders, symptoms, causes, treatments, and more.")
651
+ st.write("Type 'exit' to end the conversation.")
652
+
653
+ # Initialize session state for chat history and user_id if they don't exist
654
+ if 'chat_history' not in st.session_state:
655
+ st.session_state.chat_history = []
656
+ if 'user_id' not in st.session_state:
657
+ st.session_state.user_id = None
658
+
659
+ # Login form: Only if user is not logged in
660
+ if st.session_state.user_id is None:
661
+ with st.form("login_form", clear_on_submit=True):
662
+ user_id = st.text_input("Please enter your name to begin:")
663
+ submit_button = st.form_submit_button("Login")
664
+ if submit_button and user_id:
665
+ st.session_state.user_id = user_id
666
+ st.session_state.chat_history.append({
667
+ "role": "assistant",
668
+ "content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
669
+ })
670
+ st.session_state.login_submitted = True # Set flag to trigger rerun
671
+ if st.session_state.get("login_submitted", False):
672
+ st.session_state.pop("login_submitted")
673
+ st.rerun()
674
+ else:
675
+ # Display chat history
676
+ for message in st.session_state.chat_history:
677
+ with st.chat_message(message["role"]):
678
+ st.write(message["content"])
679
+
680
+ # Chat input with custom placeholder text
681
+ user_query = st.chat_input(__________) # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...")
682
+ if user_query:
683
+ if user_query.lower() == "exit":
684
+ st.session_state.chat_history.append({"role": "user", "content": "exit"})
685
+ with st.chat_message("user"):
686
+ st.write("exit")
687
+ goodbye_msg = "Goodbye! Feel free to return if you have more questions about nutrition disorders."
688
+ st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg})
689
+ with st.chat_message("assistant"):
690
+ st.write(goodbye_msg)
691
+ st.session_state.user_id = None
692
+ st.rerun()
693
+ return
694
+
695
+ st.session_state.chat_history.append({"role": "user", "content": user_query})
696
+ with st.chat_message("user"):
697
+ st.write(user_query)
698
+
699
+ # Filter input using Llama Guard
700
+ filtered_result = __________(user_query) # Blank #2: Fill in with the function name for filtering input (e.g., filter_input_with_llama_guard)
701
+ filtered_result = filtered_result.replace("\n", " ") # Normalize the result
702
+
703
+ # Check if input is safe based on allowed statuses
704
+ if filtered_result in [__________, __________, __________]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
705
+ try:
706
+ if 'chatbot' not in st.session_state:
707
+ st.session_state.chatbot = __________() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
708
+ response = st.session_state.chatbot.__________(st.session_state.user_id, user_query)
709
+ # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
710
+ st.write(response)
711
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
712
+ except Exception as e:
713
+ error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
714
+ st.write(error_msg)
715
+ st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
716
+ else:
717
+ inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
718
+ st.write(inappropriate_msg)
719
+ st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
720
+
721
+ if __name__ == "__main__":
722
+ nutrition_disorder_streamlit()