本指南将引导您使用 Antigravity 智能体,在 Gemini API 上创建和使用托管式智能体。您将进行首次代理调用、继续多轮对话、流式传输响应、从沙盒下载文件,以及使用 Antigravity 托管代理。
运行您的首次智能体互动
只需对 Interactions API 进行一次调用,即可预配 Linux 沙盒、运行智能体循环并返回结果。您将定义三个参数:
- 传入
agent作为"antigravity-preview-05-2026",,这是我们预定义的一般用途的受管代理的当前版本。 - 定义
environment="remote",以预配新的沙盒环境。 创建输入,定义您希望代理执行的操作。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-05-2026",
input="Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents.",
environment="remote",
)
# Print the agent's final output
print(f"Interaction ID: {interaction.id}")
print(f"Environment ID: {interaction.environment_id}")
print(f"Output: {interaction.output_text}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-05-2026",
input: "Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents.",
environment: "remote",
});
console.log(`Interaction ID: ${interaction.id}`);
console.log(`Environment ID: ${interaction.environment_id}`);
console.log(`Output: ${interaction.output_text}`);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-05-2026",
"input": [{"type": "text", "text": "Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents."}],
"environment": {"type": "remote"}
}'
响应会返回一个 Interaction 对象。存储 interaction.id 和 interaction.environment_id,以便在同一沙盒中继续对话。使用 interaction.output_text 访问代理的最终回答。interaction.steps 列出了智能体采取的每个步骤(推理、工具调用、代码执行)。
继续对话(多回合)
该 API 会跟踪两个独立的状态维度:
- 对话上下文:聊天记录、推理轨迹、工具使用情况、使用
previous_interaction_id。 - 环境状态:使用
environment的文件、已安装的软件包和沙盒状态。
在各自的位置传递这两个实参以恢复:
Python
interaction_2 = client.interactions.create(
agent="antigravity-preview-05-2026",
previous_interaction_id=interaction.id,
environment=interaction.environment_id,
input="Now plot the Fibonacci sequence as a line chart and save it as chart.png.",
)
print(interaction_2.output_text)
JavaScript
const interaction2 = await client.interactions.create({
agent: "antigravity-preview-05-2026",
previous_interaction_id: interaction.id,
environment: interaction.environment_id,
input: "Now plot the Fibonacci sequence as a line chart and save it as chart.png.",
}, { timeout: 300_000 });
console.log(interaction2.output_text);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json"