mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 09:44:47 +00:00
plugins: add wiki search (#2886)
* plugins: add wiki search * 扁平化代码 * fix: url error
This commit is contained in:
43
packages/plugins/src/wiki/index.ts
Normal file
43
packages/plugins/src/wiki/index.ts
Normal 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;
|
Reference in New Issue
Block a user