using showdown js with openAi streaming response

news/2024/9/14 20:39:10 标签: javascript, 前端, 开发语言, stream, openai api

题意:"使用 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>');
                    }
                } 
                        
            }


http://www.niftyadmin.cn/n/5634863.html

相关文章

2024年Java最新面试题总结(三年经验)

目录 第一章、基础技术栈1.1&#xff09;集合&#xff0c;string等基础问题1、arraylist &#xff0c;linkedlist的区别&#xff0c;为啥集合有的快有的慢2、字符串倒叙输出2.1、字符串常用方法2.2、字符串号拼接的底层原理3、讲一下Java的集合框架4、定义线程安全的map&#x…

【人工智能工具】

以下总结了用途不同的人工智能工具&#xff0c;仅供参考&#xff01; 一、文字生成 通义千问、文心一言、讯飞星火、字节豆包、智谱清言 二、图像生成 Midjourney、Stable Diffusion、文心一格 三、视频生成 Sora、Lumen5、Kimi.AI 四、工作处理 [WPS AI)、Xmind AI、C…

JVM GC 调优

文章目录 引言I 调整JVM的默认堆内存配置1.1 java命令启动jar包时配置JVM 的内存参数1.2 基于Tomcat服务器部署的java应用,配置JVM 的内存参数II JVM GC 调优基本概念: 应用程序的响应时间(RT)和吞吐量(QPS)JVM调优原理调优思路调优方法JVM调优技巧建议引言 内存参数:ht…

【Qt】 QDateTimeEdit | QDial

文章目录 QDateTimeEdit —— 时间日期 微调框QDateTimeEdit 属性核心信号QDateTimeEdit 的使用 QDial —— 按钮QDial 属性核心信号QDial 使用 QDateTimeEdit —— 时间日期 微调框 QDateTimeEdit 属性 QDateTimeEdit 作为 时间日期 的 微调框 dateTime —— 时间⽇期的值…

AI问答:.NET核心组成概要、程序运行步骤和查询SDK版本的方法

.NET三大组成 ①Runtime (运行时)&#xff1a; CLR&#xff1a;公共语言运行时&#xff0c;执行程序、内存管理、垃圾回收&#xff08;GC&#xff09;、安全性检查、异常处理&#xff0c;是跨平台的关键要素。 JIT&#xff1a;实时编译器&#xff0c;将中间语言…

七. 部署YOLOv8检测器-load-save-tensor

目录 前言0. 简述1. 案例运行2. 补充说明3. 代码分析3.1 main.cpp3.2 create_data.py 结语下载链接参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战课程》&#xff0c;链接。记录下个人学习笔记&#xff0c;仅供自己参考 本次课程我们来学习课程第六章—部署分类器&…

刷题DAY24

求绝对值最大值 题目&#xff1a;求n个整数中的绝对值最大的数 输入&#xff1a;输入数据有2行&#xff0c;第一行为n(1<n<10)&#xff0c;第二行是n个整数 输出&#xff1a;输出n个数中绝对值最大的数。数据保证结果唯一 输入&#xff1a; 5 -1 2 3 4 -5 输出&…

【uniapp 解决h5 uni.saveFile 不生效】2种方法解决

用uni.saveFile h5报错 saveFile API saveFile is not yet implemented 查看文档发现不支持h5 解决方法&#xff1a; 这个我用了pc 端一样的方法用a标签来下载保存代码如下&#xff1a; 第一种&#xff1a; const a document.createElement(a);a.href filePath; //filePath …