@langchain/community
Version:
Third-party integrations for LangChain.js
65 lines (63 loc) • 2.2 kB
JavaScript
import { __export } from "../_virtual/rolldown_runtime.js";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { Tool } from "@langchain/core/tools";
//#region src/tools/google_places.ts
var google_places_exports = {};
__export(google_places_exports, { GooglePlacesAPI: () => GooglePlacesAPI });
/**
* Tool that queries the Google Places API
*/
var GooglePlacesAPI = class extends Tool {
static lc_name() {
return "GooglePlacesAPI";
}
get lc_secrets() {
return { apiKey: "GOOGLE_PLACES_API_KEY" };
}
name = "google_places";
apiKey;
description = `A wrapper around Google Places API. Useful for when you need to validate or
discover addresses from ambiguous text. Input should be a search query.`;
constructor(fields) {
super(...arguments);
const apiKey = fields?.apiKey ?? getEnvironmentVariable("GOOGLE_PLACES_API_KEY");
if (apiKey === void 0) throw new Error(`Google Places API key not set. You can set it as "GOOGLE_PLACES_API_KEY" in your environment variables.`);
this.apiKey = apiKey;
}
async _call(input) {
const res = await fetch(`https://places.googleapis.com/v1/places:searchText`, {
method: "POST",
body: JSON.stringify({
textQuery: input,
languageCode: "en"
}),
headers: {
"X-Goog-Api-Key": this.apiKey,
"X-Goog-FieldMask": "places.displayName,places.formattedAddress,places.id,places.internationalPhoneNumber,places.websiteUri",
"Content-Type": "application/json"
}
});
if (!res.ok) {
let message;
try {
const json$1 = await res.json();
message = json$1.error.message;
} catch {
message = "Unable to parse error message: Google did not return a JSON response.";
}
throw new Error(`Got ${res.status}: ${res.statusText} error from Google Places API: ${message}`);
}
const json = await res.json();
const results = json?.places?.map((place) => ({
name: place.displayName?.text,
id: place.id,
address: place.formattedAddress,
phoneNumber: place.internationalPhoneNumber,
website: place.websiteUri
})) ?? [];
return JSON.stringify(results);
}
};
//#endregion
export { GooglePlacesAPI, google_places_exports };
//# sourceMappingURL=google_places.js.map