@langchain/community
Version:
Third-party integrations for LangChain.js
79 lines (78 loc) • 3.16 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/google_scholar.ts
var google_scholar_exports = /* @__PURE__ */ require_runtime.__exportAll({ SERPGoogleScholarAPITool: () => SERPGoogleScholarAPITool });
/**
* Tool for querying Google Scholar using the SerpApi service.
*/
var SERPGoogleScholarAPITool = class extends _langchain_core_tools.Tool {
/**
* Specifies the name of the tool, used internally by LangChain.
*/
static lc_name() {
return "SERPGoogleScholarAPITool";
}
/**
* Returns a mapping of secret environment variable names to their usage in the tool.
* @returns {object} Mapping of secret names to their environment variable counterparts.
*/
get lc_secrets() {
return { apiKey: "SERPAPI_API_KEY" };
}
name = "serp_google_scholar";
apiKey;
/**
* Description of the tool for usage documentation.
*/
description = `A wrapper around Google Scholar API via SerpApi. Useful for querying academic
articles and papers by keywords or authors. Input should be a search query string.`;
/**
* Constructs a new instance of SERPGoogleScholarAPITool.
* @param fields - Optional parameters including an API key.
*/
constructor(fields) {
super(...arguments);
const apiKey = fields?.apiKey ?? (0, _langchain_core_utils_env.getEnvironmentVariable)("SERPAPI_API_KEY");
if (!apiKey) throw new Error(`SerpApi key not set. You can set it as "SERPAPI_API_KEY" in your environment variables.`);
this.apiKey = apiKey;
}
/**
* Makes a request to SerpApi for Google Scholar results.
* @param input - Search query string.
* @returns A JSON string containing the search results.
* @throws Error if the API request fails or returns an error.
*/
async _call(input) {
const url = `https://serpapi.com/search.json?q=${encodeURIComponent(input)}&engine=google_scholar&api_key=${this.apiKey}`;
const response = await fetch(url);
if (!response.ok) {
let message;
try {
message = (await response.json()).error;
} catch {
message = "Unable to parse error message: SerpApi did not return a JSON response.";
}
throw new Error(`Got ${response.status}: ${response.statusText} error from SerpApi: ${message}`);
}
const results = (await response.json()).organic_results?.map((item) => ({
title: item.title,
link: item.link,
snippet: item.snippet,
publication_info: item.publication_info?.summary?.split(" - ").slice(1).join(" - ") ?? "",
authors: item.publication_info?.authors?.map((author) => author.name).join(", ") ?? "",
total_citations: item.inline_links?.cited_by?.total ?? ""
})) ?? `No results found for ${input} on Google Scholar.`;
return JSON.stringify(results, null, 2);
}
};
//#endregion
exports.SERPGoogleScholarAPITool = SERPGoogleScholarAPITool;
Object.defineProperty(exports, "google_scholar_exports", {
enumerable: true,
get: function() {
return google_scholar_exports;
}
});
//# sourceMappingURL=google_scholar.cjs.map