Type something to search...
轻松打造高效研究助手!AutoGen与Panel UI结合的神奇旅程

轻松打造高效研究助手!AutoGen与Panel UI结合的神奇旅程

🎉 新年快乐,大家好!在开始之前,衷心感谢大家对我们之前故事的支持和反馈——这对我们意义重大!🙌 现在,让我们以一个有趣的项目开始新的一年。在人工智能和数据科学的世界中,打造智能和动态的代理可以为自动化研究工作流程带来重大变化。利用 AutoGenPanel UI,您可以构建一个研究代理,与多个子代理协作,每个子代理都设计用于特定任务,如编码、规划或批评。✨ 让我们深入了解如何设置一个具有流畅和互动用户界面的多代理系统。

什么让这个研究代理变得有趣?

想象一下,将重复性的任务、复杂的工作流程规划或研究过程的评估委托给AI代理。以下是构建这样一个代理的独特之处:

  • 提升生产力:通过自动化日常任务,您可以专注于更高层次的思考。
  • 促进AI与人类的协作:每个代理扮演着独特的角色,将机器的高效与人类的创造力相结合。
  • 简化研究流程:提供结构化、可重复的方法,使复杂的研究变得可管理和高效。

你需要的东西

  • Python 3.9+
  • AutoGen:一个用于创建和管理AI代理的库。
  • Panel UI:用于构建响应式和交互式前端。
  • LLM模型:如OpenAI或本地托管模型如Llama。
  • 对Python和机器学习概念的基本知识。

设置环境

首先,创建一个新的 Python 环境以保持依赖关系的隔离。您可以使用 Conda 或 Python 的 venv 模块:

使用 Conda:

conda create -n research_agent_env python=3.9 -y
conda activate research_agent_env
```python
conda create -n research_agent_env python=3.9 -y
conda activate research_agent_env

使用 venv:

python -m venv research_agent_env
source research_agent_env/bin/activate  
## 在 Windows 上:research_agent_env\Scripts\activate
```python
python -m venv research_agent_env
source research_agent_env/bin/activate  
## 在 Windows 上:research_agent_env\Scripts\activate
然后,安装所需的库:

```python
pip install autogen panel 
```python
pip install autogen panel 

代码逐步讲解

这是创建您的研究代理的逐步指南:

1. 导入库并配置LLM

import autogen
import panel as pn

## Configuration for the LLM model
model_configurations = [
    {
        "model": "llama3.2",
        "base_url": "http://localhost:11434/v1",
        'api_key': 'ollama',
    },
]

llm_settings = {"config_list": model_configurations, "temperature": 0, "seed": 53}
```python
import autogen
import panel as pn

## Configuration for the LLM model
model_configurations = [
    {
        "model": "llama3.2",
        "base_url": "http://localhost:11434/v1",
        'api_key': 'ollama',
    },
]

llm_settings = {"config_list": model_configurations, "temperature": 0, "seed": 53}

这里,config_list 定义了您的LLM的连接详细信息,包括模型、基础URL和API密钥。

2. 定义 AI 代理

每个代理都有不同的角色。例如:

  • Admin: 监督工作流程并批准任务。
  • Engineer: 编写和调试代码。
  • Scientist: 分析研究数据。
  • Planner: 制定可行计划。
  • Executor: 执行代码。
  • Critic: 评估计划和输出。
## Define the UserProxyAgent (Admin)
admin_agent = autogen.UserProxyAgent(
    name="Admin",
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("exit"),
    system_message="""A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin. 
    Only say APPROVED in most cases, and say EXIT when nothing to be done further. Do not say others.""",
    code_execution_config=False,
    default_auto_reply="Approved",
    human_input_mode="NEVER",
    llm_config=llm_settings,
)

## Define the AssistantAgent (Engineer)
engineer_agent = autogen.AssistantAgent(
    name="Engineer",
    llm_config=llm_settings,
    system_message='''Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor.
    Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the executor.
    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.
    ''',
)

## Define the AssistantAgent (Scientist)
scientist_agent = autogen.AssistantAgent(
    name="Scientist",
    llm_config=llm_settings,
    system_message="""Scientist. You follow an approved plan. You are able to categorize papers after seeing their abstracts printed. You don't write code."""
)

## Define the AssistantAgent (Planner)
planner_agent = autogen.AssistantAgent(
    name="Planner",
    system_message='''Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval.
    The plan may involve an engineer who can write code and a scientist who doesn't write code.
    Explain the plan first. Be clear which step is performed by an engineer, and which step is performed by a scientist.
    ''',
    llm_config=llm_settings,
)

## Define the UserProxyAgent (Executor)
executor_agent = autogen.UserProxyAgent(
    name="Executor",
    system_message="Executor. Execute the code written by the engineer and report the result.",
    human_input_mode="NEVER",
    code_execution_config={"last_n_messages": 3, "work_dir": "paper"},
)

## Define the AssistantAgent (Critic)
critic_agent = autogen.AssistantAgent(
    name="Critic",
    system_message="Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.",
    llm_config=llm_settings,
)
```python
## Define the UserProxyAgent (Admin)
admin_agent = autogen.UserProxyAgent(
    name="Admin",
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("exit"),
    system_message="""A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin. 
    Only say APPROVED in most cases, and say EXIT when nothing to be done further. Do not say others.""",
    code_execution_config=False,
    default_auto_reply="Approved",
    human_input_mode="NEVER",
    llm_config=llm_settings,
)

## Define the AssistantAgent (Engineer)
engineer_agent = autogen.AssistantAgent(
    name="Engineer",
    llm_config=llm_settings,
    system_message='''Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor.
    Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the executor.
    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.
    ''',
)

## Define the AssistantAgent (Scientist)
scientist_agent = autogen.AssistantAgent(
    name="Scientist",
    llm_config=llm_settings,
    system_message="""Scientist. You follow an approved plan. You are able to categorize papers after seeing their abstracts printed. You don't write code."""
)

## Define the AssistantAgent (Planner)
planner_agent = autogen.AssistantAgent(
    name="Planner",
    system_message='''Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval.
    The plan may involve an engineer who can write code and a scientist who doesn't write code.
    Explain the plan first. Be clear which step is performed by an engineer, and which step is performed by a scientist.
    ''',
    llm_config=llm_settings,
)

## Define the UserProxyAgent (Executor)
executor_agent = autogen.UserProxyAgent(
    name="Executor",
    system_message="Executor. Execute the code written by the engineer and report the result.",
    human_input_mode="NEVER",
    code_execution_config={"last_n_messages": 3, "work_dir": "paper"},
)

## Define the AssistantAgent (Critic)
critic_agent = autogen.AssistantAgent(
    name="Critic",
    system_message="Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.",
    llm_config=llm_settings,
)

注意: 我在 Docker 容器中运行所有代理生成的代码,并建议您也这样做,这需要您的系统中运行 Docker Desktop。

创建协作的群聊

将所有代理链接到协作聊天界面:

## Create a GroupChat with all agents
group_chat = autogen.GroupChat(agents=[admin_agent, engineer_agent, scientist_agent, planner_agent, executor_agent, critic_agent], messages=[], max_round=50)
chat_manager = autogen.GroupChatManager(groupchat=group_chat, llm_config=llm_settings)
```python
## Create a GroupChat with all agents
group_chat = autogen.GroupChat(agents=[admin_agent, engineer_agent, scientist_agent, planner_agent, executor_agent, critic_agent], messages=[], max_round=50)
chat_manager = autogen.GroupChatManager(groupchat=group_chat, llm_config=llm_settings)

设置面板 UI 和回复处理程序:

def print_messages(recipient, messages, sender, config):
    """
    打印并通过聊天界面将发件人的最新消息发送给收件人。
    参数:
        recipient (object): 包含收件人详细信息的收件人对象。
        messages (list): 消息字典的列表,每个字典包含消息详细信息。
        sender (object): 包含发件人详细信息的发件人对象。
        config (dict): 用于其他设置的配置字典。
    返回:
        tuple: 包含布尔值和 None 的元组。布尔值始终为 False,以确保代理通信流继续。
    备注:
        - 该函数打印最新消息的详细信息。
        - 如果最新消息包含键 'name',则使用消息中的名称和头像发送消息。
        - 如果缺少 'name' 键,则使用默认用户 'SecretGuy' 和忍者头像发送消息。
    """
    print(f"Messages from: {sender.name} sent to: {recipient.name} | num messages: {len(messages)} | message: {messages[-1]}")
  
    if all(key in messages[-1] for key in ['name']):
        chat_interface.send(messages[-1]['content'], user=messages[-1]['name'], avatar=agent_avatars[messages[-1]['name']], respond=False)
    else:
        chat_interface.send(messages[-1]['content'], user='SecretGuy', avatar='🥷', respond=False)

    return False, None  # required to ensure the agent communication flow continues
```python
def print_messages(recipient, messages, sender, config):
    """
    打印并通过聊天界面将发件人的最新消息发送给收件人。
    参数:
        recipient (object): 包含收件人详细信息的收件人对象。
        messages (list): 消息字典的列表,每个字典包含消息详细信息。
        sender (object): 包含发件人详细信息的发件人对象。
        config (dict): 用于其他设置的配置字典。
    返回:
        tuple: 包含布尔值和 None 的元组。布尔值始终为 False,以确保代理通信流继续。
    备注:
        - 该函数打印最新消息的详细信息。
        - 如果最新消息包含键 'name',则使用消息中的名称和头像发送消息。
        - 如果缺少 'name' 键,则使用默认用户 'SecretGuy' 和忍者头像发送消息。
    """
    print(f"Messages from: {sender.name} sent to: {recipient.name} | num messages: {len(messages)} | message: {messages[-1]}")
  
    if all(key in messages[-1] for key in ['name']):
        chat_interface.send(messages[-1]['content'], user=messages[-1]['name'], avatar=agent_avatars[messages[-1]['name']], respond=False)
    else:
        chat_interface.send(messages[-1]['content'], user='SecretGuy', avatar='🥷', respond=False)

    return False, None  # required to ensure the agent communication flow continues

## 将 print_messages 函数注册为每个代理的回复处理程序
admin_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
)

engineer_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
scientist_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
planner_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
)

executor_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
critic_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
```python
## 将 print_messages 函数注册为每个代理的回复处理程序
admin_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
)

engineer_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
scientist_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
planner_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
)

executor_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 
critic_agent.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 

## 使用材料设计初始化面板扩展
pn.extension(design="material")

def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    # 与 admin_agent 启动聊天
    admin_agent.initiate_chat(chat_manager, message=contents)

## 创建聊天界面并发送初始消息
chat_interface = pn.chat.ChatInterface(callback=callback)
chat_interface.send("发送一条消息!", user="系统", respond=False)
chat_interface.servable()
```python
## 使用材料设计初始化面板扩展
pn.extension(design="material")

def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    # 与 admin_agent 启动聊天
    admin_agent.initiate_chat(chat_manager, message=contents)

## 创建聊天界面并发送初始消息
chat_interface = pn.chat.ChatInterface(callback=callback)
chat_interface.send("发送一条消息!", user="系统", respond=False)
chat_interface.servable()

整合所有内容

一旦设置完成,您的研究代理就准备好处理工作流程。系统将任务分配给适当的代理,确保:

  • 无缝的任务执行。
  • 全面的计划批评和优化。
  • 高效的研究输出。

运行设置:

panel serve .\autogen_panel_example.py
```python
panel serve .\autogen_panel_example.py

此设置的应用

  • 学术研究:自动化文献综述和编码实验。
  • 商业智能:分析报告并生成洞察。
  • 数据工程:调试管道和处理大型数据集。

最后的思考

随着我们迈入全新的一年,愿我们拥抱创新与创造力!借助 AutoGen 和 Panel UI 等工具,构建 AI 系统变得既简单又强大。这种架构不仅简化了工作流程,还促进了人类与 AI 代理之间的有意义合作。我们期待看到这些工具如何在 2025 年改变你的研究旅程。🚀

感谢您的阅读,衷心祝大家新年快乐!祝愿您在所有的努力中繁荣、幸福与成功。🌟

其他资源:

完整代码:https://github.com/imanoop7/AutoGen-notebooks

Related Posts

结合chatgpt-o3-mini与perplexity Deep Research的3步提示:提升论文写作质量的终极指南

结合chatgpt-o3-mini与perplexity Deep Research的3步提示:提升论文写作质量的终极指南

AI 研究报告和论文写作 合并两个系统指令以获得两个模型的最佳效果 Perplexity AI 的 Deep Research 工具提供专家级的研究报告,而 OpenAI 的 ChatGPT-o3-mini-high 擅长推理。我发现你可以将它们结合起来生成令人难以置信的论文,这些论文比任何一个模型单独撰写的都要好。你只需要将这个一次性提示复制到 **

阅读更多
让 Excel 过时的 10 种 Ai 工具:实现数据分析自动化,节省手工作业时间

让 Excel 过时的 10 种 Ai 工具:实现数据分析自动化,节省手工作业时间

Non members click here作为一名软件开发人员,多年来的一个发现总是让我感到惊讶,那就是人们还在 Excel

阅读更多
使用 ChatGPT 搜索网络功能的 10 种创意方法

使用 ChatGPT 搜索网络功能的 10 种创意方法

例如,提示和输出 你知道可以使用 ChatGPT 的“搜索网络”功能来完成许多任务,而不仅仅是基本的网络搜索吗? 对于那些不知道的人,ChatGPT 新的“搜索网络”功能提供实时信息。 截至撰写此帖时,该功能仅对使用 ChatGPT 4o 和 4o-mini 的付费会员开放。 ![](https://images.weserv.nl/?url=https://cdn-im

阅读更多
掌握Ai代理:解密Google革命性白皮书的10个关键问题解答

掌握Ai代理:解密Google革命性白皮书的10个关键问题解答

10 个常见问题解答 本文是我推出的一个名为“10 个常见问题解答”的新系列的一部分。在本系列中,我旨在通过回答关于该主题的十个最常见问题来分解复杂的概念。我的目标是使用简单的语言和相关的类比,使这些想法易于理解。 图片来自 [Solen Feyissa](https://unsplash.com/@solenfeyissa?utm_source=medium&utm_medi

阅读更多
在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和科技这样一个动态的行业中,保持领先意味着不断提升你的技能。无论你是希望深入了解人工智能模型性能、掌握数据分析,还是希望通过人工智能转变传统领域如法律,这些课程都是你成功的捷径。以下是一个精心策划的高价值课程列表,可以助力你的职业发展,并让你始终处于创新的前沿。 1. 生成性人工智能简介课程: [生成性人工智能简介](https://genai.works

阅读更多
揭开真相!深度探悉DeepSeek AI的十大误区,您被误导了吗?

揭开真相!深度探悉DeepSeek AI的十大误区,您被误导了吗?

在AI军备竞赛中分辨事实与虚构 DeepSeek AI真的是它所宣传的游戏规则改变者,还是仅仅聪明的营销和战略炒作?👀 虽然一些人将其视为AI效率的革命性飞跃,但另一些人则认为它的成功建立在借用(甚至窃取的)创新和可疑的做法之上。传言称,DeepSeek的首席执行官在疫情期间像囤积卫生纸一样囤积Nvidia芯片——这只是冰山一角。 从其声称的550万美元培训预算到使用Open

阅读更多
Type something to search...