From 565f21e277cf396829a72e962daf006f56737b9d Mon Sep 17 00:00:00 2001 From: Bailin Date: Mon, 21 Aug 2023 15:51:26 +0800 Subject: [PATCH 1/2] Update toolbox.user.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加对 json 类型数据的处理,读取历史消息时采用的并非时 event-stream方式 * 直接采用字符串替换的方式关闭blocked和flagged --- toolbox.user.js | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/toolbox.user.js b/toolbox.user.js index 2153dcd..23c8a63 100644 --- a/toolbox.user.js +++ b/toolbox.user.js @@ -751,8 +751,8 @@ setInterval(function(){ } - // 判断是否是流式响应 - if (response.body && response.body instanceof ReadableStream && response.headers.get('content-type').indexOf('event-stream') != -1) { + // 判断是否是流式响应,增加对 json 类型数据的处理,读取历史消息时采用的并非时 event-stream方式 + if (response.body && response.body instanceof ReadableStream && (response.headers.get('content-type').indexOf('event-stream') != -1 || response.headers.get('content-type').indexOf('json') != -1)) { // 如果是流式响应,使用一个新的 ReadableStream const modifiedStream = new ReadableStream({ start(controller) { @@ -815,33 +815,10 @@ setInterval(function(){ function processData(text) { - // console.log(text); - if (text.indexOf('data: ') == -1) { - return text; - } - const jsonStartIndex = text.indexOf('data: ') + 6; - const jsonString = text.substring(jsonStartIndex); - let obj; - - try { - obj = JSON.parse(jsonString); - //覆盖标注返回值 - if (obj.moderation_response) { - obj.moderation_response.flagged = false; - obj.moderation_response.blocked = false; - } - - } catch (error) { - // 发生错误,无法转换为 JSON - return text; - } - - // 将对象序列化为 JSON - const modifiedJson = JSON.stringify(obj); - - // 将 "data: " 添加到 JSON 前 - const modifiedText = `data: ${modifiedJson}`; - return modifiedText; + //直接采用字符串替换的方式关闭blocked和flagged + let mdText=text.replace(/"blocked"\s*:\s*true/g, '"blocked":false').replace(/"flagged"\s*:\s*true/g, '"flagged":false'); + //console.log(text); + return mdText } window.openaiChatCompletionsP = async function (message, api_key) { From 45ec2a7df535f4956cf93644756ca78371421db9 Mon Sep 17 00:00:00 2001 From: Bailin Date: Mon, 21 Aug 2023 15:56:16 +0800 Subject: [PATCH 2/2] Update toolbox-raw.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加对 json 类型数据的处理,读取历史消息时采用的并非时 event-stream方式 * 直接采用字符串替换的方式关闭blocked和flagged --- toolbox-raw.js | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/toolbox-raw.js b/toolbox-raw.js index 354d7c6..c74d98d 100644 --- a/toolbox-raw.js +++ b/toolbox-raw.js @@ -738,8 +738,8 @@ function main() { } - // 判断是否是流式响应 - if (response.body && response.body instanceof ReadableStream && response.headers.get('content-type').indexOf('event-stream') != -1) { + // 判断是否是流式响应,增加对 json 类型数据的处理,读取历史消息时采用的并非时 event-stream方式 + if (response.body && response.body instanceof ReadableStream && (response.headers.get('content-type').indexOf('event-stream') != -1 || response.headers.get('content-type').indexOf('json') != -1)) { // 如果是流式响应,使用一个新的 ReadableStream const modifiedStream = new ReadableStream({ start(controller) { @@ -802,33 +802,10 @@ function main() { function processData(text) { - // console.log(text); - if (text.indexOf('data: ') == -1) { - return text; - } - const jsonStartIndex = text.indexOf('data: ') + 6; - const jsonString = text.substring(jsonStartIndex); - let obj; - - try { - obj = JSON.parse(jsonString); - //覆盖标注返回值 - if (obj.moderation_response) { - obj.moderation_response.flagged = false; - obj.moderation_response.blocked = false; - } - - } catch (error) { - // 发生错误,无法转换为 JSON - return text; - } - - // 将对象序列化为 JSON - const modifiedJson = JSON.stringify(obj); - - // 将 "data: " 添加到 JSON 前 - const modifiedText = `data: ${modifiedJson}`; - return modifiedText; + //直接采用字符串替换的方式关闭blocked和flagged + let mdText=text.replace(/"blocked"\s*:\s*true/g, '"blocked":false').replace(/"flagged"\s*:\s*true/g, '"flagged":false'); + //console.log(text); + return mdText } window.openaiChatCompletionsP = async function (message, api_key) {