[Gemini 3.0][Google Search] 使用 Google Search Grounding API 搭配 Gemini 3.0 Pro 來打造新聞與資訊助手
前情提要 在開發 LINE Bot 時,我想改進純文字搜尋功能:讓使用者輸入任何問題後,AI 能自動搜尋網路資訊並整理回答,同時支援連續對話。傳統做法需要串接多個 API(Gemini 提取關鍵字 → Google Custom Search → Gemini 總結),不僅慢(3次API調用)而且沒有對話記憶。 但 Google 在 2024 年推出了 Grounding with Google Search 功能,這是官方的 RAG (Retrieval-Augmented Generation) 解決方案,讓 Gemini 模型可以自動搜尋網路並引用來源,還原生支援 Chat Session!這項功能透過 Vertex AI 提供,讓 AI 回應不再憑空想像,而是基於真實的網路資訊。 畫面展示 ( 使用舊有的 Google Custom Search 的成果) 會發現他是根據 Google Search 的成果出來的結果, 主要 Repo https://github.com/kkdai/linebot-helper-python 開發過程中遇到的問題 問題 1:舊版實作的瓶頸 在實作 loader/searchtool.py 時,我使用的是傳統的搜尋流程: # ❌ 舊版的做法 - 3 次 API 調用 async def handle_text_message(event, user_id): msg = event.message.text # 第 1 次:提取關鍵字 keywords = extract_keywords_with_gemini(msg, api_key) # 第 2 次:Google Custom Search results = search_with_google_custom_search(keywords, search_api_key, cx) # 第 3 次:總結結果 summary = summarize_text(result_text, 300) # 回傳結果... 這個方法有幾個明顯的問題: ❌ 無對話記憶 - 每次都是新的對話,無法連續提問 用戶: "Python 是什麼?" Bot: [搜尋結果 + 摘要] 用戶: "它有什麼優點?" # ❌ Bot 不知道 "它" 指的是 Python ❌ 搜尋結果淺薄 - 只使用 snippet,無法深入閱讀網頁內容 ❌ 速度慢且成本高 - 3 次 API 調用(~6-8秒)+ Google Custom Search 費用($0.005/次) 問題 2:Client Closed 錯誤 當我改用 Vertex AI Grounding 後,遇到了這個錯誤: ERROR:loader.chat_session:Grounding search...
繼續閱讀