UNPKG

@langchain/community

Version:
1 lines • 3.41 kB
{"version":3,"file":"google_custom_search.cjs","names":["Tool","fields: GoogleCustomSearchParams","input: string","item: { title?: string; link?: string; snippet?: string }"],"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,4BAAK;CAC3C,OAAO,UAAU;AACf,SAAO;CACR;CAED,IAAI,aAAoD;AACtD,SAAO,EACL,QAAQ,iBACT;CACF;CAED,OAAO;CAEP,AAAU;CAEV,AAAU;CAEV,cACE;CAEF,YACEC,SAAmC;EACjC,+DAA+B,iBAAiB;EAChD,oEAAoC,gBAAgB;CACrD,GACD;EACA,MAAM,GAAG,UAAU;AACnB,MAAI,CAAC,OAAO,OACV,OAAM,IAAI,MACR,CAAC,yFAAyF,CAAC;AAG/F,MAAI,CAAC,OAAO,YACV,OAAM,IAAI,MACR,CAAC,wGAAwG,CAAC;EAG9G,KAAK,SAAS,OAAO;EACrB,KAAK,cAAc,OAAO;CAC3B;CAED,MAAM,MAAMC,OAAe;EACzB,MAAM,MAAM,MAAM,MAChB,CAAC,+CAA+C,EAAE,KAAK,OAAO,IAAI,EAChE,KAAK,YACN,GAAG,EAAE,mBAAmB,MAAM,EAAE,CAClC;AAED,MAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR,CAAC,IAAI,EAAE,IAAI,OAAO,kCAAkC,EAAE,IAAI,YAAY;EAI1E,MAAM,OAAO,MAAM,IAAI,MAAM;EAE7B,MAAM,UACJ,MAAM,OAAO,IACX,CAACC,UAA+D;GAC9D,OAAO,KAAK;GACZ,MAAM,KAAK;GACX,SAAS,KAAK;EACf,GACF,IAAI,CAAE;AACT,SAAO,KAAK,UAAU,QAAQ;CAC/B;AACF"}