@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 3.21 kB
Source Map (JSON)
{"version":3,"file":"google_custom_search.cjs","names":["Tool"],"sources":["../../src/tools/google_custom_search.ts"],"sourcesContent":["import { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { Tool } from \"@langchain/core/tools\";\n\n/**\n * Interface for parameters required by GoogleCustomSearch class.\n */\nexport interface GoogleCustomSearchParams {\n apiKey?: string;\n googleCSEId?: string;\n}\n\n/**\n * Class that uses the Google Search API to perform custom searches.\n * Requires environment variables `GOOGLE_API_KEY` and `GOOGLE_CSE_ID` to\n * be set.\n */\nexport class GoogleCustomSearch extends Tool {\n static lc_name() {\n return \"GoogleCustomSearch\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_API_KEY\",\n };\n }\n\n name = \"google-custom-search\";\n\n protected apiKey: string;\n\n protected googleCSEId: string;\n\n description =\n \"a custom search engine. useful for when you need to answer questions about current events. input should be a search query. outputs a JSON array of results.\";\n\n constructor(\n fields: GoogleCustomSearchParams = {\n apiKey: getEnvironmentVariable(\"GOOGLE_API_KEY\"),\n googleCSEId: getEnvironmentVariable(\"GOOGLE_CSE_ID\"),\n }\n ) {\n super(...arguments);\n if (!fields.apiKey) {\n throw new Error(\n `Google API key not set. You can set it as \"GOOGLE_API_KEY\" in your environment variables.`\n );\n }\n if (!fields.googleCSEId) {\n throw new Error(\n `Google custom search engine id not set. You can set it as \"GOOGLE_CSE_ID\" in your environment variables.`\n );\n }\n this.apiKey = fields.apiKey;\n this.googleCSEId = fields.googleCSEId;\n }\n\n async _call(input: string) {\n const res = await fetch(\n `https://www.googleapis.com/customsearch/v1?key=${this.apiKey}&cx=${\n this.googleCSEId\n }&q=${encodeURIComponent(input)}`\n );\n\n if (!res.ok) {\n throw new Error(\n `Got ${res.status} error from Google custom search: ${res.statusText}`\n );\n }\n\n const json = await res.json();\n\n const results =\n json?.items?.map(\n (item: { title?: string; link?: string; snippet?: string }) => ({\n title: item.title,\n link: item.link,\n snippet: item.snippet,\n })\n ) ?? [];\n return JSON.stringify(results);\n }\n}\n"],"mappings":";;;;;;;;;;;AAgBA,IAAa,qBAAb,cAAwCA,sBAAAA,KAAK;CAC3C,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,QAAQ,kBACT;;CAGH,OAAO;CAEP;CAEA;CAEA,cACE;CAEF,YACE,SAAmC;EACjC,SAAA,GAAA,0BAAA,wBAA+B,iBAAiB;EAChD,cAAA,GAAA,0BAAA,wBAAoC,gBAAgB;EACrD,EACD;AACA,QAAM,GAAG,UAAU;AACnB,MAAI,CAAC,OAAO,OACV,OAAM,IAAI,MACR,4FACD;AAEH,MAAI,CAAC,OAAO,YACV,OAAM,IAAI,MACR,2GACD;AAEH,OAAK,SAAS,OAAO;AACrB,OAAK,cAAc,OAAO;;CAG5B,MAAM,MAAM,OAAe;EACzB,MAAM,MAAM,MAAM,MAChB,kDAAkD,KAAK,OAAO,MAC5D,KAAK,YACN,KAAK,mBAAmB,MAAM,GAChC;AAED,MAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR,OAAO,IAAI,OAAO,oCAAoC,IAAI,aAC3D;EAKH,MAAM,WAFO,MAAM,IAAI,MAAM,GAGrB,OAAO,KACV,UAA+D;GAC9D,OAAO,KAAK;GACZ,MAAM,KAAK;GACX,SAAS,KAAK;GACf,EACF,IAAI,EAAE;AACT,SAAO,KAAK,UAAU,QAAQ"}