新增了Bing Customer Search api的調用支持

This commit is contained in:
TianlinLiang 2024-07-03 16:09:29 +08:00
parent 3dec01c6d5
commit 3336f1e954
2 changed files with 13 additions and 8 deletions

View File

@ -172,13 +172,18 @@ abstract class BaseBrowser {
logger.debug(`Searching for: ${query}`);
const search = new URLSearchParams({ q: query });
recency_days > 0 && search.append('recency_days', recency_days.toString());
search.append('customconfig', config.CUSTOM_CONFIG_ID);
const url = `${config.BING_SEARCH_API_URL}/search?${search.toString()}`;
console.log('Full URL:', url); // 输出完整的 URL查看是否正确
return withTimeout(
config.BROWSER_TIMEOUT,
fetch(`${config.BING_SEARCH_API_URL}/search?${search.toString()}`, {
fetch(url, {
headers: {
'Ocp-Apim-Subscription-Key': config.BING_SEARCH_API_KEY,
}
}).then(
})
.then(
res =>
res.json() as Promise<{
queryContext: {
@ -255,11 +260,11 @@ abstract class BaseBrowser {
}
})
.catch(err => {
logger.error(err.message);
logger.error(`搜索请求失败:${query},错误信息:${err.message}`);
if (err.code === 'ECONNABORTED') {
throw new Error(`Timeout while executing search for: ${query}`);
}
throw new Error(`Network or server error occurred`);
throw new Error(`网络或服务器发生错误请检查URL: ${url}`);
});
},
open_url: (url: string) => {

View File

@ -2,9 +2,9 @@ export default {
LOG_LEVEL: 'debug',
BROWSER_TIMEOUT: 10000,
BING_SEARCH_API_URL: 'https://api.bing.microsoft.com/',
BING_SEARCH_API_KEY: '',
BING_SEARCH_API_URL: 'https://api.bing.microsoft.com/v7.0/custom/',
BING_SEARCH_API_KEY: 'YOUR_BING_SEARCH_API_KEY',
CUSTOM_CONFIG_ID : 'YOUR_CUSTOM_CONFIG_ID', //将你的Custom Configuration ID放在此处
HOST: 'localhost',
PORT: 3000,
};
};