Trying to wrap my head around ChatInterface.
Use case: a fairly complex dialgue, with steps controled by python code.
I want to capture streaming output from the chatbot, parse it on the fly,
and stream a modified version of that output to a chatinterface instance for display.
Something like
import asyncio
import nest_asyncio
nest_asyncio.apply()
async def stream_text(chat_interface):
for i in range(10): # Simulate sending 10 messages
message = {"text": f"Message {i+1}", "user": "bot", "avatar": "https://via.placeholder.com/24"}
# Directly append the message dictionary to the chat_interface's value list
# WHAT GOES HERE??? chat_interface.value.append(message)
await asyncio.sleep(1)
pn.chat.ChatInterface(height=400, width=400).servable()
await stream_text(chat_interface)
I.e., the user provices a prompt (pushed to chat_interface, and sent to chatbot)
the streaming chatbot response is parsed, modified, and the modified stream is sent to the chat_interface.
How can I achieve this?
Any better way to achieve it than what I am currently pursuing?
message = None
for n in "123456789 abcdefghijklmnopqrstuvxyz":
message = chat_feed.stream(n, message=message, user="Parrot", avatar="🦜")
message = None
for n in "9765":
message = chat_feed.stream(n, message=message, user="Dog", avatar="🐕")
While that works, it does not let me customize the appearance of the ChatMessage.
I tried reading the docs and the code, but am currently stuck on the following:
How do I get the chatFeed display to scroll automatically such that the latest message stays in view?
How do I get the avatar to line up with the top of the message?
When the message is a single line, the avatar extends way beyond the text.
Also, can I resize it somehow? It’s rather overwhelming!
Is there a way to print the dialogue from the chatFeed display?
Given chat_feed = pn.chat.ChatFeed() and message = pn.chat.ChatMessage( user="System"),
how do I add message to chat_feed for display?
Or rather: I want to add a ChatMessage to ChatFeed and stream data to it.
Is this the way to go?
message = pn.chat.ChatMessage( user="System")
chat_feed._replace_placeholder(message)
txt = ""
for n in " I see - will do":
txt += n
message.update(txt)
import panel as pn
chat_feed = pn.chat.ChatFeed()
message = pn.chat.ChatMessage(user="System")
chat_feed.send(message)
for i in range(0, 12):
chat_feed.stream(f"new message {i}", message=message)
auto_scroll_limit (int): Max pixel distance from the latest object in the Column to activate automatic scrolling upon update. Setting to 0 disables auto-scrolling.
I thought that is how it is supposed to work.
It however hangs in chat_feed.stream() on my system, without ever updating the message. (with both the official panel release and with 1.4.0rc)
Re styling: found it finally! set height and min-height on .left
Re scrolling: updating text in a text message: once the text gets too long,
scolling stops. I would prefer seeing the end of the message without having to scroll manually. Can that be achieved?
show how to create a minimal example that uses ChatFeed (or ChatInstance),
or a new class as a GUI only. I.e., all user system interactions are carried out
in separate code, with the GUI used to display the messages only.
show how to remove all the extra ChatMessage features,
and tighten up the spacing.
The avatars are still too large, and so is the spacing
between the avatar and the message
I am currently struggling with adding some buttons and a larger text input area.
I am running into sizing an alignment issues.
Given how much effort it turned out to be trying to put this together,
I think it should definitely be documented.
Any suggestions on how to put this together?
Incidentally, the default_avatars in message_params does not seem useful in this context. Maybe ChatFeed should have an append_message() fn
that one can stream data to? I have not been able to make that work,
Instead writing