该 SDK 支持多模态输入附件、实时响应流以及结构化 JSON 输出的 Pydantic 模式验证。
流式响应与推理过程
在具有扩展推理的对话轮中,模型在生成文本响应标记之前发出想法。您可以在发生内部模型想法和文本标记时对其流式传输:
import sys
from google.antigravity import Agent, LocalAgentConfig
config = LocalAgentConfig()
async with Agent(config) as agent:
response = await agent.chat("Solve this riddle and explain your reasoning:")
# Stream internal model thoughts in real time
print("Reasoning:")
async for thought in response.thoughts:
sys.stdout.write(thought)
sys.stdout.flush()
print("\n")
# Stream conversational response tokens
print("Response:")
async for token in response:
sys.stdout.write(token)
sys.stdout.flush()
print("\n")
多模态附件
如果您正在构建多模态 Agent 应用程序,您还可以在文本提示中包含图像和 PDF。
例如,您可以使用 from_file() 或 Image.from_file() 附加 PDF 规范和图像:
from google.antigravity import Agent, LocalAgentConfig
from google.antigravity.types import Image, from_file
config = LocalAgentConfig()
async with Agent(config) as agent:
pdf_spec = from_file("spec.pdf")
chart_image = Image.from_file("chart.png")
prompt = [
"Analyze this chart against the specification:",
chart_image,
pdf_spec,
]
response = await agent.chat(prompt)
print(await response.text())
如需使用本地模型在设备端分析图像附件,请参阅本地模型中的视觉支持。
使用 Pydantic 生成结构化输出
在某些情况下,您可能希望 Agent 应用程序始终以特定格式输出其响应。
例如,您可以使用 response_schema 强制执行与 Pydantic 模式匹配的类型化 JSON 响应:
import pydantic
from google.antigravity import Agent, LocalAgentConfig
class TaskSummary(pydantic.BaseModel):
summary: str
action_items: list[str]
config = LocalAgentConfig(response_schema=TaskSummary)
async with Agent(config) as agent:
response = await agent.chat("Summarize the meeting notes.")
data = await response.structured_output()
print(data["action_items"])
示例代码
有关完整的工作代码示例,请查看 GitHub 存储库: