Fixed the duplicate data check problem, history filter and add tts stream (#477)
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@fastgpt/service/common/response';
|
||||
import { connectToDatabase } from '@/service/mongo';
|
||||
import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
|
||||
import { GetChatSpeechProps } from '@/global/core/chat/api.d';
|
||||
import { text2Speech } from '@fastgpt/service/core/ai/audio/speech';
|
||||
import { pushAudioSpeechBill } from '@/service/support/wallet/bill/push';
|
||||
import { authCert } from '@fastgpt/service/support/permission/auth/common';
|
||||
import { authType2BillSource } from '@/service/support/wallet/bill/utils';
|
||||
import { getAudioSpeechModel } from '@/service/core/ai/model';
|
||||
import { MongoTTSBuffer } from '@fastgpt/service/common/buffer/tts/schema';
|
||||
|
||||
/*
|
||||
1. get tts from chatItem store
|
||||
@@ -18,50 +19,66 @@ import { authType2BillSource } from '@/service/support/wallet/bill/utils';
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
await connectToDatabase();
|
||||
const { chatItemId, ttsConfig, input } = req.body as GetChatSpeechProps;
|
||||
const { ttsConfig, input } = req.body as GetChatSpeechProps;
|
||||
|
||||
if (!ttsConfig.model || !ttsConfig.voice) {
|
||||
throw new Error('model or voice not found');
|
||||
}
|
||||
|
||||
const { teamId, tmbId, authType } = await authCert({ req, authToken: true });
|
||||
|
||||
const chatItem = await (async () => {
|
||||
if (!chatItemId) return null;
|
||||
return await MongoChatItem.findOne(
|
||||
{
|
||||
dataId: chatItemId
|
||||
},
|
||||
'tts'
|
||||
);
|
||||
})();
|
||||
const ttsModel = getAudioSpeechModel(ttsConfig.model);
|
||||
const voiceData = ttsModel.voices.find((item) => item.value === ttsConfig.voice);
|
||||
|
||||
if (chatItem?.tts) {
|
||||
return jsonRes(res, {
|
||||
data: chatItem.tts
|
||||
});
|
||||
if (!voiceData) {
|
||||
throw new Error('voice not found');
|
||||
}
|
||||
|
||||
const { tts, model } = await text2Speech({
|
||||
const ttsBuffer = await MongoTTSBuffer.findOne(
|
||||
{
|
||||
bufferId: voiceData.bufferId,
|
||||
text: input
|
||||
},
|
||||
'buffer'
|
||||
);
|
||||
|
||||
if (ttsBuffer?.buffer) {
|
||||
return res.end(new Uint8Array(ttsBuffer.buffer.buffer));
|
||||
}
|
||||
|
||||
await text2Speech({
|
||||
res,
|
||||
input,
|
||||
model: ttsConfig.model,
|
||||
voice: ttsConfig.voice,
|
||||
input
|
||||
});
|
||||
props: {
|
||||
// temp code
|
||||
baseUrl: ttsModel.baseUrl || '',
|
||||
key: ttsModel.key || ''
|
||||
},
|
||||
onSuccess: async ({ model, buffer }) => {
|
||||
try {
|
||||
pushAudioSpeechBill({
|
||||
model: model,
|
||||
textLength: input.length,
|
||||
tmbId,
|
||||
teamId,
|
||||
source: authType2BillSource({ authType })
|
||||
});
|
||||
|
||||
(async () => {
|
||||
if (!chatItem) return;
|
||||
try {
|
||||
chatItem.tts = tts;
|
||||
await chatItem.save();
|
||||
} catch (error) {}
|
||||
})();
|
||||
|
||||
jsonRes(res, {
|
||||
data: tts
|
||||
});
|
||||
|
||||
pushAudioSpeechBill({
|
||||
model: model,
|
||||
textLength: input.length,
|
||||
tmbId,
|
||||
teamId,
|
||||
source: authType2BillSource({ authType })
|
||||
await MongoTTSBuffer.create({
|
||||
bufferId: voiceData.bufferId,
|
||||
text: input,
|
||||
buffer
|
||||
});
|
||||
} catch (error) {}
|
||||
},
|
||||
onError: (err) => {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
jsonRes(res, {
|
||||
|
||||
@@ -34,8 +34,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
qaModels: global.qaModels,
|
||||
cqModels: global.cqModels,
|
||||
extractModels: global.extractModels,
|
||||
qgModels: global.qgModels,
|
||||
vectorModels: global.vectorModels,
|
||||
audioSpeechModels: global.audioSpeechModels.map((item) => ({
|
||||
...item,
|
||||
baseUrl: undefined,
|
||||
key: undefined
|
||||
})),
|
||||
priceMd: global.priceMd,
|
||||
systemVersion: global.systemVersion || '0.0.0'
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
if (chatMessages[chatMessages.length - 1].obj !== ChatRoleEnum.Human) {
|
||||
chatMessages.pop();
|
||||
}
|
||||
|
||||
// user question
|
||||
const question = chatMessages.pop();
|
||||
if (!question) {
|
||||
@@ -173,15 +174,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
const isAppOwner = !shareId && String(user.team.tmbId) === String(app.tmbId);
|
||||
|
||||
/* format prompts */
|
||||
const prompts = history.concat(gptMessage2ChatType(messages));
|
||||
|
||||
// set sse response headers
|
||||
if (stream) {
|
||||
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('X-Accel-Buffering', 'no');
|
||||
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
}
|
||||
const concatHistory = history.concat(chatMessages);
|
||||
|
||||
/* start flow controller */
|
||||
const { responseData, answerText } = await dispatchModules({
|
||||
@@ -193,7 +186,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
tmbId: user.team.tmbId,
|
||||
variables,
|
||||
params: {
|
||||
history: prompts,
|
||||
history: concatHistory,
|
||||
userChatInput: question.value
|
||||
},
|
||||
stream,
|
||||
|
||||
Reference in New Issue
Block a user