v-4.0.0 开源可二开源码

This commit is contained in:
vastxie
2024-12-12 23:10:51 +08:00
parent c831009379
commit 22ee5a71b2
1017 changed files with 69316 additions and 9248 deletions

View File

@@ -8,25 +8,37 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var AzureTtsService_1;
var AiPptService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureTtsService = void 0;
exports.AiPptService = void 0;
const common_1 = require("@nestjs/common");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const models_service_1 = require("../models/models.service");
const upload_service_1 = require("../upload/upload.service");
let AzureTtsService = AzureTtsService_1 = class AzureTtsService {
constructor(uploadService, globalConfigService, chatLogService) {
const openaiChat_service_1 = require("./openaiChat.service");
let AiPptService = AiPptService_1 = class AiPptService {
constructor(uploadService, globalConfigService, chatLogService, openAIChatService, modelsService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.logger = new common_1.Logger(AzureTtsService_1.name);
this.openAIChatService = openAIChatService;
this.modelsService = modelsService;
this.logger = new common_1.Logger(AiPptService_1.name);
}
async aiPPT(inputs) {
return;
}
async pptCover(inputs) {
return;
}
};
AzureTtsService = AzureTtsService_1 = __decorate([
AiPptService = AiPptService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService])
], AzureTtsService);
exports.AzureTtsService = AzureTtsService;
chatLog_service_1.ChatLogService,
openaiChat_service_1.OpenAIChatService,
models_service_1.ModelsService])
], AiPptService);
exports.AiPptService = AiPptService;

214
dist/modules/ai/cogVideo.service.js vendored Normal file
View File

@@ -0,0 +1,214 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CogVideoService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let CogVideoService = class CogVideoService {
constructor(chatLogService, globalConfigService, uploadService) {
this.chatLogService = chatLogService;
this.globalConfigService = globalConfigService;
this.uploadService = uploadService;
}
async cogVideo(inputs) {
var _a, _b, _c;
const { apiKey, proxyUrl, fileInfo, prompt, timeout, assistantLogId } = inputs;
let result = {
text: '',
fileInfo: '',
taskId: '',
taskData: '',
status: 2,
};
let response = null;
let url = '';
let payloadJson = {};
const headers = { Authorization: `Bearer ${apiKey}` };
url = `${proxyUrl}/cogvideox/v4/videos/generations`;
payloadJson = {
model: 'cogvideox',
prompt: prompt,
};
if (fileInfo) {
payloadJson['image_url'] = fileInfo;
}
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'CogService');
try {
response = await axios_1.default.post(url, payloadJson, { headers });
common_1.Logger.debug(`任务提交结果,状态码: ${response.status}, 状态消息: ${response.statusText}, 数据: ${JSON.stringify(response.data)}`);
}
catch (error) {
common_1.Logger.error(`任务提交失败: ${error.message}`, 'CogService');
throw new Error('任务提交失败');
}
if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.id) {
result.taskId = (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.id;
common_1.Logger.log(`任务提交成功, 任务ID: ${(_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.id}`, 'CogService');
}
else {
throw new Error('未能获取结果数据, 即将重试');
}
try {
await this.pollCogVideoResult({
proxyUrl,
apiKey,
taskId: response.data.id,
timeout,
prompt,
onSuccess: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: '100%',
status: 3,
taskId: data === null || data === void 0 ? void 0 : data.taskId,
taskData: data === null || data === void 0 ? void 0 : data.taskData,
});
common_1.Logger.log('视频任务已完成', 'CogService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
onGenerating: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || '视频生成中...',
progress: data === null || data === void 0 ? void 0 : data.progress,
status: data.status,
});
common_1.Logger.log('视频生成中...', 'CogService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
onFailure: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: '视频生成失败',
status: data.status,
});
common_1.Logger.log('生成失败', 'Lum aService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
});
}
catch (error) {
common_1.Logger.error('查询生成结果时发生错误:', error.message, 'CogService');
throw new Error('查询生成结果时发生错误');
}
return result;
}
async pollCogVideoResult(inputs) {
const { proxyUrl, apiKey, taskId, timeout, onSuccess, onFailure, onGenerating, prompt, action, } = inputs;
let result = {
videoUrl: '',
audioUrl: '',
fileInfo: '',
drawId: '',
taskData: '',
status: 2,
progress: 0,
answer: '',
};
const headers = { Authorization: `Bearer ${apiKey}` };
const url = `${proxyUrl}/cogvideox/v4/async-result/${taskId}`;
const startTime = Date.now();
const totalDuration = 300000;
const POLL_INTERVAL = 5000;
let retryCount = 0;
try {
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
try {
const res = await axios_1.default.get(url, { headers });
const interval = setInterval(() => {
const elapsed = Date.now() - startTime;
let percentage = Math.floor((elapsed / totalDuration) * 100);
if (percentage >= 99)
percentage = 99;
result.answer = `视频生成中 ${percentage}%`;
}, 1000);
const responses = res.data;
common_1.Logger.debug(`轮询结果: ${JSON.stringify(responses)}`, 'CogService');
if (responses.task_status === 'SUCCESS') {
result.taskId = responses.request_id;
result.taskData = JSON.stringify(responses);
common_1.Logger.log('视频生成成功', 'CogService');
result.fileInfo = responses.video_result[0].url;
common_1.Logger.log(result.fileInfo, 'CogService');
try {
const localStorageStatus = await this.globalConfigService.getConfigs([
'localStorageStatus',
]);
if (Number(localStorageStatus)) {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: responses.video_result[0].url,
dir: `video/cog/${currentDate}`,
});
}
}
catch (error) {
common_1.Logger.error(`上传文件失败: ${error.message}`, 'CogService');
}
result.answer = `提示词: "${prompt}"`;
onSuccess(result);
clearInterval(interval);
return;
}
else {
onGenerating(result);
}
if (result.progress) {
}
}
catch (error) {
retryCount++;
common_1.Logger.error(`轮询失败,重试次数: ${retryCount}`, 'CogService');
}
}
common_1.Logger.error('轮询超时,请稍后再试!', 'CogService');
result.status = 4;
onFailure(result);
throw new Error('查询超时,请稍后再试!');
}
catch (error) {
common_1.Logger.error(`轮询过程中发生错误: ${error}`, 'CogService');
result.status = 5;
onFailure(result);
}
}
};
CogVideoService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [chatLog_service_1.ChatLogService,
globalConfig_service_1.GlobalConfigService,
upload_service_1.UploadService])
], CogVideoService);
exports.CogVideoService = CogVideoService;

148
dist/modules/ai/fluxDraw.service.js vendored Normal file
View File

@@ -0,0 +1,148 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var FluxDrawService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FluxDrawService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
const openaiChat_service_1 = require("./openaiChat.service");
let FluxDrawService = FluxDrawService_1 = class FluxDrawService {
constructor(uploadService, globalConfigService, chatLogService, openAIChatService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.openAIChatService = openAIChatService;
this.logger = new common_1.Logger(FluxDrawService_1.name);
}
async fluxDraw(inputs, buildMessageFromParentMessageId) {
var _a, _b, _c, _d;
common_1.Logger.log('开始提交 Flux 绘图任务 ', 'DrawService');
const { apiKey, model, proxyUrl, prompt, extraParam, timeout, onSuccess, onFailure, groupId, } = inputs;
const isDalleChat = await this.globalConfigService.getConfigs([
'isDalleChat',
]);
let drawPrompt;
if (isDalleChat === '1') {
try {
common_1.Logger.log('已开启连续绘画模式', 'FluxDraw');
const { messagesHistory } = await buildMessageFromParentMessageId(`参考上文,结合我的需求,给出绘画描述。我的需求是:${prompt}`, {
groupId,
systemMessage: '你是一个绘画提示词生成工具,请根据用户的要求,结合上下文,用一段文字,描述用户需要的绘画需求,不用包含任何礼貌性的寒暄,只需要场景的描述,可以适当联想',
maxModelTokens: 8000,
maxRounds: 5,
fileInfo: '',
}, this.chatLogService);
drawPrompt = await this.openAIChatService.chatFree(prompt, undefined, messagesHistory);
}
catch (error) {
console.error('调用chatFree失败', error);
drawPrompt = prompt;
}
}
else {
drawPrompt = prompt;
}
const size = (extraParam === null || extraParam === void 0 ? void 0 : extraParam.size) || '1024x1024';
let result = { answer: '', fileInfo: '', status: 2 };
try {
const options = {
method: 'POST',
url: `${proxyUrl}/v1/images/generations`,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: {
model: model,
prompt: drawPrompt,
size,
},
};
common_1.Logger.log(`正在准备发送请求到 ${options.url}payload: ${JSON.stringify(options.data)}, headers: ${JSON.stringify(options.headers)}`, 'FluxDrawService');
const response = await (0, axios_1.default)(options);
common_1.Logger.debug(`请求成功${JSON.stringify(response.data.data[0])}`);
common_1.Logger.debug(`请求状态${JSON.stringify(response.status)}`);
const url = response.data.data[0].url;
try {
common_1.Logger.log(`------> 开始上传图片!!!`, 'DrawService');
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `images/dalle/${currentDate}`,
});
common_1.Logger.log(`图片上传成功URL: ${result.fileInfo}`, 'DrawService');
}
catch (error) {
common_1.Logger.error(`上传图片过程中出现错误: ${error}`, 'DrawService');
}
let revised_prompt_cn;
try {
revised_prompt_cn = await this.openAIChatService.chatFree(`根据提示词{${drawPrompt}}, 模拟AI绘画机器人的语气用中文回复告诉用户已经画好了`);
}
catch (error) {
revised_prompt_cn = `${prompt} 绘制成功`;
common_1.Logger.error('翻译失败: ', error);
}
result.answer = revised_prompt_cn;
result.status = 3;
onSuccess(result);
return;
}
catch (error) {
result.status = 5;
onFailure(result);
const status = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || 500;
console.log('draw error: ', JSON.stringify(error), status);
const message = (_d = (_c = (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) === null || _d === void 0 ? void 0 : _d.message;
if (status === 429) {
result.text = '当前请求已过载、请稍等会儿再试试吧!';
return result;
}
if (status === 400 &&
message.includes('This request has been blocked by our content filters')) {
result.text = '您的请求已被系统拒绝。您的提示可能存在一些非法的文本。';
return result;
}
if (status === 400 &&
message.includes('Billing hard limit has been reached')) {
result.text =
'当前模型key已被封禁、已冻结当前调用Key、尝试重新对话试试吧';
return result;
}
if (status === 500) {
result.text = '绘制图片失败,请检查你的提示词是否有非法描述!';
return result;
}
if (status === 401) {
result.text = '绘制图片失败,此次绘画被拒绝了!';
return result;
}
result.text = '绘制图片失败,请稍后试试吧!';
return result;
}
}
};
FluxDrawService = FluxDrawService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService,
openaiChat_service_1.OpenAIChatService])
], FluxDrawService);
exports.FluxDrawService = FluxDrawService;

View File

@@ -29,6 +29,9 @@ let OpenAIChatService = class OpenAIChatService {
errMsg: '',
modelAvatar: modelAvatar,
};
const data = Object.assign({ model, messages: messagesHistory }, (isFileUpload === 2 && { max_tokens: 2048 }));
data.stream = true;
data.temperature = temperature;
const options = {
method: 'POST',
url: `${proxyUrl}/v1/chat/completions`,
@@ -38,9 +41,11 @@ let OpenAIChatService = class OpenAIChatService {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: Object.assign({ stream: true, model, temperature: temperature, messages: messagesHistory }, (isFileUpload === 2 && { max_tokens: 2048 })),
data: data,
};
console.log('请求配置:', JSON.stringify(options, null, 2), 'ChatService');
const sanitizedOptions = await this.sanitizeOptionsForLogging(options);
console.log('请求配置:', JSON.stringify(sanitizedOptions, null, 2), 'ChatService');
console.log('请求配置:', JSON.stringify(sanitizedOptions, null, 2), 'ChatService');
try {
const response = await (0, axios_1.default)(options);
const stream = response.data;
@@ -92,6 +97,36 @@ let OpenAIChatService = class OpenAIChatService {
return result;
}
}
async sanitizeOptionsForLogging(options) {
const sanitizedOptions = JSON.parse(JSON.stringify(options));
if (sanitizedOptions.headers && sanitizedOptions.headers.Authorization) {
const authHeader = sanitizedOptions.headers.Authorization;
if (authHeader.startsWith('Bearer ')) {
const token = authHeader.slice(7);
if (token.length > 10) {
sanitizedOptions.headers.Authorization = `Bearer ${token.slice(0, 5)}****${token.slice(-4)}`;
}
}
}
if (sanitizedOptions.data &&
sanitizedOptions.data.messages &&
Array.isArray(sanitizedOptions.data.messages)) {
sanitizedOptions.data.messages = sanitizedOptions.data.messages.map((message) => {
if (message.content && Array.isArray(message.content)) {
message.content = message.content.map((content) => {
if (content.type === 'image_url' &&
content.image_url &&
content.image_url.url) {
content.image_url.url = 'data:image/***;base64 ... ...';
}
return content;
});
}
return message;
});
}
return sanitizedOptions;
}
async chatFree(prompt, systemMessage, messagesHistory) {
const { openaiBaseUrl = '', openaiBaseKey = '', openaiBaseModel, } = await this.globalConfigService.getConfigs([
'openaiBaseKey',

View File

@@ -13,17 +13,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.StableDiffusionService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let StableDiffusionService = StableDiffusionService_1 = class StableDiffusionService {
constructor(uploadService, globalConfigService, chatLogService) {
constructor(uploadService, globalConfigService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.logger = new common_1.Logger(StableDiffusionService_1.name);
}
async sdxl(messagesHistory, inputs) {
async sdxl(inputs) {
const { onSuccess, onFailure, apiKey, model, proxyUrl, modelName, timeout, chatId, prompt, } = inputs;
let result = {
answer: '',
@@ -101,7 +99,6 @@ let StableDiffusionService = StableDiffusionService_1 = class StableDiffusionSer
StableDiffusionService = StableDiffusionService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService])
globalConfig_service_1.GlobalConfigService])
], StableDiffusionService);
exports.StableDiffusionService = StableDiffusionService;

View File

@@ -24,6 +24,7 @@ let SunoService = class SunoService {
async suno(inputs) {
var _a, _b, _c;
const { apiKey, proxyUrl, action, prompt, timeout, assistantLogId, taskData, extraParam, } = inputs;
common_1.Logger.debug(`SunoService: ${JSON.stringify(inputs)}`, 'SunoService');
let result = {
text: '',
fileInfo: '',
@@ -53,6 +54,7 @@ let SunoService = class SunoService {
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'SunoService');
try {
response = await axios_1.default.post(url, payloadJson, { headers });
common_1.Logger.debug(`任务提交结果,状态码: ${response.status}, 状态消息: ${response.statusText}, 数据: ${JSON.stringify(response.data)}`);
}
catch (error) {
common_1.Logger.error(`任务提交失败: ${error.message}`, 'SunoService');