Phoenix21 commited on
Commit
3e00622
·
verified ·
1 Parent(s): 8972636

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -3,20 +3,22 @@ from my_memory_logic import run_with_session_memory
3
 
4
  def chat_interface_fn(message, history, session_id):
5
  """
6
- Simple chat function for Gradio's ChatInterface that only shows the most recent question and answer pair.
7
- The session_id is used to maintain session-based memory.
 
8
  """
9
  # 1) Get answer from the session-based memory pipeline
10
  answer = run_with_session_memory(message, session_id)
11
 
12
- # 2) Append only the new question and answer pair to history
13
- history.append((message, answer))
 
 
14
 
15
- # 3) Convert the most recent question-answer pair to message dictionaries for display
16
  message_dicts = []
17
- for user_msg, ai_msg in history[-1:]: # Only use the latest Q&A
18
- message_dicts.append({"role": "user", "content": user_msg})
19
- message_dicts.append({"role": "assistant", "content": ai_msg})
20
 
21
  # Return the message dicts and updated history
22
  return message_dicts, history
 
3
 
4
  def chat_interface_fn(message, history, session_id):
5
  """
6
+ Multi-turn chat function for Gradio's ChatInterface.
7
+ 'session_id' is used to store conversation across turns.
8
+ Deduplicates consecutive repeated Q&A pairs to avoid repetition.
9
  """
10
  # 1) Get answer from the session-based memory pipeline
11
  answer = run_with_session_memory(message, session_id)
12
 
13
+ # 2) Deduplicate consecutive identical exchanges
14
+ if not history or history[-1]["content"] != answer:
15
+ history.append({"role": "user", "content": message})
16
+ history.append({"role": "assistant", "content": answer})
17
 
18
+ # 3) Convert history to message dictionaries for display
19
  message_dicts = []
20
+ for msg in history:
21
+ message_dicts.append(msg) # Directly append the dictionary
 
22
 
23
  # Return the message dicts and updated history
24
  return message_dicts, history