-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the duplicate data check problem, history filter and add tts st…
…ream (#477)
- Loading branch information
Showing
31 changed files
with
363 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { connectionMongo, type Model } from '../../../common/mongo'; | ||
const { Schema, model, models } = connectionMongo; | ||
import { TTSBufferSchemaType } from './type.d'; | ||
|
||
export const collectionName = 'ttsbuffers'; | ||
|
||
const TTSBufferSchema = new Schema({ | ||
bufferId: { | ||
type: String, | ||
required: true | ||
}, | ||
text: { | ||
type: String, | ||
required: true | ||
}, | ||
buffer: { | ||
type: Buffer, | ||
required: true | ||
}, | ||
createTime: { | ||
type: Date, | ||
default: () => new Date() | ||
} | ||
}); | ||
|
||
try { | ||
TTSBufferSchema.index({ bufferId: 1 }); | ||
// 24 hour | ||
TTSBufferSchema.index({ createTime: 1 }, { expireAfterSeconds: 24 * 60 * 60 }); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
export const MongoTTSBuffer: Model<TTSBufferSchemaType> = | ||
models[collectionName] || model(collectionName, TTSBufferSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type TTSBufferSchemaType = { | ||
bufferId: string; | ||
text: string; | ||
buffer: Buffer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
import { Text2SpeechProps } from '@fastgpt/global/core/ai/speech/api'; | ||
import type { NextApiResponse } from 'next'; | ||
import { getAIApi } from '../config'; | ||
import { defaultAudioSpeechModels } from '../../../../global/core/ai/model'; | ||
import { Text2SpeechVoiceEnum } from '@fastgpt/global/core/ai/speech/constant'; | ||
import { UserModelSchema } from '@fastgpt/global/support/user/type'; | ||
|
||
export async function text2Speech({ | ||
model = defaultAudioSpeechModels[0].model, | ||
voice = Text2SpeechVoiceEnum.alloy, | ||
res, | ||
onSuccess, | ||
onError, | ||
input, | ||
speed = 1 | ||
}: Text2SpeechProps) { | ||
const ai = getAIApi(); | ||
const mp3 = await ai.audio.speech.create({ | ||
model = defaultAudioSpeechModels[0].model, | ||
voice, | ||
speed = 1, | ||
props | ||
}: { | ||
res: NextApiResponse; | ||
onSuccess: (e: { model: string; buffer: Buffer }) => void; | ||
onError: (e: any) => void; | ||
input: string; | ||
model: string; | ||
voice: string; | ||
speed?: number; | ||
props?: UserModelSchema['openaiAccount']; | ||
}) { | ||
const ai = getAIApi(props); | ||
const response = await ai.audio.speech.create({ | ||
model, | ||
// @ts-ignore | ||
voice, | ||
input, | ||
response_format: 'mp3', | ||
speed | ||
}); | ||
const buffer = Buffer.from(await mp3.arrayBuffer()); | ||
return { | ||
model, | ||
voice, | ||
tts: buffer | ||
}; | ||
|
||
const readableStream = response.body as unknown as NodeJS.ReadableStream; | ||
readableStream.pipe(res); | ||
|
||
let bufferStore = Buffer.from([]); | ||
|
||
readableStream.on('data', (chunk) => { | ||
bufferStore = Buffer.concat([bufferStore, chunk]); | ||
}); | ||
readableStream.on('end', () => { | ||
onSuccess({ model, buffer: bufferStore }); | ||
}); | ||
readableStream.on('error', (e) => { | ||
onError(e); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import type { AppTTSConfigType } from '@/types/app'; | ||
|
||
export type GetChatSpeechProps = { | ||
chatItemId?: string; | ||
ttsConfig: AppTTSConfigType; | ||
input: string; | ||
}; |
Oops, something went wrong.