Skip to content

Commit

Permalink
plugins: add wiki search (labring#2886)
Browse files Browse the repository at this point in the history
* plugins: add wiki search

* 扁平化代码

* fix: url error
  • Loading branch information
Patrickill authored and shilin66 committed Oct 12, 2024
1 parent 9bce721 commit 701643f
Show file tree
Hide file tree
Showing 7 changed files with 632 additions and 70 deletions.
7 changes: 4 additions & 3 deletions packages/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const packagePluginList = [
'duckduckgo/searchNews',
'duckduckgo/searchVideo',
'drawing',
'drawing/baseChart'
'drawing/baseChart',
'wiki'
];

export const list = [...staticPluginList, ...packagePluginList];
Expand Down
43 changes: 43 additions & 0 deletions packages/plugins/src/wiki/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 701643f

Please sign in to comment.