import { getEnvironmentVariable } from "../util/env.js"; import { Tool } from "./base.js"; class BingSerpAPI extends Tool { toJSON() { return this.toJSONNotImplemented(); } name = "bing-search"; description = "a search engine. useful for when you need to answer questions about current events. input should be a search query."; key: string; params: Record; constructor( apiKey: string | undefined = getEnvironmentVariable("BingApiKey"), params: Record = {} ) { super(...arguments); if (!apiKey) { throw new Error( "BingSerpAPI API key not set. You can set it as BingApiKey in your .env file." ); } this.key = apiKey; this.params = params; } /** @ignore */ async _call(input: string): Promise { const headers = { "Ocp-Apim-Subscription-Key": this.key }; const params = { q: input, textDecorations: "true", textFormat: "HTML" }; const searchUrl = new URL("https://api.bing.microsoft.com/v7.0/search"); Object.entries(params).forEach(([key, value]) => { searchUrl.searchParams.append(key, value); }); const response = await fetch(searchUrl, { headers }); if (!response.ok) { throw new Error(`HTTP error ${response.status}`); } const res = await response.json(); const results: [] = res.webPages.value; if (results.length === 0) { return "No good results found."; } const snippets = results .map((result: { snippet: string }) => result.snippet) .join(" "); return snippets; } } export { BingSerpAPI };