AdaptAI-appdev1 commited on
Commit
259566d
·
verified ·
1 Parent(s): 611acdf

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +80 -55
app.py CHANGED
@@ -55,6 +55,10 @@ from datetime import datetime
55
 
56
  #====================================SETUP=====================================#
57
  # Fetch secrets from Hugging Face Spaces
 
 
 
 
58
  api_key = os.getenv("API_KEY")
59
  endpoint = os.getenv("OPENAI_API_BASE")
60
  llama_api_key = os.getenv("GROQ_API_KEY")
@@ -87,8 +91,8 @@ llm = ChatOpenAI(
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
 
@@ -117,7 +121,9 @@ def expand_query(state):
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([
@@ -158,7 +164,7 @@ def retrieve_context(state):
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
@@ -173,7 +179,7 @@ def retrieve_context(state):
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
@@ -191,7 +197,15 @@ def craft_response(state: Dict) -> Dict:
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),
@@ -202,7 +216,7 @@ def craft_response(state: Dict) -> Dict:
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)
@@ -222,7 +236,10 @@ def score_groundedness(state: Dict) -> Dict:
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),
@@ -232,7 +249,7 @@ def score_groundedness(state: Dict) -> Dict:
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
@@ -254,17 +271,20 @@ def check_precision(state: Dict) -> Dict:
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)
@@ -286,7 +306,10 @@ def refine_response(state: Dict) -> Dict:
286
  """
287
  print("---------refine_response---------")
288
 
289
- system_message = '''________________________'''
 
 
 
290
 
291
  refine_response_prompt = ChatPromptTemplate.from_messages([
292
  ("system", system_message),
@@ -316,7 +339,9 @@ def refine_query(state: Dict) -> Dict:
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),
@@ -339,7 +364,7 @@ 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:
@@ -353,15 +378,15 @@ def should_continue_groundedness(state):
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
 
@@ -380,17 +405,17 @@ 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")
@@ -401,28 +426,28 @@ def create_workflow() -> StateGraph:
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
 
@@ -447,16 +472,16 @@ def agentic_rag(query: str):
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)
@@ -505,8 +530,8 @@ class NutritionBot:
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
 
@@ -587,7 +612,7 @@ class NutritionBot:
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
 
@@ -678,7 +703,7 @@ def nutrition_disorder_streamlit():
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"})
@@ -697,15 +722,15 @@ def nutrition_disorder_streamlit():
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})
 
55
 
56
  #====================================SETUP=====================================#
57
  # Fetch secrets from Hugging Face Spaces
58
+ # api_key = os.environ("API_KEY")
59
+ # endpoint = os.environ("OPENAI_API_BASE")
60
+ # llama_api_key = os.environ("GROQ_API_KEY")"
61
+ # MEM0_api_key = os.environ("mem0")
62
  api_key = os.getenv("API_KEY")
63
  endpoint = os.getenv("OPENAI_API_BASE")
64
  llama_api_key = os.getenv("GROQ_API_KEY")
 
91
  # This initializes the Chat OpenAI model with the provided endpoint, API key, deployment name, and a temperature setting of 0 (to control response variability).
92
 
93
  # set the LLM and embedding model in the LlamaIndex settings.
94
+ Settings.llm = llm # Complete the code to define the LLM model
95
+ Settings.embedding = embedding_model # Complete the code to define the embedding model
96
 
97
  #================================Creating Langgraph agent======================#
98
 
 
121
  Dict: The updated state with the expanded query.
122
  """
123
  print("---------Expanding Query---------")
124
+ system_message = '''You are an AI assistant tasked with expanding user queries related to nutrition disorders to improve retrieval accuracy.
125
+ Analyze the user's query and generate several alternative phrasings or add relevant keywords that could help retrieve more comprehensive information from a nutritional database, without altering the original intent of the query.
126
+ If feedback on a previous query expansion is provided, use that feedback to refine the expansion.'''
127
 
128
 
129
  expand_prompt = ChatPromptTemplate.from_messages([
 
164
  Dict: The updated state with the retrieved context.
165
  """
166
  print("---------retrieve_context---------")
167
+ query = state['expanded_query'] # Complete the code to define the key for the expanded query
168
  #print("Query used for retrieval:", query) # Debugging: Print the query
169
 
170
  # Retrieve documents from the vector store
 
179
  }
180
  for doc in docs
181
  ]
182
+ state['context'] = context # Complete the code to define the key for storing the context
183
  print("Extracted context with metadata:", context) # Debugging: Print the extracted context
184
  #print(f"Groundedness loop count: {state['groundedness_loop_count']}")
185
  return state
 
197
  Dict: The updated state with the generated response.
198
  """
199
  print("---------craft_response---------")
200
+ system_message = '''You are a helpful AI assistant specializing in nutrition disorders. Your task is to generate a comprehensive and accurate response to the user's query based *strictly* on the provided context.
201
+
202
+ If the context does not contain enough information to fully answer the query, state that you cannot provide a complete answer based on the available information.
203
+
204
+ Pay close attention to the provided feedback and use it to refine your response from the previous attempt. Ensure your response is well-structured, easy to understand, and directly addresses the user's question using only the facts presented in the context.
205
+
206
+ Context: {context}
207
+ Query: {query}
208
+ Feedback: {feedback}'''
209
 
210
  response_prompt = ChatPromptTemplate.from_messages([
211
  ("system", system_message),
 
216
  response = chain.invoke({
217
  "query": state['query'],
218
  "context": "\n".join([doc["content"] for doc in state['context']]),
219
+ "feedback": state['feedback'] # add feedback to the prompt
220
  })
221
  state['response'] = response
222
  print("intermediate response: ", response)
 
236
  Dict: The updated state with the groundedness score.
237
  """
238
  print("---------check_groundedness---------")
239
+ system_message = '''You are an AI assistant that evaluates the groundedness of a generated response based on a given context.
240
+ Your task is to assess how well the response is supported by the factual information in the context.
241
+ Provide a score between 0 and 1, where 1 means the response is fully grounded in the context and 0 means it is not grounded at all.
242
+ Output only the score as a float.'''
243
 
244
  groundedness_prompt = ChatPromptTemplate.from_messages([
245
  ("system", system_message),
 
249
  chain = groundedness_prompt | llm | StrOutputParser()
250
  groundedness_score = float(chain.invoke({
251
  "context": "\n".join([doc["content"] for doc in state['context']]),
252
+ "response": state['response'] # Complete the code to define the response
253
  }))
254
  print("groundedness_score: ", groundedness_score)
255
  state['groundedness_loop_count'] += 1
 
271
  Dict: The updated state with the precision score.
272
  """
273
  print("---------check_precision---------")
274
+ system_message = '''You are an AI assistant that evaluates the precision of a generated response in addressing the user's query.
275
+ Your task is to assess how well the response directly and accurately answers the user's question.
276
+ Provide a score between 0 and 1, where 1 means the response is highly precise and directly answers the query, and 0 means it is not precise at all.
277
+ Output only the score as a float.'''
278
 
279
  precision_prompt = ChatPromptTemplate.from_messages([
280
  ("system", system_message),
281
  ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
282
  ])
283
 
284
+ chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processing
285
  precision_score = float(chain.invoke({
286
  "query": state['query'],
287
+ "response":state['response'] # Complete the code to access the response from the state
288
  }))
289
  state['precision_score'] = precision_score
290
  print("precision_score:", precision_score)
 
306
  """
307
  print("---------refine_response---------")
308
 
309
+ system_message = '''You are an AI assistant that provides constructive feedback on a generated response to a user's query, based on the original query.
310
+ Your task is to identify any gaps, ambiguities, or missing details in the response that prevent it from fully and accurately addressing the query.
311
+ Suggest specific improvements to enhance the accuracy and completeness of the response.
312
+ Do not rewrite the response, just provide feedback on how to improve it.'''
313
 
314
  refine_response_prompt = ChatPromptTemplate.from_messages([
315
  ("system", system_message),
 
339
  Dict: The updated state with query refinement suggestions.
340
  """
341
  print("---------refine_query---------")
342
+ system_message = '''You are an AI assistant that suggests improvements for a user's query to enhance search precision in a nutritional database.
343
+ Your task is to analyze the original query and the expanded query and identify areas where the expanded query could be made more specific, include more relevant keywords, or narrow down the scope to retrieve more accurate results from a database of nutritional disorders.
344
+ Suggest concrete improvements for a better search based on the context of nutritional information.'''
345
 
346
  refine_query_prompt = ChatPromptTemplate.from_messages([
347
  ("system", system_message),
 
364
  """Decides if groundedness is sufficient or needs improvement."""
365
  print("---------should_continue_groundedness---------")
366
  print("groundedness loop count: ", state['groundedness_loop_count'])
367
+ if state['groundedness_score'] >= 0.8: # Complete the code to define the threshold for groundedness
368
  print("Moving to precision")
369
  return "check_precision"
370
  else:
 
378
  def should_continue_precision(state: Dict) -> str:
379
  """Decides if precision is sufficient or needs improvement."""
380
  print("---------should_continue_precision---------")
381
+ print("precision loop count: ", state['precision_loop_count'])
382
+ if state['precision_score'] >= 0.8: # Threshold for precision
383
  return "pass" # Complete the workflow
384
  else:
385
+ if state["precision_loop_count"] > state['loop_max_iter']: # Maximum allowed loops
386
  return "max_iterations_reached"
387
  else:
388
  print(f"---------Precision Score Threshold Not met. Refining Query-----------") # Debugging
389
+ return "refine_query" # Refine the query
390
 
391
 
392
 
 
405
 
406
  def create_workflow() -> StateGraph:
407
  """Creates the updated workflow for the AI nutrition agent."""
408
+ workflow = StateGraph(AgentState ) # Complete the code to define the initial state of the agent
409
 
410
  # Add processing nodes
411
+ workflow.add_node("expand_query", expand_query ) # Step 1: Expand user query. Complete with the function to expand the query
412
+ workflow.add_node("retrieve_context", retrieve_context ) # Step 2: Retrieve relevant documents. Complete with the function to retrieve context
413
+ workflow.add_node("craft_response", craft_response ) # Step 3: Generate a response based on retrieved data. Complete with the function to craft a response
414
+ workflow.add_node("score_groundedness", score_groundness ) # Step 4: Evaluate response grounding. Complete with the function to score groundedness
415
+ workflow.add_node("refine_response", refine_response ) # Step 5: Improve response if it's weakly grounded. Complete with the function to refine the response
416
+ workflow.add_node("check_precision", check_precision ) # Step 6: Evaluate response precision. Complete with the function to check precision
417
+ workflow.add_node("refine_query", refine_query ) # Step 7: Improve query if response lacks precision. Complete with the function to refine the query
418
+ workflow.add_node("max_iterations_reached", max_iterations_reached ) # Step 8: Handle max iterations. Complete with the function to handle max iterations
419
 
420
  # Main flow edges
421
  workflow.add_edge(START, "expand_query")
 
426
  # Conditional edges based on groundedness check
427
  workflow.add_conditional_edges(
428
  "score_groundedness",
429
+ should_continue_groundedness, # Use the conditional function
430
  {
431
+ "check_precision": "check_precision", # If well-grounded, proceed to precision check.
432
+ "refine_response": "refine_response", # If not, refine the response.
433
+ "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit.
434
  }
435
  )
436
 
437
+ workflow.add_edge("refine_response", "craft_response") # Refined responses are reprocessed.
438
 
439
  # Conditional edges based on precision check
440
  workflow.add_conditional_edges(
441
  "check_precision",
442
+ should_continue_precision, # Use the conditional function
443
  {
444
+ "pass": END, # If precise, complete the workflow.
445
+ "refine_query": "refine_query", # If imprecise, refine the query.
446
+ "max_iterations_reached": "max_iterations_reached" # If max loops reached, exit.
447
  }
448
  )
449
 
450
+ workflow.add_edge("refine_query", "expand_query") # Refined queries go through expansion again.
451
 
452
  workflow.add_edge("max_iterations_reached", END)
453
 
 
472
  # Initialize state with necessary parameters
473
  inputs = {
474
  "query": query, # Current user query
475
+ "expanded_query": "", # Complete the code to define the expanded version of the query
476
  "context": [], # Retrieved documents (initially empty)
477
+ "response": "", # Complete the code to define the AI-generated response
478
+ "precision_score": 0.0, # Complete the code to define the precision score of the response
479
+ "groundedness_score": 0.0, # Complete the code to define the groundedness score of the response
480
+ "groundedness_loop_count": 0, # Complete the code to define the counter for groundedness loops
481
+ "precision_loop_count": 0, # Complete the code to define the counter for precision loops
482
+ "feedback": "", # Complete the code to define the feedback
483
+ "query_feedback": "", # Complete the code to define the query feedback
484
+ "loop_max_iter": 3 # Complete the code to define the maximum number of iterations for loops
485
  }
486
 
487
  output = WORKFLOW_APP.invoke(inputs)
 
530
  # Initialize the OpenAI client using the provided credentials
531
  self.client = ChatOpenAI(
532
  model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
533
+ api_key=api_key, # API key for authentication
534
+ base_url = endpoint,
535
  temperature=0 # Controls randomness in responses; 0 ensures deterministic results
536
  )
537
 
 
612
  return self.memory.search(
613
  query=query, # Search for interactions related to the query
614
  user_id=user_id, # Restrict search to the specific user
615
+ limit=5 # Complete the code to define the limit for retrieved interactions
616
  )
617
 
618
 
 
703
  st.write(message["content"])
704
 
705
  # Chat input with custom placeholder text
706
+ user_query = st.chat_input("Type your question here (or 'exit' to end)...") # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...")
707
  if user_query:
708
  if user_query.lower() == "exit":
709
  st.session_state.chat_history.append({"role": "user", "content": "exit"})
 
722
  st.write(user_query)
723
 
724
  # Filter input using Llama Guard
725
+ 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)
726
  filtered_result = filtered_result.replace("\n", " ") # Normalize the result
727
 
728
  # Check if input is safe based on allowed statuses
729
+ 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")
730
  try:
731
  if 'chatbot' not in st.session_state:
732
+ st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
733
+ response = st.session_state.chatbot.handle_customer_query(st.session_state.user_id, user_query)
734
  # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
735
  st.write(response)
736
  st.session_state.chat_history.append({"role": "assistant", "content": response})