本页目录
Profiles / 模型配置Note 75

04 HarnessProfileConfig:配置文件加载

它是什么

它是什么

HarnessProfileConfig 是可序列化的 profile 配置,适合从 JSON/YAML 加载。它覆盖的是 HarnessProfile 的声明式子集,比如提示词、工具描述、隐藏工具、禁用 general-purpose subagent。运行时对象、middleware 实例和工厂函数不适合放这里。

最小代码

文件:deepagent_src/profiles_teach/04_config_profile.py

config = HarnessProfileConfig.from_dict(json.loads(CONFIG_PATH.read_text()))
register_harness_profile("openai:gpt-5.5", config)

运行

uv run python deepagent_src/profiles_teach/04_config_profile.py

预期输出末尾:

config profile real agent ok

验证方式

脚本写入一个最小 JSON profile,读取成 HarnessProfileConfig 后注册,并真实调用 Agent;断言 config 能导出 excluded_tools,也断言模型按配置 suffix 返回指定标记。

常见误区

配置文件只适合声明式字段。extra_middleware 这种运行时对象别硬塞 JSON/YAML,放 Python 代码里更清楚。

相关资源

  • 查看示例代码:deepagent_src/profiles_teach/04_config_profile.py
    from __future__ import annotations
    
    import json
    from pathlib import Path
    
    from deepagents import (
        GeneralPurposeSubagentProfile,
        HarnessProfileConfig,
        create_deep_agent,
        register_harness_profile,
    )
    
    from _model import MODEL_PROFILE_KEY, get_real_model
    from deepagent_src.agent_output import invoke_and_pretty_print
    
    
    ROOT_DIR = Path(__file__).resolve().parent
    CONFIG_PATH = ROOT_DIR / "workspace" / "openai_profile.json"
    
    
    def main() -> None:
        config = HarnessProfileConfig.from_dict(json.loads(CONFIG_PATH.read_text()))
        register_harness_profile(MODEL_PROFILE_KEY, config)
    
        agent = create_deep_agent(model=get_real_model())
        result = invoke_and_pretty_print(
            agent,
            {
                "messages": [
                    {
                        "role": "user",
                        "content": "config profile status",
                    }
                ]
            },
        )
    
        assert config.to_dict()["excluded_tools"] == ["execute"]
        assert config.general_purpose_subagent == GeneralPurposeSubagentProfile(
            enabled=False
        )
        assert "CONFIG_PROFILE_ACTIVE" in result["messages"][-1].content
        print("config profile real agent ok")
    
    
    if __name__ == "__main__":
        main()