
Dynamic Profile in DB-GPT用 DynConfig 实现 Agent 配置的动态化【免费下载链接】DB-GPTopen-source agentic AI data assistant for the next generation of AI Data products.项目地址: https://gitcode.com/GitHub_Trending/db/DB-GPT一、为什么需要动态 Profile在 Profile 模块 中Agent 通过ProfileConfig定义角色名称name、职责role、目标goal和描述desc这些信息最终会被写入 System Prompt 与 User Prompt影响 LLM 的行为。静态 Profile 适合固定场景但在实际工程中我们常常需要在同一套 Agent 逻辑下根据不同的运行环境或用户输入动态切换角色设定。例如同一个摘要 Agent在英文环境下叫 Summarizer、在中文环境下叫 总结助手或者同一套多 Agent 系统中通过外部配置统一覆盖每个 Agent 的名称与提示语。DB-GPT 提供的动态 Profile 机制正是为了解决这类问题通过DynConfig把 Profile 的某个字段声明为可动态解析的配置项在create_profile()时再按需从指定数据源环境变量、Prompt 管理服务等查询真实值无需修改 Agent 类本身。二、快速开始用 DynConfig 创建动态 Profile按照 profile_dynamic.md 的说明首先新建一个 Python 文件profile_dynamic.pyfrom dbgpt.agent import ProfileConfig, DynConfig profile: ProfileConfig ProfileConfig( # The name of the agent nameDynConfig( Aristotle, keysummary_profile_name, providerenv ), # The role of the agent roleSummarizer, )这里name字段不再是一个普通字符串而是DynConfig对象。从源码看DynConfig是配置动态化的入口util/configure/base.py其完整签名如下参数类型说明defaultAny默认值当配置源查不到值或未设置key时使用keystr配置键名用于向 Provider 查询providerstr / ProviderType / ConfigProvider配置来源env环境变量或prompt_managerPrompt 管理服务categorystr / ConfigCategory配置分类若为ConfigCategory.AGENT且未指定 provider默认使用prompt_manageris_listbool值为列表时置为True若默认值本身就是 list会自动置为Trueseparatorstr列表值拆分分隔符默认[LIST_SEP]descriptionstr配置项说明在本例中providerenv表示name的值将从环境变量summary_profile_name中读取其底层实现为EnvironmentConfigProvider见 util/configure/base.py通过os.environ.get(key, None)查询若环境变量不存在则回退到默认值Aristotle。DynConfig内部实际返回一个ConfigInfo对象util/configure/base.pyProfileConfig中所有字段的类型声明均为str | ConfigInfo | None见 agent/core/profile/base.py因此 name、role、goal、constraints、desc、expand_prompt、examples 乃至三个 prompt 模板字段都可以替换为DynConfig实现整份 Profile 的动态化。三、生成 Promptcreate_profile 与 format 方法配置好 Profile 后需要调用create_profile()生成真实的Profile实例再通过format_system_prompt/format_user_prompt得到最终 Promptreal_profile profile.create_profile() system_prompt real_profile.format_system_prompt(questionWhat can you do?) user_prompt real_profile.format_user_prompt(questionWhat can you do?) print(fSystem Prompt: \n{system_prompt}) print(# * 50) print(fUser Prompt: \n{user_prompt})底层调用链从 agent/core/profile/base.py 的ProfileConfig.create_profile()实现可以看到动态解析发生的位置逐个字段检查是否为ConfigInfo即由DynConfig创建实例若是则调用field.query(prefer_prompt_language..., prefer_model...)触发动态查询未命中时返回default若指定了factory优先使用工厂的create_profile()结果详见 profile_creation.md否则构造DefaultProfile并返回。format_system_prompt与format_user_prompt的实现位于 Profile 抽象类它们基于 Jinja2 模板渲染SandboxedEnvironment并按模板中实际出现的变量动态传入role、name、goal、constraints等上下文见_format_prompt方法 base.py。默认的 System Prompt 模板会输出You are a {{ role }}, named {{ name }}, your goal is {{ goal }}这样的结构。四、验证动态效果不设与设置环境变量的运行对比不设置环境变量运行python profile_dynamic.py由于环境变量summary_profile_name不存在DynConfig回退到默认值输出为System Prompt: You are a Summarizer, named Aristotle, your goal is None. Please think step by step to achieve the goal. You can use the resources given below. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER. *** IMPORTANT REMINDER *** Please answer in English. ################################################## User Prompt: Question: What can you do?设置环境变量运行summary_profile_namePlato python profile_dynamic.py这次环境变量命中Agent 名称被动态替换为 PlatoSystem Prompt: You are a Summarizer, named Plato, your goal is None. Please think step by step to achieve the goal. You can use the resources given below. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER. *** IMPORTANT REMINDER *** Please answer in English. ################################################## User Prompt: Question: What can you do?对比两组输出可以清晰看到只改了环境变量无需改动任何 Agent 代码Prompt 中的 name 就从 Aristotle 变成了 Plato。这正是动态 Profile 的核心价值——把角色配置与Agent 实现解耦。五、动态 Profile 的更多用法5.1 动态化多个字段除name外role、goal、constraints、expand_prompt、examples等字段同样支持DynConfig。例如让角色与目标同时跟随环境变化from dbgpt.agent import ProfileConfig, DynConfig profile: ProfileConfig ProfileConfig( nameDynConfig(Aristotle, keysummary_profile_name, providerenv), roleDynConfig(Summarizer, keysummary_profile_role, providerenv), goalDynConfig( Summarize answer summaries based on user questions from provided resource information or from historical conversation memories., keysummary_profile_goal, providerenv, ), )5.2 使用 Prompt 管理服务作为数据源DynConfig的 provider 还支持prompt_manager。从源码看util/configure/base.pyPromptManagerConfigProvider会在 DB-GPT Web 服务运行时通过prompt_manager.prefer_query(key)从 Prompt 管理服务中查询模板内容并返回。这意味着运营人员可以在管理后台维护提示词Agent 侧无需发布新代码即可生效。值得注意的是当 provider 未显式指定且categoryConfigCategory.AGENT时DynConfig会自动采用prompt_manager作为默认 providerutil/configure/base.py。5.3 列表型动态字段constraints是列表字段可通过is_listTrue配合分隔符把环境变量字符串拆分为列表。DynConfig默认用[LIST_SEP]作为分隔符from dbgpt.agent import ProfileConfig, DynConfig profile: ProfileConfig ProfileConfig( nameAristotle, roleSummarizer, constraintsDynConfig( None, keysummary_profile_constraints, providerenv, is_listTrue, separator[LIST_SEP], ), )六、与其他 Profile 机制的搭配动态 Profile 并非孤立特性它与 Profile 创建、Profile 到 Prompt 的转换共同构成完整的 Agent 角色配置体系静态配置ProfileConfig直接传入字符串适合固定角色的 Agent工厂创建通过ProfileFactory如DefaultProfile的定制封装批量生成 Profile适合千人千面的海量 Agent 场景动态解析通过DynConfig在运行时从外部数据源取值适合跨环境部署、多租户定制、提示词运营等场景。三者可以组合使用例如ProfileConfig中既保留静态的role又把name声明为DynConfigcreate_profile()内部也会先完成所有ConfigInfo字段的解析再交由factory或DefaultProfile生成最终 Profilebase.py。七、小结与调试建议动态 Profile 的关键点可以总结为声明用DynConfig(default, key..., provider...)声明需要动态解析的字段解析时机字段真正的解析发生在profile.create_profile()调用时通过ConfigInfo.query()完成回退策略当key未设置、provider 查询不到值或 provider 不可用时一律回退到default结果可见通过format_system_prompt/format_user_prompt可以直接打印出真实生成的 Prompt便于在调试与上线前核对动态值是否正确注入。与静态 Profile 一样动态 Profile 生成的 Prompt 完全透明可见你始终可以先用print()验证 Prompt 内容再接入实际对话流程完整的调用示例见 profile.md 中的create_profileformat_*演示以及 custom_agents.md 中自定义 Agent 的接入方式。这种配置与实现分离的设计让 DB-GPT 的 Agent 在多环境、多租户场景下保持灵活的同时依旧易于排查与维护。【免费下载链接】DB-GPTopen-source agentic AI data assistant for the next generation of AI Data products.项目地址: https://gitcode.com/GitHub_Trending/db/DB-GPT创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考