mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-27 09:41:20 -08:00
fix: update translation API and increase character limit for requests
This commit is contained in:
1 parent
674c6420e9
commit
a64e13c9c1
1 file changed
+15
-9
+15
-9
@@ -8,12 +8,12 @@ import dotenv from 'dotenv';
|
|||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
// 配置常量
|
// 配置常量
|
||||||
const MAX_CHARS_PER_REQUEST = 512;
|
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 3;
|
||||||
const RETRY_DELAY = 1000; // 1秒
|
const RETRY_DELAY = 1000; // 1秒
|
||||||
|
const MAX_CHARS_PER_REQUEST = 128000;
|
||||||
const BASE_PATH = 'packages/artplayer-vitepress/docs';
|
const BASE_PATH = 'packages/artplayer-vitepress/docs';
|
||||||
const EN_PATH = 'packages/artplayer-vitepress/docs/en';
|
const EN_PATH = 'packages/artplayer-vitepress/docs/en';
|
||||||
const COPY_DIRS = ['advanced', 'component', 'library', 'plugin', 'start'];
|
const COPY_DIRS = ['advanced', 'component', 'plugin', 'start'];
|
||||||
|
|
||||||
function splitMarkdown(text, maxChars) {
|
function splitMarkdown(text, maxChars) {
|
||||||
const parts = [];
|
const parts = [];
|
||||||
@@ -80,19 +80,25 @@ async function fetchWithRetry(url, options, retries = MAX_RETRIES) {
|
|||||||
|
|
||||||
const translateContent = async (content, targetLanguage) => {
|
const translateContent = async (content, targetLanguage) => {
|
||||||
try {
|
try {
|
||||||
const TRANSLATE_URL = process.env.TRANSLATE_URL;
|
const TRANSLATE_URL = 'https://api.deepseek.com/chat/completions';
|
||||||
if (!TRANSLATE_URL) {
|
|
||||||
throw new Error('TRANSLATE_URL is not defined in environment variables');
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetchWithRetry(TRANSLATE_URL, {
|
const response = await fetchWithRetry(TRANSLATE_URL, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${process.env.DEEPL_API_KEY}`,
|
||||||
|
},
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
content: `The following text is written in VitePress's extended Markdown syntax, Please translate it into ${targetLanguage}, and keep the Markdown format and don't add your explanation:\n\n${content}`,
|
stream: false,
|
||||||
|
model: 'deepseek-chat',
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: `The following text is written in VitePress's extended Markdown syntax, Please translate it into ${targetLanguage}, and keep the Markdown format and don't add your explanation:\n\n${content}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
return response.data;
|
return response.choices[0].message.content;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Translation failed: ${error.message}`);
|
console.error(`Translation failed: ${error.message}`);
|
||||||
return content; // 返回原文作为回退
|
return content; // 返回原文作为回退
|
||||||
|
|||||||
Reference in new issue
Block a user