{ "cells": [ { "cell_type": "code", "execution_count": 14, "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": 15, "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(location:str, radius:int=2000):\n", " \"\"\" getting nearby dense community such as (community mall, hotel, school, etc), by geomatric location, radius(in meters)\n", " return list of location community nearby, name, community type.\n", " \"\"\"\n", " result = gplace.nearby_dense_community(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", " name: {address}\\n\n", " types: {location_info}\\n\n", " \"\"\"\n", " return strout" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "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)" ] } ], "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 }