OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.43k stars 218 forks source link

Setting/Adding AI messages directly #173

Closed GrayCordell closed 2 months ago

GrayCordell commented 5 months ago

I'm familiar with the getMessages, submitUserMessage, and initialMessages methods, but I'm trying to figure out how to introduce new messages from the AI into the chat. Currently, it seems the only method is to append messages to initialMessages and then re-render the component somehow. I'd like to enable the chatbot to send messages unprompted. Any suggestion?

OvidijusParsiunas commented 5 months ago

Hey @GrayCordell. Deep Chat has a currently undocumented method known as _addMessage which you can use to dynamically append messages to the chat. The way it works is it accepts a Response object as an argument and adds a message to the chat. E.g. deepChatRef._addMessage({text: 'hello'});

We plan to add this method to the core API in the next release as addMessage.

GrayCordell commented 3 months ago

I forgot to respond earlier, but thank you! The _addMessage method has been very useful. I really appreciate your work on this library!

I wanted to ask if there are any plans to support the direct setting and editing of all messages? This feature seems crucial for maintaining user conversations across page reloads at least. Currently, I’m using the _addMessage workaround and managing message tracking separately.

Additionally, I’ve encountered a few other issues/workarounds with this current setup:

Thanks again.

arreumb commented 2 months ago

Just a question on the new method _addMessage: will it be possible to use it to stream tokens to the AI bubble ?

OvidijusParsiunas commented 2 months ago

Hi @arreumb. The addMessage method is primarily used to add an entire message to the chat asynchronously. Stream-like bubble messages that are gradually populated are usually displayed in response to a user request, and for this I would recommend you to check out the connection handler function for streams, which showcases how to populate a bubble using your tokens.

OvidijusParsiunas commented 2 months ago

Hi, addMessage is now available in Deep Chat version 2.0.0.

OvidijusParsiunas commented 2 months ago

I forgot to respond earlier, but thank you! The _addMessage method has been very useful. I really appreciate your work on this library!

I wanted to ask if there are any plans to support the direct setting and editing of all messages? This feature seems crucial for maintaining user conversations across page reloads at least. Currently, I’m using the _addMessage workaround and managing message tracking separately.

Additionally, I’ve encountered a few other issues/workarounds with this current setup:

  • Handling System Messages: The system isn’t designed to handle a full message array that includes the "system" role messages. These should ideally not be presented by default. While I plan to eventually set up all the context for the LLM on the backend, for now, I simply want to provide system information as things happen in the frontend. My current workaround has been to track the “non-presented” messages separately and insert them right before sending during the requestInterceptor call.
  • Sending Current Messages unprompted: There is no way to send the current messages unless they are user-prompted. I'm currently using submitUserMessage({ text: "" }), which inadvertently adds a blank message that I then need to remove immediately. Is there a function that could be used to send the current messages without this issue?

Thanks again.

Handling system messages is something that is very dependent on the API/Server you are using, hence we are currently not planning to add specific support for it due to the difficulty in standardising it and using requestInterceptor is the correct choice.

In regards to sending system messages unprompted, this is once again not a very standard case and should instead be done by the front-end by your own JavaScript code, once you have the reply that you need you can simply then use the addMessage method to populate AI's response into the chat.