From e39992cfe7252d7a74117e076e70f6e39f14c137 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Sat, 24 Aug 2024 14:51:46 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=9F=20feat:=20Linux=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=E6=9B=B4=E5=BF=AB=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/query.js | 34 ++++++++++++++++++++++++---------- constants/query.js | 8 +++++++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/apps/query.js b/apps/query.js index a92b107..28859c8 100644 --- a/apps/query.js +++ b/apps/query.js @@ -12,12 +12,12 @@ import { HELP_DOC, REDIS_YUNZAI_ANIMELIST } from "../constants/constant.js"; -import { LINUX_AI_PROMPT, LINUX_QUERY } from "../constants/query.js"; +import { LINUX_AI_PROMPT, LINUX_QUERY, REDIS_YUNZAI_LINUX } from "../constants/query.js"; // 配置文件 import config from "../model/config.js"; import { estimateReadingTime } from "../utils/common.js"; import { OpenaiBuilder } from "../utils/openai-builder.js"; -import { redisExistAndGetKey } from "../utils/redis-util.js"; +import { redisExistAndGetKey, redisExistAndInsertObject, redisSetKey } from "../utils/redis-util.js"; import { textArrayToMakeForward } from "../utils/yunzai-util.js"; export class query extends plugin { @@ -282,12 +282,25 @@ export class query extends plugin { async linuxQuery(e) { const order = e.msg.replace(/^#([lL])inux/, "").trim(); - const resp = await fetch(LINUX_QUERY.replace("{}", order), { - headers: { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.142.86 Safari/537.36" - } - }) - const linuxOrderData = await resp.json() + // 查询 Redis 中是否存在这个命令如果存在直接返回没有的话就发起网络请求 + const linuxInRedis = await redisExistAndGetKey(REDIS_YUNZAI_LINUX) + let linuxOrderData; + // 判断这个命令是否在缓存里 + const isOrderInRedis = linuxInRedis && Object.keys(linuxInRedis).includes(order); + if (!isOrderInRedis) { + // 没有在缓存里,直接发起网络请求 + const resp = await fetch(LINUX_QUERY.replace("{}", order), { + headers: { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.142.86 Safari/537.36" + } + }); + linuxOrderData = (await resp.json()).data; + // 如果缓存里没有就保存一份到缓存里 + await redisExistAndInsertObject(REDIS_YUNZAI_LINUX, { [order]: linuxOrderData }); + } else { + // 在缓存里就取出 + linuxOrderData = linuxInRedis[order]; + } try { const builder = await new OpenaiBuilder() .setBaseURL(this.aiBaseURL) @@ -296,8 +309,9 @@ export class query extends plugin { .setPrompt(LINUX_AI_PROMPT) .build(); let aiBuilder; - if (linuxOrderData?.data) { - const { linux, content, link } = linuxOrderData.data; + if (linuxOrderData) { + const { linux, content, link } = linuxOrderData; + // 发送消息 e.reply(`识别:Linux命令 <${ linux }>\n\n功能:${ content }`); aiBuilder = await builder.kimi(`能否帮助根据${ link }网站的Linux命令内容返回一些常见的用法,内容简洁明了即可`) } else { diff --git a/constants/query.js b/constants/query.js index 4d2acb0..218d569 100644 --- a/constants/query.js +++ b/constants/query.js @@ -4,4 +4,10 @@ */ 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. "; \ No newline at end of file +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. "; + +/** + * Linux命令缓存 + * @type {string} + */ +export const REDIS_YUNZAI_LINUX = "Yz:rconsole:query:linux"; \ No newline at end of file