plugins: add wiki search (#2886)

* plugins: add wiki search

* 扁平化代码

* fix: url error
This commit is contained in:
papapatrick
2024-10-12 15:02:00 +08:00
committed by GitHub
parent fe6c8897ce
commit daa5b552b1
7 changed files with 632 additions and 70 deletions

View File

@@ -3,11 +3,12 @@
"version": "1.0.0",
"type": "module",
"dependencies": {
"duck-duck-scrape": "^2.2.5",
"lodash": "^4.17.21",
"axios": "^1.5.1",
"duck-duck-scrape": "^2.2.5",
"echarts": "5.4.1",
"expr-eval": "^2.0.2",
"echarts": "5.4.1"
"lodash": "^4.17.21",
"wikijs": "^6.4.1"
},
"devDependencies": {
"@fastgpt/global": "workspace:*",

View File

@@ -24,7 +24,8 @@ const packagePluginList = [
'duckduckgo/searchNews',
'duckduckgo/searchVideo',
'drawing',
'drawing/baseChart'
'drawing/baseChart',
'wiki'
];
export const list = [...staticPluginList, ...packagePluginList];

View File

@@ -0,0 +1,43 @@
import { getErrText } from '@fastgpt/global/common/error/utils';
import { addLog } from '@fastgpt/service/common/system/log';
import { delay } from '@fastgpt/global/common/system/utils';
import wiki from 'wikijs';
type Props = {
query: string;
};
// Response type same as HTTP outputs
type Response = Promise<{
result: string;
}>;
const main = async (props: Props, retry = 3): Response => {
const { query } = props;
try {
const searchResults = await wiki({ apiUrl: 'https://zh.wikipedia.org/w/api.php' })
.page(query)
.then((page) => {
return page.summary();
});
return {
result: searchResults
};
} catch (error) {
console.log(error);
if (retry <= 0) {
addLog.warn('search wiki error', { error });
return {
result: getErrText(error, 'Failed to fetch data from wiki')
};
}
await delay(Math.random() * 5000);
return main(props, retry - 1);
}
};
export default main;

View File

@@ -0,0 +1,340 @@
{
"author": "",
"version": "4811",
"name": "Wiki搜索",
"avatar": "core/workflow/template/wiki",
"intro": "在Wiki中查询释义。",
"showStatus": true,
"weight": 10,
"isTool": true,
"templateType": "tools",
"workflow": {
"nodes": [
{
"nodeId": "pluginInput",
"name": "插件开始",
"intro": "可以配置插件需要哪些输入,利用这些输入来运行插件",
"avatar": "core/workflow/template/workflowStart",
"flowNodeType": "pluginInput",
"showStatus": false,
"position": {
"x": 484.02074451450517,
"y": -79.06127656499825
},
"version": "481",
"inputs": [
{
"renderTypeList": ["reference"],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "query",
"label": "query",
"description": "检索词",
"required": true,
"toolDescription": "检索词"
}
],
"outputs": [
{
"id": "query",
"valueType": "string",
"key": "query",
"label": "query",
"type": "hidden"
}
]
},
{
"nodeId": "pluginOutput",
"name": "插件输出",
"intro": "自定义配置外部输出,使用插件时,仅暴露自定义配置的输出",
"avatar": "core/workflow/template/pluginOutput",
"flowNodeType": "pluginOutput",
"showStatus": false,
"position": {
"x": 1759.5180706702588,
"y": -60.56127656499825
},
"version": "481",
"inputs": [
{
"renderTypeList": ["reference"],
"valueType": "string",
"canEdit": true,
"key": "result",
"label": "result",
"description": " 检索结果",
"value": ["hjnVuJAOwyXV", "lEyy5QqyIBrK"]
}
],
"outputs": []
},
{
"nodeId": "hjnVuJAOwyXV",
"name": "HTTP 请求",
"intro": "可以发出一个 HTTP 请求,实现更为复杂的操作(联网搜索、数据库查询等)",
"avatar": "core/workflow/template/httpRequest",
"flowNodeType": "httpRequest468",
"showStatus": true,
"position": {
"x": 1054.6774638324207,
"y": -403.06127656499825
},
"version": "481",
"inputs": [
{
"key": "system_addInputParam",
"renderTypeList": ["addInputParam"],
"valueType": "dynamic",
"label": "",
"required": false,
"description": "common:core.module.input.description.HTTP Dynamic Input",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpMethod",
"renderTypeList": ["custom"],
"valueType": "string",
"label": "",
"value": "POST",
"required": true,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpTimeout",
"renderTypeList": ["custom"],
"valueType": "number",
"label": "",
"value": 30,
"min": 5,
"max": 600,
"required": true,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpReqUrl",
"renderTypeList": ["hidden"],
"valueType": "string",
"label": "",
"description": "common:core.module.input.description.Http Request Url",
"placeholder": "https://api.ai.com/getInventory",
"required": false,
"value": "wiki",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpHeader",
"renderTypeList": ["custom"],
"valueType": "any",
"value": [],
"label": "",
"description": "common:core.module.input.description.Http Request Header",
"placeholder": "common:core.module.input.description.Http Request Header",
"required": false,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpParams",
"renderTypeList": ["hidden"],
"valueType": "any",
"value": [],
"label": "",
"required": false,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpJsonBody",
"renderTypeList": ["hidden"],
"valueType": "any",
"value": "{\n \"query\": \"{{query}}\"\n}",
"label": "",
"required": false,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpFormBody",
"renderTypeList": ["hidden"],
"valueType": "any",
"value": [],
"label": "",
"required": false,
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpContentType",
"renderTypeList": ["hidden"],
"valueType": "string",
"value": "json",
"label": "",
"required": false,
"debugLabel": "",
"toolDescription": ""
},
{
"renderTypeList": ["reference"],
"valueType": "string",
"canEdit": true,
"key": "query",
"label": "query",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": ["pluginInput", "query"]
}
],
"outputs": [
{
"id": "error",
"key": "error",
"label": "workflow:request_error",
"description": "HTTP请求错误信息成功时返回空",
"valueType": "object",
"type": "static"
},
{
"id": "httpRawResponse",
"key": "httpRawResponse",
"required": true,
"label": "workflow:raw_response",
"description": "HTTP请求的原始响应。只能接受字符串或JSON类型响应数据。",
"valueType": "any",
"type": "static"
},
{
"id": "system_addOutputParam",
"key": "system_addOutputParam",
"type": "dynamic",
"valueType": "dynamic",
"label": "",
"customFieldConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": false
}
},
{
"id": "lEyy5QqyIBrK",
"valueType": "string",
"type": "dynamic",
"key": "result",
"label": "result"
}
]
},
{
"nodeId": "f1mRh1D85H2D",
"name": "系统配置",
"intro": "",
"avatar": "core/workflow/template/systemConfig",
"flowNodeType": "pluginConfig",
"position": {
"x": -28.511358745511643,
"y": -103.56127656499825
},
"version": "4811",
"inputs": [],
"outputs": []
}
],
"edges": [
{
"source": "pluginInput",
"target": "hjnVuJAOwyXV",
"sourceHandle": "pluginInput-source-right",
"targetHandle": "hjnVuJAOwyXV-target-left"
},
{
"source": "hjnVuJAOwyXV",
"target": "pluginOutput",
"sourceHandle": "hjnVuJAOwyXV-source-right",
"targetHandle": "pluginOutput-target-left"
}
],
"chatConfig": {
"welcomeText": "",
"variables": [],
"questionGuide": false,
"ttsConfig": {
"type": "web"
},
"whisperConfig": {
"open": false,
"autoSend": false,
"autoTTSResponse": false
},
"chatInputGuide": {
"open": false,
"textList": [],
"customUrl": ""
},
"instruction": "",
"_id": "67075cd2702bd7168ef8cb2d"
}
}
}

View File

@@ -228,6 +228,7 @@ export const iconPaths = {
import('./icons/core/workflow/template/duckduckgo.svg'),
'core/workflow/template/extractJson': () =>
import('./icons/core/workflow/template/extractJson.svg'),
'core/workflow/template/wiki': () => import('./icons/core/workflow/template/wiki.svg'),
'core/workflow/template/fetchUrl': () => import('./icons/core/workflow/template/fetchUrl.svg'),
'core/workflow/template/formInput': () => import('./icons/core/workflow/template/formInput.svg'),
'core/workflow/template/getTime': () => import('./icons/core/workflow/template/getTime.svg'),

View File

@@ -0,0 +1 @@
<svg t="1728613783770" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4250" width="200" height="200"><path d="M530.077727 610.234325c-49.762357 97.384981-104.639199 199.047382-151.85864 295.595298-0.433882 0.433882-22.974247 0-22.974247-0.433882-71.867818-168.012551-147.146315-334.722434-219.044832-503.168868C118.774127 361.393887 60.486606 295.04476 20.521383 295.478641c0-4.681626-0.433882-15.315824-0.433882-21.703301l247.972675 0 0 21.269419c-29.360701 1.704828-80.827887 20.400632-66.349127 52.30425 33.608445 76.146261 158.6493 367.927697 192.257745 441.935248 22.974247-45.514613 88.455611-167.143765 115.27442-218.61095-21.269419-43.405579-89.757257-205.031677-111.460558-244.995877-14.881942-27.221991-55.281047-29.795607-85.478813-30.197766 0-6.821359 0-12.340049 0-21.269419l218.177068 0.403183 0 19.99745c-29.763884 0.867764-58.256822 11.905144-45.079708 39.995923 28.92682 61.23362 46.785559 104.205317 73.604368 160.354128 8.495488-16.587793 53.141314-106.34505 73.573669-154.401556 13.610996-31.469736-5.952572-43.808762-59.125609-45.080731 0.433882-5.54939 0-15.749706 0.433882-20.866237 68.053955-0.403183 170.120562-0.403183 188.413184-0.837064l0 20.834514c-34.44551 1.270946-70.162989 19.563568-88.889493 48.490388l-90.594321 188.010001c9.766434 24.679076 96.981798 218.61095 105.910145 239.880369l187.576119-432.571996c-13.177114-35.282574-55.714929-42.940998-72.301699-43.37488 0-5.51869 0-14.044878 0-21.269419l195.636702 1.704828 0.433882 0.837064-0.433882 18.726503c-42.940998 1.270946-68.892043 24.245194-85.478813 61.667501-38.290071 88.48631-158.246117 367.493815-237.772358 549.117362l-20.834514 0L530.077727 610.234325z" fill="#3E3A39" p-id="4251"></path></svg>

After

Width:  |  Height:  |  Size: 1.7 KiB