@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 3.69 kB
Source Map (JSON)
{"version":3,"file":"google_places.cjs","names":["Tool"],"sources":["../../src/tools/google_places.ts"],"sourcesContent":["import { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { Tool } from \"@langchain/core/tools\";\n\n/**\n * Interface for parameters required by GooglePlacesAPI class.\n */\nexport interface GooglePlacesAPIParams {\n apiKey?: string;\n}\n\n/**\n * Tool that queries the Google Places API\n */\nexport class GooglePlacesAPI extends Tool {\n static lc_name() {\n return \"GooglePlacesAPI\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_PLACES_API_KEY\",\n };\n }\n\n name = \"google_places\";\n\n protected apiKey: string;\n\n description = `A wrapper around Google Places API. Useful for when you need to validate or \n discover addresses from ambiguous text. Input should be a search query.`;\n\n constructor(fields?: GooglePlacesAPIParams) {\n super(...arguments);\n const apiKey =\n fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_PLACES_API_KEY\");\n if (apiKey === undefined) {\n throw new Error(\n `Google Places API key not set. You can set it as \"GOOGLE_PLACES_API_KEY\" in your environment variables.`\n );\n }\n this.apiKey = apiKey;\n }\n\n async _call(input: string) {\n const res = await fetch(\n `https://places.googleapis.com/v1/places:searchText`,\n {\n method: \"POST\",\n body: JSON.stringify({\n textQuery: input,\n languageCode: \"en\",\n }),\n headers: {\n \"X-Goog-Api-Key\": this.apiKey,\n \"X-Goog-FieldMask\":\n \"places.displayName,places.formattedAddress,places.id,places.internationalPhoneNumber,places.websiteUri\",\n \"Content-Type\": \"application/json\",\n },\n }\n );\n\n if (!res.ok) {\n let message;\n try {\n const json = await res.json();\n message = json.error.message;\n } catch {\n message =\n \"Unable to parse error message: Google did not return a JSON response.\";\n }\n throw new Error(\n `Got ${res.status}: ${res.statusText} error from Google Places API: ${message}`\n );\n }\n\n const json = await res.json();\n\n const results =\n json?.places?.map(\n (place: {\n id?: string;\n internationalPhoneNumber?: string;\n formattedAddress?: string;\n websiteUri?: string;\n displayName?: { text?: string };\n }) => ({\n name: place.displayName?.text,\n id: place.id,\n address: place.formattedAddress,\n phoneNumber: place.internationalPhoneNumber,\n website: place.websiteUri,\n })\n ) ?? [];\n return JSON.stringify(results);\n }\n}\n"],"mappings":";;;;;;;;;AAaA,IAAa,kBAAb,cAAqCA,sBAAAA,KAAK;CACxC,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,QAAQ,yBACT;;CAGH,OAAO;CAEP;CAEA,cAAc;;CAGd,YAAY,QAAgC;AAC1C,QAAM,GAAG,UAAU;EACnB,MAAM,SACJ,QAAQ,WAAA,GAAA,0BAAA,wBAAiC,wBAAwB;AACnE,MAAI,WAAW,KAAA,EACb,OAAM,IAAI,MACR,0GACD;AAEH,OAAK,SAAS;;CAGhB,MAAM,MAAM,OAAe;EACzB,MAAM,MAAM,MAAM,MAChB,sDACA;GACE,QAAQ;GACR,MAAM,KAAK,UAAU;IACnB,WAAW;IACX,cAAc;IACf,CAAC;GACF,SAAS;IACP,kBAAkB,KAAK;IACvB,oBACE;IACF,gBAAgB;IACjB;GACF,CACF;AAED,MAAI,CAAC,IAAI,IAAI;GACX,IAAI;AACJ,OAAI;AAEF,eADa,MAAM,IAAI,MAAM,EACd,MAAM;WACf;AACN,cACE;;AAEJ,SAAM,IAAI,MACR,OAAO,IAAI,OAAO,IAAI,IAAI,WAAW,iCAAiC,UACvE;;EAKH,MAAM,WAFO,MAAM,IAAI,MAAM,GAGrB,QAAQ,KACX,WAMM;GACL,MAAM,MAAM,aAAa;GACzB,IAAI,MAAM;GACV,SAAS,MAAM;GACf,aAAa,MAAM;GACnB,SAAS,MAAM;GAChB,EACF,IAAI,EAAE;AACT,SAAO,KAAK,UAAU,QAAQ"}