Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
55 lines (48 loc) · 1.7 KB
/
Copy pathmain.js
File metadata and controls
55 lines (48 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
async function translate(text, from, to, options) {
const { config, utils } = options;
const { tauriFetch: fetch } = utils;
let { apiKey, model = "deepseek-v4-flash" } = config;
// 设置默认请求路径
const requestPath = "https://api.deepseek.com/chat/completions";
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
const body = {
model: model, // 使用用户选择的模型
messages: [
{
"role": "system",
"content": "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it."
},
{
"role": "user",
"content": `Translate into ${to}:\n${text}`
}
],
temperature: 0.1,
top_p: 0.99,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 2000
}
// 新版 v4 模型默认开启思考模式,翻译场景下关闭以加快响应速度
if (model.startsWith("deepseek-v4")) {
body.thinking = { type: "disabled" };
}
let res = await fetch(requestPath, {
method: 'POST',
url: requestPath,
headers: headers,
body: {
type: "Json",
payload: body
}
});
if (res.ok) {
let result = res.data;
return result.choices[0].message.content.trim().replace(/^"|"$/g, '');
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}