mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-29 11:11:20 -08:00
feat: Enhance DeepSeek API integration with retries and timeout handling
- Updated API key environment variable from DEEPL_API_KEY to DEEPSEEK_API_KEY - Increased maximum text chunk length from 5000 to 8000 characters - Implemented request timeout and retry logic for API calls - Added handling for rate limiting responses (HTTP 429) - Improved error handling for API responses
This commit is contained in:
1 parent
562cfe18db
commit
3adff76edf
2 files changed
+2388
-1654
No files matched your search
+2329
-1628
File diff suppressed because it is too large.
Load diff
+59
-26
@@ -12,11 +12,13 @@ const rootDir = path.resolve(__dirname, '../packages/artplayer-vitepress')
|
|||||||
const dostDir = path.resolve(__dirname, '../docs')
|
const dostDir = path.resolve(__dirname, '../docs')
|
||||||
const outputFile = path.resolve(__dirname, '../docs/llms.txt')
|
const outputFile = path.resolve(__dirname, '../docs/llms.txt')
|
||||||
const API_URL = 'https://api.deepseek.com/v1/chat/completions'
|
const API_URL = 'https://api.deepseek.com/v1/chat/completions'
|
||||||
const API_KEY = process.env.DEEPL_API_KEY
|
const API_KEY = process.env.DEEPSEEK_API_KEY
|
||||||
const MAX_CONCURRENT_REQUESTS = 3 // 每类并发请求数
|
const MAX_CONCURRENT_REQUESTS = 3 // 每类并发请求数
|
||||||
|
const REQUEST_TIMEOUT = 60000 // 请求超时时间 60秒
|
||||||
|
const MAX_RETRIES = 3 // 最大重试次数
|
||||||
|
|
||||||
if (!API_KEY) {
|
if (!API_KEY) {
|
||||||
console.error('❌ Missing DEEPL_API_KEY in .env file')
|
console.error('❌ Missing DEEPSEEK_API_KEY in .env file')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +37,7 @@ function readFiles(files) {
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitText(text, maxLen = 5000) {
|
function splitText(text, maxLen = 8000) {
|
||||||
const paragraphs = text.split(/\n{2,}/)
|
const paragraphs = text.split(/\n{2,}/)
|
||||||
const chunks = []
|
const chunks = []
|
||||||
let buffer = ''
|
let buffer = ''
|
||||||
@@ -52,31 +54,62 @@ function splitText(text, maxLen = 5000) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function askDeepSeek(prompt, text, tag, id) {
|
async function askDeepSeek(prompt, text, tag, id) {
|
||||||
console.log(`🧠 [${tag}] Sending chunk ${id}`)
|
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
||||||
const res = await fetch(API_URL, {
|
try {
|
||||||
method: 'POST',
|
console.log(`🧠 [${tag}] Sending chunk ${id} (attempt ${attempt}/${MAX_RETRIES})`)
|
||||||
headers: {
|
const controller = new AbortController()
|
||||||
'Content-Type': 'application/json',
|
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
|
||||||
'Authorization': `Bearer ${API_KEY}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: 'deepseek-chat',
|
|
||||||
messages: [
|
|
||||||
{ role: 'system', content: 'You are a professional technical writer.' },
|
|
||||||
{ role: 'user', content: `${prompt}\n\n${text}` },
|
|
||||||
],
|
|
||||||
temperature: 0.3,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!res.ok) {
|
const res = await fetch(API_URL, {
|
||||||
const err = await res.text()
|
method: 'POST',
|
||||||
console.error(`[${tag}] API Error:`, err)
|
signal: controller.signal,
|
||||||
throw new Error(`DeepSeek API error: ${res.status}`)
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${API_KEY}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: 'deepseek-chat',
|
||||||
|
messages: [
|
||||||
|
{ role: 'system', content: 'You are a professional technical writer.' },
|
||||||
|
{ role: 'user', content: `${prompt}\n\n${text}` },
|
||||||
|
],
|
||||||
|
temperature: 0.3,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
clearTimeout(timeout)
|
||||||
|
|
||||||
|
// 处理限流情况
|
||||||
|
if (res.status === 429) {
|
||||||
|
const delay = Math.pow(2, attempt) * 1000
|
||||||
|
console.log(`⏳ [${tag}] Rate limited, waiting ${delay}ms...`)
|
||||||
|
await new Promise(r => setTimeout(r, delay))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = await res.text()
|
||||||
|
console.error(`[${tag}] API Error:`, err)
|
||||||
|
throw new Error(`DeepSeek API error: ${res.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await res.json()
|
||||||
|
if (!data.choices || !data.choices[0]) {
|
||||||
|
throw new Error('Invalid API response: missing choices')
|
||||||
|
}
|
||||||
|
return data.choices[0].message?.content?.trim() || ''
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err.name === 'AbortError') {
|
||||||
|
console.error(`[${tag}] Request timeout for chunk ${id}`)
|
||||||
|
}
|
||||||
|
if (attempt === MAX_RETRIES) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
const delay = 1000 * attempt
|
||||||
|
console.log(`⚠️ [${tag}] Retry ${attempt}/${MAX_RETRIES} after ${delay}ms...`)
|
||||||
|
await new Promise(r => setTimeout(r, delay))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await res.json()
|
|
||||||
return data.choices?.[0]?.message?.content?.trim() || ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runConcurrent(tasks, limit = MAX_CONCURRENT_REQUESTS) {
|
async function runConcurrent(tasks, limit = MAX_CONCURRENT_REQUESTS) {
|
||||||
|
|||||||
Reference in new issue
Block a user