Build AI agents that work for you using Autogen - Full tutorial
Full tutorial - https://www.youtube.com/watch?v=9Wp9Jhspihk
Let's build AI agents using Microsoft Autogen. In this tutorial, we'll explore how to use the Autogen library to create and interact with different AI agents, each with its specialized task. We'll set up three agents: a Financial Assistant, a Researcher, and a Writer. The Financial Assistant will provide information on stock prices and performance, the Researcher will investigate reasons behind stock performance, and the Writer will utilize the gathered information to develop an engaging blog post.
Step 1: Import the necessary libraries
First, we need to import the autogen library, which provides the tools to create and manage our AI agents.
import autogen
Step 2: Define the tasks
Next, we define the tasks that each agent will perform. In this case, we have two financial tasks and one writing task.
financial_tasks = [
"""What are the current stock prices of NVDA and TESLA, and how is the performance over the past month in terms of percentage change?""",
"""Investigate possible reasons of the stock performance.""",
]
writing_tasks = ["""Develop an engaging blog post using any information provided."""]
Step 3: Configure the AI agents
Create a file called OAI_CONFIG_LIST to configure the AI model you want to use. For this tutorial, I have used GPT-3.5 from OpenAI.
[
{
"model": "gpt-3.5-turbo-0125"
}
]
We'll configure the agents by setting up a config_list and an llm_config. The config_list contains the necessary configurations for the agents, while the llm_config specifies the language model configuration.
领英推荐
config_list = autogen.config_list_from_json(
"OAI_CONFIG_LIST"
)
llm_config = {"config_list": config_list}
Step 4: Create the AI agents
Now, we can create our three agents using the AssistantAgent class from autogen. Each agent is initialized with a name, the llm_config, and any additional configuration (e.g., a system message for the Writer agent).
financial_assistant = autogen.AssistantAgent(
name="Financial_assistant",
llm_config=llm_config,
)
research_assistant = autogen.AssistantAgent(
name="Researcher",
llm_config=llm_config,
)
writer = autogen.AssistantAgent(
name="writer",
llm_config=llm_config,
system_message="""
You are a professional writer, known for
your insightful and engaging articles.
You transform complex concepts into compelling narratives.
Reply "TERMINATE" in the end when everything is done.
""",
Step 5: Create a UserProxyAgent
The UserProxyAgent acts as an interface for interacting with our AI agents. It allows us to initiate chats with the agents, pass along messages, and collecting their responses.
user = autogen.UserProxyAgent(
name="User",
human_input_mode="NEVER",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config={
"last_n_messages": 1,
"work_dir": "tasks",
"use_docker": False,
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)
Step 6: Initiate chats with the agents
Finally, we initiate chats with our agents by calling the initiate_chats method on the UserProxyAgent. We pass in a list of dictionaries, each containing the recipient agent, the message to send, and any additional configuration (e.g., clearing the chat history, specifying a summary method, or carrying over information).
chat_results = user.initiate_chats(
[
{
"recipient": financial_assistant,
"message": financial_tasks[0],
"clear_history": True,
"silent": False,
"summary_method": "last_msg",
},
{
"recipient": research_assistant,
"message": financial_tasks[1],
"summary_method": "reflection_with_llm",
},
{
"recipient": writer,
"message": writing_tasks[0],
"carryover": "I want to include a figure or a table of data in the blogpost.",
},
]
)
In this example, we initiate three chats:
The chat_results variable will contain the responses from each agent, which you can then process and utilize as needed.
Conclusion
Congratulations! You've learned how to build AI agents using the autogen library for specialized tasks. You can extend this tutorial by exploring additional features of autogen, such as customizing agent configurations, handling agent responses, and integrating with other libraries or services.
Senior System Reliability Engineer / Platform Engineer
6 个月??
Data Centre Engineer
7 个月Impressive work! Can't wait to see what you come up with next! ??
Project Manager at Wipro
7 个月Exciting possibilities with Microsoft AutoGen!
?? 中国广告创新国际顾问 - 综合数字传播客座教授 - 140 多个创意奖项 ?????
7 个月What about agents that help streamline customer service interactions or automate tedious office tasks?
9-figure Digital Businesses Maker based on technology (Web2, Web3, AI, and noCode) | General Manager MOVE Estrella Galicia Digital & exAmazon
7 个月Impressive work! Can't wait to see the results. ?? Sri Laxmi