加快了TTS的速度,将IPA生成设置为可选项

This commit is contained in:
2025-10-08 10:42:42 +08:00
parent 66ff0f04c4
commit 2194d93fe0
6 changed files with 284 additions and 98 deletions

View File

@@ -0,0 +1,62 @@
import { callZhipuAPI } from "@/utils";
import { NextRequest, NextResponse } from "next/server";
async function getLocale(text: string) {
console.log(`get locale of ${text}`);
const messages = [
{
role: 'user', content: `
请推断以下文本的的locale以JSON格式返回
[${text}]
结果如:
{
"locale": "zh-CN"
}
注意:
直接返回json文本
locale如果可能有多个选取最可能的一个其中使用符号"-"
locale如果推断失败就返回{"locale": "en-US"}
`
}];
try {
const response = await callZhipuAPI(messages);
let to_parse = response.choices[0].message.content.trim() as string;
if (to_parse.startsWith('`')) to_parse = to_parse.slice(7, to_parse.length - 3);
if (to_parse.length === 0) throw Error('ai啥也每说');
return JSON.parse(to_parse);
} catch (error) {
console.error(error);
return null;
}
}
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const text = searchParams.get('text');
if (!text) {
return NextResponse.json(
{ error: "查询参数错误", message: "text参数是必需的" },
{ status: 400 }
);
}
const textInfo = await getLocale(text.slice(0, 30));
if (!textInfo) {
return NextResponse.json(
{ error: "服务暂时不可用", message: "LLM API 请求失败" },
{ status: 503 }
);
}
return NextResponse.json(textInfo, { status: 200 });
} catch (error) {
console.error('API 错误:', error);
return NextResponse.json(
{ error: "服务器内部错误", message: "请稍后重试" },
{ status: 500 }
);
}
}