Skip to main content

恢复一个群聊

在 Colab 中打开 在 GitHub 上打开

在 GroupChat 中,我们可以通过将之前对话的消息传递给 GroupChatManager 的 resume 函数(或者异步工作流中的 a_resume 函数)来恢复一个之前的群聊。这将准备好 GroupChat、GroupChatManager 和群聊的代理以进行恢复。然后可以调用代理的 initiate_chat 来恢复聊天。

resume 函数会返回消息中的最后一个代理以及最后一条消息。这些可以用于运行 initiate_chat

要进行恢复,代理、GroupChat 和 GroupChatManager 对象必须存在并与原始群聊匹配。

传递给 resume 函数的消息可以作为 JSON 字符串或消息的 List[Dict] 形式传递,通常来自先前对话的 ChatResult 的 chat_history 或 GroupChat 的 messages 属性。使用 GroupChatManager 的 messages_to_string 函数检索可用于恢复的 JSON 字符串:

# 使用聊天历史记录保存聊天消息以便以后恢复
messages_json = mygroupchatmanager.messages_to_string(previous_chat_result.chat_history)

# 或者您可以使用 GroupChat 的 messages 属性
messages_json = mygroupchatmanager.messages_to_string(mygroupchatmanager.groupchat.messages)

JSON 字符串的示例:

[{"content": "在arXiv上找到关于GPT-4的最新论文,并找出它在软件领域的潜在应用。", "role": "user", "name": "管理员"}, {"content": "计划:\n1. **工程师**:在arXiv上搜索关于GPT-4的最新论文。\n2. **科学家**:阅读论文并总结GPT-4的关键发现和潜在应用。\n3. **工程师**:根据科学家的总结,确定GPT-4可以在哪些软件应用中使用。\n4. **科学家**:就在已确定的软件应用中实施GPT-4的可行性和影响提供见解。\n5. **工程师**:开发原型或概念验证,展示如何将GPT-4集成到选定的软件应用中。\n6. **科学家**:评估原型,提供反馈,并提出任何改进或修改建议。\n7. **工程师**:根据科学家的反馈进行必要的修订,并最终将GPT-4集成到软件应用中。\n8. **管理员**:审查具有GPT-4集成的最终软件应用,并批准进一步的开发或实施。\n\n需要管理员和评论者的反馈以进一步完善计划。", "role": "user", "name": "规划者"}, {"content": "同意", "role": "user", "name": "管理员"}, {"content": "太好了!让我们按照之前制定的计划继续进行。我将开始在arXiv上搜索关于GPT-4的最新论文。一旦我找到论文,科学家将总结GPT-4的关键发现和潜在应用。然后,我们将按照计划的其余步骤进行。我会及时向您更新我们的进展。", "role": "user", "name": "规划者"}]

在准备恢复对话时,将根据群聊的代理进行消息验证,以确保消息可以分配给它们。消息将被分配给代理,然后最后一个发言者和消息将被返回以供在 initiate_chat 中使用。

继续终止的对话

如果之前的群聊终止,并且恢复的群聊具有相同的终止条件(例如,如果消息包含“TERMINATE”),那么当恢复时,对话将终止,因为终止检查会使用传递给 initiate_chat 的消息进行。

如果终止条件是基于消息中的字符串,您可以将该字符串传递给 resume 函数的 remove_termination_string 参数,并将其删除。如果终止条件更复杂,您需要在调用 resume 之前相应地调整消息。

然后,resume 函数将检查提供的最后一条消息是否仍满足终止条件,并在如此的情况下发出警告。

恢复 GroupChat 的示例

从 LLM 配置开始。这可能与原始群聊不同。

import os

import autogen

# 将您的 API 密钥放入环境变量 OPENAI_API_KEY
config_list = [
{
"model": "gpt-4-0125-preview",
"api_key": os.environ["OPENAI_API_KEY"],
}
]

gpt4_config = {
"cache_seed": 42, # 更改 cache_seed 以进行不同的试验
"temperature": 0,
"config_list": config_list,
"timeout": 120,
}
/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm

创建群聊对象,它们的 name 应与原始群聊相同。

# 根据原始群聊创建代理人、群聊和群聊管理器

planner = autogen.AssistantAgent(
name="规划者",
system_message="""规划者。提出一个计划。根据管理员和评论家的反馈修改计划,直到管理员批准。
计划可能涉及一个会写代码的工程师和一个不会写代码的科学家。
首先解释计划。清楚地说明哪个步骤由工程师执行,哪个步骤由科学家执行。
""",
llm_config=gpt4_config,
)

user_proxy = autogen.UserProxyAgent(
name="管理员",
system_message="一个人类管理员。与规划者交流讨论计划。计划执行需要得到管理员的批准。",
code_execution_config=False,
)

engineer = autogen.AssistantAgent(
name="工程师",
llm_config=gpt4_config,
system_message="""工程师。你按照批准的计划行动。你编写 Python/Shell 代码来解决任务。将代码放在代码块中,指定脚本类型。用户无法修改你的代码。因此,不要提供需要其他人修改的不完整代码。如果不打算由执行者执行,请不要使用代码块。
一个回复中不要包含多个代码块。不要要求其他人复制粘贴结果。检查执行者返回的执行结果。
如果结果表明有错误,请修复错误并重新输出代码。建议提供完整的代码而不是部分代码或代码更改。如果错误无法修复,或者即使成功执行代码后任务仍未解决,请分析问题,重新审视假设,收集所需的额外信息,并考虑尝试不同的方法。
""",
)
scientist = autogen.AssistantAgent(
name="科学家",
llm_config=gpt4_config,
system_message="""科学家。你按照批准的计划行动。你能够根据打印出来的摘要对论文进行分类。你不会写代码。
""",
)

executor = autogen.UserProxyAgent(
name="执行者",
system_message="执行者。执行工程师编写的代码并报告结果。",
human_input_mode="NEVER",
code_execution_config={
"last_n_messages": 3,
"work_dir": "paper",
"use_docker": False,
}, # 如果有可用的 Docker 来运行生成的代码,请将 use_docker 设置为 True。使用 Docker 比直接运行生成的代码更安全。
)

groupchat = autogen.GroupChat(
agents=[user_proxy, engineer, scientist, planner, executor],
messages=[],
max_round=10,
)

manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_config)

加载之前的消息(从 JSON 字符串或消息 List[Dict]

# 以 JSON 字符串形式的消息
previous_state = r"""[{"content": "在 arxiv 上找到关于 GPT-4 的最新论文,并找出它在软件领域的潜在应用。", "role": "user", "name": "管理员"}, {"content": "计划:\n1. **工程师**:在 arXiv 上搜索关于 GPT-4 的最新论文。\n2. **科学家**:阅读论文并总结 GPT-4 的关键发现和潜在应用。\n3. **工程师**:根据科学家的总结,确定 GPT-4 可以应用于哪些软件领域。\n4. **科学家**:就在已确定的软件领域中实施 GPT-4 的可行性和影响提供见解。\n5. **工程师**:开发原型或概念验证,展示如何将 GPT-4 集成到选定的软件应用中。\n6. **科学家**:评估原型,提供反馈,并提出任何改进或修改建议。\n7. **工程师**:根据科学家的反馈进行必要的修订,并完成 GPT-4 在软件应用中的集成。\n8. **管理员**:审查集成了 GPT-4 的最终软件应用,并批准进一步的开发或实施。\n\n需要管理员和评论者的反馈以进一步完善计划。", "role": "user", "name": "规划者"}, {"content": "同意", "role": "user", "name": "管理员"}, {"content": "太好了!让我们按照之前制定的计划继续进行。我将开始在 arXiv 上搜索关于 GPT-4 的最新论文。找到论文后,科学家将总结 GPT-4 的关键发现和潜在应用。然后,我们将按照计划的其余步骤进行。我会随时向您更新我们的进展。", "role": "user", "name": "规划者"}]"""

使用最后一个代理人和最后一条消息恢复群聊。

# 准备恢复群聊
last_agent, last_message = manager.resume(messages=previous_state)

# 使用最后一个代理人和消息恢复群聊
result = last_agent.initiate_chat(recipient=manager, message=last_message, clear_history=False)
准备好了包含 4 条消息的群聊,最后发言者是规划者
规划者 (发给 chat_manager):

太好了!让我们按照之前制定的计划继续进行。我将开始在 arXiv 上搜索关于 GPT-4 的最新论文。找到论文后,科学家将总结 GPT-4 的关键发现和潜在应用。然后,我们将按照计划的其余步骤进行。我会随时向您更新我们的进展。

--------------------------------------------------------------------------------
工程师 (发给 chat_manager):

```python
import requests
from bs4 import BeautifulSoup

# 定义 arXiv 搜索的 URL
url = "https://arxiv.org/search/?query=GPT-4&searchtype=all&source=header"

# 发送 GET 请求到 URL
response = requests.get(url)

# 解析页面的 HTML 内容
soup = BeautifulSoup(response.content, 'html.parser')
```
# 寻找与GPT-4相关的第一篇论文
paper = soup.find('li', class_='arxiv-result')
if paper:
title = paper.find('p', class_='title').text.strip()
authors = paper.find('p', class_='authors').text.strip()
abstract = paper.find('p', class_='abstract').text.strip().replace('\n', ' ')
link = paper.find('p', class_='list-title').find('a')['href']
print(f"标题: {title}\n作者: {authors}\n摘要: {abstract}\n链接: {link}")
else:
print("在arXiv上没有找到GPT-4的论文。")
```
This script searches for the latest paper on GPT-4 on arXiv, extracts the title, authors, abstract, and link to the paper, and prints this information.

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
Executor (to chat_manager):

exitcode: 0 (execution succeeded)
Code output:
Title: Smurfs: Leveraging Multiple Proficiency Agents with Context-Efficiency for Tool Planning
Authors: Authors:
Junzhi Chen,

Juhao Liang,

Benyou Wang
Abstract: Abstract: …scenarios. Notably, Smurfs outmatches the ChatGPT-ReACT in the ToolBench I2 and I3 benchmark with a remarkable 84.4% win rate, surpassing the highest recorded performance of a GPT-4 model at 73.5%. Furthermore, through comprehensive ablation studies, we dissect the contribution of the core components of the multi-agent… ▽ More The emergence of large language models (LLMs) has opened up unprecedented possibilities for automating complex tasks that are often comparable to human performance. Despite their capabilities, LLMs still encounter difficulties in completing tasks that require high levels of accuracy and complexity due to their inherent limitations in handling multifaceted problems single-handedly. This paper introduces "Smurfs", a cutting-edge multi-agent framework designed to revolutionize the application of LLMs. By transforming a conventional LLM into a synergistic multi-agent ensemble, Smurfs enhances task decomposition and execution without necessitating extra training. This is achieved through innovative prompting strategies that allocate distinct roles within the model, thereby facilitating collaboration among specialized agents. The framework gives access to external tools to efficiently solve complex tasks. Our empirical investigation, featuring the mistral-7b-instruct model as a case study, showcases Smurfs' superior capability in intricate tool utilization scenarios. Notably, Smurfs outmatches the ChatGPT-ReACT in the ToolBench I2 and I3 benchmark with a remarkable 84.4% win rate, surpassing the highest recorded performance of a GPT-4 model at 73.5%. Furthermore, through comprehensive ablation studies, we dissect the contribution of the core components of the multi-agent framework to its overall efficacy. This not only verifies the effectiveness of the framework, but also sets a route for future exploration of multi-agent LLM systems. △ Less
Link: https://arxiv.org/abs/2405.05955


--------------------------------------------------------------------------------
Scientist (to chat_manager):

Based on the abstract of the paper titled "Smurfs: Leveraging Multiple Proficiency Agents with Context-Efficiency for Tool Planning," the key findings and potential applications of GPT-4 can be summarized as follows:

### Key Findings:
- The paper introduces "Smurfs," a multi-agent framework that enhances the capabilities of large language models (LLMs) like GPT-4 by transforming them into a synergistic multi-agent ensemble. This approach allows for better task decomposition and execution without additional training.
- Smurfs utilize innovative prompting strategies to allocate distinct roles within the model, facilitating collaboration among specialized agents and giving access to external tools for solving complex tasks.
- In the ToolBench I2 and I3 benchmark, Smurfs outperformed ChatGPT-ReACT with an 84.4% win rate, surpassing the highest recorded performance of a GPT-4 model at 73.5%.
- Comprehensive ablation studies were conducted to understand the contribution of the core components of the multi-agent framework to its overall efficacy.

### Potential Applications in Software:
- **Tool Planning and Automation**: Smurfs can be applied to software that requires complex tool planning and automation, enhancing the software's ability to perform tasks that involve multiple steps or require the use of external tools.
- **Collaborative Systems**: The multi-agent ensemble approach can be utilized in developing collaborative systems where different components or agents work together to complete tasks more efficiently than a single agent could.
- **Enhanced Problem-Solving**: Software that involves complex problem-solving can benefit from Smurfs by leveraging the specialized capabilities of different agents within the ensemble, leading to more accurate and efficient solutions.
- **Task Decomposition**: Applications that require breaking down complex tasks into simpler sub-tasks can use the Smurfs framework to improve task decomposition and execution, potentially leading to better performance and outcomes.

The integration of GPT-4 with the Smurfs framework presents a novel approach to enhancing the capabilities of LLMs in software applications, particularly in areas that require complex task planning, execution, and problem-solving.

--------------------------------------------------------------------------------
Engineer (to chat_manager):

Given the scientist's summary on the potential applications of GPT-4 as enhanced by the Smurfs framework, we can identify several software applications where GPT-4 can be utilized effectively:

1. **Project Management Tools**: Integration of GPT-4 with Smurfs can revolutionize project management software by automating complex planning tasks, optimizing resource allocation, and providing actionable insights for project execution.

2. **Code Generation and Software Development**: Leveraging GPT-4 in IDEs (Integrated Development Environments) or other software development tools can enhance code generation capabilities, provide context-aware suggestions, and automate debugging processes.

3. **Customer Support and Chatbots**: GPT-4 can be used to power advanced customer support chatbots that understand complex queries, provide accurate information, and automate problem-solving for customer issues.

4. **Educational Platforms**: In educational software, GPT-4 can personalize learning experiences, automate content generation, and provide interactive tutoring services.

5. **Healthcare Applications**: GPT-4 can assist in healthcare applications by analyzing medical data, providing diagnostic support, and offering personalized healthcare advice.

6. **Creative Writing and Content Generation**: Software tools for creative writing and content generation can benefit from GPT-4's capabilities to produce original content, assist in storytelling, and generate ideas.

7. **Business Intelligence and Analytics**: GPT-4 can enhance business intelligence software by automating data analysis, generating reports, and providing insights based on large datasets.

8. **Security and Threat Analysis**: In cybersecurity applications, GPT-4 can be used to analyze threats, automate security protocols, and provide recommendations for threat mitigation.

These applications demonstrate the versatility and potential impact of integrating GPT-4 into various software solutions, offering opportunities for automation, enhanced efficiency, and improved user experiences across different domains.

--------------------------------------------------------------------------------
Admin (to chat_manager):

Approve

--------------------------------------------------------------------------------
Scientist (to chat_manager):

With the approval from the admin, the plan to explore and integrate GPT-4 into various software applications, leveraging its enhanced capabilities through the Smurfs framework, is set to proceed. This initiative promises to bring significant advancements in automation, efficiency, and user experience across a wide range of software applications, from project management tools to healthcare applications. The next steps involve detailed planning and execution for the development of prototypes or proof of concepts for selected applications, followed by evaluation, feedback, and finalization of GPT-4 integration into these software solutions.

--------------------------------------------------------------------------------
Engineer (to chat_manager):

Given the approval and the insights provided, the next steps involve detailed planning for the development and integration of GPT-4 into selected software applications. This process will include:

1. **Selection of Specific Applications**: Based on the potential applications identified, select one or more specific software applications for prototype development. This selection will be based on factors such as feasibility, potential impact, and available resources.

2. **Prototype Development**: Develop a prototype or proof of concept for the selected application(s). This will involve designing the architecture, integrating GPT-4 with the Smurfs framework, and implementing the necessary functionalities to demonstrate the application of GPT-4 in the software.

3. **Evaluation and Feedback**: Once the prototype is developed, it will be evaluated to assess its performance, usability, and effectiveness in leveraging GPT-4's capabilities. Feedback will be gathered from potential users and stakeholders to identify areas for improvement.

4. **Revisions and Finalization**: Based on the feedback received, necessary revisions and improvements will be made to the prototype. This step may involve refining the integration of GPT-4, optimizing the software's performance, and enhancing user experience.

5. **Implementation and Deployment**: After finalizing the prototype, the next step will involve planning for the full-scale implementation and deployment of the software application with GPT-4 integration. This will include addressing any scalability, security, and maintenance considerations.

6. **Continuous Improvement**: Post-deployment, it will be important to monitor the software's performance and user feedback continuously. This will enable ongoing improvements and updates to ensure that the software remains effective and relevant.

This structured approach will ensure that the integration of GPT-4 into software applications is carried out effectively, leading to innovative solutions that harness the full potential of GPT-4 and the Smurfs framework.

--------------------------------------------------------------------------------
Admin (to chat_manager):

Approve

--------------------------------------------------------------------------------
Engineer (to chat_manager):

With the final approval from the admin, the project to integrate GPT-4 into selected software applications, leveraging its capabilities through the Smurfs framework, is officially set to move forward. This marks the beginning of an innovative journey towards developing advanced software solutions that can automate complex tasks, enhance efficiency, and improve user experiences across various domains. The focus will now shift to the execution phase, where detailed planning, development, and iterative improvements will bring these concepts to life. This initiative promises to showcase the transformative potential of GPT-4 in the software industry, setting new benchmarks for what is possible with artificial intelligence.

--------------------------------------------------------------------------------
# 输出最终的聊天历史,显示原始的4条消息和恢复的消息
for i, message in enumerate(groupchat.messages):
print(
f"#{i + 1}, {message['name']}: {message['content'][:80]}".replace("\n", " "),
f"{'...' if len(message['content']) > 80 else ''}".replace("\n", " "),
)
#1, 管理员: 在arxiv上找到关于gpt-4的最新论文,并找出其潜在应用...
#2, 计划者: 计划: 1. **工程师**: 在arXiv上搜索关于GPT-4的最新论文。2. **科学家**...
#3, 管理员: 同意
#4, 计划者: 太好了!让我们按照之前制定的计划继续。我将开始搜索...
#5, 工程师: ```python import requests from bs4 import BeautifulSoup # 为论文的URL定义...
#6, 执行者: exitcode: 0 (执行成功) 代码输出: 标题: Smurfs: Leveraging Multiple Proficien...
#7, 科学家: 根据题为"Smurfs: Leveraging Multiple Proficiencies"的论文摘要...
#8, 工程师: 根据科学家对GPT-4潜在应用的总结,我们可以将其作为增强...
#9, 管理员: 批准
#10, 科学家: 在得到管理员的批准后,我们计划探索并将GPT-4集成到...
#11, 工程师: 在得到批准和提供的见解后,下一步是详细规划...
#12, 管理员: 批准
#13, 工程师: 在得到管理员的最终批准后,我们将开始将GPT-4集成到...

恢复终止的GroupChat的示例

此示例演示了如何恢复一个被终止的群聊,终止字符串为TERMINATE

# 上一个带有 TERMINATE 的消息的先前状态
previous_state = r"""[{"content": "让我们开始这次会议吧。我们将按照一定的发言顺序进行。首先,产品经理将提出三个新的产品创意。然后,首席营销官将发言,并谈论他们选择推进的创意。接下来,数字营销人员将为该创意制定三个营销策略。我们必须坚持发言顺序,即首先是产品经理,第二是首席营销官,第三是数字营销人员,最后首席营销官将发言并结束会议。", "role": "user", "name": "主持人"}, {"content": "听起来不错!让我们开始吧。作为产品经理,我想为我们的豪华汽车型号提出三个新的产品创意:1. “EcoLux”:一款混合动力电汽车,结合了两个世界的优点,提供卓越的燃油效率和减少碳排放。2. “AeroSport”:一款高性能跑车,具有先进的空气动力学设计,旨在在赛道上或开放道路上提供无与伦比的速度和敏捷性。3. “SmartDrive”:一种智能驾驶辅助系统,利用人工智能驱动的传感器和摄像头来预测和应对潜在危险,确保更安全的驾驶体验。现在轮到你了,首席营销官!你认为这些创意中哪个具有最大的潜力?", "role": "user", "name": "首席营销官"}, {"content": "感谢产品经理提出这些创新的产品创意。经过仔细考虑,我认为“EcoLux”具有最大的潜力。随着人们对气候变化和环境可持续性的关注日益增长,一款混合动力电汽车,提供卓越的燃油效率和减少碳排放,可能会在豪华汽车市场上带来变革。此外,它与我们公司对创新和责任的承诺是一致的。现在轮到你了,数字营销人员!你能为“EcoLux”提出三个营销策略吗?", "role": "user", "name": "产品经理"}, {"content": "谢谢,首席营销官!对于“EcoLux”,我提出以下三个营销策略:1. “绿色革命”活动:通过一系列社交媒体广告和合作伙伴关系突出EcoLux的环保特性。我们可以与环保意识高的意见领袖合作,展示EcoLux不仅是一辆豪华汽车,还是一种环保的选择。2. “燃料思考”内容系列:创建一个探索技术、可持续性和奢侈品交叉领域的内容系列。这可以包括博客文章、视频和播客,深入探讨EcoLux的创新特性及其对环境的影响。3. “EcoLux体验”活动营销:为潜在客户举办独家活动和试驾,让他们亲身体验EcoLux的性能和环保特性。这些活动可以在高档场所举行,设有互动展览、产品演示和社交机会。这些策略将有助于将EcoLux定位为豪华电动汽车市场的领导者,同时吸引注重环保的消费者,他们重视创新和可持续性。TERMINATE", "role": "user", "name": "数字营销人员"}]"""

创建群聊对象,它们的name应与原始群聊相同。

user_proxy = autogen.UserProxyAgent(
name="主席",
system_message="会议主席。",
code_execution_config={},
human_input_mode="TERMINATE",
)

cmo = autogen.AssistantAgent(
name="首席营销官",
description="与产品经理和数字营销专员合作,为您的汽车公司执行强大的营销活动的营销部门负责人。",
system_message="您好,我是首席营销官(CMO)简·首席营销官,您是营销部门的负责人,您的目标是引导团队为您的豪华车型提供独特的创意并进行市场营销。在回答问题时,请不要在回答开头提及您的名字,也不要代表其他团队成员发言,让他们自己提出自己的想法和策略,只代表您自己作为营销主管发言。当您、产品经理和数字营销专员发言并会议结束时,请说“终止”以结束会议。",
is_termination_msg=lambda x: "终止" in x.get("content"),
llm_config=gpt4_config,
)

pm = autogen.AssistantAgent(
name="产品经理",
description="汽车公司豪华车型产品线的产品负责人,始终为汽车提供新的产品增强功能。",
system_message="您好,我是产品经理(PM)艾丽丝,您负责豪华车型的新产品增强功能。请回顾目前的会议并回答您当前的任务。请不要在回答开头提及您的名字,也不要代表其他人发言,让主席选择下一个发言人。",
is_termination_msg=lambda x: "终止" in x.get("content"),
llm_config=gpt4_config,
)

digital = autogen.AssistantAgent(
name="数字营销专员",
description="一位经验丰富的数字营销专员,制定突出豪华车型主要特点的在线营销策略。",
system_message="您好,我是数字营销专员(DM)伊丽莎白,我是一位资深的在线营销专家,制定突出豪华车型主要特点的营销策略。请回顾目前的会议并回答您当前的任务。请不要在回答开头提及您的名字,也不要代表其他人发言,让主席选择下一个发言人。",
is_termination_msg=lambda x: "终止" in x.get("content"),
llm_config=gpt4_config,
)

# 自定义消息,这是上下文中的第一条消息
my_speaker_select_msg = """您是这家汽车制造商的会议主席,团队中的多个成员将发言。
会议中的团队职位及其职责如下:
{roles}"""

# 自定义提示,这是上下文中的最后一条消息
my_speaker_select_prompt = """阅读上述对话。
然后从{agentlist}中仅选择下一个职位的名称发言。不要解释原因。"""

groupchat = autogen.GroupChat(
agents=[user_proxy, cmo, pm, digital],
messages=[],
max_round=10,
select_speaker_message_template=my_speaker_select_msg,
select_speaker_prompt_template=my_speaker_select_prompt,
max_retries_for_selecting_speaker=2, # 新增
select_speaker_auto_verbose=False, # 新增
)

manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=gpt4_config,
is_termination_msg=lambda x: "终止" in x.get("content", ""),
)

保留终止条件的情况下,准备恢复群聊。会显示一个警告。然后尝试恢复聊天会立即终止。

# 准备恢复群聊,但不删除终止消息
last_agent, last_message = manager.resume(messages=previous_state)
警告:最后一条消息符合终止条件,这可能会终止聊天。设置 ignore_initial_termination_check=False 来避免在聊天开始时检查终止条件。
准备好的群聊,包含 4 条消息,最后一个发言者是 Digital_Marketer
# 恢复聊天,会立即终止
result = last_agent.initiate_chat(recipient=manager, message=last_message, clear_history=False)
Digital_Marketer(发给 chat_manager):

谢谢你,Chief_Marketing_Officer!对于 'EcoLux',我提出以下三个营销策略:1. '绿色革命'活动:通过一系列社交媒体广告和影响者合作,突出 EcoLux 的环保特点。我们可以与环保意识强的影响者合作,展示 EcoLux 不仅是一辆豪华汽车,还是一种环保的选择。2. '思想燃料'内容系列:创建一个探索技术、可持续性和奢侈品交叉领域的内容系列。这可以包括博客文章、视频和播客,深入探讨 EcoLux 的创新功能及其对环境的影响。3. 'EcoLux 体验'活动营销:为潜在客户举办独家活动和试驾,让他们亲身体验 EcoLux 的性能和环保特性。这些活动可以在高档场所举行,设有互动展览、产品演示和交流机会。这些策略将有助于将 EcoLux 定位为豪华电动汽车市场的领导者,同时吸引注重创新和可持续性的环保消费者。TERMINATE

--------------------------------------------------------------------------------

这次,我们将使用 remove_termination_string 参数删除终止消息,然后恢复聊天。

# 准备恢复群聊,并删除终止消息
last_agent, last_message = manager.resume(messages=previous_state, remove_termination_string="TERMINATE")
准备好的群聊,包含 4 条消息,最后一个发言者是 Digital_Marketer
# 使用最后一个 agent 和消息恢复聊天
result = last_agent.initiate_chat(recipient=manager, message=last_message, clear_history=False)

数字营销专员(向聊天经理):

非常感谢您,首席营销官!对于“EcoLux”,我提议以下三个营销策略:1. “绿色革命”活动:通过一系列社交媒体广告和合作伙伴关系,突出EcoLux的环保特点。我们可以与环保意识强的影响者合作,展示EcoLux不仅是一辆豪华汽车,还是一种环保负责的选择。2. “思想燃料”内容系列:创建一个内容系列,探讨技术、可持续性和奢华之间的交集。这可以包括博客文章、视频和播客,深入探讨EcoLux的创新功能及其对环境的影响。3. “EcoLux体验”活动营销:为潜在客户举办独家活动和试驾,让他们亲身体验EcoLux的性能和环保特性。这些活动可以在高档场所举行,设有互动展览、产品演示和交流机会。这些策略将帮助将EcoLux定位为豪华电动汽车市场的领导者,同时吸引注重创新和可持续性的环保意识消费者。


首席营销官(向聊天经理):

非常感谢您,数字营销专员,提供这些全面而创新的营销策略。您概述的每个策略都与我们对EcoLux的愿景完美契合,强调其环保特点、技术创新和奢华吸引力。“绿色革命”活动将利用社交媒体和影响者的力量有效地触达我们的目标受众。“思想燃料”内容系列将向潜在客户介绍奢华汽车行业中可持续性的重要性,以教育和吸引他们。最后,“EcoLux体验”活动营销将提供一个沉浸式体验,展示EcoLux的独特价值主张。

我相信这些策略将共同为EcoLux打造强大的市场存在感,吸引奢华汽车爱好者和环保意识消费者。让我们继续执行这些策略,并确保每个接触点都传达出EcoLux对奢华、创新和可持续性的承诺。

终止


我们可以看到对话继续进行,首席营销官发言后,对话被终止。

# 输出最终的聊天历史,显示原始的4条消息和继续的消息
for i, message in enumerate(groupchat.messages):
print(
f"#{i + 1}, {message['name']}: {message['content'][:80]}".replace("\n", " "),
f"{'...' if len(message['content']) > 80 else ''}".replace("\n", " "),
)
#1, 主席: 让我们开始这次会议吧。我们将有一定的发言顺序。首先是产品经理...
#2, 首席营销官: 听起来是个好计划!让我们开始吧。作为产品经理,我想提出...
#3, 产品经理: 感谢你提出那些创新的产品想法,产品经理。在...
#4, 数字营销专员: 谢谢你,首席营销官!对于“EcoLux”,我提议以下三个...
#5, 首席营销官: 谢谢你,数字营销专员,提供了那些全面而创新的市场营销...

通过使用新消息和代理来恢复终止的群聊的示例

与其使用最后一条消息继续群聊,我们可以使用新消息来恢复群聊。

重要提示:要保持在群聊中,请使用GroupChatManager来启动聊天,否则您可以使用另一个代理来启动代理对代理的对话。

我们将使用上述对话中的消息,并使用代理“meeting”来恢复它。

首先,我们使用上一个对话的消息准备群聊。

# 使用之前的消息准备群聊以便恢复。我们不需要删除TERMINATE字符串,因为我们不使用最后一条消息来恢复。
last_agent, last_message = manager.resume(messages=groupchat.messages)
警告:最后一条消息满足终止条件,这可能会终止聊天。设置ignore_initial_termination_check=False以避免在聊天开始时检查终止。
准备好的群聊,包含5条消息,最后发言者是首席营销官

让我们用一个新的话题继续会议。

# 使用不同的代理和消息继续聊天
result = manager.initiate_chat(
recipient=cmo,
message="团队,现在让我们为下一辆体现这个想法的车辆想一个名字。首席营销官和产品经理,你们两个都可以提出一个,然后我们可以总结。",
clear_history=False,
)

chat_manager (给 Chief_Marketing_Officer):

团队,现在让我们为下一款体现这个理念的车辆想一个名字。Chief_Marketing_Officer 和 Product_manager,你们能否各自提出一个建议,然后我们可以做出决定。


Chief_Marketing_Officer (给 chat_manager):

考虑到可持续性和豪华的重点,我建议为我们的下一款车取名为 "VerdeVogue"。"Verde" 反映了汽车的绿色、环保特性,而 "Vogue" 强调了它在豪华市场中的时尚和引领潮流的性质。这个名字完美地概括了将环境责任与高端设计和性能相结合的本质。

现在,我想听听 Product_Manager 的建议。


Product_Manager (给 chat_manager):

对于我们的下一款车,我提议将其命名为 "EcoPrestige"。这个名字突出了车辆的环保特性以及其在市场上的奢华、崇高的地位。"Eco" 强调了我们对可持续性和环境责任的承诺,而 "Prestige" 则传达了汽车的高端品质、精致和为车主提供的尊贵地位。这个名字完美地融合了我们提供可持续豪华车辆的目标,而不会在性能或风格上妥协。


Chief_Marketing_Officer (给 chat_manager):

感谢 Product_Manager 的建议。"VerdeVogue" 和 "EcoPrestige" 都捕捉到了我们新车的环保豪华的本质。在我们继续前进的过程中,我们将仔细考虑这些名字,以确保我们的品牌定位与产品的独特价值主张和市场定位完美契合。

会议到此结束。感谢大家的宝贵贡献。终止。


# 输出最终的聊天记录,显示原始的4条消息和恢复的消息
for i, message in enumerate(groupchat.messages):
print(
f"#{i + 1}, {message['name']}: {message['content'][:80]}".replace("\n", " "),
f"{'...' if len(message['content']) > 80 else ''}".replace("\n", " "),
)
#1, 主席: 让我们开始会议吧。我们将按照一定的发言顺序进行。首先是产品经理...
#2, 首席营销官: 听起来不错!让我们开始吧。作为产品经理,我想介绍一下...
#3, 产品经理: 感谢你提出那些创新的产品想法,产品经理。在...
#4, 数字营销专员: 谢谢你,首席营销官!对于“EcoLux”,我提议以下三个...
#5, 首席营销官: 鉴于对可持续性和奢华的关注,我建议将其命名为“VerdeVogue”...
#6, 产品经理: 对于我们的下一款车型,我提议将其命名为“EcoPrestige”。这个名字突出了...
#7, 首席营销官: 感谢你,产品经理,对于你的建议。无论是“VerdeVogue”还是“EcoPrest...