-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
google_places.ts
96 lines (85 loc) · 2.55 KB
/
google_places.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { Tool } from "@langchain/core/tools";
/**
* Interface for parameters required by GooglePlacesAPI class.
*/
export interface GooglePlacesAPIParams {
apiKey?: string;
}
/**
* Tool that queries the Google Places API
*/
export class GooglePlacesAPI extends Tool {
static lc_name() {
return "GooglePlacesAPI";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "GOOGLE_PLACES_API_KEY",
};
}
name = "google_places";
protected apiKey: string;
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?: GooglePlacesAPIParams) {
super(...arguments);
const apiKey =
fields?.apiKey ?? getEnvironmentVariable("GOOGLE_PLACES_API_KEY");
if (apiKey === undefined) {
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: string) {
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 = await res.json();
message = json.error.message;
} catch (e) {
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: {
id?: string;
internationalPhoneNumber?: string;
formattedAddress?: string;
websiteUri?: string;
displayName?: { text?: string };
}) => ({
name: place.displayName?.text,
id: place.id,
address: place.formattedAddress,
phoneNumber: place.internationalPhoneNumber,
website: place.websiteUri,
})
) ?? [];
return JSON.stringify(results);
}
}