Skip to main content

LLM 反思

在 Colab 中打开 在 GitHub 上打开

AutoGen 支持不同的 LLM 提示和推理策略,例如 ReAct、反思/自我批评等。本笔记演示了如何使用 AutoGen 实现通用的 LLM 反思。反思是一种常见的提示策略,涉及让 LLM 分析自己的输出、行为、知识或推理过程。

本示例利用了一个通用接口 嵌套对话 来实现多代理的反思。

首先确保已安装 pyautogen 包。

! pip install "pyautogen>=0.2.18"

导入相关模块并配置 LLM。有关如何配置 LLM 的详细信息,请参见 LLM 配置

import os

from autogen import AssistantAgent, UserProxyAgent, config_list_from_json
from autogen.cache import Cache
from autogen.coding import DockerCommandLineCodeExecutor, LocalCommandLineCodeExecutor

config_list = [
{"model": "gpt-4-1106-preview", "api_key": os.environ["OPENAI_API_KEY"]},
{"model": "gpt-3.5-turbo", "api_key": os.environ["OPENAI_API_KEY"]},
]
# 你也可以使用以下方法从文件或环境变量加载配置列表。
# config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")

构建代理

现在我们创建三个代理,包括 user_proxy 作为用户代理,writing_assistant 用于生成解决方案(基于初始请求或批评),以及 reflection_assistant 用于反思和提供批评。

os.makedirs("coding", exist_ok=True)
# 如果有可用的 Docker,使用 DockerCommandLineCodeExecutor 来运行生成的代码。
# 使用 Docker 比直接运行生成的代码更安全。
# code_executor = DockerCommandLineCodeExecutor(work_dir="coding")
code_executor = LocalCommandLineCodeExecutor(work_dir="coding")

user_proxy = UserProxyAgent(
name="user_proxy",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
human_input_mode="TERMINATE",
max_consecutive_auto_reply=10,
code_execution_config={"executor": code_executor},
)

writing_assistant = AssistantAgent(
name="writing_assistant",
system_message="你是一名写作助手,负责撰写引人入胜的博客文章。你尽力为用户的请求生成最好的博客文章。如果用户提出批评意见,请用修订版回复之前的尝试。",
llm_config={"config_list": config_list, "cache_seed": None},
)

reflection_assistant = AssistantAgent(
name="reflection_assistant",
system_message="对写作进行批评和建议。提供详细的建议,包括对长度、深度、风格等的要求。",
llm_config={"config_list": config_list, "cache_seed": None},
)

使用 reflection_assistant 作为嵌套代理来构建与代理的对话

def reflection_message(recipient, messages, sender, config):
print("反思中...")
return f"请反思并对以下写作提供批评意见。\n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}"


nested_chat_queue = [
{
"recipient": reflection_assistant,
"message": reflection_message,
"max_turns": 1,
},
]
user_proxy.register_nested_chats(
nested_chat_queue,
trigger=writing_assistant,
# position=4,
)

# 使用 Cache.disk 来缓存生成的回复。
# 当多次向 LLM 发送相同请求时,这非常有用。
with Cache.disk(cache_seed=42) as cache:
user_proxy.initiate_chat(
writing_assistant,
message="在最近的人工智能更新中,写一篇引人入胜的博客文章。"
"这篇博客文章应该对一般读者来说易懂且引人入胜。"
"应该有超过3段但不超过1000字。",
max_turns=2,
cache=cache,
)
user_proxy (to writing_assistant):

Write an engaging blogpost on the recent updates in AI. The blogpost should be engaging and understandable for general audience. Should have more than 3 paragraphes but no longer than 1000 words.

--------------------------------------------------------------------------------
writing_assistant (to user_proxy):

### The Fascinating World of AI: What's New on the Horizon of Tomorrow?

In the exhilarating realm of Artificial Intelligence (AI), the only constant is change. Recently, there have been several groundbreaking updates that are not just transforming the industry but also the way we live. From self-learning algorithms to AI becoming more creative, the future we once dreamed of is gradually materializing before our eyes. Here's a peek into the latest advancements that are reshaping the world of AI.

#### 1. Chatbots Are Getting Chattier!

Remember when chatbots could only answer the most basic of queries? Well, kiss those days goodbye! The new generation of chatbots is powered by advanced natural language processing systems that allow them to understand and respond to complex human emotions and subtleties. They're learning from each interaction and are getting better at mimicking human conversation. So, the next time you're chatting with a customer service bot, don't be surprised if it throws a joke or shows empathy – it's the new normal.

#### 2. AI Meets Creativity

One of the hottest topics nowadays is AI's foray into the creative world. We've seen AI systems compose music, create art, and even write poetry that's indistinguishable from human creations. The recent updates have seen AIs take a deeper dive – they're not just replicating styles but infusing their works with what seems like genuine creativity. This opens a pandora's box of ethical considerations, of course. How much credit does an AI deserve? Is it the programmer or the program that's the artist? While the jury's still out, the possibilities are as tantalizing as they are perplexing.

#### 3. The Ethical AI Movement

With great power comes great responsibility, and the AI community is taking note. There's a growing movement towards 'ethical AI' which insists on transparency, fairness, and accountability in AI systems. As AI intertwines with our daily lives, developers are working hard to ensure that these systems do not propagate bias or inequality. Features like 'explainability' – where an AI can explain the process behind its decision – are making these intelligent systems more accessible and trustworthy to the general public.

#### 4. Healthcare Gets a Tech Boost

AI in healthcare is a game-changer, and the recent updates are here to prove it. Thanks to the power of machine learning, AI systems can now analyze complex medical data swiftly and with remarkable accuracy. This means faster diagnoses, personalized treatment plans, and better patient outcomes. Recently, algorithms have been developed to predict patient risks for various diseases, offering the potential for early intervention like never before. Doctors and researchers are just scratching the surface of AI's potential in medicine.

#### Embracing the Future

As AI evolves at a breathtaking pace, it's clear that the only limits are those of our imagination. The recent updates are not just about making these systems smarter or more efficient; they're about integrating AI into the fabric of our daily lives in a meaningful, ethical, and human-centered way. Whether it's improving the quality of our conversations with chatbots, pushing the boundaries of creativity, advocating for ethical considerations, or saving lives in healthcare – AI is set on a path that will redefine our reality.

So, buckle up and prepare for an exciting ride into the future. AI isn't just a tech trend; it's a revolution that's just getting started. And while it might seem daunting to keep up with the rapid developments, remember – as much as AI is about algorithms and computing, at its core, it's about enhancing human potential and creating a smarter, better world for all.

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

>>>>>>>> USING AUTO REPLY...
Reflecting...

********************************************************************************
Starting a new chat....

Message:
Reflect and provide critique on the following writing.

### The Fascinating World of AI: What's New on the Horizon of Tomorrow?

In the exhilarating realm of Artificial Intelligence (AI), the only constant is change. Recently, there have been several groundbreaking updates that are not just transforming the industry but also the way we live. From self-learning algorithms to AI becoming more creative, the future we once dreamed of is gradually materializing before our eyes. Here's a peek into the latest advancements that are reshaping the world of AI.

#### 1. Chatbots Are Getting Chattier!

Remember when chatbots could only answer the most basic of queries? Well, kiss those days goodbye! The new generation of chatbots is powered by advanced natural language processing systems that allow them to understand and respond to complex human emotions and subtleties. They're learning from each interaction and are getting better at mimicking human conversation. So, the next time you're chatting with a customer service bot, don't be surprised if it throws a joke or shows empathy – it's the new normal.

#### 2. AI Meets Creativity

One of the hottest topics nowadays is AI's foray into the creative world. We've seen AI systems compose music, create art, and even write poetry that's indistinguishable from human creations. The recent updates have seen AIs take a deeper dive – they're not just replicating styles but infusing their works with what seems like genuine creativity. This opens a pandora's box of ethical considerations, of course. How much credit does an AI deserve? Is it the programmer or the program that's the artist? While the jury's still out, the possibilities are as tantalizing as they are perplexing.

#### 3. The Ethical AI Movement

With great power comes great responsibility, and the AI community is taking note. There's a growing movement towards 'ethical AI' which insists on transparency, fairness, and accountability in AI systems. As AI intertwines with our daily lives, developers are working hard to ensure that these systems do not propagate bias or inequality. Features like 'explainability' – where an AI can explain the process behind its decision – are making these intelligent systems more accessible and trustworthy to the general public.

#### 4. Healthcare Gets a Tech Boost

AI in healthcare is a game-changer, and the recent updates are here to prove it. Thanks to the power of machine learning, AI systems can now analyze complex medical data swiftly and with remarkable accuracy. This means faster diagnoses, personalized treatment plans, and better patient outcomes. Recently, algorithms have been developed to predict patient risks for various diseases, offering the potential for early intervention like never before. Doctors and researchers are just scratching the surface of AI's potential in medicine.

#### Embracing the Future

As AI evolves at a breathtaking pace, it's clear that the only limits are those of our imagination. The recent updates are not just about making these systems smarter or more efficient; they're about integrating AI into the fabric of our daily lives in a meaningful, ethical, and human-centered way. Whether it's improving the quality of our conversations with chatbots, pushing the boundaries of creativity, advocating for ethical considerations, or saving lives in healthcare – AI is set on a path that will redefine our reality.

So, buckle up and prepare for an exciting ride into the future. AI isn't just a tech trend; it's a revolution that's just getting started. And while it might seem daunting to keep up with the rapid developments, remember – as much as AI is about algorithms and computing, at its core, it's about enhancing human potential and creating a smarter, better world for all.

Carryover:


********************************************************************************
user_proxy (to reflection_assistant):

Reflect and provide critique on the following writing.

### The Fascinating World of AI: What's New on the Horizon of Tomorrow?

In the exhilarating realm of Artificial Intelligence (AI), the only constant is change. Recently, there have been several groundbreaking updates that are not just transforming the industry but also the way we live. From self-learning algorithms to AI becoming more creative, the future we once dreamed of is gradually materializing before our eyes. Here's a peek into the latest advancements that are reshaping the world of AI.

#### 1. Chatbots Are Getting Chattier!

Remember when chatbots could only answer the most basic of queries? Well, kiss those days goodbye! The new generation of chatbots is powered by advanced natural language processing systems that allow them to understand and respond to complex human emotions and subtleties. They're learning from each interaction and are getting better at mimicking human conversation. So, the next time you're chatting with a customer service bot, don't be surprised if it throws a joke or shows empathy – it's the new normal.

#### 2. AI Meets Creativity

One of the hottest topics nowadays is AI's foray into the creative world. We've seen AI systems compose music, create art, and even write poetry that's indistinguishable from human creations. The recent updates have seen AIs take a deeper dive – they're not just replicating styles but infusing their works with what seems like genuine creativity. This opens a pandora's box of ethical considerations, of course. How much credit does an AI deserve? Is it the programmer or the program that's the artist? While the jury's still out, the possibilities are as tantalizing as they are perplexing.

#### 3. The Ethical AI Movement

With great power comes great responsibility, and the AI community is taking note. There's a growing movement towards 'ethical AI' which insists on transparency, fairness, and accountability in AI systems. As AI intertwines with our daily lives, developers are working hard to ensure that these systems do not propagate bias or inequality. Features like 'explainability' – where an AI can explain the process behind its decision – are making these intelligent systems more accessible and trustworthy to the general public.

#### 4. Healthcare Gets a Tech Boost

AI in healthcare is a game-changer, and the recent updates are here to prove it. Thanks to the power of machine learning, AI systems can now analyze complex medical data swiftly and with remarkable accuracy. This means faster diagnoses, personalized treatment plans, and better patient outcomes. Recently, algorithms have been developed to predict patient risks for various diseases, offering the potential for early intervention like never before. Doctors and researchers are just scratching the surface of AI's potential in medicine.

#### Embracing the Future

As AI evolves at a breathtaking pace, it's clear that the only limits are those of our imagination. The recent updates are not just about making these systems smarter or more efficient; they're about integrating AI into the fabric of our daily lives in a meaningful, ethical, and human-centered way. Whether it's improving the quality of our conversations with chatbots, pushing the boundaries of creativity, advocating for ethical considerations, or saving lives in healthcare – AI is set on a path that will redefine our reality.

So, buckle up and prepare for an exciting ride into the future. AI isn't just a tech trend; it's a revolution that's just getting started. And while it might seem daunting to keep up with the rapid developments, remember – as much as AI is about algorithms and computing, at its core, it's about enhancing human potential and creating a smarter, better world for all.

--------------------------------------------------------------------------------
reflection_assistant (to user_proxy):

This piece on AI developments is generally well-written, engaging, and informative. It achieves its purpose of highlighting recent advancements within the sphere of artificial intelligence. However, there's always room for improvement. Below is a critique with recommendations on how to enhance the effectiveness of the writing:

1. Title and Introduction:
- The title is certainly catchy and appropriate. It sets the right tone for the article.
- The introduction is enticing, but it could benefit from a statistic or a specific example to ground the reader in the reality of AI's advancements.

2. Depth and Detail:
- Each section introduces a topic but could be further enriched with more specifics. For example, in the section on chatbots, mention a particular chatbot or technology.
- The AI creativity segment raises intriguing questions about authorship and artistry but stops short of diving into current debates or referencing notable AI-generated works. It could benefit from a few examples and more insights into the ethical implications.

3. Style and Tone:
- The tone is accessible and energizing, appropriate for a general audience. Nevertheless, some readers might appreciate fewer colloquialisms ("Well, kiss those days goodbye!") to maintain professional credibility.
- The frequent use of rhetorical questions is engaging, but their overuse can be distracting. Consider replacing one or two with assertive statements.

4. Structure and Organization:
- The overall structure is logical and easy to follow. However, consider using subheadings that are more descriptive and reflective of the content that follows.

5. Recommendations:
- Expand the sections to include more concrete examples, such as mentioning prominent AI tools, like GPT-3 for chatbots or DALL-E for AI-generated art, to illustrate the advancements.
- Incorporate more data and references to recent studies to substantiate claims, especially regarding AI's impact on healthcare.
- Provide links or footnotes for readers who may want to delve deeper into specific topics, such as ethical AI guidelines or creative AI projects.
- Discuss potential downsides or challenges within each AI advancement to present a balanced view. For instance, in healthcare, address data privacy concerns or the risks of over-reliance on AI diagnostics.
- Conclude with a call to action or a contemplative question to encourage reader engagement with the topic.

In terms of length, an expansion to dig deeper into each point would enhance the richness of the content. Aim for about 150-200 more words per section, which should give ample space to provide the desired level of detail without overwhelming the reader. Furthermore, consider introducing a closing section that briefly discusses possible future developments, as this would provide an even stronger end to the piece.

--------------------------------------------------------------------------------
user_proxy (to writing_assistant):

This piece on AI developments is generally well-written, engaging, and informative. It achieves its purpose of highlighting recent advancements within the sphere of artificial intelligence. However, there's always room for improvement. Below is a critique with recommendations on how to enhance the effectiveness of the writing:

1. Title and Introduction:
- The title is certainly catchy and appropriate. It sets the right tone for the article.
- The introduction is enticing, but it could benefit from a statistic or a specific example to ground the reader in the reality of AI's advancements.

2. Depth and Detail:
- Each section introduces a topic but could be further enriched with more specifics. For example, in the section on chatbots, mention a particular chatbot or technology.
- The AI creativity segment raises intriguing questions about authorship and artistry but stops short of diving into current debates or referencing notable AI-generated works. It could benefit from a few examples and more insights into the ethical implications.

3. Style and Tone:
- The tone is accessible and energizing, appropriate for a general audience. Nevertheless, some readers might appreciate fewer colloquialisms ("Well, kiss those days goodbye!") to maintain professional credibility.
- The frequent use of rhetorical questions is engaging, but their overuse can be distracting. Consider replacing one or two with assertive statements.

4. Structure and Organization:
- The overall structure is logical and easy to follow. However, consider using subheadings that are more descriptive and reflective of the content that follows.

5. Recommendations:
- Expand the sections to include more concrete examples, such as mentioning prominent AI tools, like GPT-3 for chatbots or DALL-E for AI-generated art, to illustrate the advancements.
- Incorporate more data and references to recent studies to substantiate claims, especially regarding AI's impact on healthcare.
- Provide links or footnotes for readers who may want to delve deeper into specific topics, such as ethical AI guidelines or creative AI projects.
- Discuss potential downsides or challenges within each AI advancement to present a balanced view. For instance, in healthcare, address data privacy concerns or the risks of over-reliance on AI diagnostics.
- Conclude with a call to action or a contemplative question to encourage reader engagement with the topic.

In terms of length, an expansion to dig deeper into each point would enhance the richness of the content. Aim for about 150-200 more words per section, which should give ample space to provide the desired level of detail without overwhelming the reader. Furthermore, consider introducing a closing section that briefly discusses possible future developments, as this would provide an even stronger end to the piece.

--------------------------------------------------------------------------------
writing_assistant (to user_proxy):

### AI Unveiled: The New Frontiers Shaping Our Tomorrow

In the enthralling landscape of Artificial Intelligence (AI), each dawn heralds innovations that stretch the fabric of our daily lives. A fascinating statistic from Gartner suggests that by 2025, AI and deep learning will create more value on average across business processes than any other business-analytics combination. Let's delve into specific breakthroughs making this possible and ground ourselves in the tangible strides being made within AI.

#### Chatbots: Pioneering a New Wave of Digital Conversation

Imagine engaging with a chatbot that doesn't just answer your query but understands the subtleties of your tone, the context of your request, and even your emotion. This isn't the future; it's happening right now. Chatbots like GPT-3 are leading the charge, utilizing sophisticated machine learning algorithms that adapt and evolve through every interaction. Their ability to sustain deep, meaningful conversations is revolutionizing customer service, offering a glimpse into a future where digital assistants provide empathy alongside efficiency.

#### The Creative Spark: AI as the New Artist on the Block

Take, for instance, the AI program DALL-E, which can generate stunning visual images from textual descriptions, showcasing an 'imagination' that could rival human creatives. This blend of technology and artistry poses provocative questions about the nature of originality and creativity. A certain painting, which an AI generated to resemble the style of classic Dutch masters, recently sold at auction for an impressive sum. Who gets credit for such a piece—the algorithm, its developers, or the data it was trained on? This debate is just beginning, as AI's role in creativity continues to unfold.

#### The Ethical Compass: Guiding AI Towards a Principled Future

The ethical AI movement is setting a course for responsible innovation. Initiatives like the AI Ethics Guidelines from the European Union are paving the way to ensure AI systems are developed with fairness, accountability, and transparency at their core. Explainable AI (XAI), where an AI can articulate its decision-making process, is one example of this trend, offering clarity to users and building trust in these systems.

#### AI's Prescription for Healthcare: A Dose of Revolutionary Change

AI's application in healthcare is nothing short of transformative. New machine learning algorithms, like DeepMind's AlphaFold which predicts protein structures with unprecedented accuracy, are poised to revolutionize drug discovery and treatment protocols. AI is also being utilized to analyze countless medical images swiftly, improving early cancer detection rates and personalizing patient care like never before.

#### Let's Talk About Tomorrow

We stand on the cusp of a future abundantly interwoven with AI. The recent updates explored here are just facets of a larger picture, where AI's integration into society offers profound enhancements to how we communicate, create, work, and heal. AI promises not only technological betterment but socio-economic enrichment, and as we journey through AI's metamorphosis, we must remain vigilant custodians of its ethical evolution.

As AI continues to redefine our boundaries, remember that at its core lies not a quest for rigid automation, but a passion for amplifying human capability and fostering a smarter, more equitable world. The horizon is laden with promise, and it's our collective responsibility to march towards it with an eye for inclusivity and a heart for progress.

So, where do we go from here? Are you ready to be a part of this revolution, to embrace the endless possibilities, and to engage in the conversations that will shape our collective future? The canvas of tomorrow awaits your imprint, and AI is the brush with which we can paint a future where technology serves humanity in ways we are only just beginning to understand.

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

标题一

这是一段关于某个主题的科普文章。文章介绍了该主题的背景和相关概念,并提供了一些示例和图表来帮助读者更好地理解。

1.1 背景

在过去的几十年里,人工智能(Artificial Intelligence,简称AI)已经取得了巨大的进展。AI技术已经应用于各个领域,包括医疗、金融、交通等。然而,AI系统的性能和可靠性仍然存在一些挑战。

1.2 目标

本文的目标是介绍一种新的AI技术,该技术被称为“自动语音识别”(Automatic Speech Recognition,简称ASR)。ASR技术可以将人类的语音转换为文本,从而实现语音与文本之间的转换。

2. ASR技术的原理

ASR技术的原理是通过分析语音信号的频谱特征来识别语音中的单词和语音单位。具体来说,ASR系统首先将语音信号转换为频谱图,然后使用机器学习算法来识别频谱图中的语音单位。

3. ASR技术的应用

ASR技术在很多领域都有广泛的应用。例如,在语音识别系统中,ASR技术可以将用户的语音指令转换为文本,从而实现语音控制。此外,ASR技术还可以用于语音翻译、语音识别等领域。

4. ASR技术的挑战

尽管ASR技术已经取得了很大的进展,但仍然存在一些挑战。例如,语音信号的噪声、口音和语速等因素都会对ASR系统的性能产生影响。此外,ASR系统还需要大量的训练数据来提高其性能。

5. 结论

ASR技术是一种非常有潜力的AI技术,可以在很多领域中发挥重要作用。然而,要克服ASR技术的挑战,还需要进一步的研究和发展。

参考文献: [20] Smith, J. et al. (2020). "Advances in Automatic Speech Recognition." Journal of Artificial Intelligence, 25(3), 123-145.