介绍
大家好,这次给大家分享的内容是微软AutoGen框架的核心功能Human-in-the-Loop(简称:人机交互)。那么这个又是什么东西呢,我们直接进入正题。
Human-in-the-Loop
在我的微软AutoGen介绍——Teams与智能体团队协作并使用-CSDN博客这篇博文中,我们已经了解了如何创建、观察和控制智能体团队。本次分享将重点介绍如何从我们的应用程序与团队进行交互,以及如何向团队提供人工反馈。
从我们的应用程序与团队进行交互主要有两种方式:
- 在团队运行期间(即run()或run_stream()执行过程中),通过用户代理智能体(UserProxyAgent)提供反馈。
- 一旦运行结束,通过对下一次run()或run_stream()调用的输入来提供反馈。
运行期间提供反馈
用户代理智能体(UserProxyAgent)是一种特殊的内置智能体,充当用户向团队提供反馈的智能体。
要使用用户代理智能体,我们可以创建它的一个实例,并在运行团队之前将其包含在团队中。团队会决定何时调用用户代理智能体,以向用户征求反馈。
以下图表说明了在团队运行期间,如何使用用户代理智能体从用户那里获取反馈:
加粗箭头表示团队运行期间的控制流:当团队调用用户代理智能体时,它将控制权转移给应用程序/用户,并等待反馈;一旦提供反馈,控制权就会转回给团队,团队继续执行。
由于这种方法具有阻塞性,建议仅将其用于需要用户即时反馈的简短交互场景,比如通过点击按钮来征求用户同意或不同意,或者弹出一个需立即关注的提醒,否则任务就会失败。
以下是在循环轮询小组聊天(RoundRobinGroupChat)中,针对诗歌创作任务使用用户代理智能体(UserProxyAgent)的示例:
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
from autogen_agentchat.conditions import TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
# 创建智能体。
model_client = OpenAIChatCompletionClient(model="gpt-3.5-turbo")
assistant = AssistantAgent("assistant", model_client=model_client)
# 使用input()从控制台获取用户输入.
user_proxy = UserProxyAgent("user_proxy", input_func=input)
# 创建终止条件,当用户说 “APPROVE” 时结束对话。
termination = TextMentionTermination("APPROVE")
# 创建团队。
team = RoundRobinGroupChat([assistant, user_proxy], termination_condition=termination)
async def main() -> None:
# 运行对话并将内容输出到控制台。
stream = team.run_stream(task="Write a 4-line poem about the ocean.")
await Console(stream)
asyncio.run(main())
运行结果
---------- user ----------
Write a 4-line poem about the ocean.
----