十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

从 Google AI Studio 迁移到 Gemini Enterprise Agent Platform:完整迁移指南与实战验证

从 Google AI Studio 迁移到 Gemini Enterprise Agent Platform:完整迁移指南与实战验证 从 Google AI Studio 迁移到 Gemini Enterprise Agent Platform完整迁移指南与实战验证【免费下载链接】skillsAgent Skills for Google products and technologies项目地址: https://gitcode.com/GitHub_Trending/skills29/skills导读本文基于 agent-platform-migrate-from-ai-studio 技能文档系统讲解如何将基于 Gemini Developer APIGoogle AI Studiogenerativelanguage.googleapis.com构建的应用迁移到企业级 Google Cloud Agent Platform前身 Vertex AIaiplatform.googleapis.com。你将掌握计费与 Cloud 信用额度切换、IAM 认证体系改造、GenAI SDK / ADK / Antigravity CLI / OpenClaw 四种调用方式的无缝对接以及迁移过程中的关键坑点与验证手段。读完本文你可以把现有 AI Studio 应用在几乎不改业务代码的前提下平稳落地到 Google Cloud 的统一 IAM、账单与可观测性体系之中。何时需要触发本次迁移该技能文档明确了四类典型的迁移触发场景切换调用后端应用当前依赖 Google AI Studio 生态generativelanguage.googleapis.com需要迁移到 Agent Platformaiplatform.googleapis.com。使用 Google Cloud 信用额度你持有 Google Cloud 信用例如 $300 Welcome Free Trial希望将其用于 Gemini API 推理成本而 AI Studio 不接受此类信用。统一云基础设施需要将推理管线、IAM 权限、遥测与计费与现有 Google Cloud 基础设施Compute Engine、Cloud SQL、BigQuery 等统一管理。开源编排引擎上云正在 Google Cloud VM 上部署开源编排引擎如 OpenClaw 或 ADK Agents希望整个系统运行在统一的 Google Cloud 计费结构下。一句话概括只要你的 Gemini 调用要享受 Cloud 信用、企业级安全合规与统一运维就该走 Agent Platform。两个平台的 Gemini API 核心差异技能文档用一张对照表清晰刻画了两个平台的本质区别这是迁移决策的事实基础特性 / 控制项Google AI StudioGemini Developer APIAgent Platform企业级 Gemini APIAPI 端点generativelanguage.googleapis.comaiplatform.googleapis.com目标受众构建生产应用的开发者、初创团队、学生、研究者企业生产环境、MLOps 工程师GCP 信用支持不支持GCP 信用/Free Trial 不可用支持可完全使用 Welcome 或自定义信用数据隐私数据可能被审查以改进 Google 产品提示词/响应绝不用于训练安全与 IAMAPI Key、OAuthGoogle Cloud IAMService Account、OAuth 2.0、VPC-SC合规与 SLA无尽力而为的可用性7×24 企业支持、SLA、HIPAA、SOC2吞吐选项共享 / 限流按量付费Pay-as-you-go或预置吞吐Provisioned ThroughputMLOps 生态基础提示词管理Model Registry、Model Monitoring、Pipeline Evaluation推理范围仅全局端点全局与严格的区域端点均支持几点值得展开的解读数据隐私是硬差异AI Studio 的数据可能被用于改进 Google 产品而 Agent Platform 的提示词与响应承诺永不用于训练。涉及敏感业务数据的应用这是迁移的首要动因。吞吐能力差异AI Studio 是共享且限流的Agent Platform 提供按量付费Pay-as-you-go与预置吞吐Provisioned Throughput两种模式后者适合对延迟和容量有确定性要求的生产负载。仓库中的 agent-platform-inference 技能对 429 资源耗尽错误有专门剖析——OpenMaaS 与 Gemini 模型使用动态共享配额DSQ共享池瞬时耗尽会触发 429此时应实施严格的指数退避与重试而高吞吐生产负载则应考虑预置吞吐。这与本迁移文档的吞吐选项列互为印证。区域端点Agent Platform 既支持全局端点global也支持严格区域端点。迁移后可按合规要求选择区域。计费与 Cloud 信用额度迁移的第一动机技能文档特别强调Google Cloud Free Trial 信用不适用于 AI Studio。要使用信用额度支付 Gemini 模型费用必须将调用路由到 Agent Platform。迁移账单按以下步骤进行创建 Google Cloud 计费账号设置过程中必须提供有效的付款方式以验证身份。确认 Welcome 信用生效如果你是全新客户请在 Billing Console 中确认 $300 Welcome 信用处于激活状态。避免账单惊吓Billing Surprises为防止信用耗尽后自动回落到常规付款方式建议建立预算告警进入Billing → Budgets Alerts → Create Budget将阈值设置为对应你的信用额度或可接受的最大支出。提示预算告警在信用额度将尽时提前通知你避免产生预期外的账单这是企业上云的标准护栏实践。启用 Agent Platform API在目标 Google Cloud 项目上必须显式启用 Agent Platform API在本地 shell 执行gcloud services enable aiplatform.googleapis.com --project{project_id}{project_id}替换为你的 Google Cloud 项目 ID。这一步是后续所有 IAM 绑定、SDK 调用、Agent 部署的前提——API 未启用时任何aiplatform.googleapis.com请求都会返回 403/404。认证与授权IAM改造迁移到 Agent Platform 意味着从API Key 时代切换到Google Cloud IAM 时代。技能文档覆盖两种身份形态。用户认证本地开发与脚本本地调试或脚本执行时使用**应用默认凭据Application Default CredentialsADC**认证。方式一自动化脚本bash (curl -sSL https://storage.googleapis.com/cloud-samples-data/adc/setup_adc.sh)方式二手动配置gcloud auth login gcloud auth application-default login随后为用户身份授予执行推理所需的 IAM 角色gcloud projects add-iam-policy-binding {project_id} \ --memberuser:YOUR_EMAILdomain.com \ --roleroles/aiplatform.userroles/aiplatform.user是 Agent Platform 的常规用户角色授予对 AI Platform 资源含推理端点的读写访问。仓库中的 gcloud CLI 使用参考 对认证方式给出了更全面的清单可作为补充除gcloud auth login交互式与gcloud auth application-default loginADC之外还支持--no-browser无头流程容器/远程 SSH 环境、gcloud auth activate-service-account --key-file...无头自动化、以及服务账号模拟gcloud config set auth/impersonate_service_account需目标服务账号具备roles/iam.serviceAccountTokenCreator和 Workload Identity FederationCI/CD 场景。对于迁移后运行在云上的 Agent 应用推荐优先采用模拟或 Workload Identity 这类免长驻密钥的方式符合最小权限与可审计原则。服务认证云端运行当应用运行在 Google Cloud 基础设施如 Compute Engine VM上时使用机器附加的服务账号进行认证典型如 Compute Engine 默认服务账号。为 VM 底层服务账号授予用户角色gcloud projects add-iam-policy-binding {project_id} \ --memberserviceAccount:PROJECT_NUMBER-computedeveloper.gserviceaccount.com \ --roleroles/aiplatform.user注意成员标识中的PROJECT_NUMBER项目编号而非项目 ID可通过gcloud projects describe获取。检查 Compute Engine 访问范围Access Scopes遗留的访问范围机制可能覆盖 IAM 绑定。创建或修改 Compute Engine 实例时必须确认 VM 访问范围配置为Allow full access to all Cloud APIshttps://www.googleapis.com/auth/cloud-platform或显式包含标准的cloud-platform范围。否则即使 IAM 绑定正确实际请求仍可能因 scope 不足被拒绝——这是迁移中最常见的隐性坑点之一。在 Agent Platform 中使用 Gemini API统一 GenAI SDKPython 客户端库这是本次迁移中业务代码改动最小的路径继续使用统一的 Google GenAI SDKgoogle-genai它同时兼容 AI Studio 与 Agent Platform 两个后端只需通过运行时环境变量切换路由标志即可指向 Agent Platform。设置目标环境export GOOGLE_CLOUD_PROJECT{project_id} export GOOGLE_CLOUD_LOCATIONglobal # 或你选择的区域端点 export GOOGLE_GENAI_USE_ENTERPRISETRUE然后标准 Python 代码无需改动核心初始化块即可从 AI Studio 切换到 Agent Platformfrom google import genai # 客户端自动读取 GOOGLE_GENAI_USE_ENTERPRISETRUE 环境标志 client genai.Client() response client.models.generate_content( modelgemini-3-flash-preview, contentsHello world!, ) print(response.text)从源码看实现印证仓库 agent-platform-inference/scripts/gemini_genai_sdk.py 展示了显式指定后端的等价写法from google import genai import google.auth _, project_id google.auth.default() # 以 Vertex AI 后端初始化 GenAI 客户端 # Preview 模型Gemini 2.0 系列使用 locationglobal client genai.Client(enterpriseTrue, projectproject_id, locationus-central1) response client.models.generate_content( modelgemini-2.5-pro, contentsWhy is the sky blue? ) print(response.text)这里的enterpriseTrue参数与GOOGLE_GENAI_USE_ENTERPRISETRUE环境变量等价二者都让genai.Client()指向 Agent Platform 后端。同目录的 gemini_vertexai_sdk.py 展示了更传统的 Vertex AI SDK 写法vertexai.init(project..., location...)GenerativeModel可作为对照。此外 agent-platform-tuning/scripts/tune_open_model.py 与 monitor_tuning_job.py 中反复出现genai.Client(enterpriseTrue, projectproject, locationlocation)模式印证了这是仓库内 Agent Platform 系列技能的标准客户端初始化范式。客户端初始化还有两种等效方式详见 gemini-interactions-api推荐的环境变量方式配置GOOGLE_GENAI_USE_ENTERPRISEtrue、GOOGLE_CLOUD_PROJECT、GOOGLE_CLOUD_LOCATION后Python 中client genai.Client()或 TypeScript 中const ai new GoogleGenAI();自动解析。显式内联参数方式genai.Client(enterpriseTrue, projectproject_id, locationglobal)TypeScript 对应new GoogleGenAI({ enterprise: { project, location } })。依赖清单可参考 agent-platform-inference/scripts/requirements.txtgoogle-genai、google-cloud-aiplatform、google-auth等。另外注意迁移后如果调用的是自定义部署的端点可参考 agent-platform-inference 的写法用完整资源名调用client genai.Client(vertexaiTrue, projectproject_id, locationus-central1) response client.models.generate_content( modelfprojects/{project_id}/locations/us-central1/endpoints/{ENDPOINT_ID}, contentsHello! Introduce yourself briefly., config{temperature: 0.2}, )Agent Development KitADK要在 ADK Agent 中通过 Agent Platform 调用 Gemini 模型按以下步骤认证 Google Cloud若 ADK Agent 运行在 Google Cloud如 Agent Platform Runtime使用 Agent 分配的服务账号若在本地运行则执行gcloud auth application-default login设置环境变量无论 ADK Agent 运行在 Google Cloud 还是本地都必须设置export GOOGLE_CLOUD_PROJECT{project_id} export GOOGLE_CLOUD_LOCATIONglobal export GOOGLE_GENAI_USE_ENTERPRISETRUE初始化 ADK Agent可以直接沿用你在 AI Studio 中使用的模型字符串如gemini-3-flash-previewfrom google.adk.agents.llm_agent import Agent def get_current_time(city: str) - dict: Returns the current time in a specified city. return {status: success, city: city, time: 10:30 AM} root_agent Agent( modelgemini-3-flash-preview, nameroot_agent, descriptionTells the current time in a specified city., instructionYou are a helpful assistant that tells the current time in cities. Use the get_current_time tool for this purpose., tools[get_current_time], )这里的Agent来自google.adk.agents.llm_agent模型字符串与 AI Studio 完全一致工具函数以普通 Python 函数形式直接注入tools参数——迁移时 ADK 层几乎零改动。Antigravity CLIGoogle Cloud 用户可通过 Gemini Enterprise Agent Platform 访问 Antigravity 2.0含 Antigravity CLI。操作路径在本地环境安装 Antigravity CLI。启动 Antigravity CLIagy跟随 CLI 设置提示选择Use a Google Cloud Project。在打开的浏览器窗口中用已认证的 Google Cloud Workspace 或用户身份完成 OAuth 流程。复制确认令牌直接粘贴回终端。按提示输入你的 Google Cloud 项目 ID。选择 Google Cloud 位置例如global。可选运行/model命令在 Agent Platform 中选择不同的 Gemini 模型。Antigravity CLI 为偏好命令行工作流的开发者提供了一条交互式配置通道OAuth 确认令牌机制确保了凭据不在终端明文流转。OpenClaw将 OpenClaw Agent 配置为使用 Agent Platform 上的 Gemini 模型安装 OpenClaw按 OpenClaw 官方安装说明完成安装。⚠️重要OpenClaw 2026.5.28 版本当前存在google-vertexbug请暂时使用 2026.5.20 版本。IAM 角色确保 OpenClaw 运行的运行时例如带服务账号的 GCE VM具备aiplatform.userIAM 角色——参见上文认证与授权一节。获取项目编号Project Number在 GCE 之外运行export PROJECT_NUMBER$(gcloud projects describe $PROJECT_ID --formatvalue(projectNumber)) echo Project number: $PROJECT_NUMBER在 Compute Engine VM 内部运行export PROJECT_NUMBER$(curl http://metadata.google.internal/computeMetadata/v1/project/project-id -H Metadata-Flavor: Google) echo Project number: $PROJECT_NUMBER生成凭据并传送到 VM在 GCE 实例之外使用项目编号生成GOOGLE_APPLICATION_CREDENTIALS然后通过scp将凭据传送到 GCE VMmkdir -p ~/.config/gcloud gcloud iam service-accounts keys create ~/.config/gcloud/application_default_credentials.json --iam-account${PROJECT_NUMBER}-computedeveloper.gserviceaccount.com⚠️新组织策略约束排障2024 年 5 月 4 日及之后创建的新 Google Cloud 组织会强制一项遗留组织策略限制服务账号密钥创建。要完成此步骤必须临时停用该 Org Policy需要由组织策略管理员将该策略设为 Inactive。作为 Agent 执行时先检查该策略是否被强制执行若已执行则暂停并提示用户前往 Console 的 IAM Admin → Organization Policies 页面停用它https://console.cloud.google.com/iam-admin/orgpolicies。编辑配置文件通常位于~/.openclaw/openclaw.json确保 Gemini 模型名以google-vertex/前缀开头。⚠️重要不要使用 Gemini 3.5 模型OpenClaw 的google-vertexprovider 目前尚不支持。旧模型可用。在 Agent Platform 使用 Gemini 3 Flash Preview 模型时位置务必设置为global而不是区域端点。{ env: { vars: { GOOGLE_CLOUD_PROJECT: PROJECT_ID, GOOGLE_CLOUD_LOCATION: global, GOOGLE_APPLICATION_CREDENTIALS: ~/.config/gcloud/application_default_credentials.json } }, agents: { defaults: { model: { primary: google-vertex/gemini-3-flash-preview }, workspace: ~/.openclaw/workspace, compaction: { mode: safeguard }, heartbeat: { model: google-vertex/gemini-3-flash-preview } }, list: [ { id: main, workspace: ~/.openclaw/workspace, model: google-vertex/gemini-3-flash-preview } ] }, session: { dmScope: per-channel-peer }, tools: { profile: coding } }该配置的要点env.vars注入项目、位置与凭据路径agents.defaults.model.primary与heartbeat.model都以google-vertex/前缀指向 Agent Platform 的 Gemini 模型session.dmScope控制会话作用域tools.profile指定工具画像。重启 OpenClawopenclaw gateway restart验证 OpenClaw 与 Agent Platform 的连接openclaw models status openclaw agent --agent main --message Hello world!models status检查模型路由状态agent命令则用一个 Hello World 消息做端到端冒烟验证。迁移后的统一收益完成上述迁移后你的 Gemini 推理调用将自动获得统一的 IAM 治理服务账号、OAuth 2.0、VPC-SC 网络边界与现有 Cloud 资源一致统一的计费与遥测推理成本进入 Cloud 账单体系配合预算告警可控可审计监控与日志与 Compute Engine、Cloud SQL、BigQuery 等基础设施打通企业级合规保障SLA、7×24 企业支持、HIPAA/SOC2以及提示词/响应绝不用于训练的数据隐私承诺更灵活的吞吐与区域控制按量付费或预置吞吐全局或严格区域端点按需选择。参考资源本技能主文档agent-platform-migrate-from-ai-studio/SKILL.mdAgent Platform 推理调用与错误码剖析agent-platform-inference/SKILL.mdGenAI SDK 客户端初始化环境变量与内联参数双方案gemini-interactions-api/SKILL.mdGemini API 使用与模型调参gemini-api/SKILL.mdgcloud 认证与本地配置全指南gcloud/references/cli-usage.mdAgent Platform 常见问题与排查agent-platform-troubleshooting/references/field-manual.mdAgent Platform 部署与验证agent-platform-deploy【免费下载链接】skillsAgent Skills for Google products and technologies项目地址: https://gitcode.com/GitHub_Trending/skills29/skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表