diff --git a/package-lock.json b/package-lock.json index ae77d11..1a608ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "laitool", - "version": "3.0.1-preview.7", + "version": "3.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "laitool", - "version": "3.0.1-preview.7", + "version": "3.0.2", "hasInstallScript": true, "dependencies": { "@alicloud/alimt20181012": "^1.2.0", diff --git a/package.json b/package.json index e72d627..d842f46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "laitool", - "version": "3.0.1-preview.7", + "version": "3.0.2", "description": "An AI tool for image processing, video processing, and other functions.", "main": "./out/main/index.js", "author": "laitool.cn", diff --git a/resources/scripts/db/book.realm.lock b/resources/scripts/db/book.realm.lock index 014b7e8..ce583d0 100644 Binary files a/resources/scripts/db/book.realm.lock and b/resources/scripts/db/book.realm.lock differ diff --git a/resources/scripts/db/software.realm.lock b/resources/scripts/db/software.realm.lock index 651e455..ae99976 100644 Binary files a/resources/scripts/db/software.realm.lock and b/resources/scripts/db/software.realm.lock differ diff --git a/src/define/Tools/common.ts b/src/define/Tools/common.ts index af35846..10e6639 100644 --- a/src/define/Tools/common.ts +++ b/src/define/Tools/common.ts @@ -9,3 +9,31 @@ export function ContainsChineseOrPunctuation(str: string): boolean { str ) } + +/** + * 通用的重试函数 + * @param fn 要执行的函数 + * @param retries 最大重试次数 + * @param delay 每次重试之间的延迟(毫秒) + * @returns 返回函数的结果 + */ +export async function RetryWithBackoff(fn: () => Promise, retries: number = 5, delay: number = 2000): Promise { + let attempts = 0; + while (attempts < retries) { + try { + return await fn(); + } catch (error) { + attempts++; + // 这边记下日志吧 + global.logger.error( + fn.name + '_RetryWithBackoff', + `第 ${attempts} 请求失败,开始下一次重试,失败信息如下:` + error.toString() + ) + if (attempts >= retries) { + throw new Error(`失败次数超过 ${retries} 错误信息如下: ${error.message}`); + } + await new Promise(resolve => setTimeout(resolve, delay)); + } + } + throw new Error('所有重试失败'); // 理论上不会到达这里 +} diff --git a/src/main/IPCEvent/bookIpc.ts b/src/main/IPCEvent/bookIpc.ts index 9147d9d..e5bae53 100644 --- a/src/main/IPCEvent/bookIpc.ts +++ b/src/main/IPCEvent/bookIpc.ts @@ -120,7 +120,7 @@ export function BookIpc() { // 开始执行获取小说文案的方法 ipcMain.handle( DEFINE_STRING.BOOK.GET_COPYWRITING, - async (event, bookId, bookTaskId, operateBookType) => await subtitleService.GetCopywriting(bookId, bookTaskId, operateBookType) + async (event, bookId, bookTaskId, operateBookType, coverData) => await subtitleService.GetCopywriting(bookId, bookTaskId, operateBookType, coverData) ) // 获取小说的文案数据,然后保存到对应的文件中 diff --git a/src/main/Original/MJOriginalImageGenerate.js b/src/main/Original/MJOriginalImageGenerate.js index bfd9115..f9c8a56 100644 --- a/src/main/Original/MJOriginalImageGenerate.js +++ b/src/main/Original/MJOriginalImageGenerate.js @@ -161,13 +161,13 @@ export class MJOriginalImageGenerate { * 初始化MJ设置 */ async InitMjSetting() { - let mjSetting_res = await ImageSetting.GetDefineConfigJsonByProperty( - JSON.stringify(['img_base', 'mj_config', false, null]) - ) - if (mjSetting_res.code == 0 || !mjSetting_res.data) { - throw new Error('请先添加MJ配置') + // 获取MJ配置,从数据库中 + let _mjSettingService = await MJSettingService.getInstance() + let mjSettings = _mjSettingService.GetMJSettingTreeData() + if (mjSettings.code == 0) { + throw new Error(mjSettings.message) } - let mjSetting = mjSetting_res.data + let mjSetting = mjSettings.data return mjSetting } @@ -247,6 +247,7 @@ export class MJOriginalImageGenerate { let result = [] // 浏览器生图模式 if (request_model == 'browser_mj') { + throw new Error('该模式不支持获取已经生图完成的数据,并获取图片') let param = [] // 循环数据,直传需要的数据 for (let i = 0; i < value.length; i++) { @@ -285,18 +286,18 @@ export class MJOriginalImageGenerate { // 请求 for (let i = 0; i < value.length; i++) { const element = value[i] - if (element.mj_message.progress == 100) { - continue - } - if (element.mj_message.progress.status == 'success') { - continue - } + // if (element.mj_message.progress == 100) { + // continue + // } + // if (element.mj_message.progress.status == 'success') { + // continue + // } let task_res = await this.discordAPI.GetMJAPITaskByID( element.mj_message.message_id, once_get_task, mjSetting.apiSetting.apiKey - ) + ) // 这边价格判断,是不是真的结束了 if (task_res.code == 0) { task_res['id'] = element.id task_res['mj_api_url'] = mjSetting.apiSetting.mjApiUrl @@ -306,7 +307,6 @@ export class MJOriginalImageGenerate { if (task_res.progress != 100) { continue } - result.push({ id: element.id, image_id: null, @@ -314,6 +314,8 @@ export class MJOriginalImageGenerate { name: element.name }) } + } else { + throw new Error('未知的生图模式,请检查配置') } let res = [] diff --git a/src/main/Public/GPT.js b/src/main/Public/GPT.js index 912e508..5717075 100644 --- a/src/main/Public/GPT.js +++ b/src/main/Public/GPT.js @@ -1,470 +1,526 @@ -import axios from "axios"; -import path from "path"; -import { DEFINE_STRING } from "../../define/define_string"; -import { define } from "../../define/define"; -let fspromises = require("fs").promises; -import { gptDefine } from "../../define/gptDefine"; -import { apiUrl } from "../../define/api/apiUrlDefine"; -import { successMessage } from "../Public/generalTools"; +import axios from 'axios' +import path from 'path' +import { DEFINE_STRING } from '../../define/define_string' +import { define } from '../../define/define' +let fspromises = require('fs').promises +import { gptDefine } from '../../define/gptDefine' +import { apiUrl } from '../../define/api/apiUrlDefine' +import { successMessage } from '../Public/generalTools' +import { RetryWithBackoff } from '../../define/Tools/common' export class GPT { - constructor(global) { - this.global = global; + constructor(global) { + this.global = global + } + + /** + * 输出测试案例 + * @param {*} value 传入的值(整个数据) + */ + async GenerateGptExampleOut(value) { + try { + let data = JSON.parse(value) + let message = gptDefine.CustomizeGptPrompt(data) + let content = await RetryWithBackoff( + async () => { + return await this.FetchGpt(message) + }, + 5, + 2000 + ) + console.log(content) + return { + code: 1, + data: content + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } + } - - /** - * 输出测试案例 - * @param {*} value 传入的值(整个数据) - */ - async GenerateGptExampleOut(value) { - try { - - let data = JSON.parse(value); - let message = gptDefine.CustomizeGptPrompt(data); - let content = await this.FetchGpt(message); - console.log(content); - return { - code: 1, - data: content - } - - } catch (error) { - return { - code: 0, - message: error.toString() - } + /** + * GPT推理提示词的方法 + * @param {*} element 当前推理的句子 + * @param {*} gpt_count 设置的GPT上下文理解数量 + * @param {*} auto_analyze_character 当前的角色数据 + * @returns + */ + async GPTPromptGenerate(element, gpt_count, auto_analyze_character) { + try { + // 获取当前的推理模式 + let gpt_auto_inference = this.global.config.gpt_auto_inference + let message = null + if (gpt_auto_inference == 'customize') { + // 自定义模式 + // 获取当前自定义的推理提示词 + let customize_gpt_prompt = ( + await gptDefine.getGptDataByTypeAndProperty('dynamic', 'customize_gpt_prompt', []) + ).data + let index = customize_gpt_prompt.findIndex( + (item) => item.id == this.global.config.customize_gpt_prompt + ) + if (this.global.config.customize_gpt_prompt && index < 0) { + throw new Error('自定义推理默认要选择对应的自定义推理词') } - } - - /** - * GPT推理提示词的方法 - * @param {*} element 当前推理的句子 - * @param {*} gpt_count 设置的GPT上下文理解数量 - * @param {*} auto_analyze_character 当前的角色数据 - * @returns - */ - async GPTPromptGenerate(element, gpt_count, auto_analyze_character) { - try { - // 获取当前的推理模式 - let gpt_auto_inference = this.global.config.gpt_auto_inference; - let message = null; - if (gpt_auto_inference == "customize") { - // 自定义模式 - // 获取当前自定义的推理提示词 - let customize_gpt_prompt = (await gptDefine.getGptDataByTypeAndProperty("dynamic", "customize_gpt_prompt", [])).data; - let index = customize_gpt_prompt.findIndex(item => item.id == this.global.config.customize_gpt_prompt); - if (this.global.config.customize_gpt_prompt && index < 0) { - throw new Error("自定义推理默认要选择对应的自定义推理词"); - } - message = gptDefine.CustomizeGptPrompt(customize_gpt_prompt[index], element.after_gpt); - message.push({ - "role": "user", - "content": element.after_gpt - }) - } else { - // 内置模式 - // 获取 - let prefix_word = ""; - // 拼接一个word - let i = element.no - 1; - if (i <= gpt_count) { - prefix_word = this.all_data.filter((item, index) => index < i).map(item => item.after_gpt).join('\r\n'); - } else if (i > gpt_count) { - prefix_word = this.all_data.filter((item, index) => i - index <= gpt_count && i - index > 0).map(item => item.after_gpt).join('\r\n'); - } - - let suffix_word = ""; - let o_i = this.all_data.length - i; - if (o_i <= gpt_count) { - suffix_word = this.all_data.filter((item, index) => index > i).map(item => item.after_gpt).join('\r\n'); - } else if (o_i > gpt_count) { - suffix_word = this.all_data.filter((item, index) => index - i <= gpt_count && index - i > 0).map(item => item.after_gpt).join('\r\n'); - } - - let word = `${prefix_word}\r\n${element.after_gpt}\r\n${suffix_word}`; - let single_word = element.after_gpt; - - // 判断当前的格式 - if (["superSinglePrompt", 'onlyPromptMJ'].includes(this.global.config.gpt_auto_inference)) { - // 有返回案例的 - message = gptDefine.GetExamplePromptMessage(this.global.config.gpt_auto_inference); - // 加当前提问的 - message.push({ - "role": "user", - "content": single_word - }) - - } else { - // 直接返回,没有案例的 - message = [ - { - "role": "system", - "content": gptDefine.getSystemContentByType(this.global.config.gpt_auto_inference, { - textContent: word, - characterContent: auto_analyze_character - }) - }, - { - "role": "user", - "content": gptDefine.getUserContentByType(this.global.config.gpt_auto_inference, { - textContent: single_word, - wordCount: this.global.config.gpt_model && this.global.config.gpt_model.includes("gpt-4") ? '20' : '40' - }) - } - ] - } - } - - let res = await this.FetchGpt(message); - return res; - } catch (error) { - throw error; + message = gptDefine.CustomizeGptPrompt(customize_gpt_prompt[index], element.after_gpt) + message.push({ + role: 'user', + content: element.after_gpt + }) + } else { + // 内置模式 + // 获取 + let prefix_word = '' + // 拼接一个word + let i = element.no - 1 + if (i <= gpt_count) { + prefix_word = this.all_data + .filter((item, index) => index < i) + .map((item) => item.after_gpt) + .join('\r\n') + } else if (i > gpt_count) { + prefix_word = this.all_data + .filter((item, index) => i - index <= gpt_count && i - index > 0) + .map((item) => item.after_gpt) + .join('\r\n') } - } - /** - * 将推理提示词添加到任务 - */ - async GPTPrompt(data) { - try { - console.log(data) - let value = JSON.parse(data[0]); - let show_global_message = data[1]; - this.all_data = JSON.parse(data[2]); - // 获取data中的after_gpt,然后使用换行符拼接成一个字符串 - // let word = value.map(item => item.after_gpt).join('\r\n'); - let batch = DEFINE_STRING.QUEUE_BATCH.SD_ORIGINAL_GPT_PROMPT; + let suffix_word = '' + let o_i = this.all_data.length - i + if (o_i <= gpt_count) { + suffix_word = this.all_data + .filter((item, index) => index > i) + .map((item) => item.after_gpt) + .join('\r\n') + } else if (o_i > gpt_count) { + suffix_word = this.all_data + .filter((item, index) => index - i <= gpt_count && index - i > 0) + .map((item) => item.after_gpt) + .join('\r\n') + } - // 获取人物角色数据 - let config_json = JSON.parse(await fspromises.readFile(path.join(this.global.config.project_path, "scripts/config.json"), 'utf-8')); - let auto_analyze_character = config_json.auto_analyze_character; - let gpt_count = this.global.config.gpt_count ? this.global.config.gpt_count : 10; - for (let i = 0; i < value.length; i++) { - const element = value[i]; - this.global.requestQuene.enqueue(async () => { - try { + let word = `${prefix_word}\r\n${element.after_gpt}\r\n${suffix_word}` + let single_word = element.after_gpt - let content = await this.GPTPromptGenerate(element, gpt_count, auto_analyze_character); - - if (content) { - content = content.replace(/\)\s*\(/g, ", ").replace(/^\(/, "").replace(/\)$/, "") - } - // 获取对应的数据,将数据返回前端事件 - this.global.newWindow[0].win.webContents.send(DEFINE_STRING.GPT_GENERATE_PROMPT_RETURN, { - id: element.id, - gpt_prompt: content - }) - - this.global.fileQueue.enqueue(async () => { - // 将推理出来的数据写入执行的文件中 - let json_config = JSON.parse(await fspromises.readFile(element.prompt_json, 'utf-8')); - // 写入 - json_config.gpt_prompt = content; - await fspromises.writeFile(element.prompt_json, JSON.stringify(json_config)); - }) - } catch (error) { - throw error; - } - }, `${batch}_${element.id}`, batch); + // 判断当前的格式 + if (['superSinglePrompt', 'onlyPromptMJ'].includes(this.global.config.gpt_auto_inference)) { + // 有返回案例的 + message = gptDefine.GetExamplePromptMessage(this.global.config.gpt_auto_inference) + // 加当前提问的 + message.push({ + role: 'user', + content: single_word + }) + } else { + // 直接返回,没有案例的 + message = [ + { + role: 'system', + content: gptDefine.getSystemContentByType(this.global.config.gpt_auto_inference, { + textContent: word, + characterContent: auto_analyze_character + }) + }, + { + role: 'user', + content: gptDefine.getUserContentByType(this.global.config.gpt_auto_inference, { + textContent: single_word, + wordCount: + this.global.config.gpt_model && this.global.config.gpt_model.includes('gpt-4') + ? '20' + : '40' + }) } + ] + } + } + let res = await RetryWithBackoff( + async () => { + return await this.FetchGpt(message) + }, + 5, + 2000 + ) + return res + } catch (error) { + throw error + } + } - this.global.requestQuene.setBatchCompletionCallback(batch, (failedTasks) => { - if (failedTasks.length > 0) { - let message = ` + /** + * 将推理提示词添加到任务 + */ + async GPTPrompt(data) { + try { + console.log(data) + let value = JSON.parse(data[0]) + let show_global_message = data[1] + this.all_data = JSON.parse(data[2]) + // 获取data中的after_gpt,然后使用换行符拼接成一个字符串 + // let word = value.map(item => item.after_gpt).join('\r\n'); + let batch = DEFINE_STRING.QUEUE_BATCH.SD_ORIGINAL_GPT_PROMPT + + // 获取人物角色数据 + let config_json = JSON.parse( + await fspromises.readFile( + path.join(this.global.config.project_path, 'scripts/config.json'), + 'utf-8' + ) + ) + let auto_analyze_character = config_json.auto_analyze_character + let gpt_count = this.global.config.gpt_count ? this.global.config.gpt_count : 10 + for (let i = 0; i < value.length; i++) { + const element = value[i] + this.global.requestQuene.enqueue( + async () => { + try { + let content = await this.GPTPromptGenerate(element, gpt_count, auto_analyze_character) + + if (content) { + content = content + .replace(/\)\s*\(/g, ', ') + .replace(/^\(/, '') + .replace(/\)$/, '') + } + // 获取对应的数据,将数据返回前端事件 + this.global.newWindow[0].win.webContents.send( + DEFINE_STRING.GPT_GENERATE_PROMPT_RETURN, + { + id: element.id, + gpt_prompt: content + } + ) + + this.global.fileQueue.enqueue(async () => { + // 将推理出来的数据写入执行的文件中 + let json_config = JSON.parse( + await fspromises.readFile(element.prompt_json, 'utf-8') + ) + // 写入 + json_config.gpt_prompt = content + await fspromises.writeFile(element.prompt_json, JSON.stringify(json_config)) + }) + } catch (error) { + throw error + } + }, + `${batch}_${element.id}`, + batch + ) + } + + this.global.requestQuene.setBatchCompletionCallback(batch, (failedTasks) => { + if (failedTasks.length > 0) { + let message = ` 推理提示词任务都已完成。 但是以下任务执行失败: ` - failedTasks.forEach(({ taskId, error }) => { - message += `${taskId}-, \n 错误信息: ${error}` + '\n'; - }); + failedTasks.forEach(({ taskId, error }) => { + message += `${taskId}-, \n 错误信息: ${error}` + '\n' + }) - this.global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, { - code: 0, - message: message - }) - } else { - if (show_global_message) { - this.global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, { - code: 1, - message: "所有推理任务完成" - }) - } - } - }); - - return { - code: 1, - } - - - } catch (error) { - return { - code: 0, - message: error.toString() - } - } - } - - /** - * 修改请求的参数 - * @param {*} data - * @returns - */ - ModifyData(gpt_url, data) { - let res = data; - if (gpt_url.includes("dashscope.aliyuncs.com")) { - res = { - "model": data.model, - "input": { - "messages": data.messages, - }, - "parameters": { - "result_format": "message" - } - } - } - return res; - } - - /** - * 获取返回的内容 - * @param {*} gpt_url GPT请求的内容 - * @param {*} res 请求返回的数据 - * @returns - */ - GetResponseContent(gpt_url, res) { - let content = ""; - if (gpt_url.includes("dashscope.aliyuncs.com")) { - content = res.data.output.choices[0].message.content; + this.global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, { + code: 0, + message: message + }) } else { - - content = res.data.choices[0].message.content; + if (show_global_message) { + this.global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, { + code: 1, + message: '所有推理任务完成' + }) + } } - return content; + }) + + return { + code: 1 + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } + } - /** - * 发送GPT请求 - * @param {*} message 请求的信息 - * @param {*} gpt_url gpt的url,默认在global中取 - * @param {*} gpt_key gpt的key,默认在global中取 - * @param {*} gpt_model gpt的model,默认在global中取 - * @returns - */ - async FetchGpt(message, - gpt_url = this.global.config.gpt_business, - gpt_key = this.global.config.gpt_key, - gpt_model = this.global.config.gpt_model) { - try { - // 还有自定义的 - let all_options = (await this.GetGPTBusinessOption("all", (value) => value.gpt_url)).data; - // 判断gpt_business 是不是一个http开头的 - if (!gpt_url.includes("http")) { - // 获取对应Id的gpt_url - let index = all_options.findIndex(item => item.value == gpt_url && item.gpt_url); - if (index < 0) { - throw new Error("获取GPT的服务商配置失败"); - } - gpt_url = all_options[index].gpt_url; - } - - - let data = { - "model": gpt_model, - "messages": message - }; - - data = this.ModifyData(gpt_url, data); - let config = { - method: 'post', - maxBodyLength: Infinity, - url: gpt_url, - headers: { - 'Authorization': `Bearer ${gpt_key}`, - 'Content-Type': 'application/json' - }, - data: JSON.stringify(data) - }; - - let res = await axios.request(config); - let content = this.GetResponseContent(gpt_url, res); - return content; - } catch (error) { - throw error; + /** + * 修改请求的参数 + * @param {*} data + * @returns + */ + ModifyData(gpt_url, data) { + let res = data + if (gpt_url.includes('dashscope.aliyuncs.com')) { + res = { + model: data.model, + input: { + messages: data.messages + }, + parameters: { + result_format: 'message' } + } } + return res + } - /** - * 自动分析文本,返回人物场景。角色。 - * @param {要分析的文本} value - * @returns - */ - async AutoAnalyzeCharacter(value) { - try { - let message = [ - { - "role": "system", - "content": gptDefine.getSystemContentByType("character", { textContent: value }) - }, - { - "role": "user", - "content": gptDefine.getUserContentByType("character", {}) - } - ] - let content = await this.FetchGpt(message); + /** + * 获取返回的内容 + * @param {*} gpt_url GPT请求的内容 + * @param {*} res 请求返回的数据 + * @returns + */ + GetResponseContent(gpt_url, res) { + let content = '' + if (gpt_url.includes('dashscope.aliyuncs.com')) { + content = res.data.output.choices[0].message.content + } else { + content = res.data.choices[0].message.content + } + return content + } - return { - code: 1, - data: content - } - } catch (error) { - return { - code: 0, - message: error.toString() - } + /** + * 发送GPT请求 + * @param {*} message 请求的信息 + * @param {*} gpt_url gpt的url,默认在global中取 + * @param {*} gpt_key gpt的key,默认在global中取 + * @param {*} gpt_model gpt的model,默认在global中取 + * @returns + */ + async FetchGpt( + message, + gpt_url = this.global.config.gpt_business, + gpt_key = this.global.config.gpt_key, + gpt_model = this.global.config.gpt_model + ) { + try { + // 还有自定义的 + let all_options = (await this.GetGPTBusinessOption('all', (value) => value.gpt_url)).data + // 判断gpt_business 是不是一个http开头的 + if (!gpt_url.includes('http')) { + // 获取对应Id的gpt_url + let index = all_options.findIndex((item) => item.value == gpt_url && item.gpt_url) + if (index < 0) { + throw new Error('获取GPT的服务商配置失败') } + gpt_url = all_options[index].gpt_url + } + + let data = { + model: gpt_model, + messages: message + } + + data = this.ModifyData(gpt_url, data) + let config = { + method: 'post', + maxBodyLength: Infinity, + url: gpt_url, + headers: { + Authorization: `Bearer ${gpt_key}`, + 'Content-Type': 'application/json' + }, + data: JSON.stringify(data) + } + + let res = await axios.request(config) + let content = this.GetResponseContent(gpt_url, res) + return content + } catch (error) { + throw error } + } - - /** - * 获取GPT的服务商配置,默认的和自定义的 - * @returns - */ - async GetGPTBusinessOption(value, callback = null) { - let res = await gptDefine.getGptDataByTypeAndProperty(value, "gpt_options", []); - if (res.code == 0) { - return res; - } else { - if (callback) { - callback(res.data) - } - return successMessage(res.data) + /** + * 自动分析文本,返回人物场景。角色。 + * @param {要分析的文本} value + * @returns + */ + async AutoAnalyzeCharacter(value) { + try { + let message = [ + { + role: 'system', + content: gptDefine.getSystemContentByType('character', { textContent: value }) + }, + { + role: 'user', + content: gptDefine.getUserContentByType('character', {}) } - } + ] + let content = await RetryWithBackoff( + async () => { + return await this.FetchGpt(message) + }, + 5, + 2000 + ) - /** - * 获取GPT的模型配置,默认的和自定义的 - * @returns - */ - async GetGPTModelOption(value) { - return await gptDefine.getGptDataByTypeAndProperty(value, "gpt_model_options", []); + return { + code: 1, + data: content + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } + } - /** - * 获取GPT的自动推理模式配置,默认的和自定义的 - * @returns - */ - async GetGptAutoInferenceOptions(value) { - return await gptDefine.getGptDataByTypeAndProperty(value, "gpt_auto_inference", []); + /** + * 获取GPT的服务商配置,默认的和自定义的 + * @returns + */ + async GetGPTBusinessOption(value, callback = null) { + let res = await gptDefine.getGptDataByTypeAndProperty(value, 'gpt_options', []) + if (res.code == 0) { + return res + } else { + if (callback) { + callback(res.data) + } + return successMessage(res.data) } + } - /** - * 获取GPT的自动推理模式配置,默认的和自定义的 - * @returns - */ - async GetCustomizeGptPrompt(value) { - return await gptDefine.getGptDataByTypeAndProperty(value, "customize_gpt_prompt", []); + /** + * 获取GPT的模型配置,默认的和自定义的 + * @returns + */ + async GetGPTModelOption(value) { + return await gptDefine.getGptDataByTypeAndProperty(value, 'gpt_model_options', []) + } + + /** + * 获取GPT的自动推理模式配置,默认的和自定义的 + * @returns + */ + async GetGptAutoInferenceOptions(value) { + return await gptDefine.getGptDataByTypeAndProperty(value, 'gpt_auto_inference', []) + } + + /** + * 获取GPT的自动推理模式配置,默认的和自定义的 + * @returns + */ + async GetCustomizeGptPrompt(value) { + return await gptDefine.getGptDataByTypeAndProperty(value, 'customize_gpt_prompt', []) + } + + /** + * 保存自定义的GPT服务商配置 + * @param {*} value 配置信息 0 : 传入的数据 1: 属性名称 + * @returns + */ + async SaveDynamicGPTOption(value) { + try { + let res = await gptDefine.saveDynamicGPTOption(value) + return { + code: 1 + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } + } - /** - * 保存自定义的GPT服务商配置 - * @param {*} value 配置信息 0 : 传入的数据 1: 属性名称 - * @returns - */ - async SaveDynamicGPTOption(value) { - try { - let res = await gptDefine.saveDynamicGPTOption(value); - return { - code: 1, - } - } catch (error) { - return { - code: 0, - message: error.toString() - } + /** + * 删除指定Id的自定义GPT服务商配置 + * @param {*} value id 0 : 删除的数据 1: 属性名称 + * @returns + */ + async DeleteDynamicGPTOption(value) { + try { + let res = await gptDefine.deleteDynamicGPTOption(value) + return { + code: 1, + data: res + } + } catch (error) { + return { + code: 0, + message: error.toString() + } + } + } + + /** + * + * @param {Stirng} value 传入的GPT网址和key,判断是不是可以链接成功 + */ + async TestGPTConnection(value) { + try { + value = JSON.parse(value) + let message = [ + { + role: 'system', + content: '你好' + }, + { + role: 'user', + content: '你好' } + ] + let content = await RetryWithBackoff( + async () => { + return await this.FetchGpt(message, value.gpt_business, value.gpt_key, value.gpt_model) + }, + 5, + 2000 + ) + return { + code: 1 + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } + } - /** - * 删除指定Id的自定义GPT服务商配置 - * @param {*} value id 0 : 删除的数据 1: 属性名称 - * @returns - */ - async DeleteDynamicGPTOption(value) { - try { - let res = await gptDefine.deleteDynamicGPTOption(value); - return { - code: 1, - data: res - } - } catch (error) { - return { - code: 0, - message: error.toString() - } + /** + * 单句洗稿 + * @param {文案参数} value + */ + async AIModifyOneWord(value) { + try { + let message = [ + { + role: 'system', + content: + 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.' + }, + { + role: 'user', + content: `请您扮演一个抖音网文改写专家,我会给你一句文案,请你不要改变文案的结构,不改变原来的意思,仅对文案进行同义转换改写,不要有奇怪的写法,说法通俗一点,不要其他的标点符号,每一小句话之间都是以句号连接,参考抖音网文解说,以下是文案:${value[1]}。` } + ] + let content = await RetryWithBackoff( + async () => { + return await this.FetchGpt(message) + }, + 5, + 2000 + ) + + return { + code: 1, + data: { no: value[0], content: content } + } + } catch (error) { + return { + code: 0, + message: error.toString() + } } - - /** - * - * @param {Stirng} value 传入的GPT网址和key,判断是不是可以链接成功 - */ - async TestGPTConnection(value) { - try { - value = JSON.parse(value); - let message = [ - { - "role": "system", - "content": "你好" - }, - { - "role": "user", - "content": "你好" - } - ]; - - let content = await this.FetchGpt(message, value.gpt_business, value.gpt_key, value.gpt_model); - return { - code: 1, - } - } catch (error) { - return { - code: 0, - message: error.toString() - } - } - } - - /** - * 单句洗稿 - * @param {文案参数} value - */ - async AIModifyOneWord(value) { - try { - - let message = [ - { - "role": "system", - "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible." - }, - { - "role": "user", - "content": `请您扮演一个抖音网文改写专家,我会给你一句文案,请你不要改变文案的结构,不改变原来的意思,仅对文案进行同义转换改写,不要有奇怪的写法,说法通俗一点,不要其他的标点符号,每一小句话之间都是以句号连接,参考抖音网文解说,以下是文案:${value[1]}。` - } - ] - let content = await this.FetchGpt(message); - - return { - code: 1, - data: { no: value[0], content: content } - } - } catch (error) { - return { - code: 0, - message: error.toString() - } - } - } - -} \ No newline at end of file + } +} diff --git a/src/main/Public/Translate.js b/src/main/Public/Translate.js index 0c0609c..959c37e 100644 --- a/src/main/Public/Translate.js +++ b/src/main/Public/Translate.js @@ -14,6 +14,7 @@ import { isEmpty } from 'lodash' import { ValidateJson } from '../../define/Tools/validate' import { GetOpenAISuccessResponse } from '../../define/response/openAIResponse' import { successMessage } from './generalTools' +import { RetryWithBackoff } from '../../define/Tools/common' let { Signer } = require('@volcengine/openapi') @@ -267,69 +268,83 @@ export class Translate { messages: [] } // 开始调用GPT进行翻译 - if (from == 'en' && to == "zh") { + if (from == 'en' && to == 'zh') { data.messages.push({ - "role": "system", - "content": '我想让你充当英译中专家,用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。' + role: 'system', + content: + '我想让你充当英译中专家,用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。' }) // 添加示例 data.messages.push({ - "role": "user", - "content": 'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’s face with tear marks.' + role: 'user', + content: + 'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’s face with tear marks.' }) data.messages.push({ - "role": "assistant", - "content": '一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。' + role: 'assistant', + content: + '一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。' }) data.messages.push({ - "role": "user", - "content": 'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.' + role: 'user', + content: + 'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.' }) data.messages.push({ - "role": "assistant", - "content": '一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。' + role: 'assistant', + content: + '一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。' }) data.messages.push({ - "role": "user", - "content": 'In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man\'s calm demeanor. The man\'s cell phone is ringing. The scene is set in the present.' + role: 'user', + content: + "In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man's calm demeanor. The man's cell phone is ringing. The scene is set in the present." }) data.messages.push({ - "role": "assistant", - "content": '在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。' + role: 'assistant', + content: + '在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。' }) - } else if (from == 'zh' && to == "en") { + } else if (from == 'zh' && to == 'en') { data.messages.push({ - "role": "system", - "content": '我想让你充当中译英专家,用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。' + role: 'system', + content: + '我想让你充当中译英专家,用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。' }) // 添加示例 data.messages.push({ - "role": "user", - "content": '一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。' + role: 'user', + content: + '一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。' }) data.messages.push({ - "role": "assistant", - "content": 'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’s face with tear marks.' + role: 'assistant', + content: + 'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’s face with tear marks.' }) data.messages.push({ - "role": "user", - "content": '一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。' + role: 'user', + content: + '一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。' }) data.messages.push({ - "role": "assistant", - "content": 'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.' + role: 'assistant', + content: + 'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.' }) data.messages.push({ - "role": "user", - "content": '在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。' + role: 'user', + content: + '在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。' }) data.messages.push({ - "role": "assistant", - "content": 'In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man\'s calm demeanor. The man\'s cell phone is ringing. The scene is set in the present.' + role: 'assistant', + content: + "In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man's calm demeanor. The man's cell phone is ringing. The scene is set in the present." }) - } else { - throw new Error("GPT翻译只支持中英互译") - } + } else { + throw new Error('GPT翻译只支持中英互译') + } data.messages.push({ role: 'user', @@ -346,7 +361,13 @@ export class Translate { }, data: JSON.stringify(data) } - let res = await axios.request(config) + let res = await RetryWithBackoff( + async () => { + return await axios.request(config) + }, + 5, + 2000 + ) // 将返回的数据进行拼接数据处理 let res_data = [] @@ -416,7 +437,6 @@ export class Translate { if (arr_data.length <= 0) { return } - let req_data = {} for (let j = 0; j < arr_data.length; j++) { const element = arr_data[j] diff --git a/src/main/Service/Book/bookTask.ts b/src/main/Service/Book/bookTask.ts index ede97db..78ac24c 100644 --- a/src/main/Service/Book/bookTask.ts +++ b/src/main/Service/Book/bookTask.ts @@ -243,7 +243,7 @@ export class BookTask { startTime: element.startTime, endTime: element.endTime, timeLimit: element.timeLimit, - subValue: element.subValue, + subValue: element.subValue && element.subValue.length > 0 ? JSON.stringify(element.subValue) : undefined, characterTags: element.characterTags && element.characterTags.length > 0 ? cloneDeep(element.characterTags) : [], gptPrompt: element.gptPrompt, outImagePath: path.relative(define.project_path, outImagePath), diff --git a/src/main/Service/GPT/gpt.ts b/src/main/Service/GPT/gpt.ts index eb8d028..6aa1945 100644 --- a/src/main/Service/GPT/gpt.ts +++ b/src/main/Service/GPT/gpt.ts @@ -1,6 +1,7 @@ import { isEmpty } from "lodash"; import { gptDefine } from "../../../define/gptDefine"; import axios from "axios"; +import { RetryWithBackoff } from "../../../define/Tools/common"; /** * 一些GPT相关的服务都在这边 @@ -168,7 +169,10 @@ export class GptService { let baseSubUrl = baseUrl ? (baseUrl.endsWith('/') ? baseUrl + 'v1/chat/completions' : baseUrl + '/v1/chat/completions') : null; let url = baseSubUrl ? baseSubUrl : "https://api.laitool.cc/v1/chat/completions" // 开始请求,这个默认是使用的是LAI API的gpt-4o-mini - let content = await this.FetchGpt(message, 'gpt-4o-mini', apiKey, url) + let content = await RetryWithBackoff(async () => { + return await this.FetchGpt(message, 'gpt-4o-mini', apiKey, url); + }, 5, 2000) + return content } catch (error) { throw error diff --git a/src/main/Service/Subtitle/subtitle.ts b/src/main/Service/Subtitle/subtitle.ts index 465aa8b..4283566 100644 --- a/src/main/Service/Subtitle/subtitle.ts +++ b/src/main/Service/Subtitle/subtitle.ts @@ -22,6 +22,7 @@ import { BookTaskStatus, OperateBookType } from '../../../define/enum/bookEnum' import axios from 'axios' import { GptService } from '../GPT/gpt' import FormData from 'form-data' +import { RetryWithBackoff } from '../../../define/Tools/common' const util = require('util') const { exec } = require('child_process') const execAsync = util.promisify(exec) @@ -596,17 +597,22 @@ export class Subtitle { headers: { 'Accept': 'application/json', 'Authorization': subtitleSetting.laiWhisper.apiKey, - 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)', 'Content-Type': 'multipart/form-data', ...formdata.getHeaders() // 在Node.js环境中需要添加这一行 }, data: formdata }; - let res = await axios(config) + // laiwhisper 要做重试机制 + let res = await RetryWithBackoff(async () => { + return await axios(config) + }, 5, 2000) let text = res.data.text; // 但是这边是繁体,需要转化为简体 - let simpleText = await this.gptService.ChineseTraditionalToSimplified(text, subtitleSetting.laiWhisper.apiKey, url); + // 请求也要做重试 + let simpleText = await RetryWithBackoff(async () => { + return await this.gptService.ChineseTraditionalToSimplified(text, subtitleSetting.laiWhisper.apiKey, url); + }, 5, 2000); console.log(res.data) return simpleText; diff --git a/src/main/Service/Subtitle/subtitleService.ts b/src/main/Service/Subtitle/subtitleService.ts index 752c874..deb1c56 100644 --- a/src/main/Service/Subtitle/subtitleService.ts +++ b/src/main/Service/Subtitle/subtitleService.ts @@ -11,9 +11,7 @@ import fs from 'fs' import { CheckFileOrDirExist } from "../../../define/Tools/file"; import { BookServiceBasic } from "../ServiceBasic/bookServiceBasic"; import { Subtitle } from "./subtitle"; -import { LoggerStatus, ResponseMessageType } from "../../../define/enum/softwareEnum"; import { TaskScheduler } from "../taskScheduler"; -import { OperationType } from "realm/dist/public-types/internal"; import { OperateBookType } from "../../../define/enum/bookEnum"; import { Book } from "../../../model/book"; @@ -126,9 +124,14 @@ export class SubtitleService { //#region 语音转文案或者是字幕识别 /** - * 反推提取文案 + * 反推提取文案的入口方法 + * @param bookId 小说ID + * @param bookTaskId 小说批次任务ID + * @param operateBookType 操作的小说类型 + * @param coverData 是不是要覆盖旧的数据 + * @returns */ - async GetCopywriting(bookId: string, bookTaskId: string, operateBookType: OperateBookType): Promise { + async GetCopywriting(bookId: string, bookTaskId: string, operateBookType: OperateBookType, coverData: boolean): Promise { try { let subtitleSettingRes = await this.GetSubtitleSetting(); if (subtitleSettingRes.code == 0) { @@ -152,8 +155,13 @@ export class SubtitleService { } else { throw new Error("未知的操作类型") } + + if (!coverData) { // 不覆盖数据,将已经有的数据过滤掉 + bookTaskDetails = bookTaskDetails.filter(item => isEmpty(item.afterGpt) && isEmpty(item.word)) + } + if (bookTaskDetails.length <= 0) { - throw new Error("分镜信息不存在"); + throw new Error("分镜信息不存在 / 已经有文案,无需提取"); } let { book, bookTask } = await this.bookServiceBasic.GetBookAndTask(bookId, tempBookTaskId) diff --git a/src/main/Service/Translate/Translate.ts b/src/main/Service/Translate/Translate.ts index bebf3c5..4552ca7 100644 --- a/src/main/Service/Translate/Translate.ts +++ b/src/main/Service/Translate/Translate.ts @@ -16,6 +16,7 @@ import { SoftwareService } from "../../../define/db/service/SoftWare/softwareSer import { isEmpty } from "lodash"; import { ValidateJson } from "../../../define/Tools/validate"; import { GetOpenAISuccessResponse } from "../../../define/response/openAIResponse"; +import { RetryWithBackoff } from "../../../define/Tools/common"; let { Signer @@ -202,7 +203,10 @@ export class Translate { }, data: JSON.stringify(data) }; - let res = await axios.request(config); + let res = await RetryWithBackoff(async () => { + return await axios.request(config); + }, 5, 2000) + // let res = await axios.request(config); // 将返回的数据进行拼接数据处理 let res_data = []; diff --git a/src/preload/book.js b/src/preload/book.js index 2823163..709e2cf 100644 --- a/src/preload/book.js +++ b/src/preload/book.js @@ -72,12 +72,13 @@ const book = { //#region 文案相关信息 // 获取文案信息 - GetCopywriting: async (bookId, bookTaskId, operateBookType) => + GetCopywriting: async (bookId, bookTaskId, operateBookType, coverData) => await ipcRenderer.invoke( DEFINE_STRING.BOOK.GET_COPYWRITING, bookId, bookTaskId, - operateBookType + operateBookType, + coverData ), // 将文案信息导出,方便修改 diff --git a/src/renderer/src/components/Book/Components/AddBook.vue b/src/renderer/src/components/Book/Components/ManageBook/AddBook.vue similarity index 98% rename from src/renderer/src/components/Book/Components/AddBook.vue rename to src/renderer/src/components/Book/Components/ManageBook/AddBook.vue index b4cd013..9d8e070 100644 --- a/src/renderer/src/components/Book/Components/AddBook.vue +++ b/src/renderer/src/components/Book/Components/ManageBook/AddBook.vue @@ -115,10 +115,10 @@