using showdown js with openAi streaming response

题意:"使用 Showdown.js 处理 OpenAI 流式响应"

问题背景:

I tried using showdownjs to translate streamed markdown from OpenAi to HTML

"我尝试使用 Showdown.js 将来自 OpenAI 的流式 Markdown 转换为 HTML"

I changed the code given at https://github.com/orhanerday/ChatGPT and just added the showdown part

"我修改了在 https://github.com/orhanerday/ChatGPT 上提供的代码,并仅添加了 Showdown 部分。"

The system prompt to OpenAi includes returning responses using markdown, which it does

After the showdownjs parsed , the results are weird. Each chunk is in a separate line and the markdown isn't parsed!

"系统提示给 OpenAI 包括使用 Markdown 返回响应,OpenAI 确实这样做了。

但在 Showdown.js 解析后,结果很奇怪。每个块都在单独的行中,Markdown 并没有被解析!"

javascript 复制代码
  let converter = new showdown.Converter({smoothLivePreview: true});
                        let parsedHtml = converter.makeHtml(txt);

                        div.innerHTML += parsedHtml;

The data does come back from the backend as a stream

"数据确实以流的形式从后端返回。"

Am totally flummoxed. What am i doing wrong here? I have the references all included and the data does come back from the php file in the backend.

"我完全困惑了。我在这里做错了什么?我已经包含了所有参考资料,数据确实从后端的 PHP 文件中返回了。"

enter image description here

Thanks in advance

EDITED

I just realized that showdown is adding a "html p" tag around every word in every stream:-( And, the text with markdown (sometimes incomplete in the chunk), do not get processed and converted to html :-(

"我刚刚意识到 Showdown 在每个流中的每个单词周围添加了一个 'html p' 标签 :-( 而且,带有 Markdown 的文本(有时在块中不完整)没有被处理和转换为 HTML :-( "

问题解决:

I finally figured out a very simple solution. Duh.

"我终于找到了一个非常简单的解决方案。真是的。"

I decided to use the markdown-it library from https://github.com/markdown-it/markdown-it

And in the above code, rather than applying markdown when the text is streamed, i do it at the end on getting "done"

"我决定使用来自 https://github.com/markdown-it/markdown-it 的 markdown-it 库。

在上述代码中,我不是在文本流式传输时应用 Markdown,而是在获取到 'done' 后在最后进行转换。"

Am reproducing just the relevant part of the code here for brevity, with my solution. works like charm. Should have thought of it before! Ideally, i would like it to happen when the data streams, but looks like that is either not possible or too much hard work !!!

"为了简洁起见,我在这里重现了代码的相关部分,并展示了我的解决方案。效果很好,早该想到这个方法!理想情况下,我希望在数据流式传输时进行处理,但看来要么不可能,要么工作量太大!!!"

javascript 复制代码
     if (e.data == "[DONE]") {
                    msgerSendBtn.disabled = false;
                    document.getElementById("userSendButtonAV").value="Send";
                    var elemB = document.getElementById("userSendButtonAV");
                    elemB.value = "Send";
                
                    const md = window.markdownit();
                    const renderedText = md.render(div.innerText);
                    div.innerHTML = renderedText;


                    document.getElementById('userMessageAV').placeholder='Enter your message now...';
                    document.getElementById('userMessageAV').disabled = false;
                

                    eventSource.close();
                } else {
                    
                    //original code  let txt = JSON.parse(e.data).choices[0].delta.content
                    if (isJsonString(e.data)) {
                        let txt = JSON.parse(e.data).choices[0].delta.content;

                    if (txt !== undefined) {
                        div.innerHTML += txt.replace(/(?:\r\n|\r|\n)/g, '<br>');
                    }
                } 
                        
            }
相关推荐
Ajiang28247353045 分钟前
贪吃蛇项目实现(C语言)——附源码
c语言·开发语言
guicai_guojia11 分钟前
面试题篇: 跨域问题如何处理(Java和Nginx处理方式)
java·开发语言·nginx
鼠鼠龙年发大财38 分钟前
fly专享
开发语言·php
hunandede42 分钟前
直播相关02-录制麦克风声音,QT 信号与槽,自定义信号和槽
开发语言·qt
lzb_kkk1 小时前
【Redis】redis5种数据类型(哈希)
开发语言·redis·算法·缓存·哈希算法
Good_tea_h2 小时前
如何实现Java中的多态性
java·开发语言·python
tiandyoin3 小时前
Notepad++ 修改 About
前端·notepad++·html5
格林威3 小时前
Baumer工业相机堡盟工业相机如何通过BGAPISDK使用短曝光功能(曝光可设置1微秒)(C语言)
c语言·开发语言·人工智能·数码相机·计算机视觉
老身聊发少年狂3 小时前
R语言xlsx,txt文件处理:以《书摘》00年-10年资源合集整理为例
开发语言·r语言
为为-180-3121-14553 小时前
R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习技术应用
开发语言·r语言