How do I retrieve gpt-4 and gpt3.5-tubo API responses in html?

I am developing a sort of chatbot using php within the Drupal CMS framework. It is designed to give responses using site documents as context.

I am making my API calls to gpt-4 and gpt3.5-tubo.

I am getting responses fine, but they come back as plain text. This is how they are coming back directly from the server. So, for example, when the LLM responds with a list, it uses linefeed characters \n which of course are ignored in html.

So far, the best I can come up with in code(javascript and php) is:

var output = {{ newOutput|json_encode|raw }};
output = output.replace(/\n/g, '<br>');  // Add this line
output = output.split('<br>');

Any ideas on better solutions for displaying model API responses (chat completions)?

How about using CSS in the element that displays the response? For example:

.text {
white-space: pre-wrap;
}
1 Like

https://www.php.net/manual/en/function.nl2br.php

Thank you for the response. I was going to follow up with some questions, but decided to ask GPT-4. After going down a couple of rabbit holes and a few wild goose chases, finally figured it out. But, your answer was the key. Thank you!