From 735c927c1f157036d97a097ba85a9e2cf0f2ee6b Mon Sep 17 00:00:00 2001 From: white <1293616053@qq.com> Date: Mon, 14 Oct 2024 16:42:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hybrid/html/ai.html | 258 ++++++++-------- unpackage/dist/dev/app-plus/app-service.js | 172 ++--------- unpackage/dist/dev/app-plus/app-view.js | 253 +-------------- .../dev/app-plus/assets/music.a081d3b1.png | Bin 8332 -> 0 bytes .../dist/dev/app-plus/hybrid/html/ai.html | 288 +++++++++--------- 5 files changed, 312 insertions(+), 659 deletions(-) delete mode 100644 unpackage/dist/dev/app-plus/assets/music.a081d3b1.png diff --git a/hybrid/html/ai.html b/hybrid/html/ai.html index a8f48b1..3d37d60 100755 --- a/hybrid/html/ai.html +++ b/hybrid/html/ai.html @@ -79,6 +79,7 @@ @@ -267,8 +243,6 @@ ...value })); - speed('你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛你干嘛'); - /* * 实例化迅飞语音听写(流式版)WebAPI */ @@ -298,13 +272,10 @@ // 判断关键字是否存在 const result = parseHealthQuery(params.msg); isCallbackExecuted = true; - console.log(result, '判断关键字是否存在') if (result) { DetailDay(result.dataKey, result.date).then(res => { - console.log(JSON.stringify(res), 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzz') let TargetData = res.data.data; - var QSindex = containsKeywordRegex(params.msg); if (result.dataKey == 'ECGData') { TargetData.map(item => { @@ -324,118 +295,148 @@ Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。` - console.log(Question, '========================='); + console.log(Question, '问题=========================问题'); + /* * 调用接口 传递关键信息 文字转语音 */ const xhr = new XMLHttpRequest(); - xhr.open('POST', 'http://sc2.agrimedia.cn:8787/api/user/ask', true); + const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'; + const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d'; + // 打开请求,设置为异步 + xhr.open('POST', url, true); + + // 设置请求头 + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey); + xhr.setRequestHeader('X-DashScope-SSE', 'enable'); + + // 处理流式数据的接收(使用 progress 事件) + xhr.onprogress = function () { + const data = xhr.responseText; + + // SSE 数据解析处理 + const lines = data.split('\n'); + lines.forEach(line => { + if (line.startsWith('data:')) { + const jsonStr = line.substring(5); // 去掉 'data:' 前缀 + const parsedData = JSON.parse(jsonStr); + + // 解析 content 内容 + const content = parsedData.output.choices[0].message.content; + if (content) { + // 检查内容是否已经存在 + if (!Subtitles.includes(content)) { + Subtitles += content; + } + }; + } + }); + }; + + // 检查请求完成 + xhr.onload = function () { + if (xhr.status === 200) { + statusTxt.value = '正在说话'; + RequestMicrosoft(); + } + }; + + // 错误处理 + xhr.onerror = function () { + console.error('An error occurred during the transaction', xhr.statusText); + }; + /* * 关键字转换 */ - var data = JSON.stringify({ - "messages": [ - {"role": "system", "content": Question}, - {"role": "user", "content": `请问我${params.msg}正常吗`} - ] - }); - - xhr.onreadystatechange = function() { - if (xhr.readyState === 4) { - const chunk = xhr.responseText; - const str = extractStopEvent(chunk); - - statusTxt.value = '正在说话'; - - /* - * 微软接口识别 - */ - fetch("https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1", { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + token, - 'Ocp-Apim-Subscription-Key': '58e9b39b8f6f48fe8d01f85b727ff737', - 'Content-Type': 'application/ssml+xml', - 'X-Microsoft-OutputFormat': 'audio-24khz-48kbitrate-mono-mp3' - }, - responseType: 'arraybuffer', - body: ` - - - - ${filterString(str.output.text, ['*', ' '])} - - - - `, - }).then(async(response) => { - startRec.style.display = "none"; - endRec.style.display = "block"; - - NextPlayVideo(response, filterString(str.output.text, ['*', ' '])); - }).catch(e => { - hideModal(); - }); - } + const requestBody = { + model: 'qwen-turbo', + input: { + messages: [ + {"role": "system", "content": Question}, + {"role": "user", "content": `请问我${params.msg}正常吗`} + ] + }, + parameters: { + result_format: 'message', + incremental_output: true + } }; - xhr.send(data); + + xhr.send(JSON.stringify(requestBody)); isCallbackExecuted = true; - return + return; }) } else { - // 如果不存在关键字,就直接走下面 const xhr = new XMLHttpRequest(); - xhr.open('POST', 'http://sc2.agrimedia.cn:8787/api/user/ask', true); - var data = JSON.stringify({ - "messages": [ - {"role": "system", "content": `请模仿全科医生的口吻与我对话`}, - {"role": "user", "content": params.msg} - ] - }) + const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'; + const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d'; - xhr.onreadystatechange = function() { - if (xhr.readyState === 4) { - const chunk = xhr.responseText; - const str = extractStopEvent(chunk); - - statusTxt.value = '正在说话'; - - /* - * 微软接口识别 - */ - fetch("https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1", { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + token, - 'Ocp-Apim-Subscription-Key': '58e9b39b8f6f48fe8d01f85b727ff737', - 'Content-Type': 'application/ssml+xml', - 'X-Microsoft-OutputFormat': 'audio-24khz-48kbitrate-mono-mp3' - }, - responseType: 'arraybuffer', - body: ` - - - - ${filterString(str.output.text, ['*', ' '])} - - - - `, - }).then(async(response) => { - startRec.style.display = "none"; - endRec.style.display = "block"; - - NextPlayVideo(response, filterString(str.output.text, ['*', ' '])); - }).catch(e => { - hideModal(); + // 打开请求,设置为异步 + xhr.open('POST', url, true); + + // 设置请求头 + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey); + xhr.setRequestHeader('X-DashScope-SSE', 'enable'); + + // 处理流式数据的接收(使用 progress 事件) + xhr.onprogress = function () { + const data = xhr.responseText; + + // SSE 数据解析处理 + const lines = data.split('\n'); + lines.forEach(line => { + if (line.startsWith('data:')) { + const jsonStr = line.substring(5); // 去掉 'data:' 前缀 + const parsedData = JSON.parse(jsonStr); + + // 解析 content 内容 + const content = parsedData.output.choices[0].message.content; + if (content) { + // 检查内容是否已经存在 + if (!Subtitles.includes(content)) { + Subtitles += content; + } + }; + } }); - } }; - xhr.send(data); + + // 检查请求完成 + xhr.onload = function () { + if (xhr.status === 200) { + statusTxt.value = '正在说话'; + RequestMicrosoft(); + } + }; + + // 错误处理 + xhr.onerror = function () { + console.error('An error occurred during the transaction', xhr.statusText); + }; + + // 发送请求 + const requestBody = { + model: 'qwen-turbo', + input: { + messages: [ + {"role": "system", "content": `请模仿全科医生的口吻与我对话`}, + {"role": "user", "content": params.msg} + ] + }, + parameters: { + result_format: 'message', + incremental_output: true + } + }; + + xhr.send(JSON.stringify(requestBody)); isCallbackExecuted = true; - return + return; } }, 3000); } @@ -503,7 +504,7 @@ modal.style.display = 'none'; dialog.style.display = 'none'; } - + // 查询详情数据 async function DetailDay(type, data) { try { @@ -519,7 +520,7 @@ "device_id": deviceId, "type": type }) - }); + }); if (!response.ok) { throw new Error('Network response was not ok: ' + response.statusText); @@ -533,10 +534,8 @@ } } - - // 语音输出 - function speed(str) { - console.log(str) + // 请求微软文字转语音 + function RequestMicrosoft() { fetch("https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1", { method: 'POST', headers: { @@ -550,19 +549,22 @@ - ${str} + ${filterString(Subtitles, ['*', ' '])} `, }).then(async(response) => { - NextPlayVideo(response, str); + startRec.style.display = "none"; + endRec.style.display = "block"; + + NextPlayVideo(response, filterString(Subtitles, ['*', ' '])); }).catch(e => { hideModal(); + Subtitles = ""; }); } - // 字幕播放视频等操作 async function NextPlayVideo(response, str) { const content_bytes = await response.arrayBuffer(); @@ -594,6 +596,8 @@ replayVideoSegment(0, 60); hideModal() }); + + Subtitles = ""; } // 防抖 @@ -611,20 +615,6 @@ }; } - // 分钟转小时 - function minutesToHoursMinutesStringSimplified(minutes) { - let hours = Math.floor(minutes / 60); - let remainingMinutes = minutes % 60; - - let formattedMinutes = remainingMinutes.toString().padStart(2, '0'); - - if (hours > 0) { - return `${hours}小时${formattedMinutes}分`; - } else { - return `${formattedMinutes}分`; - } - } - // 拿到的数据移除* function filterString(str, charsToRemove) { // 这里的正则表达式是通过将charsToRemove数组中的字符转换为字符类(character class)来构建的 @@ -673,7 +663,7 @@ targetDate.setDate(day - 1); } else if (query.includes("前天")) { targetDate.setDate(day - 2); - } + } const formattedDate = `${targetDate.getFullYear()}-${('0' + targetDate.getMonth()).slice(-2)}-${('0' + targetDate.getDate()).slice(-2)}`; @@ -681,7 +671,7 @@ function isBloodLiquidMatch(bloodLiquidData, query) { return bloodLiquidData.some(element => query.includes(element)) } - + // 在查询逻辑中使用这个辅助函数 for (let key in demoData) { if (typeof demoData[key] === 'object' && key === 'bloodLiquid' || typeof demoData[key] === 'object' && key === 'stepIndex' ) {