使用AutoGen框架进行多智能体协作:AI Agentic Design Patterns with AutoGen

news2025/1/18 20:24:11

AI Agentic Design Patterns with AutoGen

本文是学习https://www.deeplearning.ai/short-courses/ai-agentic-design-patterns-with-autogen/ 这门课的学习笔记。

在这里插入图片描述

What you’ll learn in this course

In AI Agentic Design Patterns with AutoGen you’ll learn how to build and customize multi-agent systems, enabling agents to take on different roles and collaborate to accomplish complex tasks using AutoGen, a framework that enables development of LLM applications using multi-agents.

In this course you’ll create:

  • A two-agent chat that shows a conversation between two standup comedians, using “ConversableAgent,” a built-in agent class of AutoGen for constructing multi-agent conversations.
  • A sequence of chats between agents to provide a fun customer onboarding experience for a product, using the multi-agent collaboration design pattern.
  • A high-quality blog post by using the agent reflection framework. You’ll use the “nested chat” structure to develop a system where reviewer agents, nested within a critic agent, reflect on the blog post written by another agent.
  • A conversational chess game where two agent players can call a tool and make legal moves on the chessboard, by implementing the tool use design pattern.
  • A coding agent capable of generating the necessary code to plot stock gains for financial analysis. This agent can also integrate user-defined functions into the code.
  • Agents with coding capabilities to complete a financial analysis task. You’ll create two systems where agents collaborate and seek human feedback. The first system will generate code from scratch using an LLM, and the second will use user-provided code.

You can use the AutoGen framework with any model via API call or locally within your own environment.

By the end of the course, you’ll have hands-on experience with AutoGen’s core components and a solid understanding of agentic design patterns. You’ll be ready to effectively implement multi-agent systems in your workflows.

文章目录

  • AI Agentic Design Patterns with AutoGen
    • What you’ll learn in this course
  • Lesson 1: Multi-Agent Conversation and Stand-up Comedy
    • Setup
    • Define an AutoGen agent
    • Conversation
    • Print some results
    • Get a better summary of the conversation
    • Chat Termination
  • Lesson 2: Sequential Chats and Customer Onboarding
    • Setup
    • Creating the needed agents
    • Creating tasks
    • Start the onboarding process
    • Print out the summary
    • Print out the cost
  • Lesson 3: Reflection and Blogpost Writing
    • The task!
    • Create a writer agent
    • Adding reflection
    • Nested chat
    • Orchestrate the nested chats to solve the task
    • Get the summary
  • Lesson 4: Tool Use and Conversational Chess
    • Setup
    • Initialize the chess board
    • Define the needed tools
      • 1. Tool for getting legal moves
      • 2. Tool for making a move on the board
    • Create agents
    • Register the tools
    • Register the nested chats
    • Start the Game
    • Adding a fun chitchat to the game!
  • Lesson 5: Coding and Financial Analysis
    • Define a code executor
    • Create agents
      • 1. Agent with code executor configuration
      • 2. Agent with code writing capability
    • The task!
    • Let's see the plot!
    • User-Defined Functions
      • Create a new executor with the user-defined functions
      • Let's update the agents with the new system message
      • Start the same task again!
  • Lesson 6: Planning and Stock Report Generation
    • The task!
    • Build a group chat
    • Define the group chat
    • Start the group chat!
    • Add a speaker selection policy
  • 后记

Lesson 1: Multi-Agent Conversation and Stand-up Comedy

在这里插入图片描述

Setup

from utils import get_openai_api_key
OPENAI_API_KEY = get_openai_api_key()
llm_config = {"model": "gpt-3.5-turbo"}

requirements.txt

# requirements file
# note which revision of python, for example 3.9.6
# in this file, insert all the pip install needs, include revision

# python==3.10.13
pyautogen==0.2.25
chess==1.10.0
matplotlib
numpy
pandas
yfinance

utils.py

# Add your utilities or helper functions to this file.

import os
from dotenv import load_dotenv, find_dotenv

# these expect to find a .env file at the directory above the lesson.                                                                                                                     # the format for that file is (without the comment)                                                                                                                                       #API_KEYNAME=AStringThatIsTheLongAPIKeyFromSomeService                                                                                                                                     
def load_env():
    _ = load_dotenv(find_dotenv())

def get_openai_api_key():
    load_env()
    openai_api_key = os.getenv("OPENAI_API_KEY")
    return openai_api_key

Define an AutoGen agent

from autogen import ConversableAgent

agent = ConversableAgent(
    name="chatbot",
    llm_config=llm_config,
    human_input_mode="NEVER",
)
reply = agent.generate_reply(
    messages=[{"content": "Tell me a joke.", "role": "user"}]
)
print(reply)

Output

Sure! Here you go:

Why don't scientists trust atoms?

Because they make up everything!
reply = agent.generate_reply(
    messages=[{"content": "Repeat the joke.", "role": "user"}]
)
print(reply)

Output

Sure, which joke would you like me to repeat for you?

Conversation

Setting up a conversation between two agents, Cathy and Joe, where the memory of their interactions is retained.

cathy = ConversableAgent(
    name="cathy",
    system_message=
    "Your name is Cathy and you are a stand-up comedian.",
    llm_config=llm_config,
    human_input_mode="NEVER",
)

joe = ConversableAgent(
    name="joe",
    system_message=
    "Your name is Joe and you are a stand-up comedian. "
    "Start the next joke from the punchline of the previous joke.",
    llm_config=llm_config,
    human_input_mode="NEVER",
)
chat_result = joe.initiate_chat(
    recipient=cathy, 
    message="I'm Joe. Cathy, let's keep the jokes rolling.",
    max_turns=2,
)

Output

scarecrow: 美 [ˈskerkroʊ] 稻草人

joe (to cathy):

I'm Joe. Cathy, let's keep the jokes rolling.

--------------------------------------------------------------------------------
cathy (to joe):

Sure thing, Joe! Why did the scarecrow win an award? Because he was outstanding in his field!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, that scarecrow sure knows how to stand out! Just like me at a buffet!

--------------------------------------------------------------------------------
cathy (to joe):

Haha, I bet you're a real "stand-out" at the buffet line, Joe! I always try to stand out too, but usually I just end up blending in with the crowd...just like my dad's dad jokes!

Print some results

You can print out:

  1. Chat history
  2. Cost
  3. Summary of the conversation
import pprint

pprint.pprint(chat_result.chat_history)

Output

[{'content': "I'm Joe. Cathy, let's keep the jokes rolling.",
  'role': 'assistant'},
 {'content': 'Sure thing, Joe! Why did the scarecrow win an award? Because he '
             'was outstanding in his field!',
  'role': 'user'},
 {'content': 'Haha, that scarecrow sure knows how to stand out! Just like me '
             'at a buffet!',
  'role': 'assistant'},
 {'content': 'Haha, I bet you\'re a real "stand-out" at the buffet line, Joe! '
             'I always try to stand out too, but usually I just end up '
             "blending in with the crowd...just like my dad's dad jokes!",
  'role': 'user'}]
pprint.pprint(chat_result.cost)

Output

{'usage_excluding_cached_inference': {'gpt-3.5-turbo-0125': {'completion_tokens': 90,
                                                             'cost': 0.00023350000000000004,
                                                             'prompt_tokens': 197,
                                                             'total_tokens': 287},
                                      'total_cost': 0.00023350000000000004},
 'usage_including_cached_inference': {'gpt-3.5-turbo-0125': {'completion_tokens': 90,
                                                             'cost': 0.00023350000000000004,
                                                             'prompt_tokens': 197,
                                                             'total_tokens': 287},
                                      'total_cost': 0.00023350000000000004}}
pprint.pprint(chat_result.summary)

Output

('Haha, I bet you\'re a real "stand-out" at the buffet line, Joe! I always try '
 'to stand out too, but usually I just end up blending in with the '
 "crowd...just like my dad's dad jokes!")

Get a better summary of the conversation

chat_result = joe.initiate_chat(
    cathy, 
    message="I'm Joe. Cathy, let's keep the jokes rolling.", 
    max_turns=2, 
    summary_method="reflection_with_llm",
    summary_prompt="Summarize the conversation",
)
pprint.pprint(chat_result.summary)

Output

('Joe and Cathy exchanged jokes and banter, poking fun at themselves and each '
 'other in a light-hearted manner. They shared humorous quips about standing '
 'out and blending in, showcasing their playful personalities.')

Chat Termination

Chat can be terminated using a termination conditions.

cathy = ConversableAgent(
    name="cathy",
    system_message=
    "Your name is Cathy and you are a stand-up comedian. "
    "When you're ready to end the conversation, say 'I gotta go'.",
    llm_config=llm_config,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "I gotta go" in msg["content"],
)

joe = ConversableAgent(
    name="joe",
    system_message=
    "Your name is Joe and you are a stand-up comedian. "
    "When you're ready to end the conversation, say 'I gotta go'.",
    llm_config=llm_config,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "I gotta go" in msg["content"] or "Goodbye" in msg["content"],
)
chat_result = joe.initiate_chat(
    recipient=cathy,
    message="I'm Joe. Cathy, let's keep the jokes rolling."
)

Output


joe (to cathy):

I'm Joe. Cathy, let's keep the jokes rolling.

--------------------------------------------------------------------------------
cathy (to joe):

Hey Joe, glad you're enjoying the jokes! Alright, here's one for you: Why did the math book look sad? Because it had too many problems. 

Alright, your turn! Hit me with your best joke, Joe!
......
--
cathy (to joe):

Haha, that's a timely joke, Joe! Alright, here's one for you: Why did the coffee file a police report? It got mugged. 

You're crushing it, Joe! Your turn!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, I love that one, Cathy! Alright, here's a joke: I told my wife she should embrace her mistakes. She gave me a hug. 

Your turn, Cathy!

--------------------------------------------------------------------------------
cathy (to joe):

Haha, that's a good one, Joe! Let's end with this one: Why did the old man fall in the well? Because he couldn't see that well. 

Thanks for the laughs, Joe! I gotta go.

--------------------------------------------------------------------------------
cathy.send(message="What's last joke we talked about?", recipient=joe)

Output

cathy (to joe):

What's last joke we talked about?

--------------------------------------------------------------------------------
joe (to cathy):

The last joke we talked about was: Why did the old man fall in the well? Because he couldn't see that well. Thanks for the laughs, and have a great day!

--------------------------------------------------------------------------------
cathy (to joe):

Thank you! You too!

--------------------------------------------------------------------------------
joe (to cathy):

You're welcome! Take care, Cathy!

--------------------------------------------------------------------------------
cathy (to joe):

You too, Joe! Bye!

--------------------------------------------------------------------------------
joe (to cathy):

Goodbye!

--------------------------------------------------------------------------------
cathy (to joe):

Lesson 2: Sequential Chats and Customer Onboarding

在这里插入图片描述

Setup

llm_config={"model": "gpt-3.5-turbo"}
from autogen import ConversableAgent

Creating the needed agents

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",
)
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",
)
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(),
)
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(),
)

Creating tasks

Now, you can craft a series of tasks to facilitate the onboarding process.

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",
    },
]

Start the onboarding process

from autogen import initiate_chats

chat_results = initiate_chats(chats)

Output

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

Shizheng Li, shanghai, China

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

Thank you for providing that information. Is there anything else you would like to add?

--------------------------------------------------------------------------------
Provide feedback to Onboarding Personal Information Agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: I enjoy reading books and watch TED speech.
customer_proxy_agent (to Onboarding Personal Information Agent):

I enjoy reading books and watch TED speech.

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

********************************************************************************
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': 'Shizheng Li', 'location': 'Shanghai'}
{
  "name": "Shizheng Li",
  "location": "Shanghai, China"
}

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

Biography and Business, finance books

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

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

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

Let's find something fun to read.
Context: 
{'name': 'Shizheng Li', 'location': 'Shanghai'}
The user is interested in knowing what topics Shizheng Li is interested in reading about.
{
  "name": "Shizheng Li",
  "location": "Shanghai, China"
}
Biography and Business, finance books

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

Hi Shizheng Li from Shanghai! Did you know that Shanghai is known as the financial capital of China? It's a bustling city with a rich history and vibrant culture. Since you enjoy reading about business and finance, have you ever read "The Intelligent Investor" by Benjamin Graham? It's a classic book on value investing that's both informative and engaging. If you haven't already, you might enjoy diving into the world of finance through the eyes of one of the greatest investors of all time! Happy reading! 
TERMINATE

Print out the summary

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

Output

{
  "name": "Shizheng Li",
  "location": "Shanghai, China"
}


Biography and Business, finance books


Shizheng Li from Shanghai enjoys reading about biography and business, finance books. Consider exploring "The Intelligent Investor" by Benjamin Graham for an engaging read on value investing.

Print out the cost

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

Output

{'usage_including_cached_inference': {'total_cost': 0.00015900000000000002, 'gpt-3.5-turbo-0125': {'cost': 0.00015900000000000002, 'prompt_tokens': 213, 'completion_tokens': 35, 'total_tokens': 248}}, 'usage_excluding_cached_inference': {'total_cost': 0.00015900000000000002, 'gpt-3.5-turbo-0125': {'cost': 0.00015900000000000002, 'prompt_tokens': 213, 'completion_tokens': 35, 'total_tokens': 248}}}


{'usage_including_cached_inference': {'total_cost': 5.9499999999999996e-05, 'gpt-3.5-turbo-0125': {'cost': 5.9499999999999996e-05, 'prompt_tokens': 62, 'completion_tokens': 19, 'total_tokens': 81}}, 'usage_excluding_cached_inference': {'total_cost': 5.9499999999999996e-05, 'gpt-3.5-turbo-0125': {'cost': 5.9499999999999996e-05, 'prompt_tokens': 62, 'completion_tokens': 19, 'total_tokens': 81}}}


{'usage_including_cached_inference': {'total_cost': 0.000343, 'gpt-3.5-turbo-0125': {'cost': 0.000343, 'prompt_tokens': 305, 'completion_tokens': 127, 'total_tokens': 432}}, 'usage_excluding_cached_inference': {'total_cost': 0.000343, 'gpt-3.5-turbo-0125': {'cost': 0.000343, 'prompt_tokens': 305, 'completion_tokens': 127, 'total_tokens': 432}}}

Lesson 3: Reflection and Blogpost Writing

在这里插入图片描述

The task!

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

task = '''
        Write a concise but engaging blogpost about
       DeepLearning.AI. Make sure the blogpost is
       within 100 words.
       '''

Create a writer agent

import autogen

writer = autogen.AssistantAgent(
    name="Writer",
    system_message="You are a writer. You write engaging and concise " 
        "blogpost (with title) on given topics. You must polish your "
        "writing based on the feedback you receive and give a refined "
        "version. Only return your final work without additional comments.",
    llm_config=llm_config,
)
reply = writer.generate_reply(messages=[{"content": task, "role": "user"}])
print(reply)

Output

Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.

Adding reflection

Create a critic agent to reflect on the work of the writer agent.

critic = autogen.AssistantAgent(
    name="Critic",
    is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
    llm_config=llm_config,
    system_message="You are a critic. You review the work of "
                "the writer and provide constructive "
                "feedback to help improve the quality of the content.",
)
res = critic.initiate_chat(
    recipient=writer,
    message=task,
    max_turns=2,
    summary_method="last_msg"
)

Output

Critic (to Writer):


        Write a concise but engaging blogpost about
       DeepLearning.AI. Make sure the blogpost is
       within 100 words.
       

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.

--------------------------------------------------------------------------------
Critic (to Writer):

This blogpost effectively captures the essence of DeepLearning.AI and outlines the benefits of the platform. However, to enhance its impact, consider adding specific examples of courses offered, success stories from learners, or statistics on job placements to provide concrete evidence of the platform's effectiveness. Additionally, incorporating a call-to-action at the end, inviting readers to explore the platform or share their experiences, can further engage the audience. Overall, the blogpost is well-written and engaging, with a few tweaks to make it more informative and interactive.

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Dive into the dynamic world of artificial intelligence through DeepLearning.AI, a premier platform founded by AI luminary Andrew Ng. Explore cutting-edge courses like "Neural Networks and Deep Learning," "Computer Vision," and "Natural Language Processing" to hone your skills. Join a thriving community of learners and tap into exclusive resources to advance your AI expertise. Through DeepLearning.AI, individuals worldwide have landed impactful roles in top tech companies. Ready to embark on a transformative learning journey? Join DeepLearning.AI today and be part of the AI revolution. Explore your potential and shape the future with DeepLearning.AI.

Nested chat

SEO_reviewer = autogen.AssistantAgent(
    name="SEO Reviewer",
    llm_config=llm_config,
    system_message="You are an SEO reviewer, known for "
        "your ability to optimize content for search engines, "
        "ensuring that it ranks well and attracts organic traffic. " 
        "Make sure your suggestion is concise (within 3 bullet points), "
        "concrete and to the point. "
        "Begin the review by stating your role.",
)

legal_reviewer = autogen.AssistantAgent(
    name="Legal Reviewer",
    llm_config=llm_config,
    system_message="You are a legal reviewer, known for "
        "your ability to ensure that content is legally compliant "
        "and free from any potential legal issues. "
        "Make sure your suggestion is concise (within 3 bullet points), "
        "concrete and to the point. "
        "Begin the review by stating your role.",
)
ethics_reviewer = autogen.AssistantAgent(
    name="Ethics Reviewer",
    llm_config=llm_config,
    system_message="You are an ethics reviewer, known for "
        "your ability to ensure that content is ethically sound "
        "and free from any potential ethical issues. " 
        "Make sure your suggestion is concise (within 3 bullet points), "
        "concrete and to the point. "
        "Begin the review by stating your role. ",
)
meta_reviewer = autogen.AssistantAgent(
    name="Meta Reviewer",
    llm_config=llm_config,
    system_message="You are a meta reviewer, you aggragate and review "
    "the work of other reviewers and give a final suggestion on the content.",
)

Orchestrate the nested chats to solve the task

def reflection_message(recipient, messages, sender, config):
    return f'''Review the following content. 
            \n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}'''

review_chats = [
    {
     "recipient": SEO_reviewer, 
     "message": reflection_message, 
     "summary_method": "reflection_with_llm",
     "summary_args": {"summary_prompt" : 
        "Return review into as JSON object only:"
        "{'Reviewer': '', 'Review': ''}. Here Reviewer should be your role",},
     "max_turns": 1},
    {
    "recipient": legal_reviewer, "message": reflection_message, 
     "summary_method": "reflection_with_llm",
     "summary_args": {"summary_prompt" : 
        "Return review into as JSON object only:"
        "{'Reviewer': '', 'Review': ''}.",},
     "max_turns": 1},
    {"recipient": ethics_reviewer, "message": reflection_message, 
     "summary_method": "reflection_with_llm",
     "summary_args": {"summary_prompt" : 
        "Return review into as JSON object only:"
        "{'reviewer': '', 'review': ''}",},
     "max_turns": 1},
     {"recipient": meta_reviewer, 
      "message": "Aggregrate feedback from all reviewers and give final suggestions on the writing.", 
     "max_turns": 1},
]

critic.register_nested_chats(
    review_chats,
    trigger=writer,
)
res = critic.initiate_chat(
    recipient=writer,
    message=task,
    max_turns=2,
    summary_method="last_msg"
)

Output

Critic (to Writer):


        Write a concise but engaging blogpost about
       DeepLearning.AI. Make sure the blogpost is
       within 100 words.
       

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.

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

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

********************************************************************************
Critic (to SEO Reviewer):

Review the following content. 
            

 Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.

--------------------------------------------------------------------------------
SEO Reviewer (to Critic):

As an SEO reviewer, my feedback on the content is as follows:

- Include target keywords like "artificial intelligence", "deep learning", "Andrew Ng", "deep learning courses" strategically throughout the content for better search engine visibility.
- Add meta title and description with relevant keywords to improve search engine ranking and attract more organic traffic.
- Incorporate internal links to related content within the website to enhance overall SEO performance.

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

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

********************************************************************************
Critic (to Legal Reviewer):

Review the following content. 
            

 Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.
Context: 
{'Reviewer': 'SEO Reviewer', 'Review': 'Content includes key topics related to artificial intelligence and deep learning. Suggested improvements include adding target keywords, creating meta title and description, and incorporating internal links for better search engine optimization.'}

--------------------------------------------------------------------------------
Legal Reviewer (to Critic):

As a Legal Reviewer:

- Ensure that the use of terms such as "top-tier courses" or "master AI technologies" are substantiated with specific evidence to avoid potential false advertising claims.
- Consider adding a disclaimer regarding the results or career advancements users can expect from the platform to manage expectations and mitigate any potential liability.
- Review the text for any claims that could be misleading or potentially deceptive to consumers, such as exaggerated promises related to career progression through the courses.

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

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

********************************************************************************
Critic (to Ethics Reviewer):

Review the following content. 
            

 Title: "Unleashing the Power of AI: A Dive into DeepLearning.AI"

Delve into the realm of artificial intelligence with DeepLearning.AI. Founded by Andrew Ng, this platform offers top-tier courses designed to unravel the mysteries of deep learning. Whether you're a novice or a seasoned professional, DeepLearning.AI provides the tools to master AI technologies and propel your career forward. From computer vision to natural language processing, the possibilities are endless. Join a vibrant community of learners and enthusiasts, and embark on a transformative learning journey. Discover the potential of AI and unlock new opportunities with DeepLearning.AI.
Context: 
{'Reviewer': 'SEO Reviewer', 'Review': 'Content includes key topics related to artificial intelligence and deep learning. Suggested improvements include adding target keywords, creating meta title and description, and incorporating internal links for better search engine optimization.'}
{'Reviewer': 'Legal Reviewer', 'Review': '- Ensure that the use of terms such as "top-tier courses" or "master AI technologies" are substantiated with specific evidence to avoid potential false advertising claims. - Consider adding a disclaimer regarding the results or career advancements users can expect from the platform to manage expectations and mitigate any potential liability. - Review the text for any claims that could be misleading or potentially deceptive to consumers, such as exaggerated promises related to career progression through the courses.'}

--------------------------------------------------------------------------------
Ethics Reviewer (to Critic):

As an Ethics Reviewer:

- Ensure that any claims made about the effectiveness of DeepLearning.AI courses in advancing careers are supported by verifiable evidence to avoid false advertising.
- Add a clear disclaimer outlining the expected outcomes and results users can anticipate from the platform, managing expectations and potential liability.
- Review the content to eliminate any potentially misleading or exaggerated statements regarding career advancements through the courses.

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

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

********************************************************************************
Critic (to Meta Reviewer):

Aggregrate feedback from all reviewers and give final suggestions on the writing.
Context: 
{'Reviewer': 'SEO Reviewer', 'Review': 'Content includes key topics related to artificial intelligence and deep learning. Suggested improvements include adding target keywords, creating meta title and description, and incorporating internal links for better search engine optimization.'}
{'Reviewer': 'Legal Reviewer', 'Review': '- Ensure that the use of terms such as "top-tier courses" or "master AI technologies" are substantiated with specific evidence to avoid potential false advertising claims. - Consider adding a disclaimer regarding the results or career advancements users can expect from the platform to manage expectations and mitigate any potential liability. - Review the text for any claims that could be misleading or potentially deceptive to consumers, such as exaggerated promises related to career progression through the courses.'}
{'reviewer': 'Ethics Reviewer', 'review': '- Ensure that any claims made about the effectiveness of DeepLearning.AI courses in advancing careers are supported by verifiable evidence to avoid false advertising. - Add a clear disclaimer outlining the expected outcomes and results users can anticipate from the platform, managing expectations and potential liability. - Review the content to eliminate any potentially misleading or exaggerated statements regarding career advancements through the courses.'}

--------------------------------------------------------------------------------
Meta Reviewer (to Critic):

Aggregate Feedback:
1. SEO Reviewer: Suggested improvements for better search engine optimization.
2. Legal Reviewer: Emphasized the need to substantiate claims and manage expectations with disclaimers to avoid potential legal issues.
3. Ethics Reviewer: Highlighted the importance of providing verifiable evidence for claims and avoiding misleading or exaggerated statements.

Final Suggestions:
- Address the SEO Reviewer's suggestions by incorporating target keywords, creating meta titles and descriptions, and adding internal links for better optimization.
- Substantiate claims about the effectiveness of courses and career advancements with specific evidence to avoid false advertising, as recommended by the Legal and Ethics Reviewers.
- Include a clear disclaimer outlining expected outcomes and managing user expectations to mitigate potential legal and ethical concerns.
- Review the content thoroughly to eliminate any potentially misleading or exaggerated statements, as advised by the Legal and Ethics Reviewers. 

Overall, it is essential to ensure that the content is accurate, transparent, and complies with legal and ethical standards to build trust with users and avoid potential liabilities.

--------------------------------------------------------------------------------
Critic (to Writer):

Aggregate Feedback:
1. SEO Reviewer: Suggested improvements for better search engine optimization.
2. Legal Reviewer: Emphasized the need to substantiate claims and manage expectations with disclaimers to avoid potential legal issues.
3. Ethics Reviewer: Highlighted the importance of providing verifiable evidence for claims and avoiding misleading or exaggerated statements.

Final Suggestions:
- Address the SEO Reviewer's suggestions by incorporating target keywords, creating meta titles and descriptions, and adding internal links for better optimization.
- Substantiate claims about the effectiveness of courses and career advancements with specific evidence to avoid false advertising, as recommended by the Legal and Ethics Reviewers.
- Include a clear disclaimer outlining expected outcomes and managing user expectations to mitigate potential legal and ethical concerns.
- Review the content thoroughly to eliminate any potentially misleading or exaggerated statements, as advised by the Legal and Ethics Reviewers. 

Overall, it is essential to ensure that the content is accurate, transparent, and complies with legal and ethical standards to build trust with users and avoid potential liabilities.

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Empowering Minds: Unlocking the Depths of DeepLearning.AI"

Embark on a transformative journey through the realm of artificial intelligence with DeepLearning.AI led by Andrew Ng. Tailor-made courses promise to demystify deep learning, nurturing skills from novice to expert levels. Witness firsthand the boundless potential in computer vision and natural language processing. With a vibrant learning community, new horizons await. Dive deep into AI's realm, unlocking endless possibilities.
 
(Note: The suggestions provided by the SEO, Legal, and Ethics reviewers have been taken into account to refine the blog post and ensure accuracy, transparency, and compliance with all standards.)

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

Get the summary

print(res.summary)

Output

Title: "Empowering Minds: Unlocking the Depths of DeepLearning.AI"

Embark on a transformative journey through the realm of artificial intelligence with DeepLearning.AI led by Andrew Ng. Tailor-made courses promise to demystify deep learning, nurturing skills from novice to expert levels. Witness firsthand the boundless potential in computer vision and natural language processing. With a vibrant learning community, new horizons await. Dive deep into AI's realm, unlocking endless possibilities.
 
(Note: The suggestions provided by the SEO, Legal, and Ethics reviewers have been taken into account to refine the blog post and ensure accuracy, transparency, and compliance with all standards.)

Lesson 4: Tool Use and Conversational Chess

在这里插入图片描述

Setup

llm_config = {"model": "gpt-4-turbo"}
import chess
import chess.svg
from typing_extensions import Annotated

Initialize the chess board

board = chess.Board()
made_move = False

Define the needed tools

1. Tool for getting legal moves

def get_legal_moves(
    
) -> Annotated[str, "A list of legal moves in UCI format"]:
    return "Possible moves are: " + ",".join(
        [str(move) for move in board.legal_moves]
    )

2. Tool for making a move on the board

def make_move(
    move: Annotated[str, "A move in UCI format."]
) -> Annotated[str, "Result of the move."]:
    move = chess.Move.from_uci(move)
    board.push_uci(str(move))
    global made_move
    made_move = True
    
    # Display the board.
    display(
        chess.svg.board(
            board,
            arrows=[(move.from_square, move.to_square)],
            fill={move.from_square: "gray"},
            size=200
        )
    )
    
    # Get the piece name.
    piece = board.piece_at(move.to_square)
    piece_symbol = piece.unicode_symbol()
    piece_name = (
        chess.piece_name(piece.piece_type).capitalize()
        if piece_symbol.isupper()
        else chess.piece_name(piece.piece_type)
    )
    return f"Moved {piece_name} ({piece_symbol}) from "\
    f"{chess.SQUARE_NAMES[move.from_square]} to "\
    f"{chess.SQUARE_NAMES[move.to_square]}."

Create agents

You will create the player agents and a board proxy agents for the chess board.

from autogen import ConversableAgent
# Player white agent
player_white = ConversableAgent(
    name="Player White",
    system_message="You are a chess player and you play as white. "
    "First call get_legal_moves(), to get a list of legal moves. "
    "Then call make_move(move) to make a move.",
    llm_config=llm_config,
)
# Player black agent
player_black = ConversableAgent(
    name="Player Black",
    system_message="You are a chess player and you play as black. "
    "First call get_legal_moves(), to get a list of legal moves. "
    "Then call make_move(move) to make a move.",
    llm_config=llm_config,
)
def check_made_move(msg):
    global made_move
    if made_move:
        made_move = False
        return True
    else:
        return False

board_proxy = ConversableAgent(
    name="Board Proxy",
    llm_config=False,
    is_termination_msg=check_made_move,
    default_auto_reply="Please make a move.",
    human_input_mode="NEVER",
)

Register the tools

A tool must be registered for the agent that calls the tool and the agent that executes the tool.

from autogen import register_function

for caller in [player_white, player_black]:
    register_function(
        get_legal_moves,
        caller=caller,
        executor=board_proxy,
        name="get_legal_moves",
        description="Get legal moves.",
    )
    
    register_function(
        make_move,
        caller=caller,
        executor=board_proxy,
        name="make_move",
        description="Call this tool to make a move.",
    )
player_black.llm_config["tools"]

Output

[{'type': 'function',
  'function': {'description': 'Get legal moves.',
   'name': 'get_legal_moves',
   'parameters': {'type': 'object', 'properties': {}, 'required': []}}},
 {'type': 'function',
  'function': {'description': 'Call this tool to make a move.',
   'name': 'make_move',
   'parameters': {'type': 'object',
    'properties': {'move': {'type': 'string',
      'description': 'A move in UCI format.'}},
    'required': ['move']}}}]

Register the nested chats

Each player agent will have a nested chat with the board proxy agent to
make moves on the chess board.

player_white.register_nested_chats(
    trigger=player_black,
    chat_queue=[
        {
            "sender": board_proxy,
            "recipient": player_white,
            "summary_method": "last_msg",
        }
    ],
)

player_black.register_nested_chats(
    trigger=player_white,
    chat_queue=[
        {
            "sender": board_proxy,
            "recipient": player_black,
            "summary_method": "last_msg",
        }
    ],
)

Start the Game

The game will start with the first message.

board = chess.Board()

chat_result = player_black.initiate_chat(
    player_white,
    message="Let's play chess! Your move.",
    max_turns=2,
)

Output

在这里插入图片描述

Adding a fun chitchat to the game!

player_white = ConversableAgent(
    name="Player White",
    system_message="You are a chess player and you play as white. "
    "First call get_legal_moves(), to get a list of legal moves. "
    "Then call make_move(move) to make a move. "
    "After a move is made, chitchat to make the game fun.",
    llm_config=llm_config,
)
player_black = ConversableAgent(
    name="Player Black",
    system_message="You are a chess player and you play as black. "
    "First call get_legal_moves(), to get a list of legal moves. "
    "Then call make_move(move) to make a move. "
    "After a move is made, chitchat to make the game fun.",
    llm_config=llm_config,
)
for caller in [player_white, player_black]:
    register_function(
        get_legal_moves,
        caller=caller,
        executor=board_proxy,
        name="get_legal_moves",
        description="Get legal moves.",
    )

    register_function(
        make_move,
        caller=caller,
        executor=board_proxy,
        name="make_move",
        description="Call this tool to make a move.",
    )

player_white.register_nested_chats(
    trigger=player_black,
    chat_queue=[
        {
            "sender": board_proxy,
            "recipient": player_white,
            "summary_method": "last_msg",
            "silent": True,
        }
    ],
)

player_black.register_nested_chats(
    trigger=player_white,
    chat_queue=[
        {
            "sender": board_proxy,
            "recipient": player_black,
            "summary_method": "last_msg",
            "silent": True,
        }
    ],
)
board = chess.Board()

chat_result = player_black.initiate_chat(
    player_white,
    message="Let's play chess! Your move.",
    max_turns=2,
)

Output

在这里插入图片描述

Lesson 5: Coding and Financial Analysis

在这里插入图片描述

Define a code executor

llm_config = {"model": "gpt-4-turbo"}
from autogen.coding import LocalCommandLineCodeExecutor
executor = LocalCommandLineCodeExecutor(
    timeout=60,
    work_dir="coding",
)

Create agents

from autogen import ConversableAgent, AssistantAgent

1. Agent with code executor configuration

code_executor_agent = ConversableAgent(
    name="code_executor_agent",
    llm_config=False,
    code_execution_config={"executor": executor},
    human_input_mode="ALWAYS",
    default_auto_reply=
    "Please continue. If everything is done, reply 'TERMINATE'.",
)

2. Agent with code writing capability

code_writer_agent = AssistantAgent(
    name="code_writer_agent",
    llm_config=llm_config,
    code_execution_config=False,
    human_input_mode="NEVER",
)
code_writer_agent_system_message = code_writer_agent.system_message
print(code_writer_agent_system_message)

Output

You are a helpful AI assistant.
Solve tasks using your coding and language skills.
In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.
    1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
    2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
Reply "TERMINATE" in the end when everything is done.

The task!

Ask the two agents to collaborate on a stock analysis task.

import datetime

today = datetime.datetime.now().date()
message = f"Today is {today}. "\
"Create a plot showing stock gain YTD for NVDA and TLSA. "\
"Make sure the code is in markdown code block and save the figure"\
" to a file ytd_stock_gains.png."""
chat_result = code_executor_agent.initiate_chat(
    code_writer_agent,
    message=message,
)

Output

code_executor_agent (to code_writer_agent):

Today is 2024-06-07. Create a plot showing stock gain YTD for NVDA and TLSA. Make sure the code is in markdown code block and save the figure to a file ytd_stock_gains.png.

--------------------------------------------------------------------------------
code_writer_agent (to code_executor_agent):

To get the year-to-date (YTD) price data for NVIDIA (NVDA) and Tesla (TSLA) stocks and then plot this information, we will use the `yfinance` library which allows for easy access to historical market data from Yahoo Finance. We will plot the data using `matplotlib`. The script will import the necessary libraries, fetch the YTD stock data for both NVDA and TSLA starting from January 1, 2024, compute the percentage gain for each day relative to the start of the year, and save the resulting plot to a file named `ytd_stock_gains.png`.

Please execute the following Python script:

```python
# filename: plot_stock_gains.py
import yfinance as yf
import matplotlib.pyplot as plt

# Fetching data from the start of the year until today for NVDA and TSLA
start_date = '2024-01-01'
end_date = '2024-06-07'
nvda = yf.download('NVDA', start=start_date, end=end_date)
tsla = yf.download('TSLA', start=start_date, end=end_date)

# Calculating percent gain from the start of the year
nvda['Gain'] = (nvda['Adj Close'] / nvda['Adj Close'].iloc[0] - 1) * 100
tsla['Gain'] = (tsla['Adj Close'] / tsla['Adj Close'].iloc[0] - 1) * 100

# Plotting the gains
plt.figure(figsize=(10, 6))
plt.plot(nvda['Gain'], label='NVIDIA (NVDA)', color='green')
plt.plot(tsla['Gain'], label='Tesla (TSLA)', color='blue')
plt.title('YTD Stock Gains as of 2024-06-07')
plt.xlabel('Date')
plt.ylabel('Gain (%)')
plt.legend()
plt.grid(True)
plt.savefig('ytd_stock_gains.png')
plt.show()
```

This code will save the plot to `ytd_stock_gains.png` and display the plot. Ensure that the `yfinance` and `matplotlib` libraries are installed before running the script. You can install these packages using `pip install yfinance matplotlib` if they are not already installed.

Let’s see the plot!

Note:

  • Your plot might differ from the one shown in the video because the LLM’s freestyle code generation could choose a different plot type, such as a bar plot.
  • You can re-run the previous cell and check the generated code. If it produces a bar plot, remember you can directly specify your preference by asking for a specific plot type instead of a bar plot.
import os
from IPython.display import Image

Image(os.path.join("coding", "ytd_stock_gains.png"))

Output

在这里插入图片描述

User-Defined Functions

Instead of asking LLM to generate the code for downloading stock data
and plotting charts each time, you can define functions for these two tasks and have LLM call these functions in the code.

def get_stock_prices(stock_symbols, start_date, end_date):
    """Get the stock prices for the given stock symbols between
    the start and end dates.

    Args:
        stock_symbols (str or list): The stock symbols to get the
        prices for.
        start_date (str): The start date in the format 
        'YYYY-MM-DD'.
        end_date (str): The end date in the format 'YYYY-MM-DD'.
    
    Returns:
        pandas.DataFrame: The stock prices for the given stock
        symbols indexed by date, with one column per stock 
        symbol.
    """
    import yfinance

    stock_data = yfinance.download(
        stock_symbols, start=start_date, end=end_date
    )
    return stock_data.get("Close")
def plot_stock_prices(stock_prices, filename):
    """Plot the stock prices for the given stock symbols.

    Args:
        stock_prices (pandas.DataFrame): The stock prices for the 
        given stock symbols.
    """
    import matplotlib.pyplot as plt

    plt.figure(figsize=(10, 5))
    for column in stock_prices.columns:
        plt.plot(
            stock_prices.index, stock_prices[column], label=column
                )
    plt.title("Stock Prices")
    plt.xlabel("Date")
    plt.ylabel("Price")
    plt.grid(True)
    plt.savefig(filename)

Create a new executor with the user-defined functions

executor = LocalCommandLineCodeExecutor(
    timeout=60,
    work_dir="coding",
    functions=[get_stock_prices, plot_stock_prices],
)

code_writer_agent_system_message += executor.format_functions_for_prompt()
print(code_writer_agent_system_message)

Let’s update the agents with the new system message

code_writer_agent = ConversableAgent(
    name="code_writer_agent",
    system_message=code_writer_agent_system_message,
    llm_config=llm_config,
    code_execution_config=False,
    human_input_mode="NEVER",
)

code_executor_agent = ConversableAgent(
    name="code_executor_agent",
    llm_config=False,
    code_execution_config={"executor": executor},
    human_input_mode="ALWAYS",
    default_auto_reply=
    "Please continue. If everything is done, reply 'TERMINATE'.",
)

Start the same task again!

chat_result = code_executor_agent.initiate_chat(
    code_writer_agent,
    message=f"Today is {today}."
    "Download the stock prices YTD for NVDA and TSLA and create"
    "a plot. Make sure the code is in markdown code block and "
    "save the figure to a file stock_prices_YTD_plot.png.",
)

Output

code_executor_agent (to code_writer_agent):

Today is 2024-06-07.Download the stock prices YTD for NVDA and TSLA and createa plot. Make sure the code is in markdown code block and save the figure to a file stock_prices_YTD_plot.png.

--------------------------------------------------------------------------------
code_writer_agent (to code_executor_agent):

First, let’s download the Year-To-Date (YTD) stock prices for the companies Nvidia (NVDA) and Tesla (TSLA), starting from January 1, 2024, up until today (June 7, 2024). We will use the `get_stock_prices` function to obtain the data and then use the `plot_stock_prices` function to create a plot and save it to 'stock_prices_YTD_plot.png'.

Here is the code:

```python
# Import necessary functions
from functions import get_stock_prices, plot_stock_plot_prices

# Define the stock symbols and the date range
stock_symbols = ['NVDA', 'TSLA']
start_date = '2024-01-01'
end_date = '2024-06-07'

# Get the stock prices
stock_prices = get_stock_prices(stock_symbols, start_date, end_date)

# Plot the stock prices and save the figure to a file
plot_stock_prices(stock_prices, 'stock_prices_YTD_plot.png')
```

Please execute this script to download the stock prices for NVDA and TSLA and create the plot.

Lesson 6: Planning and Stock Report Generation

在这里插入图片描述

The task!

llm_config={"model": "gpt-4-turbo"}
task = "Write a blogpost about the stock price performance of "\
"Nvidia in the past month. Today's date is 2024-04-23."

Build a group chat

This group chat will include these agents:

  1. User_proxy or Admin: to allow the user to comment on the report and ask the writer to refine it.
  2. Planner: to determine relevant information needed to complete the task.
  3. Engineer: to write code using the defined plan by the planner.
  4. Executor: to execute the code written by the engineer.
  5. Writer: to write the report.
import autogen

user_proxy = autogen.ConversableAgent(
    name="Admin",
    system_message="Give the task, and send "
    "instructions to writer to refine the blog post.",
    code_execution_config=False,
    llm_config=llm_config,
    human_input_mode="ALWAYS",
)


planner = autogen.ConversableAgent(
    name="Planner",
    system_message="Given a task, please determine "
    "what information is needed to complete the task. "
    "Please note that the information will all be retrieved using"
    " Python code. Please only suggest information that can be "
    "retrieved using Python code. "
    "After each step is done by others, check the progress and "
    "instruct the remaining steps. If a step fails, try to "
    "workaround",
    description="Planner. Given a task, determine what "
    "information is needed to complete the task. "
    "After each step is done by others, check the progress and "
    "instruct the remaining steps",
    llm_config=llm_config,
)



engineer = autogen.AssistantAgent(
    name="Engineer",
    llm_config=llm_config,
    description="An engineer that writes code based on the plan "
    "provided by the planner.",
)

Note: In this lesson, you’ll use an alternative method of code execution by providing a dict config. However, you can always use the LocalCommandLineCodeExecutor if you prefer. For more details about code_execution_config, check this: https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent/#init

executor = autogen.ConversableAgent(
    name="Executor",
    system_message="Execute the code written by the "
    "engineer and report the result.",
    human_input_mode="NEVER",
    code_execution_config={
        "last_n_messages": 3,
        "work_dir": "coding",
        "use_docker": False,
    },
)


writer = autogen.ConversableAgent(
    name="Writer",
    llm_config=llm_config,
    system_message="Writer."
    "Please write blogs in markdown format (with relevant titles)"
    " and put the content in pseudo ```md```code block. "
    "You take feedback from the admin and refine your blog.",
    description="Writer."
    "Write blogs based on the code execution results and take "
    "feedback from the admin to refine the blog."
)

Define the group chat

groupchat = autogen.GroupChat(
    agents=[user_proxy, engineer, writer, executor, planner],
    messages=[],
    max_round=10,
)

manager = autogen.GroupChatManager(
    groupchat=groupchat, llm_config=llm_config
)

Start the group chat!

groupchat_result = user_proxy.initiate_chat(
    manager,
    message=task,
)

Add a speaker selection policy

user_proxy = autogen.ConversableAgent(
    name="Admin",
    system_message="Give the task, and send "
    "instructions to writer to refine the blog post.",
    code_execution_config=False,
    llm_config=llm_config,
    human_input_mode="ALWAYS",
)

planner = autogen.ConversableAgent(
    name="Planner",
    system_message="Given a task, please determine "
    "what information is needed to complete the task. "
    "Please note that the information will all be retrieved using"
    " Python code. Please only suggest information that can be "
    "retrieved using Python code. "
    "After each step is done by others, check the progress and "
    "instruct the remaining steps. If a step fails, try to "
    "workaround",
    description="Given a task, determine what "
    "information is needed to complete the task. "
    "After each step is done by others, check the progress and "
    "instruct the remaining steps",
    llm_config=llm_config,
)

engineer = autogen.AssistantAgent(
    name="Engineer",
    llm_config=llm_config,
    description="Write code based on the plan "
    "provided by the planner.",
)

writer = autogen.ConversableAgent(
    name="Writer",
    llm_config=llm_config,
    system_message="Writer. "
    "Please write blogs in markdown format (with relevant titles)"
    " and put the content in pseudo ```md```code block. "
    "You take feedback from the admin and refine your blog.",
    description="After all the info is available, "
    "write blogs based on the code execution results and take "
    "feedback from the admin to refine the blog. ",
)

executor = autogen.ConversableAgent(
    name="Executor",
    description="Execute the code written by the "
    "engineer and report the result.",
    human_input_mode="NEVER",
    code_execution_config={
        "last_n_messages": 3,
        "work_dir": "coding",
        "use_docker": False,
    },
)
groupchat = autogen.GroupChat(
    agents=[user_proxy, engineer, writer, executor, planner],
    messages=[],
    max_round=10,
    allowed_or_disallowed_speaker_transitions={
        user_proxy: [engineer, writer, executor, planner],
        engineer: [user_proxy, executor],
        writer: [user_proxy, planner],
        executor: [user_proxy, engineer, planner],
        planner: [user_proxy, engineer, writer],
    },
    speaker_transitions_type="allowed",
)
manager = autogen.GroupChatManager(
    groupchat=groupchat, llm_config=llm_config
)

groupchat_result = user_proxy.initiate_chat(
    manager,
    message=task,
)

后记

2024年6月7日周五19点08分完成这门short course,对AutoGen这个框架有了基本的了解。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1801995.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue+vscode 快速搭建运行调试环境与发布

1.安装node.js Node.js — Run JavaScript Everywhere 默认不断next 2.更换镜像地址 运行-cmd 执行以下代码安装 npm config set registry https://registry.npmmirror.com 检查node.js和镜像是否是否成功 node -v npm -v npm config get registry 3.安装打包工具 …

【SpringBoot】SpringBoot整合RabbitMQ消息中间件,实现延迟队列和死信队列

&#x1f4dd;个人主页&#xff1a;哈__ 期待您的关注 目录 一、&#x1f525;死信队列 RabbitMQ的工作模式 死信队列的工作模式 二、&#x1f349;RabbitMQ相关的安装 三、&#x1f34e;SpringBoot引入RabbitMQ 1.引入依赖 2.创建队列和交换器 2.1 变量声明 2.2 创建…

Java采取擦除式泛型到底兼容了什么场景?

在开始前刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「 Java的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01;Java擦除式泛型是一个妥协,…

高光谱图像聚类的像素-超像素对比学习与伪标签校正

Pixel-Superpixel Contrastive Learning and Pseudo-Label Correction for Hyperspectral Image Clustering 文章目录 Pixel-Superpixel Contrastive Learning and Pseudo-Label Correction for Hyperspectral Image Clustering摘要引言相关方法对比学习 方法超像素对比学习像素…

【Flask开发实战】首页模板

一、前言 前面我们已经完成登录页面的设定&#xff0c;登录后临时调转到“hello flask”的界面。现在我们根据实际首页的设计需要&#xff0c;来完成首页相关内容的开发。一般系统首页会放一些分析数据&#xff0c;多以图表的方式展示&#xff0c;方便使用者了解信息。以防火墙…

JavaScript事件监听之其它事件(页面加载事件、元素滚动事件、页面尺寸事件、M端事件)

目录 1. 页面加载事件(load、DOMContentLoaded)2. 元素滚动事件(scroll)3. 页面尺寸事件3.1 resize3.2 获取元素宽高3.3 获取元素位置(offsetLeft和offsetTop、getBoundingClientRect) 4. M端事件 1. 页面加载事件(load、DOMContentLoaded) load事件&#xff1a; 使用场景: 等…

MyBatis二级缓存开启条件

MyBatis缓存为俩层体系。分为一级缓存和二级缓存。 一级缓存&#xff1a; 一级缓存默认开启&#xff0c;一级缓存的作用域是SqlSession级别的&#xff0c;这意味着当你更换SqlSession之后就不能再利用原来的SqlSession的一级缓存了。不同的SqlSession之间的一级缓存是隔离的。…

基础概念解析:SOCKS5代理究竟是什么?SOCKS5代理ip使用场景有哪些?

在当今数字化时代&#xff0c;网络安全和隐私保护已成为我们日常生活中不可忽视的问题。随着网络攻击手段的日益复杂&#xff0c;如何安全地访问互联网资源成为了一个亟待解决的问题。SOCKS5代理作为一种先进的网络协议&#xff0c;为我们提供了解决这一问题的有效方案。 本文…

实用的 C 盘搬家软件

一、简介 1、一款专门用于 Windows 系统的文件夹移动工具&#xff0c;它允许用户将程序或游戏的安装文件夹从一台驱动器移动到另一台驱动器&#xff0c;或者同一个驱动器内的不同路径&#xff0c;而无需重新安装或破坏现有的程序安装。 二、下载 1、下载地址&#xff1a; 官网链…

3-1RT-Thread时钟管理

这里写自定义目录标题 时钟节拍是RT thread操作系统的最小时间单位。 第一个功能&#xff0c;rt tick值自动加1&#xff0c;在RT thread当中通过RT_USING_SMP定义了多核和单核的场景。第二个功能&#xff0c;检查当前线程的时间片&#xff0c;首先获取当前线程&#xff0c;将当…

AI 写高考作文丨10 款大模型 “交卷”,实力水平如何?

前言 在科技日新月异的今天&#xff0c;人工智能&#xff08;AI&#xff09;已不再是遥不可及的未来科技&#xff0c;而是逐渐融入我们日常生活的实用工具。从智能语音助手到自动驾驶汽车&#xff0c;从智能家居系统到精准医疗诊断&#xff0c;AI技术正以其强大的计算能力和数…

端午搞个零花钱,轻松赚取创业的第一桶金!2024最受欢迎的创业项目,2024新的创业机会

好好的端午节&#xff0c; 净给我添堵&#xff01; 本来我打算在端午节愉快的玩耍&#xff0c; 结果一大早起床却看到舍友在给一堆设备充电&#xff0c; 然后装的整整齐齐&#xff0c; 满满一书包。 我好奇他小子这是要干嘛&#xff1f; 不会是打算今天回去给亲朋好友准备…

Centos7 安装配置SFTP

Centos7安装配置SFTP 更新源安装 OpenSSH 服务启动服务设置为开机自启动新建一个用户 (sftpuser为你要设置的用户的用户名)编辑配置文件设置sftp用户的根目录重启SSH服务代码实现 由于最近工作中需要实现动态上传文件到帆软服务器&#xff0c;但是帆软没有提供相关API&#xff…

Allegro限制走线区域和元器件摆放区域

一、Line to Shape 当你的板框是线条形式时&#xff0c;先将线条闭合成shape&#xff0c;点击Shape–>Compose Shape,选择如下参数&#xff0c;再逐一选中分散线条&#xff0c;选择完毕右键Done即可 二、Z-Cope使用 点击Edit–>Z-Cope&#xff0c;选择如下参数&…

Django项目上线-报错汇总

Django项目上线-报错汇总 下列报错基本都是Python环境相关 pip install 报错 WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. debian运行pip报错ssl module in Python is not available - z417 - 博…

SM481,SM432和利时DCS备件

SM481,SM432和利时DCS备件。POU名只能包含字母、数字、下划线&#xff0c;第一个字符必须是字母或者下划线&#xff0c;且遵循以下原则&#xff1a;SM481,SM432和利时DCS备件。关于重名&#xff0c;不能与变量名、变量组名、POU文件夹名、任务名、SM481,SM432和利时DCS备件。工…

OpenCompass 大模型评测平台C-Eval 基准任务评估实战

1. 引言 在人工智能迅速发展的今天&#xff0c;大型语言模型&#xff08;LLMs&#xff09;在多个领域展现出了巨大的潜力和应用价值。然而&#xff0c;如何评价这些模型的性能&#xff0c;了解它们的优缺点&#xff0c;成为了一个重要课题。OpenCompass&#xff0c;一个由上海…

【Java】解决Java报错:ArrayIndexOutOfBoundsException

文章目录 引言1. 错误详解2. 常见的出错场景2.1 直接访问数组越界2.2 循环中的索引错误2.3 多维数组的错误访问 3. 解决方案3.1 检查数组长度3.2 正确使用循环3.3 多维数组的正确访问 4. 预防措施4.1 使用增强型 for 循环4.2 编写防御性代码4.3 单元测试 结语 引言 在Java编程…

C++学习插曲:“name“的初始化操作由“case“标签跳过

问题 "name"的初始化操作由"case"标签跳过 问题代码 case 3: // 3、删除联系人string name;cout << "请输入删除联系人姓名&#xff1a;" << endl;cin >> name;if (isExistPerson(&abs, name) -1){cout << "…

Linux--进程间通信(system V共享内存)

目录 1.原理部分 2.系统调用接口 参数说明 返回值 1. 函数原型 2. 参数说明 3. 返回值 4. 原理 5. 注意事项 3.使用一下shmget&#xff08;一段代码&#xff09; 4.一个案例&#xff08;一段代码) 1.简单封装一下 2.使用共享内存 2.1挂接&#xff08;shmat&#x…