Antigravity SDK 概览

本页目录

Google Antigravity SDK 是一个 Python SDK,用于构建由 Antigravity 和 Gemini 提供支持的自主 AI Agent。它提供了一个安全、有状态的运行时工具,可处理工具执行、上下文管理、安全策略和子 Agent 委派。

如果您正在寻找托管云 REST/gRPC API 而不是本地 Python SDK 运行时,请查看 Gemini API Antigravity Agent 文档。

快速入门

使用 pip 安装 SDK 包并配置您的 API 密钥以开始使用:

pip install google-antigravity

在您的环境中设置 Gemini API 密钥:

export GEMINI_API_KEY="your_api_key_here"

初始化一个Agent并开始对话:

import asyncio
from google.antigravity import Agent, LocalAgentConfig

async def main():
    config = LocalAgentConfig()
    async with Agent(config) as agent:
        response = await agent.chat("What files are in the current directory?")
        print(await response.text())

if __name__ == "__main__":
    asyncio.run(main())

Gemini Enterprise

要将 SDK 连接到 Gemini Enterprise,请在 vertex=True 中设置 LocalAgentConfig 以及 GCP project 和 location:

from google.antigravity import Agent, LocalAgentConfig

config = LocalAgentConfig(
    vertex=True,
    project="your-gcp-project",
    location="us-central1",
)

async with Agent(config) as agent:
    response = await agent.chat("Hello!")
    print(await response.text())

还支持环境变量:

export GOOGLE_GENAI_USE_VERTEXAI=True
export GOOGLE_CLOUD_PROJECT="your-gcp-project"
export GOOGLE_CLOUD_LOCATION="us-central1"
gcloud auth application-default login

注意: 设置 vertex=True(或 GOOGLE_GENAI_USE_VERTEXAI=True)将 SDK 配置为使用 Google Cloud 凭据通过 Gemini Enterprise API (aiplatform.googleapis.com) 路由请求。

Agent 核心基础架构

Agent 类管理异步上下文管理器背后的二进制发现、工具执行和会话生命周期。

例如,您可以使用自定义系统指令配置 Agent 以使用特定角色交互:

import asyncio
from google.antigravity import Agent, LocalAgentConfig

async def main():
    config = LocalAgentConfig(
        system_instructions=(
            "You are a helpful pirate assistant. Speak like a pirate."
        ),
    )
    async with Agent(config) as agent:
        response = await agent.chat("Explain the repository layout.")
        print(await response.text())

if __name__ == "__main__":
    asyncio.run(main())

下一步

浏览以下指南,了解有关使用 Python SDK 构建和自定义 Agent 的更多信息:

  • 本地模型:使用 LiteRT 或本地 OpenAI 兼容服务器在设备上运行 Agent。
  • 角色:使用模板或自定义系统指令自定义 Agent 身份。
  • 工具和技能:注册自定义Python函数、使用内置工具、加载技能。
  • MCP:将外部 Model Context Protocol (MCP) 服务器连接到您的 Agent。
  • 政策:强制实施明确的工具执行策略和交互式审批流程。
  • 子 Agent:使用动态自克隆或静态子 Agent 构建多 Agent 系统。
  • 结构化输出:处理多模态输入、流模型思想并验证输出。
  • 生命周期和钩子:管理后台事件触发器、会话持久性和自定义钩子。

示例代码与用例

您可以在 GitHub 上的 开始目录 中找到每个 SDK 功能的完整、可运行的 Python 脚本: