System plugin (#2091)
* System template (#2082) * feat: system plugin (#2024) * add plugin cost & change plugin avatar (#2030) * add plugin cost & change plugin avatar * add author * feat: duckduckgo plugin * duckduck search * perf: templates select system plugin * perf: system plugin avatar * feat: duckduck plugins * doc * perf: plugin classify * perf: icon avatar component * perf: system template avatar --------- Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com> * feat: system plugin search * perf: plugin packages important * perf: source avatar * nextconfig * perf: i18n * perf: default model * perf: system plugin author --------- Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
This commit is contained in:
46
packages/plugins/src/duckduckgo/searchImg/index.ts
Normal file
46
packages/plugins/src/duckduckgo/searchImg/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { searchImages, SafeSearchType } from 'duck-duck-scrape';
|
||||
import { delay } from '@fastgpt/global/common/system/utils';
|
||||
import { addLog } from '@fastgpt/service/common/system/log';
|
||||
|
||||
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 searchImages(query, {
|
||||
safeSearch: SafeSearchType.STRICT
|
||||
});
|
||||
|
||||
const result = searchResults.results
|
||||
.map((item) => ({
|
||||
title: item.title,
|
||||
image: item.image
|
||||
}))
|
||||
.slice(0, 10);
|
||||
|
||||
return {
|
||||
result: JSON.stringify(result)
|
||||
};
|
||||
} catch (error) {
|
||||
if (retry <= 0) {
|
||||
return {
|
||||
result: 'Failed to fetch data'
|
||||
};
|
||||
}
|
||||
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
|
||||
await delay(Math.random() * 2000);
|
||||
return main(props, retry - 1);
|
||||
}
|
||||
};
|
||||
|
||||
export default main;
|
||||
Reference in New Issue
Block a user