mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-21 11:43:56 +00:00

* simple mode tool reason * model config cannot set empty * perf: read files code * perf: mongo gridfs chunks * perf: doc
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import type { NextApiResponse } from 'next';
|
|
import { getAIApi } from '../config';
|
|
import { getTTSModel } from '../model';
|
|
|
|
export async function text2Speech({
|
|
res,
|
|
onSuccess,
|
|
onError,
|
|
input,
|
|
model,
|
|
voice,
|
|
speed = 1
|
|
}: {
|
|
res: NextApiResponse;
|
|
onSuccess: (e: { model: string; buffer: Buffer }) => void;
|
|
onError: (e: any) => void;
|
|
input: string;
|
|
model: string;
|
|
voice: string;
|
|
speed?: number;
|
|
}) {
|
|
const modelData = getTTSModel(model)!;
|
|
const ai = getAIApi();
|
|
const response = await ai.audio.speech.create(
|
|
{
|
|
model,
|
|
// @ts-ignore
|
|
voice,
|
|
input,
|
|
response_format: 'mp3',
|
|
speed
|
|
},
|
|
modelData.requestUrl && modelData.requestAuth
|
|
? {
|
|
path: modelData.requestUrl,
|
|
headers: {
|
|
Authorization: `Bearer ${modelData.requestAuth}`
|
|
}
|
|
}
|
|
: {}
|
|
);
|
|
|
|
const readableStream = response.body as unknown as NodeJS.ReadableStream;
|
|
readableStream.pipe(res);
|
|
|
|
const chunks: Uint8Array[] = [];
|
|
|
|
readableStream.on('data', (chunk) => {
|
|
chunks.push(chunk);
|
|
});
|
|
readableStream.on('end', () => {
|
|
onSuccess({ model, buffer: Buffer.concat(chunks) });
|
|
});
|
|
readableStream.on('error', (e) => {
|
|
onError(e);
|
|
});
|
|
}
|