* doc * feat: file upload config * perf: chat box file params * feat: markdown show file * feat: chat file store and clear * perf: read file contentType * feat: llm vision config * feat: file url output * perf: plugin error text * perf: image load * feat: ai chat document * perf: file block ui * feat: read file node * feat: file read response field * feat: simple mode support read files * feat: tool call * feat: read file histories * perf: select file * perf: select file config * i18n * i18n * fix: ts; feat: tool response preview result
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { searchImages, SafeSearchType } from 'duck-duck-scrape';
|
|
import { delay } from '@fastgpt/global/common/system/utils';
|
|
import { addLog } from '@fastgpt/service/common/system/log';
|
|
import { getErrText } from '@fastgpt/global/common/error/utils';
|
|
|
|
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) {
|
|
addLog.warn('DuckDuckGo error', { error });
|
|
return {
|
|
result: getErrText(error, 'Failed to fetch data from DuckDuckGo')
|
|
};
|
|
}
|
|
|
|
await delay(Math.random() * 5000);
|
|
return main(props, retry - 1);
|
|
}
|
|
};
|
|
|
|
export default main;
|