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

Refactor chat history formatting in app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -7
app.py CHANGED
@@ -81,22 +81,19 @@ with gr.Blocks() as demo:
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
-
100
  submit.click(
101
  fn=update_chatbot,
102
  inputs=[message, chat_history_state],
 
81
 
82
  def update_chatbot(message, chat_history, system_message=BASE_SYSTEM_MESSAGE):
83
  print("Updating chatbot...")
 
84
  if not chat_history or (chat_history and chat_history[-1]["role"] != "user"):
 
85
  chat_history = user(message, chat_history, system_message if not chat_history else None)
86
  else:
 
87
  chat_history = user(message, chat_history)
88
  chat_history, _ = chat(chat_history, system_message, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
 
89
  formatted_chat_history = []
90
  for entry in chat_history:
91
+ if entry["role"] == "user":
92
+ formatted_chat_history.append([entry["content"], ""])
93
+ elif entry["role"] == "assistant":
94
+ formatted_chat_history.append(["", entry["content"]])
95
  return formatted_chat_history, chat_history, ""
96
 
 
97
  submit.click(
98
  fn=update_chatbot,
99
  inputs=[message, chat_history_state],