From 76008952b50847fda3c50a2c94afc5cee8191f72 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sat, 24 Jun 2023 12:13:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AAgoo?= =?UTF-8?q?gle=E6=90=9C=E7=B4=A2=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 27 +++++++++++++-- utils/config.js | 1 + utils/tools/SerpGoogleTool.js | 30 +++++++++++++++++ utils/tools/SerpTool.js | 63 +++++++++++++++++------------------ 4 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 utils/tools/SerpGoogleTool.js diff --git a/apps/chat.js b/apps/chat.js index bc6cfb5..1082930 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -55,8 +55,9 @@ import { SearchVideoTool } from '../utils/tools/SearchBilibiliTool.js' import { SearchMusicTool } from '../utils/tools/SearchMusicTool.js' import { QueryStarRailTool } from '../utils/tools/QueryStarRailTool.js' import { WebsiteTool } from '../utils/tools/WebsiteTool.js' -import {WeatherTool} from "../utils/tools/WeatherTool.js"; -import {SerpTool} from "../utils/tools/SerpTool.js"; +import { WeatherTool } from '../utils/tools/WeatherTool.js' +import { SerpTool } from '../utils/tools/SerpTool.js' +import { SerpGoogleTool } from '../utils/tools/SerpGoogleTool.js' try { await import('emoji-strip') } catch (err) { @@ -1929,6 +1930,26 @@ export class chatgpt extends plugin { } let isAdmin = e.sender.role === 'admin' || e.sender.role === 'owner' let sender = e.sender.user_id + let serpTool + switch (Config.serpSource) { + case 'google': { + serpTool = new SerpGoogleTool() + break + } + case 'bing': { + if (!Config.azSerpKey) { + logger.warn('未配置bing搜索密钥,转为使用google搜索') + serpTool = new SerpGoogleTool() + } else { + serpTool = new SerpTool() + } + break + } + default: { + serpTool = new SerpGoogleTool() + } + } + let tools = [ new SearchVideoTool(), new SendVideoTool(), @@ -1942,7 +1963,7 @@ export class chatgpt extends plugin { new JinyanTool(), new KickOutTool(), new WeatherTool(), - new SerpTool(), + serpTool ] // if (e.sender.role === 'admin' || e.sender.role === 'owner') { // tools.push(...[new JinyanTool(), new KickOutTool()]) diff --git a/utils/config.js b/utils/config.js index da28f08..a1b2f28 100644 --- a/utils/config.js +++ b/utils/config.js @@ -127,6 +127,7 @@ const defaultConfig = { enableGenerateContents: false, amapKey: '', azSerpKey: '', + serpSource: 'google', version: 'v2.6.2' } const _path = process.cwd() diff --git a/utils/tools/SerpGoogleTool.js b/utils/tools/SerpGoogleTool.js new file mode 100644 index 0000000..6c67a3a --- /dev/null +++ b/utils/tools/SerpGoogleTool.js @@ -0,0 +1,30 @@ +import { AbstractTool } from './AbstractTool.js' + +export class SerpGoogleTool extends AbstractTool { + name = 'google' + + parameters = { + properties: { + q: { + type: 'string', + description: 'search keyword' + } + }, + required: ['q'] + } + + func = async function (opts) { + let { q } = opts + let serpRes = await fetch(`https://serp.ikechan8370.com/google?q=${encodeURIComponent(q)}&lang=zh-CN&limit=10`, { + headers: { + 'X-From-Library': 'ikechan8370' + } + }) + serpRes = await serpRes.json() + + let res = serpRes.data + return `the search results are here in json format:\n${JSON.stringify(res)}` + } + + description = 'Useful when you want to search something from the internet. If you don\'t know much about the user\'s question, just search about it!' +} diff --git a/utils/tools/SerpTool.js b/utils/tools/SerpTool.js index c987ced..af579ea 100644 --- a/utils/tools/SerpTool.js +++ b/utils/tools/SerpTool.js @@ -1,41 +1,40 @@ import { AbstractTool } from './AbstractTool.js' -import {Config} from "../config.js"; +import { Config } from '../config.js' export class SerpTool extends AbstractTool { - name = 'serp' + name = 'serp' - parameters = { - properties: { - q: { - type: 'string', - description: 'search keyword' - } - }, - required: ['q'] - } + parameters = { + properties: { + q: { + type: 'string', + description: 'search keyword' + } + }, + required: ['q'] + } - func = async function (opts) { - let { q } = opts - let key = Config.azSerpKey + func = async function (opts) { + let { q } = opts + let key = Config.azSerpKey - let serpRes = await fetch(`https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(q)}&mkt=zh-CN`, { - headers: { - 'Ocp-Apim-Subscription-Key': key - } - }) - serpRes = await serpRes.json() + let serpRes = await fetch(`https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(q)}&mkt=zh-CN`, { + headers: { + 'Ocp-Apim-Subscription-Key': key + } + }) + serpRes = await serpRes.json() - let res = serpRes.webPages.value - res.forEach(p => { - delete p.displayUrl - delete p.isFamilyFriendly - delete p.thumbnailUrl - delete p.id - delete p.isNavigational + let res = serpRes.webPages.value + res.forEach(p => { + delete p.displayUrl + delete p.isFamilyFriendly + delete p.thumbnailUrl + delete p.id + delete p.isNavigational + }) + return `the search results are here in json format:\n${JSON.stringify(res)}` + } - }) - return `the search results are here in json format:\n${JSON.stringify(res)}` - } - - description = 'Useful when you want to search something from the internet. If you don\'t know much about the user\'s question, just search about it!' + description = 'Useful when you want to search something from the internet. If you don\'t know much about the user\'s question, just search about it!' }