GoogleCloudPlatform/generative-ai · 上手攻略
- 仓库:GoogleCloudPlatform/generative-ai
- 链接:https://github.com/GoogleCloudPlatform/generative-ai
- 分类:ai
- 作者:Tom
- 更新:2026-07-13
这是什么
GoogleCloudPlatform/generative-ai 是 Google 官方维护的 Google Cloud 生成式 AI 示例代码库,收录 Gemini 模型调用、RAG、Agent Build、搜索增强、视觉生成、音频处理等全场景的 Jupyter Notebook 和代码示例。截至 2026-07 仍保持活跃更新(最近提交 2026-07-10)。
该仓库也是 Google Cloud GenAI 产品体系的官方入口之一,关联了 Vertex AI、Agent Development Kit (ADK)、Agent Starter Pack、Gemini Enterprise Agent Platform 等多条产品线。
解决什么问题
- 不知怎么用 Gemini API:官方文档偏产品说明,代码示例在 GitHub 这里
- 不知道 Google Cloud GenAI 全貌:Vertex AI、Agent Platform、Enterprise Search…傻傻分不清 → 看目录结构就明白了
- 需要 RAG / Function Calling / 视觉生成示例:各子目录对号入座
- 想快速跑一个 Gemini 接入 demo:Notebook 拿来就能在 Colab 跑
快速安装
# 克隆仓库
git clone https://github.com/GoogleCloudPlatform/generative-ai.git
cd generative-ai
# 快速浏览目录结构
ls -la
# gemini/ - Gemini 模型入门与用例
# search/ - Agent Search (原 Enterprise Search)
# rag-grounding/ - RAG 与 Grounding
# vision/ - Imagen / Veo 视觉生成
# audio/ - Chirp 音频模型
# setup-env/ - 环境配置
# 进入 Gemini 示例目录
cd gemini
在 Google Colab 中直接运行(推荐方式):
# 在 Colab 中安装 SDK
!pip install google-genai
# 或使用 Vertex AI SDK
!pip install google-cloud-aiplatform
# 认证
import google.auth
credentials, project = google.auth.default()
本地环境配置(setup-env/):
# 安装 Python SDK
pip install google-genai
# 设置 Google Cloud 项目
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="us-central1"
# 应用默认凭证
gcloud auth application-default login
⚠️ 版本注意:Google Cloud 的 GenAI SDK 有两个体系——旧版
google-generativeai(直接调用 Gemini API)和新版google-genai(功能更全)。建议优先用新版,详见 Google GenAI SDK 文档。
核心用法
目录速查
genai-repository/
├── gemini/ # Gemini 模型核心用法
│ ├── get_started/ # 快速入门
│ ├── use_cases/ # 应用场景
│ ├── function_calling/ # Function Calling
│ └── sample_apps/ # 示例应用
├── search/ # Agent Search(RAG 企业搜索)
├── rag-grounding/ # RAG 与 Grounding 专题索引
├── vision/ # Imagen + Veo 视觉生成
├── audio/ # Chirp 音频处理
├── setup-env/ # 环境配置
├── RESOURCES.md # 学习资源汇总
└── gemini/launch/ # 新手指南
1. Gemini API 调用(gemini/)
# 使用 google-genai SDK
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="Explain quantum computing in simple terms"
)
print(response.text)
# 使用 Vertex AI SDK(企业级,含 IAM 鉴权)
import vertexai
from vertexai.generative_models import GenerativeModel
vertexai.init(project="your-project", location="us-central1")
model = GenerativeModel("gemini-2.0-flash")
response = model.generate_content("Hello, Gemini!")
print(response.text)
2. Function Calling(gemini/function_calling/)
from vertexai.generative_models import GenerativeModel, Tool, FunctionDeclaration
# 定义工具
get_weather = FunctionDeclaration(
name="get_weather",
description="Get weather for a location",
parameters={
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
}
}
)
tools = Tool(function_declarations=[get_weather])
model = GenerativeModel("gemini-2.0-flash", tools=[tools])
response = model.generate_content(
"What's the weather in Tokyo?",
generation_config={"tools": [tools]}
)
# response.candidates[0].function_calls 返回函数调用结果
3. RAG with Grounding(rag-grounding/)
# Vertex AI + Grounding(将回答锚定到可信数据源)
from vertexai.generative_models import GenerativeModel, Groundings
model = GenerativeModel("gemini-2.0-flash")
response = model.generate_content(
prompt="What are the GCP sustainability commitments?",
grounding_config={
"grounding_sources": Groundings.datastore(
datastore_id="your-datastore-id"
)
}
)
print(response.grounding_metadata)
4. Agent Search(search/)
Agent Search(前身 Enterprise Search on Generative AI App Builder)让你快速搭建私有数据搜索引擎,支持网站和企业数据源。
5. 视觉生成(vision/)
from vertexai.vision_models import ImageGenerationModel
model = ImageGenerationModel.from_pretrained("imagegeneration@006")
response = model.generate_images(
prompt="A futuristic cityscape with flying cars",
number_of_images=1
)
response.images[0].save(location="output.png")
6. 音频处理(audio/)
Chirp-3(Google 通用语音模型)用于语音转文字,支持 100+ 语言。
典型适用场景
- 快速接入 Gemini API:从 Notebook 复制代码,改个 API Key 就能跑
- 构建 RAG 系统:Grounding + Vertex AI Search 组合使用
- 企业级 AI 应用:IAM 鉴权、VPC 部署、数据驻留合规
- Function Calling / 工具调用:Gemini 作为 Toolformer 调用外部 API
- 多模态:视觉生成(Imagen)、音频处理(Chirp)、视频生成(Veo)
- Agent 开发:结合 ADK(Agent Development Kit)和 Agent Starter Pack
坑与注意
| 坑 | 说明 |
|---|---|
| 需要 GCP 项目 | 大部分功能需要 Google Cloud 项目,部分功能需开启收费 |
| 区域限制 | Gemini 某些版本和功能只在美国区域(us-central1)可用 |
| SDK 版本混乱 | google-generativeai vs google-genai vs vertexai,三个包容易混 |
| Colab 强依赖 | 很多 Notebook 深度绑定 Colab 环境,本地跑要额外配置 |
| 权限配置复杂 | 企业环境需要精细的 IAM 配置,不是开箱即用 |
| Notebook ≠ 生产代码: | 示例是 Notebook,在生产环境使用需要工程化改造 |
| 文档位置分散 | README 引用了大量外部链接(ADK、Cookbook 等),容易迷路 |
⚠️ 特别提醒:该仓库明确声明「This repository itself is not an officially supported Google product」,代码仅供演示。生产环境建议使用 Google 官方支持的 SDK 和产品。
与同类对比
| 维度 | generative-ai 仓库 | OpenAI API 官方 Cookbook | Anthropic Cookbook |
|---|---|---|---|
| 定位 | Google Cloud 官方示例 | OpenAI 官方示例 | Anthropic 官方示例 |
| 深度 | Notebook 为主,偏浅 | Notebook 为主 | 代码片段为主 |
| 覆盖 | 全模态(文本/视觉/音频) | 文本为主 | 文本为主 |
| 可直接跑 | ✅ Colab 可直接跑 | ✅ | ✅ |
| 许可证 | Apache-2.0 | CC-BY-NC | CC-BY-NC |
| 关联产品 | Vertex AI、ADK、Gemini | OpenAI API | Anthropic API |
结论:如果你用 Google Cloud + Gemini,这是最权威的示例库;如果你是 OpenAI 或 Anthropic 重度用户,对应官方的 Cookbook 更合适。
一句话推荐结论
Google Cloud Gemini 生态最官方的代码示例库,Colab 直跑适合快速验证思路,但示例代码不等于生产级代码,企业使用需配合官方支持的 Vertex AI SDK 并注意区域和权限配置。