Skip to content

Commit c3cedf6

Browse files
committed
✨ 实现了增量翻译
1 parent ed401eb commit c3cedf6

File tree

6 files changed

+323
-237
lines changed

6 files changed

+323
-237
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "dweb-browser开发者文档",
55
"type": "module",
66
"devDependencies": {
7+
"@ai-sdk/deepseek": "^0.1.5",
8+
"@ai-sdk/google": "^1.1.4",
79
"@gaubee/util": "^0.21.0",
810
"@plaoc/is-dweb": "^0.1.2",
911
"@plaoc/plugins": "^1.1.1",
@@ -12,8 +14,11 @@
1214
"@shikijs/vitepress-twoslash": "^2.1.0",
1315
"@std/cli": "npm:@jsr/std__cli@^1.0.11",
1416
"@std/path": "npm:@jsr/std__path@^1.0.8",
17+
"@types/diff": "^7.0.0",
1518
"@types/node": "^22.10.6",
19+
"ai": "^4.1.7",
1620
"autoprefixer": "^10.4.19",
21+
"diff": "^7.0.0",
1722
"dotenv": "^16.4.7",
1823
"front-matter": "^4.0.2",
1924
"gray-matter": "^4.0.3",

scripts/ai-model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { z } from "zod";
2+
3+
export const google = async () => {
4+
const { google, createGoogleGenerativeAI } = await import("@ai-sdk/google");
5+
return createGoogleGenerativeAI({
6+
apiKey: z
7+
.string({ required_error: "required env.GOOGLE_API_KEY" })
8+
.parse(process.env.GOOGLE_API_KEY),
9+
})("gemini-1.5-pro");
10+
};
11+
12+
export const deepseek = async () => {
13+
const { createDeepSeek } = await import("@ai-sdk/deepseek");
14+
return createDeepSeek({
15+
apiKey: z
16+
.string({ required_error: "required env.OPENAI_API_KEY" })
17+
.parse(process.env.OPENAI_API_KEY),
18+
})("deepseek-chat");
19+
};

scripts/i18n-ai-core.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { OpenAI } from "openai";
2+
import { z } from "zod";
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
const system_prompt = fs.readFileSync(
6+
path.resolve(import.meta.dirname, "system-prompt.en.md"),
7+
"utf-8"
8+
);
9+
export const getOpenaiOutput = async <T>(
10+
user_content: string,
11+
schema: z.ZodType<T>
12+
) => {
13+
const openai = new OpenAI({
14+
baseURL: z
15+
.string({ required_error: "required env.AI_GATEWAY_ENDPOINT" })
16+
.parse(process.env.AI_GATEWAY_ENDPOINT),
17+
apiKey: z
18+
.string({ required_error: "required env.OPENAI_API_KEY" })
19+
.parse(process.env.OPENAI_API_KEY),
20+
});
21+
22+
const stream = await openai.chat.completions.create({
23+
messages: [
24+
{
25+
role: "system",
26+
content: system_prompt,
27+
},
28+
{
29+
role: "user",
30+
content: user_content,
31+
},
32+
],
33+
model: "deepseek-chat",
34+
response_format: {
35+
type: "json_object",
36+
},
37+
stream: true,
38+
});
39+
40+
let res = "";
41+
for await (const chunk of stream) {
42+
const part = chunk.choices[0].delta.content ?? "";
43+
res += part;
44+
process.stdout.write(part);
45+
}
46+
return schema.parse(JSON.parse(res));
47+
};
48+
import { type LanguageModelV1, streamObject } from "ai";
49+
50+
export const getAiOutput = async <T>(
51+
model: LanguageModelV1,
52+
schema: z.ZodType<T>,
53+
user_content: string
54+
) => {
55+
const stream = streamObject({
56+
model,
57+
schema,
58+
system: system_prompt,
59+
prompt: user_content,
60+
});
61+
for await (const partialObject of stream.partialObjectStream) {
62+
console.clear();
63+
console.log(partialObject);
64+
}
65+
66+
return await stream.object;
67+
};

scripts/i18n-core.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)