109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import path from 'path'
|
||
import os from 'os'
|
||
import { define } from '../../define/define'
|
||
import { errorMessage, successMessage } from '../Public/generalTools'
|
||
import axios from 'axios'
|
||
import { ServiceBase } from '../../define/db/service/serviceBase'
|
||
import { isEmpty } from 'lodash'
|
||
import { ValidateJson } from '../../define/Tools/validate'
|
||
|
||
export class GptSetting extends ServiceBase {
|
||
constructor() {
|
||
super()
|
||
axios.defaults.baseURL = define.serverUrl
|
||
}
|
||
|
||
/**
|
||
* 初始化数据
|
||
* @returns
|
||
*/
|
||
async InitAISetting() {
|
||
let defaultData = {
|
||
laiapi: {
|
||
gpt_url: 'https://laitool.net/v1/chat/completions',
|
||
api_key: "请输入你的LAI API 的 Key",
|
||
model: "请输入你的LAI API 的 Model名称"
|
||
},
|
||
kimi: {
|
||
gpt_url: 'https://api.moonshot.cn/v1/chat/completions',
|
||
api_key: "请输入你的Kimi API 的 Key",
|
||
model: "请输入你的Kimi API 的 Model名称"
|
||
},
|
||
doubao: {
|
||
gpt_url: 'https://ark.cn-beijing.volces.com/api/v3/chat/completions',
|
||
api_key: "请输入你的Doubao API 的 Key",
|
||
model: "请输入你的Doubao API 的 Model名称"
|
||
}
|
||
}
|
||
await this.SaveAISetting(defaultData)
|
||
return defaultData
|
||
}
|
||
|
||
/**
|
||
* 获取服务强上面的提示词相关的数据
|
||
*/
|
||
async InitServerGptOptions() {
|
||
try {
|
||
// 获取提示词类型数据
|
||
let promptRes = await axios.get('/api/Prompt/GetPromptType/100/1')
|
||
let promptType = promptRes.data.data
|
||
|
||
// 获取提示词数据
|
||
let prompt = await axios.get('/api/Prompt/GetPromptString/all/100/1')
|
||
let promptData = prompt.data.data
|
||
|
||
let gptOptions = {
|
||
promptType: promptType,
|
||
promptData: promptData
|
||
}
|
||
return successMessage(gptOptions, '请求成功', 'GptSetting_InitServerGptOptions')
|
||
} catch (error) {
|
||
return errorMessage(
|
||
'获取服务端提示词数据错误,错误信息如下:' + error.toString(),
|
||
'GptSetting_InitServerGptOptions'
|
||
)
|
||
}
|
||
}
|
||
|
||
// 获取软件设置里面的AI设置,没有的话设置初始值
|
||
async GetAISetting() {
|
||
try {
|
||
await this.InitService()
|
||
let aiSetting = undefined
|
||
let res = this.softService.GetSoftWarePropertyData('aiSetting')
|
||
if (isEmpty(res)) {
|
||
aiSetting = await this.InitAISetting();
|
||
} else {
|
||
let tryP = ValidateJson(res)
|
||
if (!tryP) {
|
||
throw new Error('AI设置的数据格式不正确')
|
||
}
|
||
aiSetting = JSON.parse(res)
|
||
}
|
||
return successMessage(aiSetting, '请求成功', 'GptSetting_GetAISetting')
|
||
} catch (error) {
|
||
return errorMessage(
|
||
'获取软件设置里面的AI设置错误,错误信息如下:' + error.toString(),
|
||
'GptSetting_GetAISetting'
|
||
)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存软件中的AI设置
|
||
* @param {*} value
|
||
*/
|
||
async SaveAISetting(value) {
|
||
try {
|
||
await this.InitService()
|
||
let res = this.softService.SaveSoftwarePropertyData('aiSetting', JSON.stringify(value))
|
||
return res
|
||
} catch (error) {
|
||
return errorMessage(
|
||
'保存软件设置里面的AI设置错误,错误信息如下:' + error.toString(),
|
||
'GptSetting_SaveAISetting'
|
||
)
|
||
}
|
||
}
|
||
}
|