File size: 3,515 Bytes
115169a
 
5831cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115169a
 
 
 
 
 
 
 
 
 
 
 
 
5831cdb
115169a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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": [
    "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",
    "        strout = strout + f\"\"\"\n",
    "        address: {r['vicinity']}\\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",
    "        plus code: {r['plus_code']['global_code']}\\n\\n\n",
    "        \"\"\"\n",
    "    return strout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\n    address: 587-589/7-9 ถ. รามอินทรา แขวงคันนายาว เขตคันนายาว กรุงเทพมหานคร 10230 ไทย\\n\\n    location: {'lat': 13.8261789, 'lng': 100.6794462}\\n\\n    name: แฟชั่นไอส์แลนด์\\n\\n    opening hours: {'open_now': True}\\n\\n    rating: 4.5\\n\\n    \""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_place_from_text(\"fashion island\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gplace_tools.py\n",
    "from langgraph.prebuilt import ToolNode\n",
    "from langchain_core.tools import tool\n",
    "\n",
    "find_place_from_text = tool(find_place_from_text)\n",
    "nearby_search = tool(nearby_search)\n",
    "\n",
    "tools = [find_place_from_text, nearby_search]\n",
    "\n",
    "# Create ToolNodes for each tool\n",
    "tool_node = ToolNode(tools)"
   ]
  }
 ],
 "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
}