{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Selector Group Chat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 选择器群聊\n", "\n", "`SelectorGroupChat` 实现了一种团队协调模式,参与者轮流发布消息,下一个发言者由生成式模型(LLM)根据对话上下文选择。这实现了动态的、上下文感知的多代理对话。\n", "\n", "`SelectorGroupChat` 提供了几个关键特性:\n", "- 使用 LLM 分析对话上下文来动态选择发言者\n", "- 可配置的参与者角色和描述\n", "- 可选的防止同一发言者连续发言\n", "- 可自定义的选择提示\n", "\n", "### 发言者选择过程\n", "\n", "聊天使用 LLM 通过以下方式选择下一个发言者:\n", "1. 分析对话历史\n", "2. 评估参与者角色和描述\n", "3. 使用可配置的提示模板进行选择\n", "4. 验证是否恰好选择了一个参与者" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "from typing import List, Sequence\n", "\n", "from autogen_agentchat.agents import (\n", " BaseChatAgent,\n", " CodingAssistantAgent,\n", " ToolUseAssistantAgent,\n", ")\n", "from autogen_agentchat.base import Response\n", "from autogen_agentchat.messages import ChatMessage, StopMessage, TextMessage\n", "from autogen_agentchat.task import TextMentionTermination\n", "from autogen_agentchat.teams import SelectorGroupChat\n", "from autogen_core.base import CancellationToken\n", "from autogen_core.components.tools import FunctionTool\n", "from autogen_ext.models import OpenAIChatCompletionClient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 定义代理\n", "`UserProxyAgent` 允许用户直接输入消息。该代理等待用户输入,并返回文本消息,如果用户决定终止对话,则返回停止消息。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class UserProxyAgent(BaseChatAgent):\n", " def __init__(self, name: str) -> None:\n", " super().__init__(name, \"A human user.\")\n", "\n", " @property\n", " def produced_message_types(self) -> List[type[ChatMessage]]:\n", " return [TextMessage, StopMessage]\n", "\n", " async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> Response:\n", " user_input = await asyncio.get_event_loop().run_in_executor(None, input, \"Enter your response: \")\n", " if \"TERMINATE\" in user_input:\n", " return Response(chat_message=StopMessage(content=\"User has terminated the conversation.\", source=self.name))\n", " return Response(chat_message=TextMessage(content=user_input, source=self.name))\n", "\n", " async def on_reset(self, cancellation_token: CancellationToken) -> None:\n", " pass" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "async def flight_search(start: str, destination: str, date: str) -> str:\n", " return \"\\n\".join(\n", " [\n", " f\"AC24 from {start} to {destination} on {date} is $500\",\n", " f\"UA23 from {start} to {destination} on {date} is $450\",\n", " f\"AL21 from {start} to {destination} on {date} is $400\",\n", " ]\n", " )\n", "\n", "\n", "async def flight_booking(flight: str, date: str) -> str:\n", " return f\"Booked flight {flight} on {date}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ToolUseAssistantAgent` 负责调用外部工具。在这个例子中,定义了两个工具:`flight_search` 和 `flight_booking`。\n", "\n", "此外,`CodingAssistantAgent` 作为一个通用的旅行助手,其行为在 `system_message` 中预先定义。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "user_proxy = UserProxyAgent(\"User\")\n", "flight_broker = ToolUseAssistantAgent(\n", " \"FlightBroker\",\n", " description=\"An assistant for booking flights\",\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\"),\n", " registered_tools=[\n", " FunctionTool(flight_search, description=\"Search for flights\"),\n", " FunctionTool(flight_booking, description=\"Book a flight\"),\n", " ],\n", ")\n", "travel_assistant = CodingAssistantAgent(\n", " \"TravelAssistant\",\n", " description=\"A travel assistant\",\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\"),\n", " system_message=\"You are a travel assistant.\",\n", ")\n", "\n", "termination = TextMentionTermination(\"TERMINATE\")\n", "team = SelectorGroupChat(\n", " [user_proxy, flight_broker, travel_assistant],\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\"),\n", " termination_condition=termination,\n", ")\n", "await team.run(task=\"Help user plan a trip and book a flight.\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }