提交 2a5d4ce4 编写于 作者: DCloud_JSON's avatar DCloud_JSON

替换 SSEChannel 为 sseChannel

上级 fe468eca
...@@ -88,7 +88,7 @@ ...@@ -88,7 +88,7 @@
"vueVersion" : "3", "vueVersion" : "3",
"h5" : { "h5" : {
"unipush" : { "unipush" : {
"enable" : false "enable" : true
} }
}, },
"fallbackLocale" : "zh-Hans" "fallbackLocale" : "zh-Hans"
......
...@@ -323,11 +323,11 @@ ...@@ -323,11 +323,11 @@
console.log('send to ai messages:', messages); console.log('send to ai messages:', messages);
let SSEChannel = false; let sseChannel = false;
if(this.stream){ if(this.stream){
SSEChannel = new uniCloud.SSEChannel() // 创建消息通道 sseChannel = new uniCloud.SSEChannel() // 创建消息通道
// console.log('SSEChannel',SSEChannel); // console.log('sseChannel',sseChannel);
SSEChannel.on('message', (message) => { // 监听message事件 sseChannel.on('message', (message) => { // 监听message事件
// console.log('on message', message); // console.log('on message', message);
if (this.sseIndex === 0) { if (this.sseIndex === 0) {
this.msgList.push({ this.msgList.push({
...@@ -344,7 +344,7 @@ ...@@ -344,7 +344,7 @@
} }
this.sseIndex++ this.sseIndex++
}) })
SSEChannel.on('end', (e) => { // 监听end事件,如果云端执行end时传了message,会在客户端end事件内收到传递的消息 sseChannel.on('end', (e) => { // 监听end事件,如果云端执行end时传了message,会在客户端end事件内收到传递的消息
// console.log('on end', e); // console.log('on end', e);
if(e && (e.summarize || e.insufficientPoints)){ if(e && (e.summarize || e.insufficientPoints)){
this.updateLastMsg(lastMsg=>{ this.updateLastMsg(lastMsg=>{
...@@ -358,7 +358,7 @@ ...@@ -358,7 +358,7 @@
this.sseIndex = 0 this.sseIndex = 0
this.showLastMsg() this.showLastMsg()
}) })
await SSEChannel.open() // 等待通道开启 await sseChannel.open() // 等待通道开启
} }
...@@ -367,11 +367,11 @@ ...@@ -367,11 +367,11 @@
}) })
uniAiChat.send({ uniAiChat.send({
messages, messages,
SSEChannel sseChannel
}) })
.then(res => { .then(res => {
this.updateLastMsg({state:100}) this.updateLastMsg({state:100})
if (!SSEChannel) { if (!sseChannel) {
// console.log(res, res.reply); // console.log(res, res.reply);
this.msgList.push({ this.msgList.push({
isAi: true, isAi: true,
......
...@@ -61,8 +61,8 @@ module.exports = { ...@@ -61,8 +61,8 @@ module.exports = {
requestId: this.getUniCloudRequestId() requestId: this.getUniCloudRequestId()
}) })
this.textSecCheck = async (content)=>{ this.textSecCheck = async (content)=>{
let {SSEChannel} = this.getParams()[0]||{} let {sseChannel} = this.getParams()[0]||{}
if(SSEChannel){ if(sseChannel){
throw "流式响应模式,内容安全识别功能无效" throw "流式响应模式,内容安全识别功能无效"
} }
// 检测文本 // 检测文本
...@@ -109,9 +109,9 @@ module.exports = { ...@@ -109,9 +109,9 @@ module.exports = {
} }
}else if(error == 'insufficientPoints'){ }else if(error == 'insufficientPoints'){
let reply = "积分不足,请看完激励视频广告后再试" let reply = "积分不足,请看完激励视频广告后再试"
let {SSEChannel} = this.getParams()[0]||{} let {sseChannel} = this.getParams()[0]||{}
if(SSEChannel){ if(sseChannel){
const channel = uniCloud.deserializeSSEChannel(SSEChannel) const channel = uniCloud.deserializeSSEChannel(sseChannel)
await channel.write(reply) await channel.write(reply)
await channel.end({ await channel.end({
"insufficientPoints":true "insufficientPoints":true
...@@ -147,7 +147,7 @@ module.exports = { ...@@ -147,7 +147,7 @@ module.exports = {
}, },
async send({ async send({
messages, messages,
SSEChannel sseChannel
}) { }) {
// 初次调试时,可不从客户端获取数据,直接使用下面写死在云函数里的数据 // 初次调试时,可不从客户端获取数据,直接使用下面写死在云函数里的数据
// messages = [{ // messages = [{
...@@ -165,13 +165,13 @@ module.exports = { ...@@ -165,13 +165,13 @@ module.exports = {
let {llm,chatCompletionOptions} = config let {llm,chatCompletionOptions} = config
return await chatCompletion({ return await chatCompletion({
messages, //消息内容 messages, //消息内容
SSEChannel, //sse渠道对象 sseChannel, //sse渠道对象
llm llm
}) })
async function chatCompletion({ async function chatCompletion({
messages, messages,
summarize = false, summarize = false,
SSEChannel = false, sseChannel = false,
llm llm
}) { }) {
...@@ -179,13 +179,13 @@ module.exports = { ...@@ -179,13 +179,13 @@ module.exports = {
let res = await llmManager.chatCompletion({ let res = await llmManager.chatCompletion({
...chatCompletionOptions, ...chatCompletionOptions,
messages, messages,
stream: SSEChannel !== false stream: sseChannel !== false
}) })
if (SSEChannel) { if (sseChannel) {
let reply = "" let reply = ""
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const channel = uniCloud.deserializeSSEChannel(SSEChannel) const channel = uniCloud.deserializeSSEChannel(sseChannel)
res.on('message', async (message) => { res.on('message', async (message) => {
// await channel.write(message) // await channel.write(message)
// console.log('---message----', message) // console.log('---message----', message)
...@@ -256,7 +256,7 @@ module.exports = { ...@@ -256,7 +256,7 @@ module.exports = {
messages, messages,
summarize: true, summarize: true,
stream: false, stream: false,
SSEChannel: false sseChannel: false
}) })
return res.reply return res.reply
} }
......
{
"bsonType": "object",
"required": [
"_id",
"value"
],
"properties": {
"_id": {
"bsonType": "string",
"description": "自动生成的id"
},
"app_id": {
"bsonType": "string",
"description": "客户端DCloud AppId"
},
"device_id": {
"bsonType": "string",
"description": "客户端设备id"
},
"private_key": {
"bsonType": "string",
"description": "私钥,仅保存在云端"
},
"public_key": {
"bsonType": "string",
"description": "公钥,下发给客户端使用"
},
"create_date": {
"bsonType": "timestamp",
"description": "创建时间"
}
},
"version": "0.0.1"
}
\ No newline at end of file
// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema
{
"bsonType": "object",
"required": [],
"permission": {
"read": false,
"create": false,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"user_id":{
"bsonType": "string",
"description": "用户id"
},
"balance":{
"bsonType": "int",
"description": "剩余可对话的次数"
},
"update_time":{
"bsonType": "timestamp",
"forceDefaultValue":{
"$env": "now"
}
}
}
}
\ No newline at end of file
{
"bsonType": "object",
"required": [
"user_id",
"score",
"balance"
],
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"user_id": {
"bsonType": "string",
"description": "用户id,参考uni-id-users表"
},
"score": {
"bsonType": "int",
"description": "本次变化的积分"
},
"type": {
"bsonType": "int",
"enum": [
1,
2
],
"description": "积分类型 1:收入 2:支出"
},
"balance": {
"bsonType": "int",
"description": "变化后的积分余额"
},
"comment": {
"bsonType": "string",
"description": "备注,说明积分新增、消费的缘由",
"trim": "both"
},
"create_date": {
"bsonType": "timestamp",
"description": "创建时间",
"forceDefaultValue": {
"$env": "now"
}
}
},
"version": "0.0.1"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册