Skip to content

Commit 9691170

Browse files
committed
WIP
1 parent 3bde59f commit 9691170

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

llms_wrapper/llms_wrapper_webchat.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def get_args():
2121
parser.add_argument("--promptfile", type=str, help="Prompt file containing prompts, overrides config (or use prompt section in config)", required=False)
2222
parser.add_argument("--max_messages", type=int, default=20,
2323
help="Max number of messages to keep in the chat history (default: 20)", required=False)
24+
parser.add_argument("--clear_page", action="store_true",
25+
help="Clear the page too when history is cleared (default: keep, add info message)", required=False)
2426
parser.add_argument('--verbose', action="store_true",
2527
help='Be more verbose', required=False)
2628
parser.add_argument("--debug", action="store_true", help="Debug mode, overrides loglevel", required=False)
@@ -92,6 +94,10 @@ def get_args():
9294
.chat-bubble-secondary {
9395
background-color: #0A6C0A; /* or #228B22 */
9496
color: white; /* Text color for readability */
97+
}
98+
.chat-bubble-other {
99+
background-color: blue; /* or #228B22 */
100+
color: black; /* Text color for readability */
95101
}
96102
.btn.btn-primary.btn-disabled.thinking-red-text {
97103
color: #EF4444 !important; /* Tailwind's red-500 color, with !important to override disabled styles */
@@ -158,10 +164,14 @@ def ChatMessage(msg, kind):
158164
# other classes: chat-bubble-accent/neutral/info/success/warning/error
159165
if kind == "error":
160166
bubble_class = "chat-bubble-error"
167+
elif kind == "warning":
168+
bubble_class = "chat-bubble-other"
161169
elif kind == "user":
162170
bubble_class = "chat-bubble-primary"
163-
else:
171+
elif kind == "assistant":
164172
bubble_class = "chat-bubble-secondary"
173+
else:
174+
bubble_class = "chat-bubble-other" # Default to warning if kind is unknown
165175
chat_class = "chat-end" if kind == "user" else 'chat-start'
166176
return Div(cls=f"chat {chat_class}")(
167177
# No content in the Div directly, it will be filled by JS
@@ -314,8 +324,11 @@ def index():
314324
ChatInput(),
315325
Button("Send", cls="btn btn-primary", id="send-button", disabled=True, onclick="sendMessage()"),
316326
Button("Clear", cls="btn btn-secondary", id="clear-button",
317-
hx_post="/clear_chat", hx_target="#chatlist", hx_swap="innerHTML",
318-
hx_include="#msg-input, #select-option", hx_trigger="click"),
327+
hx_post="/clear_chat",
328+
# hx_target="#chatlist",
329+
# hx_swap="innerHTML",
330+
hx_include="#msg-input, #select-option",
331+
hx_trigger="click"),
319332
)
320333
),
321334
Div(cls="flex space-x-2 mt-2", id="info-message")(
@@ -328,9 +341,20 @@ def index():
328341
def clear_chat_history(msg:str = "", selected_option:str = ""): # Parameters to receive from hx_include
329342
chatbot.clear_history()
330343
# Return empty Div for chatlist to clear it, and an empty input for OOB swap
331-
logger.info("Chat history cleared")
332-
return (Div(id="chatlist", cls="chat-box"), # This clears the chat list, keeping its class
333-
ChatInput(), InfoMessageDiv("Chat history cleared!", kind="info")) # Updates the info message
344+
logger.info("LLM chat history cleared")
345+
if config["clear_page"]:
346+
logger.info("Clearhing web page")
347+
# If clear_page is set, return an empty Div to clear the chat list
348+
return (Div(id="chatlist", cls="chat-box"), # This clears the chat list, keeping its class
349+
ChatInput(), InfoMessageDiv("Chat history cleared!", kind="info"))
350+
else:
351+
# Do not clear the chat list, just add an info that the LLM chat history was cleared
352+
logger.info("Chat history cleared, but keeping the chat page")
353+
return (
354+
Script("setAppState('ready');"), # Reset the app state to ready
355+
ChatMessage("LLM chat history has been reset!", "other"), # The chatbot's response
356+
ChatInput(), InfoMessageDiv("", "info")) # And clear the input field via an OOB swap
357+
334358

335359
# Handle the form submission
336360
@app.post
@@ -360,6 +384,7 @@ def send(msg:str, selected_option:str=""):
360384
return (
361385
Script("setAppState('ready');"), # Reset the app state to ready
362386
ChatInput(), InfoMessageDiv(info_txt, kind="error"))
387+
msg = msg.strip() # Ensure no leading/trailing whitespace
363388
if msg == "help" or msg == "?" or msg == "?help":
364389
chatmsg = """
365390
Available commands:
@@ -375,8 +400,18 @@ def send(msg:str, selected_option:str=""):
375400
chatbot.clear_history()
376401
# Return empty Div for chatlist to clear it, and an empty input for OOB swap
377402
logger.info("Chat history cleared")
378-
return (Div(id="chatlist", cls="chat-box"), # This clears the chat list, keeping its class
379-
ChatInput(), InfoMessageDiv("Chat history cleared!", kind="info")) # Updates the info message
403+
if config["clear_page"]:
404+
logger.info("Clearing web page")
405+
# If clear_page is set, return an empty Div to clear the chat list
406+
return (Script("setAppState('ready');"),
407+
Div(id="chatlist", cls="chat-box"), # This clears the chat list, keeping its class
408+
ChatInput(), InfoMessageDiv("Chat history cleared!", kind="info"))
409+
else:
410+
# Do not clear the chat list, just add an info that the chat history was cleared
411+
logger.info("Chat history cleared, but keeping the chat page")
412+
return ( Script("setAppState('ready');"),
413+
ChatMessage("LLM chat history cleared", "other"),
414+
ChatInput(), InfoMessageDiv("Chat history cleared!", kind="info")) # Updates the info message
380415
elif msg == "?history":
381416
# Show the history of all user requests
382417
history = chatbot.history

0 commit comments

Comments
 (0)