Skip to content

Commit

Permalink
ci: implement retry logic and batch processing for translation
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia committed Dec 31, 2024
1 parent 067b9d7 commit 52205fb
Showing 1 changed file with 46 additions and 25 deletions.
71 changes: 46 additions & 25 deletions bin/doc-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,64 @@ import path from 'path';

const endpoint = "https://api.deepseek.com";
const token = process.env["DEEPSEEK_API_KEY"];
const MAX_CONCURRENT = 5; // 最大并发数
const MAX_RETRIES = 3; // 最大重试次数

const openai = new OpenAI({
baseURL: endpoint,
apiKey: token,
});

async function translateContent(content) {
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a professional translator. Translate the following Chinese markdown content to English. Keep all markdown formatting intact." },
{ role: "user", content: content }
],
model: "deepseek-chat",
});
return completion.choices[0].message.content;
async function translateWithRetry(content, retries = 0) {
try {
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a professional translator. Translate the following Chinese markdown content to English. Keep all markdown formatting intact." },
{ role: "user", content: content }
],
model: "deepseek-chat",
});
return completion.choices[0].message.content;
} catch (error) {
if (retries < MAX_RETRIES) {
await new Promise(resolve => setTimeout(resolve, 1000 * (retries + 1)));
return translateWithRetry(content, retries + 1);
}
throw error;
}
}

async function processFile(srcPath, destPath) {
const destFolder = path.dirname(destPath);
await mkdir(destFolder, { recursive: true });

const content = await readFile(srcPath, 'utf8');
const translatedContent = await translateWithRetry(content);
const finalContent = translatedContent.replace(/\/zh-cn\//g, '/en/');
await writeFile(destPath, finalContent);
console.log(`Translated: ${path.basename(srcPath)}`);
}

async function translateFiles(srcDir, destDir) {
try {
const files = await readdir(srcDir, { recursive: true });

for (const file of files) {
if (!file.endsWith('.md')) continue;

const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
const destFolder = path.dirname(destPath);

await mkdir(destFolder, { recursive: true });

const content = await readFile(srcPath, 'utf8');
const translatedContent = await translateContent(content);
const finalContent = translatedContent.replace(/\/zh-cn\//g, '/en/');
await writeFile(destPath, finalContent);

console.log(`Translated: ${file}`);
const mdFiles = files.filter(file => file.endsWith('.md'));

// 将文件分批处理
for (let i = 0; i < mdFiles.length; i += MAX_CONCURRENT) {
const batch = mdFiles.slice(i, i + MAX_CONCURRENT);
const promises = batch.map(file => {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
return processFile(srcPath, destPath).catch(error => {
console.error(`Error translating ${file}:`, error);
});
});

await Promise.all(promises);
}

console.log('All translations completed!');
} catch (error) {
console.error('Translation error:', error);
}
Expand Down

0 comments on commit 52205fb

Please sign in to comment.