File size: 9,952 Bytes
838e061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gplace\n",
    "\n",
    "location = \"13.744677,100.5295593\"  # Latitude and Longitude\n",
    "keyword = \"ร้านกาแฟ\"\n",
    "result = gplace.nearby_search(keyword, location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import TypedDict, Optional\n",
    "\n",
    "class NearbyDenseCommunityInput(TypedDict):\n",
    "    location_name: str\n",
    "    radius: int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_place_from_text(location:str):\n",
    "    \"\"\"Finds a place and related data from the query text\"\"\"\n",
    "    \n",
    "    result = gplace.find_place_from_text(location)\n",
    "    r = result['candidates'][0]\n",
    "    return f\"\"\"\n",
    "    address: {r['formatted_address']}\\n\n",
    "    location: {r['geometry']['location']}\\n\n",
    "    name: {r['name']}\\n\n",
    "    opening hours: {r['opening_hours']}\\n\n",
    "    rating: {r['rating']}\\n\n",
    "    \"\"\"\n",
    "    \n",
    "def nearby_search(keyword:str, location:str, radius=2000, place_type=None):\n",
    "    \"\"\"Searches for many places nearby the location based on a keyword. using keyword like \\\"coffee shop\\\", \\\"restaurants\\\". radius is the range to search from the location\"\"\"\n",
    "    location = gplace.find_location(location, radius=radius)\n",
    "    result = gplace.nearby_search(keyword, location, radius)\n",
    "    \n",
    "    strout = \"\"\n",
    "    for r in result:\n",
    "        # Use .get() to handle missing keys\n",
    "        address = r.get('vicinity', 'N/A')\n",
    "        location_info = r.get('geometry', {}).get('location', 'N/A')\n",
    "        name = r.get('name', 'N/A')\n",
    "        opening_hours = r.get('opening_hours', 'N/A')\n",
    "        rating = r.get('rating', 'N/A')\n",
    "        plus_code = r.get('plus_code', {}).get('global_code', 'N/A')\n",
    "        \n",
    "        strout += f\"\"\"\n",
    "        address: {address}\\n\n",
    "        location: {location_info}\\n\n",
    "        name: {name}\\n\n",
    "        opening hours: {opening_hours}\\n\n",
    "        rating: {rating}\\n\n",
    "        plus code: {plus_code}\\n\\n\n",
    "        \"\"\"\n",
    "    return strout\n",
    "\n",
    "def nearby_dense_community(input_dict: NearbyDenseCommunityInput) -> str:\n",
    "    \"\"\" getting nearby dense community such as (community mall, hotel, school, etc), by location name, radius(in meters)\n",
    "    return list of location community nearby, name, community type.\n",
    "    \"\"\"\n",
    "    location = input_dict['location_name']\n",
    "    radius = input_dict['radius']\n",
    "    \n",
    "    location_coords = gplace.find_location(location, radius=radius)\n",
    "    result = gplace.nearby_dense_community(location_coords, radius)\n",
    "    \n",
    "    strout = \"\"\n",
    "    for r in result:\n",
    "        # Use .get() to handle missing keys\n",
    "        address = r.get('vicinity', 'N/A')\n",
    "        location_types = r.get('types', 'N/A')\n",
    "        name = r.get('name', 'N/A')\n",
    "        opening_hours = r.get('opening_hours', 'N/A')\n",
    "        rating = r.get('rating', 'N/A')\n",
    "        plus_code = r.get('plus_code', {}).get('global_code', 'N/A')\n",
    "        \n",
    "        strout += f\"\"\"\n",
    "        name: {name}\\n\n",
    "        types: {location_types}\\n\n",
    "        \"\"\".strip()\n",
    "    return strout\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "USER_AGENT environment variable not set, consider setting it to identify your requests.\n"
     ]
    }
   ],
   "source": [
    "# gplace_tools.py\n",
    "from langgraph.prebuilt import ToolNode\n",
    "from langchain_core.tools import tool\n",
    "from langchain_core.tools import Tool\n",
    "from langchain_google_community import GoogleSearchAPIWrapper\n",
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "\n",
    "import utils\n",
    "\n",
    "utils.load_env()\n",
    "\n",
    "search = GoogleSearchAPIWrapper()\n",
    "\n",
    "find_place_from_text = tool(find_place_from_text)\n",
    "nearby_search = tool(nearby_search)\n",
    "google_search = Tool(\n",
    "    name=\"google_search\",\n",
    "    description=\"Search Google for recent results.\",\n",
    "    func=search.run,\n",
    ")\n",
    "web_loader = Tool(\n",
    "    name=\"google_search\",\n",
    "    description=\"Search Google for recent results.\",\n",
    "    func=WebBaseLoader,\n",
    ")\n",
    "\n",
    "tools = [find_place_from_text, nearby_search]\n",
    "\n",
    "# Create ToolNodes for each tool\n",
    "tool_node = ToolNode(tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"name: Bangkok\\n\\n        types: ['locality', 'political']name: Metropoint Bangkok Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: The Grand Fourwings Convention Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Hua Mak Indoor Stadium\\n\\n        types: ['point_of_interest', 'establishment']name: B2 Bangkok Srinagarindra Boutique & Budget Hotel\\n\\n        types: ['clothing_store', 'lodging', 'point_of_interest', 'store', 'establishment']name: HappyLand Mansion\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Bangkok Swimming by Kru Jin\\n\\n        types: ['point_of_interest', 'establishment']name: Aunchaleena grand Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Anda Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Grand Mandarin Residence\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Wallada Place Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: NIDA Rooms Plubpla Bangkapi 591\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Bangkok Interplace\\n\\n        types: ['lodging', 'restaurant', 'food', 'point_of_interest', 'establishment']name: Vejthani Hospital\\n\\n        types: ['hospital', 'doctor', 'point_of_interest', 'health', 'establishment']name: โรงแรม ชาลีน่า ปริ้นเซส Chaleena princess\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Royal Pimand\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Baron Residence Hotel\\n\\n        types: ['lodging', 'point_of_interest', 'establishment']name: Ridwanun Islam Mosque\\n\\n        types: ['mosque', 'place_of_worship', 'point_of_interest', 'establishment']name: Thep Phanom Building\\n\\n        types: ['point_of_interest', 'establishment']name: Bang Kapi District\\n\\n        types: ['sublocality_level_1', 'sublocality', 'political']\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nearby_dense_community({'location_name': 'ลุมพินี เซ็นเตอร์ ลาดพร้าว', 'radius': 8000})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Address: 156/2 Dinso Rd, Sao Chingcha, Phra Nakhon, Bangkok 10200 ; Phone: 02-622-1525 ; Open hours: 10 am – 11 pm daily ; Prices: Most dishes are in the price... For those complaining about the price go sit in the street if you want super cheap. It\\'s inside a large shopping mall filled with luxury brands, what do you... If you are looking for quality Mediterranean cuisine in the heart of Bangkok, look no further than Mimosa BKK. ... Don\\'t forget to check for happy hour prices so... PRICE RANGE. $5.00 – $86.00 ; CUISINES. Italian, French, Pizza, Mediterranean, Spanish ; Meals. Lunch, Dinner, Brunch, Drinks. 1790 Followers, 6 Following, 111 Posts - Mimosa | Mediterranean Restaurant Bangkok (@mimosabkk) on Instagram: \"From the sunny shores of the Mediterranean... Mimosa Cake by Fast Buds is a Sativa-dominant hybrid but thanks to the handpicked terpene profile, its strong Sativa high comes hand-in-hand with a nice. ... Bangkok · Discover Our Stores · PAÑPURI FIRST · Our Stores. EN. EN · TH · 简体 · 繁體. Top. MEMORIA MIMOSA. Golden sunshine brightens the sweetness of Mimosa... Jun 10, 2020 ... I studied at Regent\\'s Pattaya throughout high school and moved to Bangkok to pursue a bachelor\\'s degree in Communication Management at... Jan 3, 2022 ... Program in Biotechnology, Faculty of Science, Chulalongkorn University, Bangkok, Thailand. ... Mimosa diplotricha, Nelumbo nucifera, Xyris... Mimosa weed in bangkok High thai ... Sale Price, ฿3000. Mimosa quantity. Add to cart. Compare · Add to wishlist. Follow...'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google_search(\"the pricing of Mimosa Bangkok in Bangkok \").replace(\"\\xa0\", \"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}