> 技术文档 > 微软AutoGen介绍:Teams与智能体团队协作并使用_roundrobingroupchat

微软AutoGen介绍:Teams与智能体团队协作并使用_roundrobingroupchat


介绍

大家好,今天给大家分享的内容是微软AutoGen框架的核心功能Teams。我们直接进入主题。

Teams

在微软AutoGen框架里,“Teams”代表着智能团队的概念,它让多个智能体相互协作,共同完成复杂任务。

AutoGen中的智能体是能够自主执行特定任务、与其他智能体交互的实体。Teams就是将多个这样的智能体组合在一起,每个智能体有不同专长和职责,它们通过彼此沟通和协作,完成单个智能体难以完成的复杂任务。

在本次的分享内容中,我们将学习如何使用AutoGen创建多智能体Team(或简称为团队)。团队是一群共同努力并实现共同目标的智能体。

我首先会向大家分享如何创建和运行团队。然后,我会解释如何观察团队(Teams)的行为(因为这对于调试和了解团队的性能至关重要),以及控制团队行为的常见操作。

创建团队

RoundRobinGroupChat是由AutoGen提供的类,它是一种简单但有效的团队配置类。在此配置下,所有智能体共享相同的上下文,并以循环赛的方式轮流做出回应。轮到每个智能体时,它会将自己的回应广播给其他所有智能体,以确保整个团队保持一致的上下文。

我们将从创建一个由两个助理智能体(AssistantAgent)组成的团队开始,并设置一个文本提及终止条件(TextMentionTermination),当在智能体的回应中检测到特定词汇时,该团队就会停止运行。

这个双智能体团队实现了反思模式,这是一种多智能体设计模式,其中一个评判智能体评估一个主要智能体的回应。

代码演示

import asynciofrom autogen_agentchat.agents import AssistantAgentfrom autogen_agentchat.conditions import TextMentionTerminationfrom autogen_agentchat.teams import RoundRobinGroupChatfrom autogen_ext.models.openai import OpenAIChatCompletionClient# 创建一个OpenAI模型客户端。model_client = OpenAIChatCompletionClient( model=\"gpt-3.5-turbo\", # api_key=\"sk-...\", # 如果已设置OPENAI_API_KEY环境变量,则此步骤可选。)# 创建主智能体。primary_agent = AssistantAgent( \"primary\", model_client=model_client, system_message=\"You are a helpful AI assistant.\",)# 创建评判智能体。critic_agent = AssistantAgent( \"critic\", model_client=model_client, system_message=\"Provide constructive feedback. Respond with \'APPROVE\' to when your feedbacks are addressed.\",)# 定义一个终止条件,若评判者批准,则停止任务。text_termination = TextMentionTermination(\"APPROVE\")# 创建一个包含主智能体和评判智能体的团队。team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=text_termination)

运行团队

让我们调用run()方法,通过一项任务运行该团队。

代码演示

result = asyncio.run(team.run(task=\"Write a short poem about the fall season.\"))print(result)

完整代码

import asynciofrom autogen_agentchat.agents import AssistantAgentfrom autogen_agentchat.conditions import TextMentionTerminationfrom autogen_agentchat.teams import RoundRobinGroupChatfrom autogen_ext.models.openai import OpenAIChatCompletionClient# 创建一个OpenAI模型客户端。model_client = OpenAIChatCompletionClient( model=\"gpt-3.5-turbo\", # api_key=\"sk-...\", # 如果已设置OPENAI_API_KEY环境变量,则此步骤可选。)# 创建主智能体。primary_agent = AssistantAgent( \"primary\", model_client=model_client, system_message=\"You are a helpful AI assistant.\",)# 创建评判智能体。critic_agent = AssistantAgent( \"critic\", model_client=model_client, system_message=\"Provide constructive feedback. Respond with \'APPROVE\' to when your feedbacks are addressed.\",)# 定义一个终止条件,若评判者批准,则停止任务。text_termination = TextMentionTermination(\"APPROVE\")# 创建一个包含主智能体和评判智能体的团队。team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=text_termination)result = asyncio.run(team.run(task=\"Write a short poem about the fall season.\"))print(result)

运行结果

TaskResult(messages=[TextMessage(source=\'user\', models_usage=None, content=\'Write a short poem about the fall season.\', type=\'TextMessage\'), TextMessage(source=\'primary\', models_usage=RequestUsage(prompt_tokens=28, completion_tokens=94), content=\"In hues of gold and crimson blaze,\\nAutumn\'s touch on trees displays,\\nA gentle chill in the air\'s embrace,\\nAs leaves gently fall with grace.\\n\\nNature\'s canvas painted with flair,\\nWhispers of change linger in the air,\\nHarvest bounty on display,\\nIn the fall season\'s gentle sway.\\n\\nCool winds rustle through the land,\\nA symphony by autumn\'s hand,\\nCrunch of leaves beneath our feet,\\nA fleeting beauty, oh so sweet.\", type=\'TextMessage\'), TextMessage(source=\'critic\', models_usage=RequestUsage(prompt_tokens=140, completion_tokens=57), content=\'I like the vivid imagery and the beautiful flow of your poem about the fall season. The use of descriptive language and sensory details really brings the essence of autumn to life. Maybe consider adding a concluding stanza to wrap up the poem or bring it to a satisfying close. Well done overall!\', type=\'TextMessage\'), TextMessage(source=\'primary\', models_usage=RequestUsage(prompt_tokens=192, completion_tokens=52), content=\"Thank you for your feedback! I\'m glad you enjoyed the imagery and flow of the poem. I will consider adding a concluding stanza to provide a s