当前位置: 首页 > article >正文

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 并没有被解析!"

  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 !!!

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

     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>');
                    }
                } 
                        
            }


http://www.kler.cn/a/286604.html

相关文章:

  • 国内flutter环境部署(记录篇)
  • 知识库建设对提升团队协作与创新能力的影响分析
  • set集合
  • uni-app 程序打包 Android apk、安卓夜神模拟器调试运行
  • C# lock使用详解
  • three.js+WebGL踩坑经验合集(5.2):THREE.Mesh和THREE.Line2在镜像处理上的区别
  • 详解CSS
  • Jenkins Environment Injector Plugin 插件详解
  • C语言 | Leetcode C语言题解之第390题消除游戏
  • Vue项目“npm run serve”总卡住的问题 已解决
  • 深度强化学习算法(七)(附带MATLAB程序)
  • 鸿蒙Harmony开发实战:自定义圆形组件-Canvas
  • NLP从零开始------文本中阶序列处理之语言模型(完整版)
  • 基于机器学习的酒店评论分析与推荐系统设计
  • JavaWeb实战教程:如何打造旅行社网站系统,提升在线服务能力?
  • Ubuntu 搭建 GLFW 环境及其相关测试 demo
  • UE开发中的设计模式(四) —— 组合模式
  • 使用自制COCO数据集进行PaddleDetection模型训练
  • 论文阅读:VideoMamba: State Space Model for Efficient Video Understanding
  • 【学习笔记】卫星通信NTN 3GPP标准化进展分析(四)- 3GPP Release18内容
  • 【Tools】二叉树后序遍历
  • C#判断用户电脑上是否安装有EXCEL的简单方法
  • 深度强化学习算法(五)(附带MATLAB程序)
  • 读懂华为V模型:连接业务与IT,用V模型指导服务化设计
  • 数据结构与算法---排序算法
  • 【艾尔登法环DLC】最新MOD整合包! 内含200+MOD !哥布林地图+BOSS复活+诚实商人+装备幻化+服装+性能优化!