format everything in zed
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { callZhipuAPI } from "@/utils";
|
||||
import { callZhipuAPI, handleAPIError } from "@/utils";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
async function getIPA(text: string) {
|
||||
console.log(`get ipa of ${text}`);
|
||||
const messages = [
|
||||
{
|
||||
role: 'user', content: `
|
||||
console.log(`get ipa of ${text}`);
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
请推断以下文本的语言,生成对应的宽式国际音标(IPA)以及locale,以JSON格式返回
|
||||
[${text}]
|
||||
结果如:
|
||||
@@ -18,47 +19,44 @@ async function getIPA(text: string) {
|
||||
ipa一定要加[],
|
||||
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;
|
||||
}
|
||||
`,
|
||||
},
|
||||
];
|
||||
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');
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const text = searchParams.get("text");
|
||||
|
||||
if (!text) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数是必需的" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const textInfo = await getIPA(text);
|
||||
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 }
|
||||
);
|
||||
if (!text) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数是必需的" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const textInfo = await getIPA(text);
|
||||
if (!textInfo) {
|
||||
return NextResponse.json(
|
||||
{ error: "服务暂时不可用", message: "LLM API 请求失败" },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(textInfo, { status: 200 });
|
||||
} catch (error) {
|
||||
handleAPIError(error, "请稍后再试");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ 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: `
|
||||
console.log(`get locale of ${text}`);
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
请推断以下文本的的locale,以JSON格式返回
|
||||
[${text}]
|
||||
结果如:
|
||||
@@ -16,47 +17,48 @@ async function getLocale(text: string) {
|
||||
直接返回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;
|
||||
}
|
||||
`,
|
||||
},
|
||||
];
|
||||
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');
|
||||
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 }
|
||||
);
|
||||
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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const url = request.url;
|
||||
return NextResponse.json({
|
||||
message: "Hello World",
|
||||
url: url
|
||||
}, { status: 200 });
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: "Hello World",
|
||||
url: url,
|
||||
},
|
||||
{ status: 200 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ import { callZhipuAPI } from "@/utils";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
async function getTextinfo(text: string) {
|
||||
console.log(`get textinfo of ${text}`);
|
||||
const messages = [
|
||||
{
|
||||
role: 'user', content: `
|
||||
console.log(`get textinfo of ${text}`);
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
请推断以下文本的语言、locale,生成宽式国际音标(IPA),以JSON格式返回
|
||||
[${text}]
|
||||
结果如:
|
||||
@@ -20,45 +21,47 @@ async function getTextinfo(text: string) {
|
||||
ipa一定要加[],
|
||||
lang的值是小写字母的英文的语言名称,
|
||||
locale如果可能有多个,选取最可能的一个,其中使用符号"-"
|
||||
`
|
||||
}];
|
||||
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;
|
||||
}
|
||||
`,
|
||||
},
|
||||
];
|
||||
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');
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const text = searchParams.get("text");
|
||||
|
||||
if (!text) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数是必需的" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const textInfo = await getTextinfo(text);
|
||||
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 }
|
||||
);
|
||||
if (!text) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数是必需的" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const textInfo = await getTextinfo(text);
|
||||
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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ import { callZhipuAPI } from "@/utils";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
async function translate(text: string, target_lang: string) {
|
||||
console.log(`translate "${text}" into ${target_lang}`);
|
||||
const messages = [
|
||||
{
|
||||
role: 'user', content: `
|
||||
console.log(`translate "${text}" into ${target_lang}`);
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
请推断以下文本的语言、locale,并翻译到目标语言[${target_lang}],同样需要locale信息,以JSON格式返回
|
||||
[${text}]
|
||||
结果如:
|
||||
@@ -18,46 +19,48 @@ async function translate(text: string, target_lang: string) {
|
||||
直接返回json文本,
|
||||
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;
|
||||
}
|
||||
`,
|
||||
},
|
||||
];
|
||||
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');
|
||||
const target_lang = searchParams.get('target');
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const text = searchParams.get("text");
|
||||
const target_lang = searchParams.get("target");
|
||||
|
||||
if (!text || !target_lang) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数, target参数是必需的" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const textInfo = await translate(text, target_lang);
|
||||
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 }
|
||||
);
|
||||
if (!text || !target_lang) {
|
||||
return NextResponse.json(
|
||||
{ error: "查询参数错误", message: "text参数, target参数是必需的" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const textInfo = await translate(text, target_lang);
|
||||
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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user