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

AutoGen实现多代理—AI Agentic Design Patterns with 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实现多代理之间按照顺序对话的过程,并最终根据上下文产生输出结果。体现了多代理之间协作的设计模式。