mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-10-14 08:09:19 +00:00
🌟 feat: #linux
命令添加AI功能自学习
This commit is contained in:
parent
7f4eed9cac
commit
041bf3a056
@ -17,7 +17,12 @@ import { LINUX_AI_PROMPT, LINUX_QUERY, REDIS_YUNZAI_LINUX } from "../constants/q
|
||||
import config from "../model/config.js";
|
||||
import { estimateReadingTime } from "../utils/common.js";
|
||||
import { OpenaiBuilder } from "../utils/openai-builder.js";
|
||||
import { redisExistAndGetKey, redisExistAndInsertObject, redisSetKey } from "../utils/redis-util.js";
|
||||
import {
|
||||
redisExistAndGetKey,
|
||||
redisExistAndInsertObject,
|
||||
redisExistAndUpdateObject,
|
||||
redisSetKey
|
||||
} from "../utils/redis-util.js";
|
||||
import { textArrayToMakeForward } from "../utils/yunzai-util.js";
|
||||
|
||||
export class query extends plugin {
|
||||
@ -322,13 +327,70 @@ export class query extends plugin {
|
||||
const { ans: kimiAns, model } = aiBuilder;
|
||||
const Msg = await Bot.makeForwardMsg(textArrayToMakeForward(e, [`「R插件 x ${ model }」联合为您总结内容:`, kimiAns]));
|
||||
await e.reply(Msg);
|
||||
// 提取AI返回的内容并进行解析
|
||||
if (linuxOrderData.content.trim() === '') {
|
||||
const parsedData = this.parseAiResponse(order, kimiAns);
|
||||
await redisExistAndUpdateObject(REDIS_YUNZAI_LINUX, order, parsedData);
|
||||
e.reply(`已重新学习命令 ${ order } 的用法,当前已经更新功能为:${ parsedData.content }`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
e.reply(`暂时无法查询到当前命令!`);
|
||||
logger.error(logger.red(`[R插件][linux]: ${ err }`));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* AI响应解析函数
|
||||
* @param order
|
||||
* @param aiResponse
|
||||
* @returns {{linux, link: string, content: (*|string)}}
|
||||
*/
|
||||
parseAiResponse(order, aiResponse) {
|
||||
// 检查 aiResponse 是否为有效字符串
|
||||
if (!aiResponse || typeof aiResponse !== 'string') {
|
||||
return {
|
||||
linux: order,
|
||||
content: '',
|
||||
link: `https://www.linuxcool.com/${order}` // 默认参考链接
|
||||
};
|
||||
}
|
||||
|
||||
// 初始化保存数据的对象
|
||||
let parsedData = {
|
||||
linux: order,
|
||||
content: '',
|
||||
link: `https://www.linuxcool.com/${order}` // 默认参考链接
|
||||
};
|
||||
|
||||
// 清理多余的换行符,避免意外的分隔问题
|
||||
const lines = aiResponse.split('\n').map(line => line.trim()).filter(line => line);
|
||||
|
||||
// 遍历每一行查找命令相关的描述
|
||||
lines.forEach(line => {
|
||||
// 允许命令带有可选的路径,修改正则表达式以适应路径变化
|
||||
const match = line.match(/[`'“](.+?)[`'”]\s*[::—-]?\s*(.*)/);
|
||||
|
||||
if (match) {
|
||||
logger.info(match)
|
||||
const command = match[1].trim(); // 提取命令部分
|
||||
const description = match[2].trim(); // 提取描述部分// 同样忽略路径
|
||||
|
||||
// 如果命令和参数部分匹配,保存描述
|
||||
if (command.includes(order)) {
|
||||
parsedData.content = description;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 如果没有找到具体的描述内容,则给出默认提示
|
||||
if (!parsedData.content) {
|
||||
parsedData.content = '暂时无法解析该命令的具体描述。';
|
||||
}
|
||||
|
||||
return parsedData;
|
||||
}
|
||||
// 删除标签
|
||||
removeTag(title) {
|
||||
const titleRex = /<[^>]+>/g;
|
||||
|
@ -4,7 +4,22 @@
|
||||
*/
|
||||
export const LINUX_QUERY = "https://api.pearktrue.cn/api/linux/?keyword={}"
|
||||
|
||||
export const LINUX_AI_PROMPT = "You are Linux Specialist in Linux Systems Administration, Engineering, Open Source Linux Development, Web Development, Syslog Analysis, Server Deployment, UNIX Troubleshooting, Creating Custom System Documentation, And BASH Shell Scripting With Python Development. ";
|
||||
export const LINUX_AI_PROMPT = "- Role: Linux命令专家\n" +
|
||||
"- Background: 用户需要从特定的Linux命令网站获取常见命令的用法,希望得到简洁明了的回复。\n" +
|
||||
"- Profile: 你是一位对Linux命令有深入理解的专家,能够快速从网站中提取关键信息,并以简洁的方式呈现给用户。\n" +
|
||||
"- Skills: 你具备快速阅读、信息提取、语言组织和表达的能力,能够将复杂的命令用法简化为易于理解的格式。\n" +
|
||||
"- Goals: 提供一个精确、简洁的Linux命令用法列表,帮助用户快速掌握和使用。\n" +
|
||||
"- Constrains: 确保回复内容准确无误,避免使用过于技术性的语言,确保用户易于理解。\n" +
|
||||
"- OutputFormat: 列表形式,每条命令用法简洁明了,不超过两句话。\n" +
|
||||
"- Workflow:\n" +
|
||||
" 1. 访问并分析用户提供的网站链接。\n" +
|
||||
" 2. 提取网站上的Linux命令及其常见用法。\n" +
|
||||
" 3. 将提取的信息以简洁明了的方式组织成列表。\n" +
|
||||
"- Examples:\n" +
|
||||
" - 例子1: 命令 'ls' — 列出目录内容。\n" +
|
||||
" - 例子2: 命令 'cd' — 更改当前目录。\n" +
|
||||
" - 例子3: 命令 'cp' — 复制文件或目录。\n" +
|
||||
"- Initialization: 在第一次对话中,请直接输出以下:您好!我是Linux命令专家,我将为您提供简洁明了的Linux命令用法。";
|
||||
|
||||
/**
|
||||
* Linux命令缓存
|
||||
|
@ -51,8 +51,23 @@ export async function redisExistAndInsertObject(key, obj) {
|
||||
let objs = await redisExistAndGetKey(key);
|
||||
if (objs) {
|
||||
objs = {...objs, ...obj};
|
||||
await redis.set(key, JSON.stringify(objs));
|
||||
await redisSetKey(key, objs);
|
||||
} else {
|
||||
await redis.set(key, JSON.stringify(obj));
|
||||
await redisSetKey(key, obj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Redis中某个对象的值
|
||||
* @param key
|
||||
* @param updateKey
|
||||
* @param updateObj
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function redisExistAndUpdateObject(key, updateKey, updateObj) {
|
||||
let objs = await redisExistAndGetKey(key);
|
||||
if (Object.keys(objs).includes(updateKey)) {
|
||||
objs[updateKey] = updateObj;
|
||||
await redisSetKey(key, objs);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user