角色设定 (Personas)

本页目录

SDK 支持您使用模板或自定义系统指令自定义 Agent 的身份、行为限制和指令。

模板化系统指令

对于大多数应用程序,建议使用模板化系统指令。此方法保留默认的 SDK 脚手架和环境上下文,同时覆盖角色身份并附加自定义结构化部分。

例如,您可以模板化系统指令来定义专注于审查拉取请求的代码质量的 Agent:

from google.antigravity import Agent, LocalAgentConfig, types

identity = "You are an expert Code Quality Reviewer."
review_criteria = types.SystemInstructionSection(
    title="review_criteria",
    content="- Focus on readability and maintainability.",
)

templated_si = types.TemplatedSystemInstructions(
    identity=identity,
    sections=[review_criteria],
)

config = LocalAgentConfig(system_instructions=templated_si)

async with Agent(config) as agent:
    response = await agent.chat("Review the latest pull request.")
    print(await response.text())

自定义系统指令

为了实现完全控制,自定义系统指令绕过所有默认 SDK 脚手架和环境上下文,支持您直接传递原始系统提示。

例如,您可以自定义系统指令来设置“自定义代码审阅者”的身份:

from google.antigravity import Agent, LocalAgentConfig, types

custom_si = types.CustomSystemInstructions(
    text="<identity>Custom Code Reviewer</identity>"
)

config = LocalAgentConfig(system_instructions=custom_si)

async with Agent(config) as agent:
    response = await agent.chat("Audit this file.")
    print(await response.text())

示例代码

有关完整的工作代码示例,请查看 GitHub 存储库: