alemonjs-aichat 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.js +162 -13
- package/lib/config.js +15 -4
- package/lib/data/help.json.js +17 -1
- package/lib/data/ttsmodels.json.js +4992 -0
- package/lib/index.js +5 -2
- package/lib/middleware/mw.js +69 -29
- package/lib/response/config/res.js +12 -7
- package/lib/response/setting/res.js +19 -1
- package/lib/response/tools/res.js +215 -38
- package/lib/response/zreply/res.js +62 -16
- package/lib/s3.js +9 -3
- package/package.json +4 -4
package/lib/api.js
CHANGED
|
@@ -2,6 +2,7 @@ import { redis } from './redis.js';
|
|
|
2
2
|
import { getConfigValue } from 'alemonjs';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
import { uploadImageToR2 } from './s3.js';
|
|
5
|
+
import ttsmodels from './data/ttsmodels.json.js';
|
|
5
6
|
|
|
6
7
|
const value = getConfigValue();
|
|
7
8
|
const getTimeString = () => {
|
|
@@ -25,6 +26,41 @@ class TTSClient {
|
|
|
25
26
|
const data = await res.json();
|
|
26
27
|
return data.models;
|
|
27
28
|
}
|
|
29
|
+
async installModel(modelname) {
|
|
30
|
+
// 遍历ttsmodels,查找model对应的下载链接,模糊匹配
|
|
31
|
+
const matchedModel = ttsmodels.find((m) => {
|
|
32
|
+
return m.models.find((md) => md.modelname.includes(modelname));
|
|
33
|
+
});
|
|
34
|
+
if (!matchedModel) {
|
|
35
|
+
return { success: false, message: "未找到对应的模型" };
|
|
36
|
+
}
|
|
37
|
+
const modelInfo = matchedModel.models.find((md) => md.modelname.includes(modelname));
|
|
38
|
+
if (!modelInfo) {
|
|
39
|
+
return { success: false, message: "未找到对应的模型" };
|
|
40
|
+
}
|
|
41
|
+
// console.log("matchedModel", matchedModel);
|
|
42
|
+
console.log("modelInfo", modelInfo);
|
|
43
|
+
// 在这里实现模型安装逻辑
|
|
44
|
+
const res = await fetch(`${this.host}/install_model`, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: {
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
category: matchedModel.category,
|
|
51
|
+
dl_url: modelInfo.dl_link,
|
|
52
|
+
language: matchedModel.lang,
|
|
53
|
+
model_name: modelInfo.modelname,
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
const data = await res.json();
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
const err = await res.text();
|
|
59
|
+
console.log(err);
|
|
60
|
+
return { success: false, message: "模型安装失败: " + err };
|
|
61
|
+
}
|
|
62
|
+
return { success: true, message: data.msg };
|
|
63
|
+
}
|
|
28
64
|
async setModel(model, emotion) {
|
|
29
65
|
const models = await this.getModels("v4");
|
|
30
66
|
if (!models) {
|
|
@@ -319,20 +355,20 @@ const availableTools = {
|
|
|
319
355
|
return "未找到AI配置";
|
|
320
356
|
}
|
|
321
357
|
const aiConfig = JSON.parse(aiConfigStr);
|
|
322
|
-
// 处理单个或多个图片
|
|
323
|
-
const imageUrls = Array.isArray(image_url) ? image_url : [image_url];
|
|
324
|
-
const imgBase64Array = [];
|
|
325
|
-
for (const url of imageUrls) {
|
|
326
|
-
let imgBase64 = "";
|
|
327
|
-
if (url.startsWith("https://imgen.x.ai")) {
|
|
328
|
-
imgBase64 = url;
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
imgBase64 = await uploadImageToR2(url);
|
|
332
|
-
}
|
|
333
|
-
imgBase64Array.push(imgBase64);
|
|
334
|
-
}
|
|
335
358
|
try {
|
|
359
|
+
// 处理单个或多个图片
|
|
360
|
+
const imageUrls = Array.isArray(image_url) ? image_url : [image_url];
|
|
361
|
+
const imgBase64Array = [];
|
|
362
|
+
for (const url of imageUrls) {
|
|
363
|
+
let imgBase64 = "";
|
|
364
|
+
if (url.startsWith("https://imgen.x.ai")) {
|
|
365
|
+
imgBase64 = url;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
imgBase64 = await uploadImageToR2(url);
|
|
369
|
+
}
|
|
370
|
+
imgBase64Array.push(imgBase64);
|
|
371
|
+
}
|
|
336
372
|
const requestBody = Array.isArray(image_url)
|
|
337
373
|
? {
|
|
338
374
|
model: "grok-imagine-image",
|
|
@@ -375,6 +411,119 @@ const availableTools = {
|
|
|
375
411
|
return "调用接口失败" + error;
|
|
376
412
|
}
|
|
377
413
|
},
|
|
414
|
+
AIVideos: async ({ image_url, instruction }) => {
|
|
415
|
+
// 从aiconfig中查询grok配置
|
|
416
|
+
const aiConfigStr = await redis.get(`ai:list:grok`);
|
|
417
|
+
if (!aiConfigStr) {
|
|
418
|
+
return "未找到AI配置";
|
|
419
|
+
}
|
|
420
|
+
const aiConfig = JSON.parse(aiConfigStr);
|
|
421
|
+
// 处理单个或多个图片
|
|
422
|
+
const imageUrls = Array.isArray(image_url) ? image_url : [image_url];
|
|
423
|
+
const imgBase64Array = [];
|
|
424
|
+
for (const url of imageUrls) {
|
|
425
|
+
let imgBase64 = "";
|
|
426
|
+
if (url) {
|
|
427
|
+
if (url.startsWith("https://imgen.x.ai")) {
|
|
428
|
+
imgBase64 = url;
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
imgBase64 = await uploadImageToR2(url);
|
|
432
|
+
}
|
|
433
|
+
imgBase64Array.push(imgBase64);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
const requestBody = imgBase64Array.length === 0
|
|
438
|
+
? {
|
|
439
|
+
model: "grok-imagine-video",
|
|
440
|
+
prompt: instruction,
|
|
441
|
+
aspect_ratio: "16:9",
|
|
442
|
+
resolution: "720p",
|
|
443
|
+
}
|
|
444
|
+
: Array.isArray(image_url)
|
|
445
|
+
? {
|
|
446
|
+
model: "grok-imagine-video",
|
|
447
|
+
images: imgBase64Array.map((base64) => ({
|
|
448
|
+
url: base64,
|
|
449
|
+
type: "image_url",
|
|
450
|
+
})),
|
|
451
|
+
prompt: instruction,
|
|
452
|
+
resolution: "720p",
|
|
453
|
+
}
|
|
454
|
+
: {
|
|
455
|
+
model: "grok-imagine-video",
|
|
456
|
+
image: {
|
|
457
|
+
url: imgBase64Array[0],
|
|
458
|
+
type: "image_url",
|
|
459
|
+
},
|
|
460
|
+
prompt: instruction,
|
|
461
|
+
resolution: "720p",
|
|
462
|
+
};
|
|
463
|
+
console.log("最终请求体", requestBody);
|
|
464
|
+
const res = await fetch(aiConfig.host + "/videos/generations", {
|
|
465
|
+
method: "POST",
|
|
466
|
+
headers: {
|
|
467
|
+
"Content-Type": "application/json",
|
|
468
|
+
Authorization: `Bearer ${aiConfig.key}`,
|
|
469
|
+
},
|
|
470
|
+
body: JSON.stringify(requestBody),
|
|
471
|
+
});
|
|
472
|
+
if (!res.ok) {
|
|
473
|
+
const err = await res.text();
|
|
474
|
+
console.log(err);
|
|
475
|
+
return err;
|
|
476
|
+
}
|
|
477
|
+
const video = await res.json();
|
|
478
|
+
console.log("画好了", video);
|
|
479
|
+
return {
|
|
480
|
+
data: video,
|
|
481
|
+
msg: "生成任务已提交,稍后会自动发送生成结果,无需继续等待~,将request_id提供给用户,方便查询生成状态",
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
catch (error) {
|
|
485
|
+
console.error("Error calling AIPS:", error);
|
|
486
|
+
return "调用接口失败" + error;
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
AIVideoResult: async ({ request_id }) => {
|
|
490
|
+
// 从aiconfig中查询grok配置
|
|
491
|
+
const aiConfigStr = await redis.get(`ai:list:grok`);
|
|
492
|
+
if (!aiConfigStr) {
|
|
493
|
+
return "未找到AI配置";
|
|
494
|
+
}
|
|
495
|
+
const aiConfig = JSON.parse(aiConfigStr);
|
|
496
|
+
try {
|
|
497
|
+
const res = await fetch(aiConfig.host + `/videos/${request_id}`, {
|
|
498
|
+
method: "GET",
|
|
499
|
+
headers: {
|
|
500
|
+
"Content-Type": "application/json",
|
|
501
|
+
Authorization: `Bearer ${aiConfig.key}`,
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
if (!res.ok) {
|
|
505
|
+
const err = await res.text();
|
|
506
|
+
console.log(err);
|
|
507
|
+
return err;
|
|
508
|
+
}
|
|
509
|
+
const video = await res.json();
|
|
510
|
+
console.log("视频生成结果", video);
|
|
511
|
+
return video;
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
console.error("Error calling AIVideoResult:", error);
|
|
515
|
+
return "调用接口失败" + error;
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
/**
|
|
519
|
+
* 获取TTS模型列表
|
|
520
|
+
* @returns TTS模型列表
|
|
521
|
+
*/
|
|
522
|
+
GetTTSModels: async () => {
|
|
523
|
+
const ttsClient = new TTSClient();
|
|
524
|
+
const models = await ttsClient.getModels("v4");
|
|
525
|
+
return models;
|
|
526
|
+
},
|
|
378
527
|
};
|
|
379
528
|
|
|
380
529
|
export { TTSClient, availableTools, getTimeString };
|
package/lib/config.js
CHANGED
|
@@ -82,8 +82,8 @@ const getAIChatHistory = async (guid) => {
|
|
|
82
82
|
};
|
|
83
83
|
const addAIChatHistory = async (guid, data) => {
|
|
84
84
|
const history = await getAIChatHistory(guid);
|
|
85
|
-
// 处理messages, 不要超过
|
|
86
|
-
while (history.length >=
|
|
85
|
+
// 处理messages, 不要超过40条,超过就删除最早的
|
|
86
|
+
while (history.length >= 40) {
|
|
87
87
|
history.shift();
|
|
88
88
|
}
|
|
89
89
|
history.push(data);
|
|
@@ -228,7 +228,7 @@ const tools = [
|
|
|
228
228
|
// function: {
|
|
229
229
|
// name: "StableDiffusionImageToPrompt",
|
|
230
230
|
// description:
|
|
231
|
-
// "使用 Stable Diffusion 将图片转换为提示词,
|
|
231
|
+
// "使用 Stable Diffusion 将图片转换为提示词, 它只对动漫图片有效, 只能用来提取动漫图片的提示词",
|
|
232
232
|
// parameters: {
|
|
233
233
|
// type: "object",
|
|
234
234
|
// properties: {
|
|
@@ -296,7 +296,7 @@ const tools = [
|
|
|
296
296
|
type: "function",
|
|
297
297
|
function: {
|
|
298
298
|
name: "AIPS",
|
|
299
|
-
description: "AI修图工具,
|
|
299
|
+
description: "AI修图工具, 可以对图片进行图像编辑, 无法用于图片分析. 使用时只需提供需要修改的图片url和修改需求即可. 当遇到审核拦截的问题时, 请不要再次尝试提交相同的修改需求, 这可能会导致账号被封禁. 请修改你的修改需求文本描述, 使其更具体和详细, 以避免触发审核拦截. ",
|
|
300
300
|
parameters: {
|
|
301
301
|
type: "object",
|
|
302
302
|
properties: {
|
|
@@ -313,6 +313,17 @@ const tools = [
|
|
|
313
313
|
},
|
|
314
314
|
},
|
|
315
315
|
},
|
|
316
|
+
{
|
|
317
|
+
type: "function",
|
|
318
|
+
function: {
|
|
319
|
+
name: "GetTTSModels",
|
|
320
|
+
description: "获取可用的TTS模型列表, 可以用来选择不同的音色进行语音合成. 模型会不定期更新, 你可以定期调用这个工具来获取最新的模型列表. 当你想要使用TTS功能时, 可以先调用这个工具来获取可用的模型列表, 然后选择一个你喜欢的模型来进行语音合成. 例如, 你可以选择一个叫做'派蒙-默认'的模型来模仿派蒙的声音进行语音合成",
|
|
321
|
+
parameters: {
|
|
322
|
+
type: "object",
|
|
323
|
+
properties: {},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
},
|
|
316
327
|
];
|
|
317
328
|
|
|
318
329
|
export { addAI, addAIChatHistory, addPrompt, affectionSwitch, clearAIChatHistory, defaultAIConfig, deleteAI, deletePrompt, getAIChatHistory, getAIConfig, getAIList, getAffectionLevel, getAffectionLevelAll, getAffectionSwitch, getPromptList, getTTSResponseSwitch, incrementAffectionLevel, resetAffectionLevel, resetAllAffectionLevels, setAffectionLevel, setBotID, switchAI, switchPrompt, systemPrompt, tools };
|
package/lib/data/help.json.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var title = "IA聊天插件";
|
|
2
2
|
var name = "aichat";
|
|
3
|
-
var version = "v1.0.
|
|
3
|
+
var version = "v1.0.15";
|
|
4
4
|
var by = "AlemonJS";
|
|
5
5
|
var list = [
|
|
6
6
|
{
|
|
@@ -115,6 +115,22 @@ var list = [
|
|
|
115
115
|
{
|
|
116
116
|
cmd: "/画图 [提示词]",
|
|
117
117
|
desc: "使用ai画图功能根据提示词生成图片。提示词可以包含对图片的描述和要求。"
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
cmd: "/[开启|关闭]画图提示词优化",
|
|
121
|
+
desc: "开启后画图提示词会自动优化以获得更好的画图效果,适合不擅长编写提示词的用户使用。"
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
cmd: "/生成视频 [提示词]",
|
|
125
|
+
desc: "使用ai生成视频功能根据提示词生成视频。提示词可以包含对视频的描述和要求。"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
cmd: "/[角色名]说 [内容]",
|
|
129
|
+
desc: "让指定角色说出内容。角色名需要在提示词中定义,内容可以是任何文本。"
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
cmd: "/安装语音 [角色名]",
|
|
133
|
+
desc: "安装指定角色的语音, 以米游的角色为主"
|
|
118
134
|
}
|
|
119
135
|
]
|
|
120
136
|
}
|