Files
2024-12-06 14:34:12 +08:00

8.2 KiB
Raw Permalink Blame History

智能体模版(Agent Template)

智能体模版用于帮助用户快速构建智能体当完成template模版层抽象后使用者只需填写智能体模版里特定属性配置信息即可执行特定编排逻辑如agentUniverse系统内置的RAG template/ReAct template/PEER template等。

如何定义智能体模版

智能体模版基础类定义

AgentTemplate基于Agent基础类,包含配置信息如下:

  • llm_name: LLM领域组件名称, 用户在agent yaml profile中llm_model配置后,模版类自动装配
  • memory_name: 记忆领域组件名称, 用户在agent yaml memory中配置后,模版类自动装配
  • tool_names: 智能体工具领域组件名称列表, 用户在agent yaml tools中配置后,模版类自动装配
  • knowledge_names: 智能体知识领域组件名称列表, 用户在agent yaml knowledge中配置后,模版类自动装配
  • prompt_version: 智能体prompt版本号, 用户在agent yaml profile中prompt_version中配置后,模版类自动装配

包含方法如下:

  • execute(self, input_object: InputObject, agent_input: dict, **kwargs) -> dict:
    智能体模版同步执行方法包含智能体记忆、LLM、Prompt的处理逻辑。
  • async_execute(self, input_object: InputObject, agent_input: dict, **kwargs) -> dict: : 智能体模版异步执行方法包含智能体记忆、LLM、Prompt的处理逻辑。

  • customized_execute(self, input_object: InputObject, agent_input: dict, memory: Memory, llm: LLM, prompt: Prompt, **kwargs) -> dict: : 智能体模版自定义同步执行方法,智能体模版基础类提供了默认实现,推荐您自定义智能体模版时,重写该方法

  • customized_async_execute(self, input_object: InputObject, agent_input: dict, memory: Memory, llm: LLM, prompt: Prompt, **kwargs) -> dict: : 智能体模版自定义异步执行方法,智能体模版基础类提供了默认实现,推荐您自定义智能体模版时,重写该方法

  • process_llm(self, **kwargs) -> LLM:
    智能体模版LLM默认处理逻辑。
  • process_memory(self, agent_input: dict, **kwargs) -> Memory | None:
    智能体模版记忆默认处理逻辑。
  • process_prompt(self, agent_input: dict, **kwargs) -> Prompt:
    智能体模版Prompt默认处理逻辑。
  • invoke_tools(self, input_object: InputObject, **kwargs):
    智能体模版默认工具调用逻辑。
  • invoke_knowledge(self, query_str: str, input_object: InputObject, **kwargs):
    智能体模版默认知识调用逻辑。
  • invoke_chain(self, chain: RunnableSerializable[Any, str], agent_input: dict, input_object: InputObject, **kwargs):
    智能体模版默认chain同步调用逻辑。
  • async_invoke_chain(self, chain: RunnableSerializable[Any, str], agent_input: dict, input_object: InputObject, **kwargs): : 智能体模版默认chain异步调用逻辑。

智能体模版具体实现类的定义

template模版实现类需继承AgentTemplate基础类并定义当前模版的input_keysoutput_keys具体出入参信息,parse_inputparse_result具体出入参解析方法。

若智能体模版基础类中的自定义执行方法如customized_execute不能满足您的自定义编排需求,推荐您重写该方法,实现特定的执行逻辑。 同理如process_llminvoke_tools等一些列智能体相关领域组件处理逻辑,均可重写方法实现自定义需求。

AgentTemplate基础类定义已足够抽象推荐您根据需求重写特定方法,不推荐完全复制,代码结构臃肿复杂,维护成本高

如何使用智能体模版

以agentUniverse内置的PEER智能体模版为例,可填写配置信息如下(以下配置信息均存在默认值):

  • planning_agent_name: plan拆解节点智能体名称用户在agent yaml profile中配置planning后,模版类自动装配
  • executing_agent_name: execute执行节点智能体名称用户在agent yaml profile中配置executing后,模版类自动装配
  • expressing_agent_name: express表达节点智能体名称用户在agent yaml profile中配置expressing后,模版类自动装配
  • reviewing_agent_name: review评价节点智能体名称用户在agent yaml profile中配置reviewing后,模版类自动装配
  • expert_framework: peer多智能体专家框架用户在agent yaml profile中配置expert_framework后,模版类自动装配

创建PEER智能体模版的实际智能体实例

如上文描述当完成template模版层抽象后使用者只需填写智能体模版里特定属性配置信息即可执行特定编排逻辑配置信息如下

info:
  name: 'demo_peer_agent'
  description: 'demo peer agent'
profile:
  planning: 'demo_planning_agent'
  executing: 'demo_executing_agent'
  expressing: 'demo_expressing_agent'
  reviewing: 'demo_reviewing_agent'
  expert_framework:
    context:
      planning: # planning节点对应专家知识
      executing: # executing节点对应专家知识
      expressing: # expressing节点对应专家知识
      reviewing: # reviewing节点对应专家知识
    selector:
memory:
  name: 'demo_memory'
metadata:
  type: 'AGENT'
  module: 'agentuniverse.agent.template.peer_agent_template'
  class: 'PeerAgentTemplate'

agentUniverse目前内置有以下智能体模版:

PeerAgentTemplate

上文已介绍,此处省略。

Peer智能体模版中四个节点智能体实例类型需为对应智能体模版类PlanningAgentTemplate/ExecutingAgentTemplate/ExpressingAgentTemplate/ReviewingAgentTemplate或对应自定义子类。

PlanningAgentTemplate

Peer中的Planning节点智能体模版。

ExecutingAgentTemplate

Peer中的Executing节点智能体模版。

ExpressingAgentTemplate

Peer中的Expressing节点智能体模版。

ReviewingAgentTemplate

Peer中的Reviewing节点智能体模版。

RagAgentTemplate

RAG智能体模版具体配置示例如下

info:
  name: 'demo_rag_agent'
  description: 'demo rag agent'
profile:
  prompt_version: demo_rag_agent.cn
  llm_model:
    name: 'qwen_llm'
action:
  tool:
    - 'xxx_tool_1'
  knowledge:
    - 'xxx_knowledge_1'
memory:
  name: 'xxx_memory'
metadata:
  type: 'AGENT'
  module: 'agentuniverse.agent.template.rag_agent_template'
  class: 'RagAgentTemplate'

ReActAgentTemplate

ReAct智能体模版具体配置示例如下

info:
  name: 'demo_react_agent'
  description: 'react agent'
profile:
  prompt_version: qwen_react_agent.cn
  llm_model:
    name: 'qwen_llm'
    model_name: 'qwen-max'
    stop: 'Observation'
    temperature: 0.1
action:
  tool:
    - 'xxx_tool_1'
    - 'xxx_tool_2'
  knowledge:
    - 'xxx_knowledge_1'
memory:
  name: 'xxx_memory'
metadata:
  type: 'AGENT'
  module: 'agentuniverse.agent.template.react_agent_template'
  class: 'ReActAgentTemplate'

Nl2ApiAgentTemplate

Nl2Api智能体模版具体配置示例如下

info:
  name: 'demo_nl2api_agent'
  description: 'demo nl2api agent'
profile:
  llm_model:
    name: 'qwen_llm'
action:
  tool:
    - 'xxx_tool_1'
    - 'xxx_tool_2'
    - 'xxx_tool_3'
    - 'xxx_tool_4'
    - 'xxx_tool_5'
metadata:
  type: 'AGENT'
  module: 'agentuniverse.agent.template.nl2api_agent_template'
  class: 'Nl2ApiAgentTemplate'

总结

至此您已了解智能体模版的作用,智能体模版的具体定义以及如何使用智能体模版,请移步至工作模式文档,我们将具体向您介绍工作模式的定义、工作模式/智能体模版与agentUniverse的计划的区别等。