具备反思功能的超级写作助手构建指南,使用AutoGen

具备反思功能的超级写作助手构建指南,使用AutoGen

环境搭建

依赖安装

首先,我们需要安装必要的 Python 库。可以使用 pip 命令进行安装:

pip install pyautogen matplotlib numpy

其中:

  • pyautogen 是用于构建自动生成文本代理的库。
  • matplotlibnumpy 用于数据可视化和处理,可能在后续步骤中用到。

代码结构概述

代码示例中展示了一个简单的超级写作助手框架,包含两个代理:

  1. Writer 代理: 负责根据用户提供的任务生成文本内容。其系统提示设定了明确的写作风格和要求,例如要写出简洁、吸引人的博客文章,并控制字数在 100 字以内。
  2. Critic 代理: 负责对 Writer 代理生成的文本进行评价和反馈。其系统提示要求以中文提供建设性的意见,帮助 Writer 代理改进内容质量。

这两个代理通过 initiate_chat 函数进行交互,Critic 代理会根据 Writer 代理的输出提供反馈,Writer 代理则会根据反馈修改文本并重新生成。

写作代理创建

任务定义

写作代理的任务是根据用户提供的主题或指令生成文本内容。在示例代码中,任务被定义为:

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

这段代码定义了一个关于 DeepLearning.AI 的博客文章写作任务,要求文章简洁、引人入胜,并且字数控制在 100 字以内。

代理配置

写作代理的配置包括代理名称、系统提示信息以及语言模型配置等参数。

  • name: 代理的名称,例如 “Writer”。
  • system_message: 系统提示信息,用于引导代理完成特定任务。示例代码中的系统提示信息如下:
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. Write in chinese."""

这段系统提示信息告诉代理,它是一个作家,需要撰写引人入胜且简洁的博客文章,并根据反馈进行修改。最后,代理只需要返回最终的写作结果,不需要额外的注释。

  • llm_config: 语言模型配置,包含了语言模型的类型、参数等信息。

生成文本

使用 generate_reply 方法,将用户定义的任务传递给写作代理,即可生成相应的文本内容。示例代码如下:

reply = writer.generate_reply(messages=[{"content": task, "role": "user"}])
print(reply)

这段代码首先将任务封装成一个消息对象,然后传递给 generate_reply 方法进行处理。最后,打印出代理生成的文本内容。

评论代理创建

反馈机制设计

评论代理的核心功能是提供针对写作代理生成内容的反馈,引导其不断改进。这需要设计一个有效的反馈机制,确保反馈信息能够有效地帮助写作代理提升文本质量。

在代码示例中,critic 代理的 system_message 被设定为:

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. Review in chinese."""

这表明评论代理的角色是提供建设性的反馈,帮助写作代理改进内容质量。

代理配置

critic 代理的配置主要包括以下几个方面:

  • name: 代理名称,设置为 “Critic”。
  • is_termination_msg: 一个 lambda 函数,用于判断对话是否终止。在这里,如果消息内容包含 “TERMINATE”,则对话终止。
  • llm_config: 语言模型配置,用于指定使用的语言模型和相关参数。
  • system_message: 代理的系统提示信息,指导其如何执行任务。

迭代改进

评论代理通过与写作代理进行多轮对话来实现迭代改进。在 critic.initiate_chat 函数中,设置了 max_turns=6,表示对话最多进行 6 轮。每轮对话中,评论代理会根据写作代理生成的内容提供反馈,写作代理则会根据反馈修改其生成的文本。

通过这种迭代过程,写作代理能够不断学习和改进,最终生成高质量的文本内容。

案例分析

深度学习博客文章撰写

本例演示如何利用 autogen 库构建一个“超级写作助手”,它能够自动生成一篇关于 DeepLearning.AI 的博客文章,并通过评论代理进行反思和优化。

步骤:

  1. 创建书写代理:
    • 定义 Writer 代理,其角色是根据用户提供的主题撰写博客文章。
    • 设置系统提示,引导 Writer 生成简洁、引人入胜的博客文章,并确保最终输出结果为经过修改的版本。
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. Write in chinese.",
    llm_config=llm_config, 
)
  1. 生成初始文章:
    • 将主题 “Write a concise but engaging blogpost about DeepLearning.AI. Make sure the blogpost is within 100 words.” 传递给 Writer 代理。
    • 获取 Writer 生成的初始博客文章。
task = '''
Write a concise but engaging blogpost about DeepLearning.AI. Make sure the blogpost is within 100 words.
'''
reply = writer.generate_reply(messages=[{"content": task, "role": "user"}])
print(reply)
  1. 创建评论代理:
    • 定义 Critic 代理,其角色是评估 Writer 生成的文章并提供反馈。
    • 设置系统提示,引导 Critic 以中文提供建设性的评论,帮助提高文章质量。
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. Review in chinese.",
)
  1. 启动评论和优化循环:
    • 使用 critic.initiate_chat 启动 CriticWriter 之间的对话。
    • 将主题传递给 Critic,并设置最大对话轮数为 6。
    • 通过 summary_method="last_msg" 获取最后一次对话结果作为最终输出。
res = critic.initiate_chat(recipient=writer, message=task, max_turns=6, summary_method="last_msg")

反思与优化

通过评论代理的反馈,Writer 可以不断改进其生成的博客文章。例如,Critic 可能指出文章缺乏具体例子或论证不够充分,Writer 则可以根据这些反馈修改文章内容,最终生成更优质的作品。

这种基于反思和优化的机制使得“超级写作助手”能够持续学习和进步,不断提高其写作质量。

完整代码

# pip install pyautogen
# ! pip install -qqq matplotlib numpy

from utils.llm_config import ConfigManager

config_man = ConfigManager()
llm_config = config_man.get_json_config()
## 创建一个书写代理
import autogen

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

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. Write in chinese.",
    llm_config=llm_config,
)

reply = writer.generate_reply(messages=[{"content": task, "role": "user"}])
print(reply)
## 创建一个评论代理
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. Review in chinese.",
)

res = critic.initiate_chat(
    recipient=writer,
    message=task,
    max_turns=6,
    summary_method="last_msg"
)
  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AutoGen Studio是一款用于嵌入式系统开发的集成开发环境(IDE),它提供了丰富的工具和功能来简化嵌入式开发过程。以下是使用AutoGen Studio的基本步骤: 1. 下载和安装:从AutoGen Studio官方网站下载最新版本的软件,并按照安装向导进行安装。 2. 创建项目:打开AutoGen Studio,在菜单栏中选择“File(文件)”->“New Project(新建项目)”来创建一个新项目。根据你的需求选择项目类型,比如C/C++项目或者嵌入式项目。 3. 配置目标平台:在创建项目时,你需要选择目标平台的硬件架构和操作系统类型。根据你的目标平台选择合适的配置。 4. 编写代码:在AutoGen Studio中,你可以使用内置的文本编辑器来编写代码。创建源文件、头文件等,并编写你的应用程序逻辑。 5. 构建项目:在菜单栏中选择“Build(构建)”->“Build Project(构建项目)”来编译你的项目。如果有错误,需要解决这些错误,直到项目成功编译通过。 6. 调试和测试:AutoGen Studio提供了强大的调试功能,可以帮助你调试和测试你的嵌入式应用程序。使用调试器来设置断点、监视变量和跟踪程序执行流程。 7. 部署和运行:完成调试后,你可以将生成的可执行文件部署到目标硬件上运行。根据目标平台的要求,将可执行文件烧录到硬件设备中,并验证应用程序的功能。 除了以上基本步骤外,AutoGen Studio还提供了其他功能,如版本控制、代码自动完成、代码生成器等,可以根据你的需求进一步探索和使用。 此外,AutoGen Studio官方网站上提供了详细的使用教程和文档,你可以参考这些资源来深入了解和学习AutoGen Studio的各种功能和用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值