OvidijusParsiunas / deep-chat

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

How to display multiline response in chat #86

Closed ittech17 closed 8 months ago

ittech17 commented 9 months ago

Hi,

Thanks for the amazing product.

I have a query (and others might also face this issue):

The response I get from openAI is multi line , but it always shows as a single paragraph in chat window. I workaround it by wrapping it in HTML, and add <br> in responseInterceptor.

Can you please suggest a better way ?

image

OvidijusParsiunas commented 9 months ago

Hi @ittech17.

Deep Chat uses the Remarkable library to automatically format text responses. It appears you are using html responses which Deep Chat does not format as their syntax is fully controlled by the developers.

Fortunately when you install Deep Chat in your app, Remarkable is automatically installed with it, meaning that you can format the text from your app. All you need to do is the following:

  1. In your app, first import the Remarkable library: import {Remarkable} from 'remarkable'; If you are using TypeScript you may also need to install the type package: npm i --save-dev @types/remarkable
  2. Then in the responseInterceptor, you can use something like the following code where Remarkable is first instantiated and then used to format the text:
    (response) => {
    const remarkable = new Remarkable({
    highlight: (str) => {
      return str;
    },
    linkTarget: '_blank',
    });
    const text = remarkable.render(response.text);
    return {
    html: `<div class="feedback">
      <div class="feedback-text">${text}</div>
      <img class="feedback-icon feedback-icon-positive" src="path-to-svg.svg">
      <img class="feedback-icon feedback-icon-negative" src="path-to-svg.svg">
    </div>`,
    };
    }

    Tip: Depending on the framework of your choice - you can instantiate remarkable on startup once and reuse its reference in the responseInterceptor.

Let me know if you need any further help!

OvidijusParsiunas commented 8 months ago

I will be closing this issue as there has been no further communication on this issue. If you have any other questions in the context of this issue, feel free to comment below. For everything else, feel free to create a new issue. Thanks!

fu3fi commented 8 months ago

Try it markdown In my case

md2html.makeHtml(`## Заклинание: Огненная Роса Лягушачьей Крови\n **Уровень:** 5\n **Описание**: Там-там\n`)