AutoGen群聊模式下获取单个Agent的消息

2024-06-09 21:58:33 浏览数 (2)

背景介绍

AutoGen是微软发布的一个multi-agent框架,它支持使用多个代理来开发LLM应用程序,这些代理可以相互通信以解决任务。

AutoGen不仅支持与单个Agent进行对话,也支持两个Agent甚至是两个以上的Agent进行对话。

两个Agent对话需要有两类输入:一条初始的message和一些指定聊天的各种参数。发送方Agent从输入生成初始消息,并将其发送给接收方Agent以开始对话。发送方Agent是调用其initiate_chat方法的代理,并指定另一个Agent为接收方Agent。对话终止后,对话的历史记录将由对话总结器处理。对话生成器汇总对话历史记录并计算聊天的令牌使用情况。

今天我们以两个Agent对话为例,演示如何获取每个Agent的对话消息。

关于AutoGen框架的更多信息,可以参考AutoGen官方文档:https://microsoft.github.io/autogen/。

问题发现

在AutoGen中,我们可以通过官网给出的示例代码来让两个Agent相互对话:

代码语言:txt复制
import os

from autogen import ConversableAgent

student_agent = ConversableAgent(
    name="Student_Agent",
    system_message="You are a student willing to learn.",
    llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
)
teacher_agent = ConversableAgent(
    name="Teacher_Agent",
    system_message="You are a math teacher.",
    llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
)

chat_result = student_agent.initiate_chat(
    teacher_agent,
    message="What is triangle inequality?",
    summary_method="reflection_with_llm",
    max_turns=2,
)

在对话的过程中,控制台会打印出两个Agent的对话消息:

代码语言:txt复制
Student_Agent (to Teacher_Agent):

What is triangle inequality?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
Teacher_Agent (to Student_Agent):

Triangle inequality theorem is a fundamental principle in geometry that states that the sum of the lengths of any two sides of a triangle must always be greater than the length of the third side. In a triangle with sides of lengths a, b, and c, the theorem can be written as:

a   b > c
a   c > b
b   c > a

Each of these represents the condition for one specific side (a, b, or c). All must be true for a triangle to exist.

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
Student_Agent (to Teacher_Agent):

Thank you for the explanation. This theorem helps in understanding the basic properties of a triangle. It can also be useful when solving geometric problems or proving other mathematical theorems. Can you give me an example of how we can use the triangle inequality theorem?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
Teacher_Agent (to Student_Agent):

Absolutely! Here's an example:

Suppose you're given three line segments with lengths 10, 7, and 3 units. The question is: "Can these three line segments form a triangle?"

To answer this, you would use the triangle inequality theorem. Adding any two side lengths together should be greater than the third:

- For sides 10 and 7: 10   7 = 17, which is larger than 3.
- For sides 10 and 3: 10   3 = 13, which is larger than 7.
- For sides 7 and 3: 7   3 = 10, which is equal to the length of the third side (10), but not greater.

So, these three lines cannot form a triangle, because not all pairs of sides satisfy the triangle inequality theorem.

--------------------------------------------------------------------------------

对话结束后,我们还可以从chat_result中获取到总结、对话的历史记录、token花费等信息:

代码语言:txt复制
print(chat_result.summary)

The triangle inequality theorem states that in a triangle, the sum of the lengths of any two sides must always be greater than the length of the third side. This principle is significant in geometry and is used in solving problems or proving theorems. For instance, if given three line segments, you can determine if they can form a triangle using this theorem.

然后,问题来了,虽然我们能从控制台上面看到对话中,每一个Agent发出的消息,但是如果我们想实时获取每个Agent的对话消息(让前端去显示),应该怎么办?

解决办法

然后,经过多方研究,我们发现了AutoGen有一个Hook功能,一共有三种Hook:

process_last_received_message:处理接收到的最后一条消息;

process_all_messages_before_reply:在回复前(LLM生成响应前)处理所有消息;

process_message_before_send:在发送给下一个Agent前处理当前这条消息;

每个对话Agent都可以去注册对应的Hook(本次我们使用process_message_before_send类型,其他两个下回分解)

代码语言:txt复制
student_agent.register_hook("process_message_before_send",custom_before_message_methon)
teacher_agent.register_hook("process_message_before_send",custom_before_message_methon)

custom_before_message_methon是我们自定义的一个方法:

代码语言:txt复制
def custom_before_message_methon(sender, message, recipient, silent):
    print(f"sender.name:{sender.name}")
    print(f"message:{message}")
    print(f"recipient.name:{recipient.name}")
    print(f"silent:{silent}")
    return message

process_message_before_send Hook会返回4个参数:发送Agent、发送人发出的消息、接收Agent、silent。

我们就可以在方法custom_before_message_methon中获取到每个Agent发送的消息以及Agent的name。

此外,如果我们不想在控制台上打印出Agent的消息,可以在init对话时设置silent参数为True:

代码语言:txt复制
chat_result = student_agent.initiate_chat(
    teacher_agent,
    message="What is triangle inequality?",
    summary_method="reflection_with_llm",
    max_turns=2,
    silent=True
)

邀请人:"沈唁"

我正在参与2024腾讯技术创作特训营最新征文,快来和我瓜分大奖!

0 人点赞