punchy
Stay foolish Stay hungry

Multi-agent学习

2025-12-31 Multi-agent

多智能体框架学习

本文提出一种多智能体,多人格特征框架,旨在通过智能体多样的个性以及批判能力,从而提高模型的推理能力。意思就是让模型扮演不同的人格特征,还要加一个苏格拉底式的critic agent,从而提高整个系统的推理能力。

Intro

介绍了当下的agent系统,要么使用单一agent执行任务,要么仅仅使用两个agent,就算使用多个agent,这多个agent之间多采用相似的推理模式和分析方法,缺少多样性,从而使得整个系统在执行任务的时候缺乏发散性的思维和多角度的探索,而这些能力在解决复杂问题的时候是非常关键的。另外,当下的agent系统缺乏反思和批判的机制,这导致模型缺乏纠错机制,比如如果一开始就理解错了问题,后面就算进行再多的对话和生成,结果都是错误的。

所以,本文提出的multi agent personality shaping 框架就能解决这两个问题。首先整个系统基于什么五人格理论,赋予五个agent不同的人格特性,shape了它们的推理表现,从而提高模型之间合作。另外引入critc模型,负责识别每一个的错误并给出反馈。

框架介绍

分别是interpreter, aligner, scholar, solver, critic

  • 使用mathvita, olympiad, EMMA三种数据集来评测模型解决问题的能力;
  • GPT-4O作为base model

代码

multiagent.py 构建了interpreter, aligner, scholar, solver, cirtic等五类模型,均继承自autogen中的basechatagent类

下面以interpreter为例,介绍该类及其方法。首先介绍basechatagent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Base class for a chat agent.

This abstract class provides a base implementation for a :class:`ChatAgent`.
To create a new chat agent, subclass this class and implement the
:meth:`on_messages`, :meth:`on_reset`, and :attr:`produced_message_types`.
If streaming is required, also implement the :meth:`on_messages_stream` method.

An agent is considered stateful and maintains its state between calls to
the :meth:`on_messages` or :meth:`on_messages_stream` methods.
The agent should store its state in the
agent instance. The agent should also implement the :meth:`on_reset` method
to reset the agent to its initialization state.

.. note::

The caller should only pass the new messages to the agent on each call
to the :meth:`on_messages` or :meth:`on_messages_stream` method.
Do not pass the entire conversation history to the agent on each call.
This design principle must be followed when creating a new agent.
"""

由以上basechatagent注释可知,创建子类至少需要实现两个方法和一个属性,on_messages方法用来处理传入的消息,on_reset方法用来重置agent的状态,produced_message_types用来定义agent生成的消息类型。

  • agent是有状态的,状态在调用on_messages之后会被保留
  • 每次调用on_messages都只传递新的消息而非整个对话历史

下面来看一下interpreter如何实现on_messages方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@property  ## 表示interpreter返回的是text类型的信息
def produced_message_types(self) -> Sequence[type[ChatMessage]]:
return [TextMessage]

async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> Response:
print("InterpreterAgent is called")

if len(messages[0]) == 2: ## 即有初始prompt和PIL Image
if "Improve the description based on the following feedback:" in messages[0][0]:
feedback = messages[0][0]
img = messages[0][1]
prompt = [prompt_interpreter + feedback, img] ## 这里的prompt_interpreter就是给interpreter设置的personality prompt
else:
prompt = [messages[0][0], messages[0][1]]
elif len(messages[0]) > 2:
if "Improve the description based on the following feedback:" in messages[0][0]:
feedback = messages[0][0]
prompt = [prompt_interpreter_m + feedback]
else:
prompt = [prompt_interpreter_m]

for i in range(1, len(messages[0])):
prompt.append(messages[0][i])
else:
response = TextMessage(content = "No image input, Caption is None.", source=self.name)
return Response(chat_message=response)

try: ## 这里是选定哪个厂家的模型作为base model
if SWITCH == 1:
response_text = await self.call_oaLMM(prompt)
elif SWITCH == 0:
response_text = await self.call_LMM(prompt)
elif SWITCH == 2:
response_text = await self.call_QWEN(prompt)

except Exception as e:
print(e)
# print(response_text)
response_message = TextMessage(content=response_text, source=self.name)
return Response(chat_message=response_message)


## 这里他没有定义reset方法
async def on_reset(self, cancellation_token: CancellationToken) -> None:
pass

其余agent的定义和上述interpreter类似,下面介绍一下整个框架的运行逻辑,即五个agent如何合作解决问题。

选择了EMMA作为评测数据集之后,运行run程序就会进入on_messages_e方法。整个逻辑如下:

初始化agent

1
2
3
4
5
6
input_agent = UserProxyAgent("input_agent")
interpreter_agent = InterpreterAgent("interpreter_agent")
aligner_agent = AlignerAgent("aligner_agent")
scholar_agent = ScholarAgent("scholar_agent")
solver_agent = SolverAgent("solver_agent")
critic_agent = CriticAgent("critic_agent")

使用interpreter分析问题, 将interpreter的返回结果输入到execute_pipeline中,这个pipeline中主要逻辑是:aliner->scholar->solver

其中aligner负责分析内容的逻辑一致性,将多个来源的信息整个成一个连贯的描述

scholar负责给问题解构,知识定义,相关性检验,给出问题所属的学科范围,使用的求解范式,相关的公式方法等,类似于一个search的功能

solver顾名思义负责推理,求解。

以上三个agent是主流水线,完成之后,上述四个agent的输出,则会整合,提交给critic,让其对每一个agent的返回结果进行一个评估和审查。cirtic的输出中包含是否需要反馈,最差的步骤,针对最差的步骤的反馈。如果需要反馈,那么针对最差步骤调用的智能体进行改进。

最后将solver的输出结果作为答案存储

总结,五个agent基本上是inter->aligner->scholar->solver的流程,而crtic负责审查每一个agent的输出,如果不够好,那么该agent就需要重新输出,则inter-align-scholar-solve的流程重新进行一遍。

Author: 武丢丢

Link: http://example.com/2025/12/31/Multi-agent%E5%AD%A6%E4%B9%A0/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

NextPost >
tmux-learn
CATALOG
  1. 1. 多智能体框架学习
    1. 1.1. Intro
    2. 1.2. 框架介绍
    3. 1.3. 代码