feat: 范式分析结果改为流式输出
- 修改API analyzeContent方法使用fetch实现流式请求 - 更新AnalysisPanel实时处理流式响应 - 分析过程中实时显示结果,提升用户体验
This commit is contained in:
@@ -39,27 +39,34 @@ class DeepSeekAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async analyzeContent(text) {
|
async analyzeContent(text) {
|
||||||
const response = await this.client.post('', {
|
const response = await fetch(this.baseURL, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${this.apiKey}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
model: 'deepseek-chat',
|
model: 'deepseek-chat',
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: '你是一个专业的写作分析师,擅长分析文章的写作范式、结构和特点。请从以下几个方面分析文章:1. 主要写作范式类型 2. 文章结构特点 3. 语言风格特征 4. 目标读者群体 5. 写作技巧总结。请用简洁明了的语言回答。'
|
content: '你是一个专业的写作分析师,擅长分析文章的写作范式、结构和特点。请从以下几个方面分析文章:1. 主要写作范式类型 2. 文章结构特点 3. 语言风格特征 4. 目标读者群体 5. 写作技巧总结。请用简洁明了的语言回答,使用Markdown格式。'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `请分析以下文章的写作范式:\n\n${text}`
|
content: `请分析以下文章的写作范式:\n\n${text}`
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
stream: false,
|
stream: true,
|
||||||
temperature: 0.3
|
temperature: 0.3
|
||||||
}, {
|
})
|
||||||
headers: {
|
|
||||||
'Authorization': `Bearer ${this.apiKey}`
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return response.data
|
if (!response.ok) {
|
||||||
|
throw new Error(`API请求失败: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -209,7 +209,12 @@ const analyzeArticle = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isAnalyzing.value = true
|
isAnalyzing.value = true
|
||||||
appStore.analysisResult = null
|
appStore.analysisResult = {
|
||||||
|
paradigm: '分析中...',
|
||||||
|
paradigmType: null,
|
||||||
|
analysis: '',
|
||||||
|
timestamp: new Date()
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const api = new DeepSeekAPI({
|
const api = new DeepSeekAPI({
|
||||||
@@ -217,21 +222,55 @@ const analyzeArticle = async () => {
|
|||||||
key: appStore.apiKey
|
key: appStore.apiKey
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = await api.analyzeContent(analysisText.value)
|
const response = await api.analyzeContent(analysisText.value)
|
||||||
|
const reader = response.body.getReader()
|
||||||
|
const decoder = new TextDecoder()
|
||||||
|
|
||||||
// 解析分析结果,提取范式类型
|
let fullContent = ''
|
||||||
const analysisContent = result.choices[0].message.content
|
|
||||||
const detectedParadigm = detectParadigm(analysisContent)
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read()
|
||||||
|
if (done) break
|
||||||
|
|
||||||
|
const chunk = decoder.decode(value)
|
||||||
|
const lines = chunk.split('\n').filter(line => line.trim())
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
if (line.startsWith('data: ')) {
|
||||||
|
const data = line.slice(6)
|
||||||
|
if (data === '[DONE]') continue
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(data)
|
||||||
|
const content = parsed.choices?.[0]?.delta?.content || ''
|
||||||
|
if (content) {
|
||||||
|
fullContent += content
|
||||||
|
// 实时更新分析结果
|
||||||
|
appStore.analysisResult = {
|
||||||
|
paradigm: '分析中...',
|
||||||
|
paradigmType: null,
|
||||||
|
analysis: fullContent,
|
||||||
|
timestamp: new Date()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// 忽略解析错误
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 分析完成后检测范式类型
|
||||||
|
const detectedParadigm = detectParadigm(fullContent)
|
||||||
appStore.analysisResult = {
|
appStore.analysisResult = {
|
||||||
paradigm: detectedParadigm.name,
|
paradigm: detectedParadigm.name,
|
||||||
paradigmType: detectedParadigm.type,
|
paradigmType: detectedParadigm.type,
|
||||||
analysis: analysisContent,
|
analysis: fullContent,
|
||||||
timestamp: new Date()
|
timestamp: new Date()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加到历史记录
|
// 添加到历史记录
|
||||||
addToHistory(detectedParadigm.name, analysisText.value, analysisContent)
|
addToHistory(detectedParadigm.name, analysisText.value, fullContent)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
appStore.analysisResult = {
|
appStore.analysisResult = {
|
||||||
error: true,
|
error: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user