feat: 增加一个google搜索源

This commit is contained in:
ikechan8370 2023-06-24 12:13:45 +08:00
parent 9c07b4fef7
commit 76008952b5
4 changed files with 86 additions and 35 deletions

View file

@ -55,8 +55,9 @@ import { SearchVideoTool } from '../utils/tools/SearchBilibiliTool.js'
import { SearchMusicTool } from '../utils/tools/SearchMusicTool.js' import { SearchMusicTool } from '../utils/tools/SearchMusicTool.js'
import { QueryStarRailTool } from '../utils/tools/QueryStarRailTool.js' import { QueryStarRailTool } from '../utils/tools/QueryStarRailTool.js'
import { WebsiteTool } from '../utils/tools/WebsiteTool.js' import { WebsiteTool } from '../utils/tools/WebsiteTool.js'
import {WeatherTool} from "../utils/tools/WeatherTool.js"; import { WeatherTool } from '../utils/tools/WeatherTool.js'
import {SerpTool} from "../utils/tools/SerpTool.js"; import { SerpTool } from '../utils/tools/SerpTool.js'
import { SerpGoogleTool } from '../utils/tools/SerpGoogleTool.js'
try { try {
await import('emoji-strip') await import('emoji-strip')
} catch (err) { } catch (err) {
@ -1929,6 +1930,26 @@ export class chatgpt extends plugin {
} }
let isAdmin = e.sender.role === 'admin' || e.sender.role === 'owner' let isAdmin = e.sender.role === 'admin' || e.sender.role === 'owner'
let sender = e.sender.user_id 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 = [ let tools = [
new SearchVideoTool(), new SearchVideoTool(),
new SendVideoTool(), new SendVideoTool(),
@ -1942,7 +1963,7 @@ export class chatgpt extends plugin {
new JinyanTool(), new JinyanTool(),
new KickOutTool(), new KickOutTool(),
new WeatherTool(), new WeatherTool(),
new SerpTool(), serpTool
] ]
// if (e.sender.role === 'admin' || e.sender.role === 'owner') { // if (e.sender.role === 'admin' || e.sender.role === 'owner') {
// tools.push(...[new JinyanTool(), new KickOutTool()]) // tools.push(...[new JinyanTool(), new KickOutTool()])

View file

@ -127,6 +127,7 @@ const defaultConfig = {
enableGenerateContents: false, enableGenerateContents: false,
amapKey: '', amapKey: '',
azSerpKey: '', azSerpKey: '',
serpSource: 'google',
version: 'v2.6.2' version: 'v2.6.2'
} }
const _path = process.cwd() const _path = process.cwd()

View file

@ -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!'
}

View file

@ -1,41 +1,40 @@
import { AbstractTool } from './AbstractTool.js' import { AbstractTool } from './AbstractTool.js'
import {Config} from "../config.js"; import { Config } from '../config.js'
export class SerpTool extends AbstractTool { export class SerpTool extends AbstractTool {
name = 'serp' name = 'serp'
parameters = { parameters = {
properties: { properties: {
q: { q: {
type: 'string', type: 'string',
description: 'search keyword' description: 'search keyword'
} }
}, },
required: ['q'] required: ['q']
} }
func = async function (opts) { func = async function (opts) {
let { q } = opts let { q } = opts
let key = Config.azSerpKey let key = Config.azSerpKey
let serpRes = await fetch(`https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(q)}&mkt=zh-CN`, { let serpRes = await fetch(`https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(q)}&mkt=zh-CN`, {
headers: { headers: {
'Ocp-Apim-Subscription-Key': key 'Ocp-Apim-Subscription-Key': key
} }
}) })
serpRes = await serpRes.json() serpRes = await serpRes.json()
let res = serpRes.webPages.value let res = serpRes.webPages.value
res.forEach(p => { res.forEach(p => {
delete p.displayUrl delete p.displayUrl
delete p.isFamilyFriendly delete p.isFamilyFriendly
delete p.thumbnailUrl delete p.thumbnailUrl
delete p.id delete p.id
delete p.isNavigational delete p.isNavigational
})
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!'
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!'
} }