ceymox commited on
Commit
3e56482
·
verified ·
1 Parent(s): 242fef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -30,7 +30,7 @@ MODEL_ID = "meta-llama/Meta-Llama-3.1-8B-Instruct"
30
  # Google Calendar API Configuration
31
  SCOPES = ['https://www.googleapis.com/auth/calendar']
32
  SERVICE_ACCOUNT_FILE = 'service-account-key.json'
33
- CALENDAR_ID = 'primary' # Update with your calendar ID if not using primary
34
 
35
  # Local appointments database (for backup)
36
  appointments_db = {}
@@ -374,9 +374,16 @@ def process_chat(message, chat_history):
374
  # Create system prompt
375
  system_prompt = """You are a friendly appointment booking assistant. You help users book appointments by collecting their name, preferred date, and time.
376
 
377
- Be polite and helpful. For any appointment request, make sure to collect the person's name, date (in YYYY-MM-DD format), and time (e.g., '10:00 AM').
378
 
379
- If the user doesn't specify all the required information, politely ask for the missing details before booking."""
 
 
 
 
 
 
 
380
 
381
  # Convert Gradio chat history to message format
382
  messages = []
@@ -410,7 +417,27 @@ If the user doesn't specify all the required information, politely ask for the m
410
  # Check if response contains a function call
411
  function_call = extract_function_call(response_text)
412
 
 
413
  if function_call and function_call["name"] == "book_appointment":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
  # Execute the booking function
415
  function_result = book_appointment(function_call["arguments"])
416
  logger.info(f"Function result: {json.dumps(function_result)[:200]}...")
@@ -607,7 +634,7 @@ def create_gradio_interface():
607
  inputs=None,
608
  outputs=[model_status, chat_history, calendar_status]
609
  ).then(
610
- lambda: [("", "Hello! I'm your appointment booking assistant. I can help you schedule an appointment. Just let me know your name, the date, and time you prefer.")],
611
  inputs=None,
612
  outputs=[chatbot]
613
  )
 
30
  # Google Calendar API Configuration
31
  SCOPES = ['https://www.googleapis.com/auth/calendar']
32
  SERVICE_ACCOUNT_FILE = 'service-account-key.json'
33
+ CALENDAR_ID = '26f5856049fab3d6648a2f1dea57c70370de6bc1629a5182be1511b0e75d11d3@group.calendar.google.com' # Update with your calendar ID if not using primary
34
 
35
  # Local appointments database (for backup)
36
  appointments_db = {}
 
374
  # Create system prompt
375
  system_prompt = """You are a friendly appointment booking assistant. You help users book appointments by collecting their name, preferred date, and time.
376
 
377
+ CRITICALLY IMPORTANT: NEVER make up or hallucinate appointment details. If the user has not explicitly provided name, date, or time, you MUST ask for these details before calling any function.
378
 
379
+ Follow these strict rules for appointment booking:
380
+ 1. When a user asks to book an appointment, first check if they've provided name, date, and time.
381
+ 2. If ANY of these details are missing, do NOT call the book_appointment function. Instead, politely ask the user for the missing information.
382
+ 3. ONLY call the book_appointment function when you have collected ALL required information directly from the user.
383
+ 4. NEVER invent, assume, or hallucinate ANY details - even common names like "John Doe" or dates like "tomorrow".
384
+ 5. Use YYYY-MM-DD format for dates (e.g., 2025-05-15) and clear time format with AM/PM (e.g., 10:00 AM).
385
+
386
+ If the user says something like "book an appointment" without providing details, your ONLY correct response is to ask for their name, preferred date, and time - NOT to make up this information or call the function."""
387
 
388
  # Convert Gradio chat history to message format
389
  messages = []
 
417
  # Check if response contains a function call
418
  function_call = extract_function_call(response_text)
419
 
420
+ # Additional validation to prevent hallucination
421
  if function_call and function_call["name"] == "book_appointment":
422
+ # Verify all required fields are present
423
+ args = function_call["arguments"]
424
+ required_fields = ["name", "date", "time"]
425
+ missing_fields = [field for field in required_fields if field not in args or not args[field]]
426
+
427
+ # Check if any date/time looks made up (basic validation)
428
+ looks_made_up = False
429
+
430
+ # Check for generic placeholder names
431
+ if "name" in args and args["name"].lower() in ["john", "john doe", "jane", "jane doe", "test", "user"]:
432
+ logger.warning(f"Detected likely hallucinated name: {args['name']}")
433
+ looks_made_up = True
434
+
435
+ # Don't proceed if missing fields or suspicious data
436
+ if missing_fields or looks_made_up:
437
+ logger.warning(f"Detected hallucination attempt. Missing fields: {missing_fields}, Suspicious data: {looks_made_up}")
438
+ # Skip function calling and let the model ask for the missing information
439
+ new_chat_history = chat_history + [(message, response_text)]
440
+ return new_chat_history, new_chat_history
441
  # Execute the booking function
442
  function_result = book_appointment(function_call["arguments"])
443
  logger.info(f"Function result: {json.dumps(function_result)[:200]}...")
 
634
  inputs=None,
635
  outputs=[model_status, chat_history, calendar_status]
636
  ).then(
637
+ lambda: [("", "Hello! I'm your appointment booking assistant. I can help you schedule an appointment. Please provide your name, preferred date (YYYY-MM-DD format), and time (like 10:00 AM) when you want to book an appointment.")],
638
  inputs=None,
639
  outputs=[chatbot]
640
  )