AutoGen实现多代理—AI Agentic Design Patterns with AutoGen(二)

1. AutoGen顺序对话在客户入职案例上的应用

在这里插入图片描述
如图,客户入职前会经历三个阶段,一个代理收集客户的信息,一个代理收集客户的感兴趣话题,一个代理根据前两个代理的基础信息与客户代理对话,产生聊天信息。

本节实验的地址:传送门

2. 代码实践

2.1 准备环境

llm_config={"model": "gpt-3.5-turbo"}

from autogen import ConversableAgent

2.2 构建Agent

# 创建获取用户信息的Agent,包括名字和地址
onboarding_personal_information_agent = ConversableAgent(
    name="Onboarding Personal Information Agent",
    system_message='''You are a helpful customer onboarding agent,
    you are here to help new customers get started with our product.
    Your job is to gather customer's name and location.
    Do not ask for other information. Return 'TERMINATE' 
    when you have gathered all the information.''',
    llm_config=llm_config,
    code_execution_config=False,
    human_input_mode="NEVER",
)
# 创建获取用户话题的Agent
onboarding_topic_preference_agent = ConversableAgent(
    name="Onboarding Topic preference Agent",
    system_message='''You are a helpful customer onboarding agent,
    you are here to help new customers get started with our product.
    Your job is to gather customer's preferences on news topics.
    Do not ask for other information.
    Return 'TERMINATE' when you have gathered all the information.''',
    llm_config=llm_config,
    code_execution_config=False,
    human_input_mode="NEVER",
)
# 创建与客户交互的Agent,基于客户提供的基础信息
customer_engagement_agent = ConversableAgent(
    name="Customer Engagement Agent",
    system_message='''You are a helpful customer service agent
    here to provide fun for the customer based on the user's
    personal information and topic preferences.
    This could include fun facts, jokes, or interesting stories.
    Make sure to make it engaging and fun!
    Return 'TERMINATE' when you are done.''',
    llm_config=llm_config,
    code_execution_config=False,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "terminate" in msg.get("content").lower(),
)
# 创建客户代理Agent
customer_proxy_agent = ConversableAgent(
    name="customer_proxy_agent",
    llm_config=False,
    code_execution_config=False,
    human_input_mode="ALWAYS",
    is_termination_msg=lambda msg: "terminate" in msg.get("content").lower(),
)

2.3 创建任务

现在,你可以制定一系列任务来促进新用户的入职流程。

chats = [
    {
        "sender": onboarding_personal_information_agent,
        "recipient": customer_proxy_agent,
        "message": 
            "Hello, I'm here to help you get started with our product."
            "Could you tell me your name and location?",
        "summary_method": "reflection_with_llm",
        "summary_args": {
            "summary_prompt" : "Return the customer information "
                             "into as JSON object only: "
                             "{'name': '', 'location': ''}",
        },
        "max_turns": 2,
        "clear_history" : True
    },
    {
        "sender": onboarding_topic_preference_agent,
        "recipient": customer_proxy_agent,
        "message": 
                "Great! Could you tell me what topics you are "
                "interested in reading about?",
        "summary_method": "reflection_with_llm",
        "max_turns": 1,
        "clear_history" : False
    },
    {
        "sender": customer_proxy_agent,
        "recipient": customer_engagement_agent,
        "message": "Let's find something fun to read.",
        "max_turns": 1,
        "summary_method": "reflection_with_llm",
    },
]

2.4 开始对话

from autogen import initiate_chats

chat_results = initiate_chats(chats)

执行阶段包含面板交互,提示输入用户的名字、地址和感兴趣的话题,输出如下:

********************************************************************************
Starting a new chat....

********************************************************************************
Onboarding Personal Information Agent (to customer_proxy_agent):

Hello, I'm here to help you get started with our product.Could you tell me your name and location?

--------------------------------------------------------------------------------
Provide feedback to Onboarding Personal Information Agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: Alice
customer_proxy_agent (to Onboarding Personal Information Agent):

Alice

--------------------------------------------------------------------------------
Onboarding Personal Information Agent (to customer_proxy_agent):

Thank you, Alice. Could you also let me know your location, please?

--------------------------------------------------------------------------------
Provide feedback to Onboarding Personal Information Agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: new york
customer_proxy_agent (to Onboarding Personal Information Agent):

new york

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

********************************************************************************
Starting a new chat....

********************************************************************************
Onboarding Topic preference Agent (to customer_proxy_agent):

Great! Could you tell me what topics you are interested in reading about?
Context: 
{
  "name": "Alice",
  "location": "New York"
}

--------------------------------------------------------------------------------
Provide feedback to Onboarding Topic preference Agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: dog
customer_proxy_agent (to Onboarding Topic preference Agent):

dog

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

********************************************************************************
Starting a new chat....

********************************************************************************
customer_proxy_agent (to Customer Engagement Agent):

Let's find something fun to read.
Context: 
{
  "name": "Alice",
  "location": "New York"
}
Alice is interested in reading about dogs.

--------------------------------------------------------------------------------
Customer Engagement Agent (to customer_proxy_agent):

Hey Alice from New York! I hope you're ready for a paw-some time because we are diving into the wonderful world of dogs! Did you know that the world's oldest known breed of domesticated dog is the Saluki, which is originally from Egypt? These elegant and swift hounds have been mankind's loyal companions for over 4,000 years!

Dogs truly are incredible creatures with so much to offer. Whether they are bounding through fields, snuggling on the couch, or just giving us those heart-melting puppy dog eyes, they never fail to bring joy and love into our lives. So, grab a cozy spot and get ready to explore the fascinating world of our four-legged friends!

TERMINATE

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

2.5 打印摘要

for chat_result in chat_results:
    print(chat_result.summary)
    print("\n")

输出如下:

{
  "name": "Alice",
  "location": "New York"
}


Alice is interested in reading about dogs.


Alice from New York is interested in reading about dogs. She is about to dive into the wonderful world of dogs, learning about the world's oldest known breed, the Saluki, and the joy and love that dogs bring into our lives.

2.6 打印花费

for chat_result in chat_results:
    print(chat_result.cost)
    print("\n")

输出如下:

{'usage_including_cached_inference': {'total_cost': 0.0001405, 'gpt-3.5-turbo-0125': {'cost': 0.0001405, 'prompt_tokens': 182, 'completion_tokens': 33, 'total_tokens': 215}}, 'usage_excluding_cached_inference': {'total_cost': 0.0001405, 'gpt-3.5-turbo-0125': {'cost': 0.0001405, 'prompt_tokens': 182, 'completion_tokens': 33, 'total_tokens': 215}}}


{'usage_including_cached_inference': {'total_cost': 4.55e-05, 'gpt-3.5-turbo-0125': {'cost': 4.55e-05, 'prompt_tokens': 67, 'completion_tokens': 8, 'total_tokens': 75}}, 'usage_excluding_cached_inference': {'total_cost': 4.55e-05, 'gpt-3.5-turbo-0125': {'cost': 4.55e-05, 'prompt_tokens': 67, 'completion_tokens': 8, 'total_tokens': 75}}}


{'usage_including_cached_inference': {'total_cost': 0.000453, 'gpt-3.5-turbo-0125': {'cost': 0.000453, 'prompt_tokens': 324, 'completion_tokens': 194, 'total_tokens': 518}}, 'usage_excluding_cached_inference': {'total_cost': 0.000453, 'gpt-3.5-turbo-0125': {'cost': 0.000453, 'prompt_tokens': 324, 'completion_tokens': 194, 'total_tokens': 518}}}

3. 总结

本篇讲述了AutoGen实现多代理之间按照顺序对话的过程,并最终根据上下文产生输出结果。体现了多代理之间协作的设计模式。

Autogen multiagent是一种自动生成多智能体系统的方法。 在传统的多智能体系统中,需要手动设计和编写每个智能体的行为规则和决策策略。这样做的问题是,当系统需要扩展或修改时,需要手动调整每个智能体的规则和策略,非常耗时和困难。而autogen multiagent方法则通过自动生成智能体系统的规则和策略,极大地简化了系统的设计和维护过程。 具体实现autogen multiagent的方法有多种。一种常用的方法是基于机器学习和优化算法。系统首先采用机器学习算法对智能体的行为规则进行训练,让系统能够从大量的实例中学习合适的决策策略。然后,使用优化算法对系统中的智能体进行优化,并自动调整它们的规则和策略,以实现更高的性能和效率。 另一种实现autogen multiagent的方法是基于进化算法。系统首先通过随机生成一组智能体的规则和策略作为初始种群,然后使用进化算法对这些智能体进行迭代优化。在每一代中,系统通过评估智能体的性能选择出适应度高的个体,并通过交叉和变异等遗传操作生成新的智能体。通过不断迭代优化,系统可以自动生成合适的智能体规则和策略。 总的来说,autogen multiagent实现是一种通过机器学习、优化算法或进化算法等方法自动生成多智能体系统的规则和策略的方法。它可以大大简化多智能体系统的设计和维护过程,并且能够在系统性能和效率方面取得更好的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

l8947943

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值