构建多智能体系统以完成复杂任务

智能科技扫地僧 2024-07-13 15:46:50

当ChatGPT首次出现时,它改变了游戏规则。现在,各行各业的人都在使用它。ChatGPT展示了这些机器学习模型的力量,而我们大多数人认为这种力量在不久的将来是不太可能实现的。然而,尽管这些大型语言模型(LLMs)在其能力上变得越来越强大,但一个非常令人兴奋且潜力巨大的发展是多智能体系统的使用。例如,第一个自主AI软件工程师Devine AI就是基于多智能体框架的。

一位气候分析师

如果你让ChatGPT为你写一篇关于任何主题的文章,由于几个问题,比如无法获取关于该主题的最新数据,这可能导致幻觉,ChatGPT可能无法生成一个好的报告。如果我们将这个复杂任务分解成单个任务会怎样呢?考虑一位气候变化分析师正在撰写有关最新环境趋势的报告;他/她需要完成许多任务(我意识到这可能是对角色的简化,但这只是为了演示目的):

· 研究以从可靠来源找到所有关键数据。

· 分析所有结果数据并提取数据的关键解释。

· 撰写一份解释发现的报告。

· 然后,报告将接受同行评审,以确保科学报告的准确性和发现得到所呈现数据的支持。

如果我们有针对每项任务的专门代理呢?即,一个代理是研究员,另一个代理充当分析师来分析找到的数据,另一个代理是作家,第四个代理是批评家,他将确保文章的发现得到所呈现数据的支持(但希望不像真正的科学家那样,他不会要求你在文章中引用他们完全不相关的工作)。这些系统利用了各个代理的优势,每个代理都有专门的角色扮演和能力,以协作完成复杂任务。本文深入探讨了LLM代理推动下一波发展的潜力,并通过构建多智能体系统的实践示例展示了它们的能力。

构建多智能体系统:气候变化分析师

你可以使用像CrewAI这样的框架来构建多智能体系统,本文展示的工作不过是尝试为多智能体系统构建一个非常简单的框架的谦逊尝试。这些代理如何沟通、记忆(他们有短期和长期记忆!)以及如何协调对它们的表现至关重要。本文的目的是通过这样做来建立一个简单框架,我们可以更深入地了解这些系统,而不是从现成的库中导入一切并将其视为黑盒。我们将构建一个能够撰写有关气候变化最新趋势的文章的系统,如上所述。我们将开发一个由专门代理组成的团队,他们可以进行研究、分析、撰写科学报告并对该报告进行同行评审。

我们开始吧!设置环境和导入关键库。我们需要为负责研究的代理提供一个工具。这个工具将允许代理通过API进行谷歌搜索,我将使用crewai库SerperDevTool来实现这一点。

import osimport openaifrom openai import OpenAIfrom crewai_tools import SerperDevToolos.environ["SERPER_API_KEY"] = "your serper api key goes here"os.environ["OPENAI_API_KEY"] = "your open AI key goes here"client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))search_tool = SerperDevTool(api_key=os.getenv("SERPER_API_KEY"))

定义代理类,每个代理将有专门的任务、目标和背景故事,我们将在稍后指定。代理能够存储被查询的任务以及相应的输出,从而实现短期记忆功能。它还可以向所有其他代理发送信息,以及阅读其他代理发送的信息。

class Agent: def __init__(self, name, role, backstory, goal, tools=None): self.name = name self.backstory = backstory self.goal = goal self.role = role self.memory = [] self.tools = tools if tools else [] self.message_box = [] # adding memory for the agent to store recent tasks and outputs def add_to_memory(self, entry): self.memory.append(entry) # sending messages to other agents def send_message(self, recipient, message): recipient.message_box.append((self.name, message)) # reading the messages sent from other agents before performing task # this is done by removing messages from message box and added to memory def read_messages(self): while self.message_box: sender, message = self.message_box.pop(0) self.add_to_memory(f"message from the {sender}: {message}") # we now define the function that will do the task assigned # reading messages and adding task to the memory first # the agent will take up the specialised role assigned and querry gpt3.5 def do_task(self, task, inputs): self.read_messages() task_info = task.info self.add_to_memory(f"doing task: {task_info}") '''for the research agent, the search_tool will be assigned to the agent which it will be able to use to do a google search online''' if 'search_tool' in self.tools: search_query = task_info search_results = search_tool.run(query=search_query) inputs['search_results'] = search_results task_info += f"\n\nsearch results:\n{search_results}" llm_response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": f"you are a {self.role}. {self.backstory} Your goal is {self.goal}."}, {"role": "user", "content": task_info} ] ) output = llm_response.choices[0].message.content self.add_to_memory(f"task output: {output}") return output

架构师的目的是(除了创建矩阵之外)将任务分配给相应的代理并协调代理之间的信息流动。在这个框架中,除了代理之间的消息传递外,工作是以顺序形式进行的,即每个代理的工作传递给下一个代理,代理之间没有任务的委托或迭代。然而,在CrewAI框架中,我认为它具有这些特性,这使得它在能力上非常强大。

class TheArchitect: def __init__(self, agents, tasks): # dictionary of all agents based on name self.agents = {agent.name: agent for agent in agents} self.tasks = tasks def process(self, inputs): results = {} current_result = None for task in self.tasks: task_agent = self.agents[task.agent.name] '''to help with debugging and also checking flow of info we can check/print which tasks are assigned to which agent''' print(f"assignin task {task.name} to agent {task_agent.name}: {task.info}") if current_result: inputs['previous_result'] = current_result if 'search' in inputs: search_query = inputs['search'] search_results = search_tool.run(query=search_query) inputs['search_results'] = search_results agent_output = task_agent.do_task(task, inputs) current_result = agent_output # send the agent's output as a message to all other agents for agent_name, agent in self.agents.items(): if agent_name != task_agent.name: task_agent.send_message(agent, agent_output) results[task.agent.name] = agent_output return results

既然我们已经定义了代理类和架构师类,让我们创建这些类的实例来定义具有不同角色的不同代理。

我们现在可以定义所有代理,并为他们命名、分配角色、目标、背景故事和工具。这些代理只是数据收集者、数据科学家和报告撰写者。在这种情况下,我们只有一个拥有工具的研究者/数据收集者。

data_collector = Agent( name="researcher", role="Climate Data Collector", goal="Collect comprehensive climate data from multiple sources.", backstory="You gather climate data on temperature, carbon dioxide levels and other variables relevant to climate change, from reliable sources.", tools=['search_tool'])data_analyst = Agent( name="Data Scientist", role="Climate Data Scientist", goal="Analyse the collected climate data to identify significant trends.", backstory="You analyse climate data to find significant trends and understand the impact of various factors on climate change.", tools=[])report_writer = Agent( name="Report Writer", role="Senior Scientific Report Writer", goal="Generate a comprehensive report on climate change findings.", backstory="You write detailed scientific reports based on the analysed climate data, highlighting key findings and implications.", tools=[])peer_reviewer = Agent( name="Peer Reviewer", role="Scientific Peer Reviewer", goal="Review the scientific report for accuracy, clarity, and completeness.", backstory="You review scientific reports to ensure they are accurate, clear, and meet the standards for scientific publication.", tools=[])final_report_writer = Agent( name="Final Report Writer", role="Final Report Writer", goal="Incorporate peer review feedback and finalize the scientific report.", backstory="You finalize the scientific report by incorporating feedback from peer reviewer and ensure it is publication ready.", tools=[])

我们需要将任何问题分解成一系列任务,并为每个任务分配一个代理。信息和预期输出对于让代理按照你的期望行事和输出至关重要。

analyse_data = Task( info=( "Using the following climate data, analyze for trends and patterns:\n{previous_result}\n" "1. Identify significant trends in temperature, CO2 levels, and precipitation.\n" "2. Determine potential causes of observed trends.\n" "3. Summarize key findings in a detailed analysis report." ), expected_output="Detailed analysis report on climate data trends and potential causes.", agent=data_analyst, name="Data Analysis")write_report = Task( info=( "Using the following analysis report, write a comprehensive scientific report on climate change findings:\n{previous_result}\n" "1. Include an introduction, methodology, results, discussion, and conclusion.\n" "2. Use clear and precise language suitable for a scientific audience.\n" "3. Ensure all findings are supported by data and analysis." ), expected_output="Comprehensive scientific report on climate change findings.", agent=report_writer, name="Report Writing")review_report = Task( info=( "Using the following scientific report, review for accuracy, clarity, and completeness:\n{previous_result}\n" "1. Ensure the report adheres to scientific standards.\n" "2. Check for any errors or inaccuracies in data and analysis.\n" "3. Provide feedback and suggestions for improvement." ), expected_output="Reviewed and revised scientific report, ready for publication.", agent=peer_reviewer, name="Peer Review")finalize_report = Task( info=( "Using the following peer-reviewed report, incorporate feedback and finalize the scientific report:\n{previous_result}\n" "1. Address all feedback and suggestions provided by the peer reviewer.\n" "2. Ensure the report is polished and ready for publication.\n" "3. Provide the final version of the scientific report." ), expected_output="Finalized scientific report, ready for publication.", agent=final_report_writer, name="Finalize Report")

现在让我们把所有东西整合起来。我们现在可以创建一个代理和任务的系统并运行它。

ClimateResearchSystem = TheArchitect( agents=[data_collector, data_analyst, report_writer, peer_reviewer, final_report_writer], tasks=[collect_data, analyse_data, write_report, review_report, finalize_report])result = ClimateResearchSystem.process(inputs={ "topic": "Climate Change", "search": "latest climate data trends"})

最终报告,让我们使用Markdown来可视化多智能体系统的输出。关键问题是,与使用ChatGPT(无代理)编写关于气候趋势的科学报告相比,这份报告是否更好?

0 阅读:1

智能科技扫地僧

简介:感谢大家的关注