iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
Cloud Native

《30 天 Cloud Native:AI 行程規劃後端開發》系列 第 24

Day24 - 強化 Agent 地點選擇能力:利用 Google API 精準選定活動地點

  • 分享至 

  • xImage
  •  

我們需要給 Agent 更多資訊才能產生出更明確的活動地點,所以使用 Google API 實作一個列舉地點的工具來給 Agent 選擇。

使用 Google API 提供活動地點

一般來說在選想去的地方時 Google 地圖的評分數會是一個讓人想去看看的重點,所以在搜尋時要將評分資訊取回。

為了達到這個功能,我們實作了一個名為 get_candidate_places_by_google_map 的函數,參數有地點名稱和活動類型,函數的運作邏輯為:

  1. 設定查詢參數呼叫 Google 地圖 Text Search API
    將地點名稱和活動類型組合成字串,使用 API 搜尋地點清單。
  2. 設定回傳欄位,取回評分資訊
    • 地點名稱(places.displayName.text
    • 評分(places.rating
    • 評分人數(places.userRatingCount
  3. 解析並回傳內容

工具整合與使用方式

這樣就有一個提供地點清單的函數,將這個函數註冊成 Tool 並且加上 Prompt 向 AI 說明如何使用。

我這邊請他要提供一個明確的活動地點,可以使用 get_candidate_places_by_google_map 函數取得地點清單,而該函數的輸入為地區名稱和活動類別:

- Each activity should be assigned to a specific place. You can retrieve candidate locations using the get_candidate_places_by_google_map tool, which takes the location name and activity type as inputs.

執行的結果為:

{
  "title": "日月潭兩日遊",
  "activities": [
    {
      "type": "accommodation",
      "location": "日月潭大飯店",
      "startTimeUtc": "2025-09-10T09:00:00Z",
      "duration": 90,
      "endTimeUtc": "2025-09-10T10:30:00Z",
      "timeZone": "Asia/Taipei",
      "transportType": null,
      "note": "抵達飯店辦理入住手續並稍作休息",
      "childActivities": [],
      "latLng": {
        "latitude": 23.8661912,
        "longitude": 120.90295119999999
      },
      "placeUri": "https://maps.google.com/?cid=16570223414603160834&g_mp=CiVnb29nbGUubWFwcy5wbGFjZXMudjEuUGxhY2VzLkdldFBsYWNlEAAYBCAA"
    },
    {
      "type": "sightseeing",
      "location": "伊達邵碼頭",
      "startTimeUtc": "2025-09-10T10:30:00Z",
      "duration": 120,
      "endTimeUtc": "2025-09-10T12:30:00Z",
      "timeZone": "Asia/Taipei",
      "transportType": "walk",
      "note": "探索伊達邵碼頭周邊,欣賞湖光山色",
      "childActivities": [],
      "latLng": {
        "latitude": 23.84974,
        "longitude": 120.92967
      },
      "placeUri": "https://maps.google.com/?cid=16747491732745082392&g_mp=CiVnb29nbGUubWFwcy5wbGFjZXMudjEuUGxhY2VzLkdldFBsYWNlEAAYBCAA"
    },
    {
      "type": "restaurant",
      "location": "日月潭-邵族頭目袁家美食",
      "startTimeUtc": "2025-09-10T12:30:00Z",
      "duration": 75,
      "endTimeUtc": "2025-09-10T13:45:00Z",
      "timeZone": "Asia/Taipei",
      "transportType": "walk",
      "note": "品嚐當地邵族風味餐",
      "childActivities": [],
      "latLng": {
        "latitude": 23.848626000000003,
        "longitude": 120.93045319999999
      },
      "placeUri": "https://maps.google.com/?cid=3408079587938168932&g_mp=CiVnb29nbGUubWFwcy5wbGFjZXMudjEuUGxhY2VzLkdldFBsYWNlEAAYBCAA"
    },
    ...
}

效能觀察與優化建議

在尚未導入此工具前,AI 所指定的住宿地點僅為「日月潭」這樣的模糊區域,導致查詢經緯度時僅能取得一個廣泛範圍。導入工具後,AI 能夠選出如「日月潭大飯店」這樣具體的地點,使路徑顯示更為精確。

不過,AI 回應時間從原本約 20 秒增加至 30 秒,接下來要考慮是否導入快取機制以加速處理速度。


上一篇
Day23 - 跨端資料串接與欄位結構優化實錄
系列文
《30 天 Cloud Native:AI 行程規劃後端開發》24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言