LLMは学習のカットオフ時点で固定されており、最近の事実については幻覚を起こします。エージェントに Google検索ツール を与えれば両方が解決します。そして serp.cheap のような安価で同期的なSERP APIは、レイテンシが予測可能で1回あたりのコストがエージェント規模で実行できるほど低いため、理想的なバックエンドです。
検索ツール(単純な関数)
import requests
def google_search(query: str, country: str = "us") -> str:
"""Search Google and return the top results as text."""
data = requests.post(
"https://api.serp.cheap/v1/search",
headers={"X-API-Key": "YOUR_KEY"},
json={"q": query, "gl": country},
timeout=30,
).json()
return "\n".join(
f'{r["position"]}. {r["title"]} — {r["link"]}\n {r.get("snippet","")}'
for r in data["organic"]
)
OpenAIのツール呼び出しAPIで
tools = [{
"type": "function",
"function": {
"name": "google_search",
"description": "Search Google for up-to-date information.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"country": {"type": "string", "default": "us"},
},
"required": ["query"],
},
},
}]
# When the model calls google_search, run the function above and return its
# output as the tool result, then let the model answer with citations.
LangChainで
from langchain.tools import Tool
search_tool = Tool(
name="google_search",
description="Search Google for current information.",
func=lambda q: google_search(q),
)
# Add search_tool to your agent's tool list.
なぜserp.cheapがエージェントに適しているか
- 同期的 な約2秒のレスポンス — ツール呼び出しの中で非同期キューを調整する必要がありません。
- 大規模でも安価 — エージェントは多数の検索をファンアウトします。1回あたり$0.0006(半分はキャッシュ)なので実行コストを抑えられます。
- 本物のgoogle.com + 引用 — ブラックボックスのインデックスではなく、モデルが引用できるリンクを返します。