@langchain/community
Version:
Third-party integrations for LangChain.js
62 lines (61 loc) • 2.33 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
let _langchain_core_tools = require("@langchain/core/tools");
let _langchain_core_utils_env = require("@langchain/core/utils/env");
//#region src/tools/bingserpapi.ts
var bingserpapi_exports = /* @__PURE__ */ require_runtime.__exportAll({ BingSerpAPI: () => BingSerpAPI });
/**
* A tool for web search functionality using Bing's search engine. It
* extends the base `Tool` class and implements the `_call` method to
* perform the search operation. Requires an API key for Bing's search
* engine, which can be set in the environment variables. Also accepts
* additional parameters for the search query.
*/
var BingSerpAPI = class extends _langchain_core_tools.Tool {
static lc_name() {
return "BingSerpAPI";
}
/**
* Not implemented. Will throw an error if called.
*/
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;
params;
constructor(apiKey = (0, _langchain_core_utils_env.getEnvironmentVariable)("BingApiKey"), params = {}) {
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) {
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 results = (await response.json()).webPages.value;
if (results.length === 0) return "No good results found.";
return results.map((result) => result.snippet).join(" ");
}
};
//#endregion
exports.BingSerpAPI = BingSerpAPI;
Object.defineProperty(exports, "bingserpapi_exports", {
enumerable: true,
get: function() {
return bingserpapi_exports;
}
});
//# sourceMappingURL=bingserpapi.cjs.map