forked from ponysb/91Writing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
742 lines (626 loc) · 21.9 KB
/
api.js
File metadata and controls
742 lines (626 loc) · 21.9 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
import apiConfig from '../config/api.json'
import billingService from './billing.js'
class APIService {
constructor() {
this.config = { ...apiConfig.openai }
this.proxyConfig = apiConfig.proxy
// 尝试从localStorage加载用户配置
this.loadUserConfig()
}
// 加载用户配置
loadUserConfig() {
try {
const saved = localStorage.getItem('apiConfig')
if (saved) {
const userConfig = JSON.parse(saved)
this.config = { ...this.config, ...userConfig }
}
} catch (error) {
console.error('加载用户API配置失败:', error)
}
}
// 获取API配置
getConfig() {
return this.config
}
// 更新API配置
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig }
// 保存到localStorage
try {
localStorage.setItem('apiConfig', JSON.stringify(this.config))
} catch (error) {
console.error('保存API配置失败:', error)
}
}
// 构建请求URL
buildURL(endpoint) {
return `${this.config.baseURL}${endpoint}`
}
// 构建请求头
buildHeaders() {
return {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.config.apiKey}`
}
}
// 通用API请求方法
async makeRequest(endpoint, options = {}) {
const url = this.buildURL(endpoint)
const headers = this.buildHeaders()
const requestOptions = {
method: 'POST',
headers,
...options
}
try {
const response = await fetch(url, requestOptions)
if (!response.ok) {
const errorData = await response.json()
throw new Error(`API请求失败: ${response.status} - ${errorData.error?.message || '未知错误'}`)
}
return await response.json()
} catch (error) {
console.error('API请求错误:', error)
throw error
}
}
// 生成文本内容
async generateText(prompt, options = {}) {
const model = options.model || this.config.selectedModel || this.config.defaultModel || 'gpt-3.5-turbo'
// 估算输入token数量(用于记录,无需检查余额)
const estimatedInputTokens = billingService.estimateTokens(prompt)
const requestBody = {
model: model,
messages: [
{
role: 'user',
content: prompt
}
],
max_tokens: options.maxTokens || this.config.maxTokens,
temperature: options.temperature || this.config.temperature,
stream: false
}
try {
const response = await this.makeRequest('/chat/completions', {
body: JSON.stringify(requestBody)
})
const content = response.choices[0]?.message?.content || ''
const usage = response.usage
// 记录实际的token使用情况
if (usage) {
billingService.recordAPICall({
type: options.type || 'generation',
model: model,
content: prompt,
response: content,
inputTokens: usage.prompt_tokens || 0,
outputTokens: usage.completion_tokens || 0,
status: 'success'
})
} else {
// 如果API没有返回usage信息,使用估算值
const outputTokens = billingService.estimateTokens(content)
billingService.recordAPICall({
type: options.type || 'generation',
model: model,
content: prompt,
response: content,
inputTokens: estimatedInputTokens,
outputTokens: outputTokens,
status: 'success'
})
}
return content
} catch (error) {
// 记录失败的API调用
billingService.recordAPICall({
type: options.type || 'generation',
model: model,
content: prompt,
response: '',
inputTokens: estimatedInputTokens,
outputTokens: 0,
status: 'failed'
})
throw error
}
}
// 流式生成文本内容
async generateTextStream(prompt, options = {}, onChunk = null) {
console.log('开始流式生成,prompt:', prompt.substring(0, 100) + '...') // 调试日志
const model = options.model || this.config.selectedModel || this.config.defaultModel || 'gpt-3.5-turbo'
// 估算输入token数量(用于记录,无需检查余额)
const estimatedInputTokens = billingService.estimateTokens(prompt)
const requestBody = {
model: model,
messages: [
{
role: 'user',
content: prompt
}
],
max_tokens: options.maxTokens || this.config.maxTokens,
temperature: options.temperature || this.config.temperature,
stream: true
}
console.log('请求体:', requestBody) // 调试日志
const url = this.buildURL('/chat/completions')
const headers = this.buildHeaders()
let fullContent = ''
let hasError = false
try {
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(requestBody)
})
console.log('API响应状态:', response.status) // 调试日志
if (!response.ok) {
const errorData = await response.json()
hasError = true
throw new Error(`API请求失败: ${response.status} - ${errorData.error?.message || '未知错误'}`)
}
const reader = response.body.getReader()
const decoder = new TextDecoder()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
const chunk = decoder.decode(value, { stream: true })
const lines = chunk.split('\n')
for (const line of lines) {
const trimmedLine = line.trim()
if (trimmedLine.startsWith('data: ')) {
const data = trimmedLine.slice(6).trim()
if (data === '[DONE]') {
console.log('流式生成完成,总内容长度:', fullContent.length) // 调试日志
break
}
// 跳过空数据
if (!data || data === '') {
continue
}
try {
const parsed = JSON.parse(data)
const content = parsed.choices?.[0]?.delta?.content || ''
if (content) {
fullContent += content
console.log('接收到内容片段:', content) // 调试日志
if (onChunk) {
onChunk(content, fullContent)
}
}
} catch (e) {
console.log('解析数据失败,原始数据:', JSON.stringify(data), '错误:', e.message) // 调试日志
// 继续处理其他数据,不中断流式处理
}
}
}
}
} catch (streamError) {
console.error('流式读取错误:', streamError)
hasError = true
throw streamError
} finally {
reader.releaseLock()
}
// 流式生成成功,记录token使用
const outputTokens = billingService.estimateTokens(fullContent)
billingService.recordAPICall({
type: options.type || 'generation',
model: model,
content: prompt,
response: fullContent,
inputTokens: estimatedInputTokens,
outputTokens: outputTokens,
status: 'success'
})
return fullContent
} catch (error) {
// 只有在发生错误时才记录失败调用
if (hasError) {
billingService.recordAPICall({
type: options.type || 'generation',
model: model,
content: prompt,
response: fullContent,
inputTokens: estimatedInputTokens,
outputTokens: billingService.estimateTokens(fullContent),
status: 'failed'
})
}
throw error
}
}
// 生成小说大纲
async generateOutline(theme, keywords, template) {
const templateInfo = template ? `\n参考模板:${template.name} - ${template.description}` : ''
const keywordList = keywords ? `\n关键词:${keywords}` : ''
const prompt = `请为以下主题生成一个详细的小说大纲:
主题:${theme}${templateInfo}${keywordList}
要求:
1. 生成5-8个章节
2. 每个章节用 ### 开头,后跟章节标题
3. 每个章节下面写2-3句话描述该章节的主要内容
4. 整体结构要完整,有开头、发展、高潮、结局
5. 符合所选模板的风格特点
请直接输出大纲内容:`
return await this.generateText(prompt)
}
// 流式生成小说大纲
async generateOutlineStream(theme, keywords, template, onChunk = null) {
const templateInfo = template ? `\n参考模板:${template.name} - ${template.description}` : ''
const keywordList = keywords ? `\n关键词:${keywords}` : ''
const prompt = `请为以下主题生成一个详细的小说大纲:
主题:${theme}${templateInfo}${keywordList}
要求:
1. 生成5-8个章节
2. 每个章节用 ### 开头,后跟章节标题
3. 每个章节下面写2-3句话描述该章节的主要内容
4. 整体结构要完整,有开头、发展、高潮、结局
5. 符合所选模板的风格特点
请直接输出大纲内容:`
return await this.generateTextStream(prompt, {}, onChunk)
}
// 生成章节内容
async generateChapterContent(chapterTitle, chapterOutline, previousContent = '', template = null, characters = [], worldSettings = []) {
const templateInfo = template ? `\n写作风格:${template.style}\n写作提示:${template.writingTips}` : ''
const contextInfo = previousContent ? `\n前文内容参考:${previousContent.slice(-500)}` : ''
// 构建人物信息
let charactersInfo = ''
if (characters.length > 0) {
charactersInfo = '\n\n人物设定:'
characters.forEach(char => {
charactersInfo += `\n- ${char.name}:${char.description}`
if (char.traits && char.traits.length > 0) {
charactersInfo += ` (特点:${char.traits.join('、')})`
}
})
}
// 构建世界观信息
let worldInfo = ''
if (worldSettings.length > 0) {
worldInfo = '\n\n世界观设定:'
worldSettings.forEach(setting => {
worldInfo += `\n- ${setting.title}:${setting.description}`
})
}
const prompt = `请根据以下信息生成小说章节内容:
章节标题:${chapterTitle}
章节大纲:${chapterOutline}${templateInfo}${contextInfo}${charactersInfo}${worldInfo}
要求:
1. 字数控制在800-1200字
2. 内容要生动有趣,符合章节大纲
3. 语言流畅,描写细腻
4. 如果有前文内容,要保持连贯性
5. 符合所选模板的风格特点
6. 充分利用提供的人物设定和世界观设定
7. 确保人物行为符合其性格特点
8. 场景描写要符合世界观设定
请直接输出章节内容:`
return await this.generateText(prompt)
}
// 流式生成章节内容
async generateChapterContentStream(chapterTitle, chapterOutline, previousContent = '', template = null, characters = [], worldSettings = [], onChunk = null) {
const templateInfo = template ? `\n写作风格:${template.style}\n写作提示:${template.writingTips}` : ''
const contextInfo = previousContent ? `\n前文内容参考:${previousContent.slice(-500)}` : ''
// 构建人物信息
let charactersInfo = ''
if (characters.length > 0) {
charactersInfo = '\n\n人物设定:'
characters.forEach(char => {
charactersInfo += `\n- ${char.name}:${char.description}`
if (char.traits && char.traits.length > 0) {
charactersInfo += ` (特点:${char.traits.join('、')})`
}
})
}
// 构建世界观信息
let worldInfo = ''
if (worldSettings.length > 0) {
worldInfo = '\n\n世界观设定:'
worldSettings.forEach(setting => {
worldInfo += `\n- ${setting.title}:${setting.description}`
})
}
const prompt = `请根据以下信息生成小说章节内容:
章节标题:${chapterTitle}
章节大纲:${chapterOutline}${templateInfo}${contextInfo}${charactersInfo}${worldInfo}
要求:
1. 字数控制在800-1200字
2. 内容要生动有趣,符合章节大纲
3. 语言流畅,描写细腻
4. 如果有前文内容,要保持连贯性
5. 符合所选模板的风格特点
6. 充分利用提供的人物设定和世界观设定
7. 确保人物行为符合其性格特点
8. 场景描写要符合世界观设定
请直接输出章节内容:`
return await this.generateTextStream(prompt, {}, onChunk)
}
// AI对话功能
async chatWithAI(message, chatHistory = []) {
const messages = [
{
role: 'system',
content: '你是一个专业的小说写作助手,擅长帮助用户进行创意写作、情节构思、人物塑造等。请用友好、专业的语气回答用户的问题。'
},
...chatHistory.map(msg => ({
role: msg.isUser ? 'user' : 'assistant',
content: msg.content
})),
{
role: 'user',
content: message
}
]
const requestBody = {
model: this.config.selectedModel || this.config.defaultModel || 'gpt-3.5-turbo',
messages,
max_tokens: this.config.maxTokens,
temperature: 0.7
}
const response = await this.makeRequest('/chat/completions', {
body: JSON.stringify(requestBody)
})
return response.choices[0]?.message?.content || ''
}
// 生成文章摘要
async generateSummary(content, options = {}) {
const { length = 'medium', type = 'keypoints' } = options
let lengthInstruction = ''
switch (length) {
case 'short':
lengthInstruction = '请生成50-100字的简短摘要'
break
case 'medium':
lengthInstruction = '请生成100-200字的中等长度摘要'
break
case 'long':
lengthInstruction = '请生成200-300字的详细摘要'
break
}
let typeInstruction = ''
switch (type) {
case 'keypoints':
typeInstruction = '重点提取文章的关键要点和核心内容'
break
case 'plot':
typeInstruction = '重点概括故事情节和主要事件'
break
case 'character':
typeInstruction = '重点分析人物特点和关系'
break
case 'theme':
typeInstruction = '重点阐述文章的主题思想和深层含义'
break
}
const prompt = `${lengthInstruction},${typeInstruction}。\n\n文章内容:\n${content}`
return await this.generateText(prompt, {
maxTokens: 400,
temperature: 0.3
})
}
// 内容优化建议
async getWritingAdvice(content) {
const prompt = `请对以下文章内容提供写作建议:
${content}
请从以下几个方面给出具体建议:
1. 语言表达
2. 情节结构
3. 人物塑造
4. 描写技巧
5. 整体改进方向
建议:`
return await this.generateText(prompt, { maxTokens: 500 })
}
// 根据语料库生成个性化内容
async generatePersonalizedContent(prompt, corpus) {
const corpusText = corpus.map(item => item.content).join('\n\n')
const personalizedPrompt = `参考以下写作风格和内容:
${corpusText}
现在请根据上述风格,生成以下内容:
${prompt}
要求:
1. 保持与参考内容相似的写作风格
2. 语言表达要一致
3. 内容要原创且符合要求
生成内容:`
return await this.generateText(personalizedPrompt)
}
// 生成通用内容
async generateGeneralContent(keywords, template, outline, wordLimit = 500) {
const templateInfo = template ? `\n写作风格:${template.style}\n写作提示:${template.writingTips}` : ''
const outlineInfo = outline ? `\n参考大纲:${outline}` : ''
const keywordList = keywords ? `\n关键词:${keywords}` : ''
const prompt = `请根据以下信息生成小说内容:${keywordList}${templateInfo}${outlineInfo}
要求:
1. 字数控制在${wordLimit}字左右
2. 内容要生动有趣,情节引人入胜
3. 语言流畅,描写细腻
4. 符合所选模板的风格特点
5. 如果有大纲,要与大纲保持一致
请直接输出小说内容:`
return await this.generateText(prompt)
}
// 流式生成通用内容
async generateGeneralContentStream(keywords, template, outline, wordLimit = 500, onChunk = null) {
const templateInfo = template ? `\n写作风格:${template.style}\n写作提示:${template.writingTips}` : ''
const outlineInfo = outline ? `\n参考大纲:${outline}` : ''
const keywordList = keywords ? `\n关键词:${keywords}` : ''
const prompt = `请根据以下信息生成小说内容:${keywordList}${templateInfo}${outlineInfo}
要求:
1. 字数控制在${wordLimit}字左右
2. 内容要生动有趣,情节引人入胜
3. 语言流畅,描写细腻
4. 符合所选模板的风格特点
5. 如果有大纲,要与大纲保持一致
请直接输出小说内容:`
return await this.generateTextStream(prompt, {}, onChunk)
}
// 获取可用模型列表
getAvailableModels() {
return this.config.models
}
// 验证API密钥
async validateAPIKey() {
try {
const url = this.buildURL('/models')
const headers = this.buildHeaders()
const response = await fetch(url, {
method: 'GET',
headers
})
return response.ok
} catch (error) {
console.error('API密钥验证失败:', error)
return false
}
}
// AI生成人物
async generateCharacter(theme, characterType = '') {
const typeInfo = characterType ? `角色类型:${characterType}` : ''
const prompt = `请根据主题"${theme}"生成一个小说人物,${typeInfo}
要求:
1. 提供人物的基本信息(姓名、年龄、职业等)
2. 详细的外貌描述
3. 性格特点和行为习惯
4. 背景故事和经历
5. 人物的特殊技能或能力
6. 与主题相关的特征
请以JSON格式返回:
{
"name": "人物姓名",
"age": "年龄",
"occupation": "职业",
"appearance": "外貌描述",
"personality": "性格特点",
"background": "背景故事",
"skills": ["技能1", "技能2"],
"traits": ["特征1", "特征2", "特征3"]
}`
try {
const response = await this.generateText(prompt)
return JSON.parse(response)
} catch (error) {
console.error('生成人物失败:', error)
throw error
}
}
// AI生成世界观设定
async generateWorldSetting(theme, settingType = '') {
const typeInfo = settingType ? `设定类型:${settingType}` : ''
const prompt = `请根据主题"${theme}"生成一个小说世界观设定,${typeInfo}
要求:
1. 设定的名称和概述
2. 详细的背景描述
3. 重要的规则或法则
4. 地理环境或空间结构
5. 历史背景或重要事件
6. 与主题相关的特色元素
请以JSON格式返回:
{
"title": "设定名称",
"overview": "概述",
"description": "详细描述",
"rules": ["规则1", "规则2"],
"geography": "地理环境",
"history": "历史背景",
"features": ["特色1", "特色2"]
}`
try {
const response = await this.generateText(prompt)
return JSON.parse(response)
} catch (error) {
console.error('生成世界观设定失败:', error)
throw error
}
}
// AI文章分析
async analyzeArticle(content) {
try {
const prompt = `请对以下文章进行深度分析,并以JSON格式返回分析结果:
文章内容:
${content}
请分析以下方面:
1. 情感倾向(积极/消极/中性)
2. 文章标签(最多5个关键标签)
3. 文章分类(玄幻/都市/悬疑/科幻/历史/校园/武侠/其他)
4. 文章评分(0-100分,考虑文笔、情节、结构等)
5. 详细评价(包括优点、缺点、改进建议)
返回格式:
{
"sentiment": "积极/消极/中性",
"tags": ["标签1", "标签2", "标签3"],
"category": "分类",
"score": 85,
"evaluation": {
"strengths": ["优点1", "优点2"],
"weaknesses": ["缺点1", "缺点2"],
"suggestions": ["建议1", "建议2"]
},
"summary": "整体评价总结"
}`
const requestBody = {
model: this.config.model,
messages: [
{
role: 'system',
content: '你是一位专业的文学评论家和编辑,擅长分析各种类型的文章。请客观、专业地分析文章,给出建设性的评价和建议。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 1000,
temperature: 0.3
}
console.log('发送文章分析请求:', requestBody)
const url = this.buildURL('/chat/completions')
const headers = this.buildHeaders()
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(requestBody)
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
console.log('文章分析响应:', data)
if (data.choices && data.choices[0] && data.choices[0].message) {
const analysisText = data.choices[0].message.content.trim()
try {
// 尝试解析JSON响应
const analysis = JSON.parse(analysisText)
console.log('解析的分析结果:', analysis)
return analysis
} catch (parseError) {
console.error('解析AI分析结果失败:', parseError)
// 如果解析失败,返回基础分析结果
return {
sentiment: '中性',
tags: ['AI分析'],
category: '其他',
score: 70,
evaluation: {
strengths: ['内容完整'],
weaknesses: ['AI分析解析失败'],
suggestions: ['请检查内容格式']
},
summary: 'AI分析暂时不可用,使用基础分析结果'
}
}
} else {
throw new Error('AI响应格式错误')
}
} catch (error) {
console.error('文章分析失败:', error)
throw error
}
}
}
export default new APIService()