|
| 1 | +# ClassDef AgentDispatcher |
| 2 | +**AgentDispatcher函数**: 这个类的功能是Agent调度器,是一个抽象基类。 |
| 3 | + |
| 4 | +Agent调度器是一个用于调度任务的基类,它负责根据任务的需求类型将任务分配给相应的Agent。Agent调度器维护了一个代理市场(agent_markets),其中包含了各种能力类型所对应的代理。它还提供了一些方法用于注册代理、分配角色以及构建代理。 |
| 5 | + |
| 6 | +AgentDispatcher类的构造函数初始化了代理市场(agent_markets)和日志记录器(logger)。代理市场是一个字典,其中的键是需求类型(RequiredAbilities),值是一个代理列表,用于存储具有相应能力的代理。日志记录器用于记录构造AgentDispatcher对象的日志。 |
| 7 | + |
| 8 | +AgentDispatcher类还定义了一个抽象方法dispatch,用于根据任务的需求类型分派任务给相应的代理。具体的分派逻辑由子类实现。此外,AgentDispatcher类还提供了dispatch_role方法用于分派角色,regist_agent方法用于注册代理。 |
| 9 | + |
| 10 | +AgentDispatcher的子类AutomaticAgentDispatcher和XAgentDispatcher分别实现了dispatch方法。AutomaticAgentDispatcher类是一个自动调度器,它根据任务的需求类型自动将任务分派给代理市场中相应能力的代理。XAgentDispatcher类是一个特定的Agent调度器,它根据任务的需求类型构建代理,并对任务的提示进行细化。 |
| 11 | + |
| 12 | +**注意**: 在使用AgentDispatcher类时,需要注意以下几点: |
| 13 | +- AgentDispatcher是一个抽象基类,不能直接实例化,需要通过继承它的子类来使用。 |
| 14 | +- 子类需要实现dispatch方法,根据任务的需求类型来具体实现任务的分派逻辑。 |
| 15 | +- 在使用regist_agent方法注册代理时,需要确保代理具有相应的能力。 |
| 16 | + |
| 17 | +**输出示例**: |
| 18 | +``` |
| 19 | +# 创建AgentDispatcher对象 |
| 20 | +dispatcher = AgentDispatcher(logger) |
| 21 | +
|
| 22 | +# 注册代理 |
| 23 | +dispatcher.regist_agent(agent1) |
| 24 | +dispatcher.regist_agent(agent2) |
| 25 | +
|
| 26 | +# 分派任务 |
| 27 | +agent = dispatcher.dispatch(RequiredAbilities.plan_generation, target_task) |
| 28 | +``` |
| 29 | +## FunctionDef dispatch_role |
| 30 | +**dispatch_role函数**:这个函数的作用是为目标任务分派一个角色。 |
| 31 | + |
| 32 | +详细代码分析和描述: |
| 33 | +这个函数接受一个名为target_task的参数,它是一个TaskSaveItem类型的对象,表示需要分派角色的任务。在函数体内部,它返回一个默认的AgentRole对象。 |
| 34 | + |
| 35 | +**注意**:使用这段代码时需要注意以下几点: |
| 36 | +- 这个函数的返回值是一个AgentRole对象。 |
| 37 | + |
| 38 | +**输出示例**:模拟代码返回值的可能外观。 |
| 39 | +``` |
| 40 | +AgentRole() |
| 41 | +``` |
| 42 | +## FunctionDef regist_agent |
| 43 | +**regist_agent函数**:该函数的功能是将代理注册到相应的代理市场中,根据其能力。 |
| 44 | + |
| 45 | +该函数接受一个参数agent,代表需要注册的代理。函数首先遍历所有的RequiredAbilities,如果某个ability在agent的abilities列表中,就将该agent添加到对应ability的代理市场中。 |
| 46 | + |
| 47 | +**注意**:使用该代码时需要注意以下几点: |
| 48 | +- 参数agent必须是BaseAgent的实例。 |
| 49 | +- agent的abilities属性必须包含所有的RequiredAbilities中的某个ability,否则该agent不会被注册到代理市场中。 |
| 50 | +*** |
| 51 | +# ClassDef AutomaticAgentDispatcher |
| 52 | +**AutomaticAgentDispatcher函数**: 这个类的功能是自动将任务分派给代理。 |
| 53 | + |
| 54 | +AutomaticAgentDispatcher是AgentDispatcher的子类,它负责自动将任务分派给代理。它具有一个dispatch方法,该方法根据任务的能力类型将任务分派给市场中对应的代理。 |
| 55 | + |
| 56 | +**dispatch方法**: |
| 57 | +这个方法接受两个参数,ability_type和target_task。ability_type是任务所需的能力类型,target_task是需要分派的任务。该方法返回一个BaseAgent对象,该对象负责执行任务。 |
| 58 | + |
| 59 | +参数: |
| 60 | +- ability_type (RequiredAbilities): 任务所需的能力类型。 |
| 61 | +- target_task: 需要分派的任务。 |
| 62 | + |
| 63 | +返回值: |
| 64 | +- BaseAgent: 负责执行任务的BaseAgent对象。 |
| 65 | + |
| 66 | +**注意**: |
| 67 | +- AutomaticAgentDispatcher类继承自AgentDispatcher类,因此它可以使用AgentDispatcher类中的方法和属性。 |
| 68 | +- dispatch方法根据任务的能力类型从agent_markets中选择一个代理,并返回该代理的实例。 |
| 69 | + |
| 70 | +**输出示例**: |
| 71 | +假设agent_markets中的ability_type为RequiredAbilities.ABILITY1,那么dispatch方法将返回agent_markets[ability_type][0]()的实例。 |
| 72 | +*** |
| 73 | +# ClassDef XAgentDispatcher |
| 74 | +**XAgentDispatcher函数**:这个类的功能是生成给定任务的提示和代理。 |
| 75 | + |
| 76 | +XAgentDispatcher是AgentDispatcher的子类,用于生成给定任务的提示和代理。它包含了初始化函数、获取示例函数、构建代理函数和调度函数。 |
| 77 | + |
| 78 | +**初始化函数**: |
| 79 | +- `__init__(self, config, enable=True, logger=None)`:初始化XAgentDispatcher对象。 |
| 80 | + - 参数: |
| 81 | + - `config`:调度器的配置。 |
| 82 | + - `enable`(可选):调度器是否激活,默认为True。 |
| 83 | + - `logger`(可选):日志记录器对象。 |
| 84 | + - 功能:初始化XAgentDispatcher对象。 |
| 85 | + |
| 86 | +**获取示例函数**: |
| 87 | +- `get_examples(self, ability_type: RequiredAbilities)`:根据能力类型获取示例。 |
| 88 | + - 参数: |
| 89 | + - `ability_type`:需要示例的能力类型。 |
| 90 | + - 返回值:调度器的示例。 |
| 91 | + - 功能:根据能力类型获取示例。 |
| 92 | + |
| 93 | +**构建代理函数**: |
| 94 | +- `build_agent(self, ability_type: RequiredAbilities, config, prompt_messages: List[Message], *args, **kwargs) -> BaseAgent`:根据能力类型构建代理。 |
| 95 | + - 参数: |
| 96 | + - `ability_type`:代理所需的能力类型。 |
| 97 | + - `config`:代理的配置。 |
| 98 | + - `prompt_messages`:代理的提示消息列表。 |
| 99 | + - `*args`:其他参数。 |
| 100 | + - `**kwargs`:其他关键字参数。 |
| 101 | + - 返回值:构建的代理对象。 |
| 102 | + - 功能:根据能力类型构建代理对象。如果构建失败,则使用默认代理。 |
| 103 | + |
| 104 | +**调度函数**: |
| 105 | +- `dispatch(self, ability_type: RequiredAbilities, target_task: TaskSaveItem, *args, **kwargs) -> BaseAgent`:将任务调度给与任务能力类型对应的代理。 |
| 106 | + - 参数: |
| 107 | + - `ability_type`:任务所需的能力类型。 |
| 108 | + - `target_task`:需要调度的任务。 |
| 109 | + - `*args`:其他参数。 |
| 110 | + - `**kwargs`:其他关键字参数。 |
| 111 | + - 返回值:负责该任务的基础代理对象。 |
| 112 | + - 功能:将任务调度给与任务能力类型对应的代理。此外,还会对任务的提示进行优化,并构建代理对象。 |
| 113 | + |
| 114 | +**注意**:在使用该代码时需要注意以下几点: |
| 115 | +- 需要提供调度器的配置。 |
| 116 | +- 可以选择是否激活调度器。 |
| 117 | +- 可以通过注册代理来扩展调度器的功能。 |
| 118 | + |
| 119 | +**输出示例**: |
| 120 | +```python |
| 121 | +dispatcher = XAgentDispatcher(config, enable=True, logger=logger) |
| 122 | +examples = dispatcher.get_examples(RequiredAbilities.plan_generation) |
| 123 | +agent = dispatcher.build_agent(RequiredAbilities.plan_generation, config, prompt_messages, *args, **kwargs) |
| 124 | +result = dispatcher.dispatch(RequiredAbilities.plan_generation, target_task, *args, **kwargs) |
| 125 | +``` |
| 126 | +## FunctionDef __init__ |
| 127 | +**__init__函数**:这个函数的功能是初始化XAgentDispatcher。 |
| 128 | + |
| 129 | +在这个函数中,有以下参数: |
| 130 | +- config:Dispatcher的配置。 |
| 131 | +- enable(可选参数):指示Dispatcher是否处于活动状态,默认为True。 |
| 132 | + |
| 133 | +在函数内部,首先将传入的logger赋值给self.logger。然后调用父类的__init__函数,将logger作为参数传入。接着将传入的config赋值给self.config。然后创建一个DispatcherAgent对象,并将config作为参数传入,将该对象赋值给self.dispatcher。最后将enable赋值给self.enable。 |
| 134 | + |
| 135 | +**注意**:关于代码使用的注意事项 |
| 136 | +## FunctionDef get_examples |
| 137 | +**get_examples函数**:该函数的功能是根据能力类型获取示例。 |
| 138 | + |
| 139 | +该函数根据传入的能力类型参数,返回对应能力类型的示例。示例的获取方式是通过根据能力类型导入相应的模块,并调用该模块中的get_examples_for_dispatcher函数来获取示例。 |
| 140 | + |
| 141 | +参数: |
| 142 | +- ability_type (RequiredAbilities):需要示例的能力类型。 |
| 143 | + |
| 144 | +返回值: |
| 145 | +- 返回dispatcher的示例。 |
| 146 | + |
| 147 | +调用情况: |
| 148 | +该函数在以下文件中被调用: |
| 149 | +文件路径:XAgent/agent/dispatcher.py |
| 150 | +调用代码如下: |
| 151 | +```python |
| 152 | +example_input, example_system_prompt, example_user_prompt = self.get_examples( |
| 153 | + ability_type |
| 154 | +) |
| 155 | +``` |
| 156 | + |
| 157 | +代码分析和描述: |
| 158 | +该函数根据传入的ability_type参数,通过判断ability_type的值,来决定导入哪个模块并调用对应模块中的get_examples_for_dispatcher函数。根据不同的ability_type值,分别导入plan_generate_agent、plan_refine_agent、tool_agent和reflect_agent模块,并调用这些模块中的get_examples_for_dispatcher函数。最后,返回get_examples_for_dispatcher函数的返回值。 |
| 159 | + |
| 160 | +注意事项: |
| 161 | +- 该函数依赖于其他模块中的get_examples_for_dispatcher函数,需要确保这些函数的正确性和可用性。 |
| 162 | + |
| 163 | +输出示例: |
| 164 | +假设ability_type为RequiredAbilities.plan_generation,那么根据该ability_type的值,将导入plan_generate_agent模块,并调用该模块中的get_examples_for_dispatcher函数。假设get_examples_for_dispatcher函数返回的示例为example,那么函数的返回值将为example。 |
| 165 | +## FunctionDef build_agent |
| 166 | +**build_agent函数**:该函数的功能是根据所需的能力类型构建代理。如果失败,则回退到使用默认代理。 |
| 167 | + |
| 168 | +该函数接受以下参数: |
| 169 | +- ability_type(RequiredAbilities):代理所需的能力类型。 |
| 170 | +- config:代理的配置。 |
| 171 | +- prompt_messages(List[Message]):代理的提示消息列表。 |
| 172 | + |
| 173 | +该函数返回一个BaseAgent对象,表示构建的代理。 |
| 174 | + |
| 175 | +该函数首先尝试使用ability_type从agent_markets字典中获取相应的代理类,并使用config、prompt_messages以及其他参数构建代理对象。如果构建失败,则捕获异常,并使用默认代理类再次尝试构建代理对象。 |
| 176 | + |
| 177 | +在代码中,该函数被以下文件调用: |
| 178 | +文件路径:XAgent/agent/dispatcher.py |
| 179 | +调用代码如下: |
| 180 | +``` |
| 181 | +agent = self.build_agent(ability_type, self.config, prompt_messages, *args, **kwargs) |
| 182 | +``` |
| 183 | + |
| 184 | +**注意**:在构建代理对象时,如果能力类型对应的代理类无法创建,则会使用默认代理类进行构建。 |
| 185 | + |
| 186 | +**输出示例**:假设能力类型为RequiredAbilities.A,config为{"param1": "value1"},prompt_messages为[Message(role="system", content="System prompt"), Message(role="user", content="User prompt")],则函数可能返回一个BaseAgent对象。 |
| 187 | +## FunctionDef dispatch |
| 188 | +**dispatch函数**:此函数的功能是将任务分派给与任务能力类型相对应的市场中的代理,并对任务进行进一步的提示细化和代理构建。 |
| 189 | + |
| 190 | +此函数接受以下参数: |
| 191 | +- ability_type (RequiredAbilities):任务所需的能力类型。 |
| 192 | +- target_task (TaskSaveItem):需要分派的任务。 |
| 193 | +- *args:可变长度的位置参数。 |
| 194 | +- **kwargs:可变长度的关键字参数。 |
| 195 | + |
| 196 | +该函数的返回值为BaseAgent对象,表示负责该任务的基础代理。 |
| 197 | + |
| 198 | +该函数首先调用get_examples方法获取示例输入、示例系统提示和示例用户提示。然后,如果启用了分派器(enable为True),将使用dispatcher对象的parse方法对目标任务、示例输入、示例系统提示和示例用户提示进行解析,获取解析后的提示信息。如果解析后的提示信息的内容为空,则表示分派器无法遵循输出格式,将使用默认提示。否则,将提示信息作为参数,调用build_agent方法构建代理对象。最后,返回构建的代理对象。 |
| 199 | + |
| 200 | +**注意**:在调用dispatch函数之前,需要确保已经调用了get_examples方法获取示例输入、示例系统提示和示例用户提示。 |
| 201 | + |
| 202 | +**输出示例**: |
| 203 | +``` |
| 204 | +agent = agent_dispatcher.dispatch(RequiredAbilities.plan_generation, target_task="Generate a plan to accomplish the task: {self.query.task}") |
| 205 | +print(agent) |
| 206 | +``` |
| 207 | +输出: |
| 208 | +``` |
| 209 | +<agent.dispatcher_agent.agent.Agent object at 0x7f8a0b1f5a90> |
| 210 | +``` |
| 211 | +*** |
0 commit comments