artificialguybr commited on
Commit
bdfee01
·
1 Parent(s): 99f63a1

Add system message support to user function

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -21,9 +21,11 @@ def clear_chat(chat_history_state, chat_message):
21
  chat_message = ''
22
  return chat_history_state, chat_message
23
 
24
- def user(message, history):
25
  print(f"User message: {message}")
26
  history = history or []
 
 
27
  history.append({"role": "user", "content": message})
28
  return history
29
 
@@ -77,16 +79,21 @@ with gr.Blocks() as demo:
77
  top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
78
  chat_history_state = gr.State([])
79
 
80
- def update_chatbot(message, chat_history):
81
  print("Updating chatbot...")
 
82
  if not chat_history or (chat_history and chat_history[-1]["role"] != "user"):
 
 
 
 
83
  chat_history = user(message, chat_history)
84
- chat_history, _ = chat(chat_history, BASE_SYSTEM_MESSAGE, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
85
  # Format the chat history for the gr.Chatbot component
86
  formatted_chat_history = []
87
- for user_msg, assistant_msg in zip([msg["content"] for msg in chat_history if msg["role"] == "user"],
88
- [msg["content"] for msg in chat_history if msg["role"] == "assistant"]):
89
- formatted_chat_history.append([user_msg, assistant_msg])
90
  return formatted_chat_history, chat_history, ""
91
 
92
 
 
21
  chat_message = ''
22
  return chat_history_state, chat_message
23
 
24
+ def user(message, history, system_message=None):
25
  print(f"User message: {message}")
26
  history = history or []
27
+ if system_message: # Check if a system message is provided and should be added
28
+ history.append({"role": "system", "content": system_message})
29
  history.append({"role": "user", "content": message})
30
  return history
31
 
 
79
  top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
80
  chat_history_state = gr.State([])
81
 
82
+ def update_chatbot(message, chat_history, system_message=BASE_SYSTEM_MESSAGE):
83
  print("Updating chatbot...")
84
+ # Check if we're starting a new conversation or continuing an existing one
85
  if not chat_history or (chat_history and chat_history[-1]["role"] != "user"):
86
+ # Include the system message only if starting a new conversation
87
+ chat_history = user(message, chat_history, system_message if not chat_history else None)
88
+ else:
89
+ # If continuing an existing conversation, don't add the system message again
90
  chat_history = user(message, chat_history)
91
+ chat_history, _ = chat(chat_history, system_message, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
92
  # Format the chat history for the gr.Chatbot component
93
  formatted_chat_history = []
94
+ for entry in chat_history:
95
+ if entry["role"] in ["user", "assistant"]:
96
+ formatted_chat_history.append([entry["content"] if entry["role"] == "user" else "", entry["content"] if entry["role"] == "assistant" else ""])
97
  return formatted_chat_history, chat_history, ""
98
 
99