Gemma
ADK Agent 可以使用谷歌 Gemma 系列生成式 AI 模型,这些模型提供广泛的功能。ADK 支持许多 Gemma 功能,包括工具调用和结构化输出。
支持版本:Python v0.1.0。
ADK Agent 可以使用 Google Gemma 系列生成式 AI 模型,这些模型提供了广泛的功能。ADK 支持许多 Gemma 功能,包括 Tool Calling 和 Structured Output。
您可以通过 Gemini API 使用 Gemma 4,或者在 Google Cloud 上使用多种自托管选项之一:Agent Platform、Google Kubernetes Engine、Cloud Run。
Gemma 3 需要与下面的 Gemma 4 示例不同的模型类。它不支持原生函数调用或系统指令,因此 ADK 在专用类中提供了解决方法:Gemini API 使用 Gemma(model="gemma-3-27b-it"),Ollama 使用 Gemma3Ollama(),均来自 google.adk.models。仅当安装了 LiteLLM(litellm>=1.84)时,Gemma3Ollama 才会被定义。
Gemini API 示例
在 Google AI Studio 创建一个 API 密钥。
vLLM 示例
要在这些服务中访问 Gemma 4 端点, 你可以通过 Python 的 LiteLLM 库使用 vLLM 模型, 也可以通过 Java 的 LangChain4j 使用。
以下示例展示了如何将 Gemma 4 vLLM 端点与 ADK Agent 一起使用。
设置
- 部署模型: 使用以下方式部署你选择的模型
Agent Platform、
Google Kubernetes Engine 或
Cloud Run,
并使用其兼容 OpenAI 的 API 端点。
注意 API 基础 URL 包括
/v1(例如https://your-vllm-endpoint.run.app/v1)。- ADK 工具注意事项: 部署时确保 服务工具支持并启用兼容的工具/函数调用和推理解析器。
- 认证: 确定你的端点如何处理认证(例如, API 密钥、Bearer 令牌)。
代码
使用 Gemma 4、ADK 和 Google 地图 MCP 构建美食游 Agent
此示例展示了如何使用 Gemma 4、ADK 以及 Google 地图 MCP 服务器构建个性化美食游 Agent。该 Agent 会根据用户的菜品照片或文字描述、地点以及可选的预算,推荐就餐地点并将其组织成步行路线。
前提条件
- 在 Google AI Studio 获取 API 密钥。
将
GEMINI_API_KEY环境变量设置为您的 Gemini API 密钥。 - 在 Google Cloud Console 上启用 Google Maps API。
- 创建 Google 地图平台 API 密钥。
将
MAPS_API_KEY环境变量设置为您的 API 密钥。 - 安装 ADK 并在您的 Python 环境中配置它,或者在 Java 项目中配置 Java 依赖项。
项目结构
bash
food_tour_app/
├── __init__.py
└── agent.pyagent.py
python
import os
import dotenv
from google.adk.agents import LlmAgent
from google.adk.models import Gemini
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
dotenv.load_dotenv()
system_instruction = """
You are an expert personalized food tour guide.
Your goal is to build a culinary tour based on the user's inputs: a photo of a dish (or a text description), a location, and a budget.
Follow these 4 rigorous steps:
1. **Identify the Cuisine/Dish:** Analyze the user's provided description or image URL to determine the primary cuisine or specific dish.
2. **Find the Best Spots:** Use the `search_places` tool to find highly rated restaurants, stalls, or cafes serving that cuisine/dish in the user's specified location.
**CRITICAL RULE FOR PLACES:** `search_places` returns AI-generated place data summaries along with `place_id`, latitude/longitude coordinates, and map links for each place, but may lack a direct, explicit name field. You must carefully associate each described place to its provided `place_id` or `lat_lng`.
3. **Build the Route:** Use the `compute_routes` tool to structure a walking-optimized route between the selected spots.
**CRITICAL ROUTING RULE:** To avoid hallucinating, you MUST provide the `origin` and `destination` using the exact `place_id` string OR `lat_lng` object returned by `search_places`. Do NOT guess or hallucinate an `address` or `place_id` if you do not know the exact name.
4. **Insider Tips:** Provide specific "order this, skip that" insider tips for each location on the tour.
Structure your response clearly and concisely. If the user provides a budget, ensure your suggestions align with it.
"""
MAPS_MCP_URL = "https://mapstools.googleapis.com/mcp"
def get_maps_mcp_toolset():
dotenv.load_dotenv()
maps_api_key = os.getenv("MAPS_API_KEY")
if not maps_api_key:
print("Warning: MAPS_API_KEY environment variable not found.")
maps_api_key = "no_api_found"
tools = McpToolset(
connection_params=StreamableHTTPConnectionParams(
url=MAPS_MCP_URL,
headers={
"X-Goog-Api-Key": maps_api_key
}
)
)
print("Google Maps MCP Toolset configured.")
return tools
maps_toolset = get_maps_mcp_toolset()
root_agent = LlmAgent(
model=Gemini(model="gemma-4-31b-it"),
name="food_tour_agent",
instruction=system_instruction,
tools=[maps_toolset],
)环境变量
在运行 Agent 之前设置所需的环境变量。
text
export MAPS_API_KEY="YOUR_GOOGLE_MAPS_API_KEY"
export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"示例用法
要测试美食旅游 Agent 的功能,可以尝试将以下提示之一粘贴到聊天中:
- “我想在多伦多进行拉面之旅。我的一天预算是60美元。给我规划一个前往前三名拉面店的步行路线,并告诉我在每家店应该点什么。”
- “我有这张芝加哥深盘披萨的照片 [插入图片网址]。我想在海军码头周围找到最好的店铺。设计一个步行路线,并告诉我每站必点的披萨。”
- “我在奥斯汀市中心寻找正宗的烧烤之旅。预算控制在100美元以内。建立三家高评价烧烤店之间的步行路线,并提供选购最佳肉类的内幕小贴士。”
该 Agent 将:
- 推断可能的菜系或菜肴风格
- 使用 Google Maps MCP 工具搜索相关地点
- 计算所选停靠点之间的步行路线
- 返回结构化的美食旅游,包括推荐和内幕小贴士
原文:Google Gemma models for ADK agents · 固定源版本
最后更新 2026-11-12 22:49 .