Skip to content

Commit

Permalink
Merge pull request #143 from meetqy/yiyan
Browse files Browse the repository at this point in the history
test
  • Loading branch information
meetqy authored Apr 25, 2024
2 parents 6f65aeb + d7dde90 commit 3ce0da0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/app/create/poem/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default function CreatePage() {
const authors = data?.data;

const mutation = {
genTranslate: api.poem.genTranslation.useMutation({
onSuccess(data) {
setTranslation(data);
},
}),
deletePoem: api.poem.deleteById.useMutation({
onSuccess: async () => {
await utils.poem.findById.invalidate({
Expand Down Expand Up @@ -316,7 +321,7 @@ export default function CreatePage() {
<div className="grid grid-cols-2 gap-x-4">
<div className="space-y-2">
<label className="font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
<span className="text-red-500">*</span> 内容
<span className="text-red-500">*</span> 内容{" "}
</label>
<Textarea
placeholder="输入内容"
Expand Down Expand Up @@ -354,6 +359,24 @@ export default function CreatePage() {
<div className="space-y-2">
<label className="font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
译文
<Button
className="ml-2"
size={"sm"}
onClick={() => {
if (mutation.genTranslate.isLoading) return;
if (!content) {
alert("请输入内容");
return;
}

mutation.genTranslate.mutate({
token,
content,
});
}}
>
生成
</Button>
</label>
<Textarea
placeholder="白话文"
Expand Down
80 changes: 80 additions & 0 deletions src/server/api/routers/poem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,69 @@ import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { LangZod, transformPoem, transformTag } from "../utils";
import { splitChineseSymbol } from "~/utils";

let token: {
access_token: string;
expires_in: number;
time: number;
};

const getToken = async () => {
if (!token || token.time + token.expires_in * 1000 < Date.now()) {
const res = await fetch(
`https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${process.env.BAIDU_YIYAN_API_KEY}&client_secret=${process.env.BAIDU_YIYAN_SECRET_KEY}`,
{ method: "POST" },
);

const data = (await res.json()) as {
access_token: string;
expires_in: number;
};

token = {
...data,
time: Date.now(),
};
}

return token;
};

const getTranslation = async (content: string) => {
const _token = await getToken();

const res = await fetch(
`https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-preview?access_token=${_token.access_token}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [
{
role: "user",
content:
"你只需要将我发送的诗词内容进行白话文翻译,不需要赏析,不需要任何多余内容,只需要白话文翻译。不需要加上描述性文字“希望能符合我的要求”类似的结束语。不需要“以下是 xxx “的开始语, 确认请回复“确认”",
},
{
role: "assistant",
content:
"确认。请提供您需要翻译的诗词内容,我会直接将其翻译成白话文。",
},
{
role: "user",
content: content,
},
],
}),
},
);

const json = (await res.json()) as { result: string };

return json.result;
};

export const poemRouter = createTRPCRouter({
count: publicProcedure.query(({ ctx }) => ctx.db.poem.count()),

Expand Down Expand Up @@ -222,6 +285,23 @@ export const poemRouter = createTRPCRouter({
};
}),

/**
* 文心一言生成诗词译文
*/
genTranslation: publicProcedure
.input(
z.object({
token: z.string(),
content: z.string(),
}),
)
.mutation(async ({ input }) => {
if (input.token !== process.env.TOKEN) throw new Error("Invalid token");

const translation = await getTranslation(input.content);
return translation;
}),

/**
* 根据 id 查找诗词
*/
Expand Down

0 comments on commit 3ce0da0

Please sign in to comment.