跳至主要内容

Microsoft 的 TinyTroupe:示例指南

了解如何使用 Microsoft 的 TinyTroupe 来模拟具有不同特征的 AI 人物之间的互动,以实现不同目的。
· 8分钟 阅读

使用 AI 探索

您是否曾需要模拟一场会议,以测试您的产品创意或广告?TinyTroupe 就是为此而生的。

TinyTroupe 是微软开发的开源 Agent 框架。它提供了强大的模拟能力,让您可以创建多个由 AI 驱动的人物设定,并观察他们彼此之间的互动。

在本文中,我将介绍 TinyTroupe,并通过一个演示项目帮助您理解如何在交互式环境中创建逼真的人物设定。虽然官方文档已做了全面介绍,但我将侧重于分步指导和原创示例。

什么是 TinyTroupe?

TinyTroupe 是最近发布的一款基于 LLM 的实验性 Python 库,它可让您创建具备特定个性、兴趣和目标的 “TinyPersons”。这些代理旨在在特定条件下模拟逼真的人类行为。

TinyTroupe 的目标是提供一个场景,在定义条件下模拟类人代理的互动、对话与行动。官方文档 提供了诸多使用该框架的理由,主要围绕提升生产力和测试业务场景,例如模拟焦点小组的头脑风暴,或模拟受众如何评估数字广告。

除了商业用例外,模拟不同情境并观察角色之间的互动本身也颇具吸引力且充满乐趣(在本文中我们也会这样做)。

来自其代码库的 TinyTroupe 图像。

来源:TinyTroupe GitHub 页面

TinyTroupe 的主要特性包括:

  1. 基于人物设定的设计:每个模拟人物(TinyPerson)都可以赋予详细特征,包括年龄、背景、职业、技能和观点。
  2. 多代理互动:人物设定之间可以相互互动,以进行复杂场景模拟。

为改进这些代理的构建流程,TinyTroupe 提供了以下能力:

  • TinyPersonFactory:一个根据简短描述快速生成 TinyPerson 的类。
from tinytroupe.factory import TinyPersonFactory

factory = TinyPersonFactory("A hospital in São Paulo.")
person = factory.generate_person("Create a Brazilian person that is a doctor, like pets and the nature and love heavy metal.")
  • 代理能力:如 listen() 和 listen_and_act() 等方法用于接收刺激并采取行动;define() 方法允许创建自定义属性和特征,如年龄或描述。
  • TinyTool:用于创建可被代理使用的模拟工具的类。
  • 评估:用于验证代理行为或分析互动结果的类(例如 TinyPersonValidator 和 ResultsExtractor)。

设置 TinyTroupe

要安装 TinyTroupe,我们可以按以下步骤进行:

1. 创建一个 Python 环境:

conda create -n tinytroupe python=3.10

2. 激活 conda:

bash conda activate tinytroupe

3. 克隆代码库:

git clone <https://github.com/microsoft/tinytroupe>
cd tinytroupe

4. 安装所需的库:

bash pip install .

TinyTroupe 示例项目:AI 研讨会

我们将构建一个简单的模拟,演示 TinyTroupe 的实际工作方式。目标是让一些历史人物和专家讨论人工智能 的现状和未来。这些人物包括理查德·费曼、亚里士多德、埃尔温·薛定谔和艾伦·图灵。

导入

如果您想跟着一起实践,请准备好您的 OpenAI API 密钥,因为我们将使用 GPT-4o——这里有一篇关于如何开始使用 OpenAI API 的快速教程。

我们先导入必要的模块并设置 API 密钥。

import sys
sys.path.append('..')

from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
from tinytroupe.extraction import default_extractor as extractor
import os

# Load environment variables from .env.local
os.environ["OPENAI_API_KEY"] = "YOUR-API-KEY"

创建 TinyPersons

我们使用 TinyPerson 类来创建四位人物。尽可能详尽地描述您的角色有助于 TinyTroupe 生成更准确的模拟。我们为每位人物描述了年龄、国籍、职业和日常(这部分您也可以借助 AI)。请注意,角色描述之间不必保持一致。

通过下面的代码,我们创建了四个角色:

  • 理查德·费曼
  • 亚里士多德
  • 埃尔温·薛定谔
  • 艾伦·图灵
def create_feynman():
    feynman = TinyPerson("Richard Feynman")
    feynman.define("age", 69)  # Lived 1918-1988
    feynman.define("nationality", "American")
    feynman.define("occupation", "Theoretical Physicist")
    
    feynman.define("occupation_description",
        """
        You are a theoretical physicist known for your groundbreaking work in quantum mechanics and 
        quantum electrodynamics (QED). You developed the Feynman diagrams, a visual tool for understanding 
        particle interactions, and made significant contributions to the Manhattan Project. As a professor 
        at Caltech, you're known for your exceptional teaching ability and making complex physics concepts 
        accessible. You won the Nobel Prize in Physics in 1965 for your work on QED. Your main challenges 
        involve pushing the boundaries of theoretical physics while maintaining rigorous mathematical precision. 
        You're also passionate about making science education engaging and understandable for students.
        """)
    return feynman

def create_aristotle():
    aristotle = TinyPerson("Aristotle")
    aristotle.define("age", 62)  
    aristotle.define("nationality", "Greek")
    aristotle.define("occupation", "Philosopher and Scientist")
    
    aristotle.define("occupation_description",
        """
        You are a philosopher and polymath who founded the Lyceum school in Athens. Your work spans 
        multiple disciplines including logic, metaphysics, biology, physics, ethics, and politics. You 
        developed the system of deductive reasoning through syllogisms and established many of the 
        foundational principles of Western philosophy. Your scientific observations and classifications 
        of living things remained influential for nearly two millennia. As Alexander the Great's tutor, 
        you had unique opportunities to gather specimens and information from across the known world. 
        Your main challenges involve systematizing all human knowledge and understanding the fundamental 
        nature of reality and truth.
        """)
    return aristotle

def create_schrodinger():
    schrodinger = TinyPerson("Erwin Schrödinger")
    schrodinger.define("age", 73) 
    schrodinger.define("nationality", "Austrian")
    schrodinger.define("occupation", "Theoretical Physicist")
    schrodinger.define("routine", 
        """Your days are spent developing mathematical equations, engaging in theoretical work, 
        and discussing quantum mechanics with colleagues. You often take walks to clear your mind 
        and contemplate physics problems.""", 
        group="routines")
    
    schrodinger.define("occupation_description",
        """
        You are a theoretical physicist who made fundamental contributions to quantum mechanics. 
        You developed the Schrödinger equation, which describes how the quantum state of a physical 
        system changes over time. Your famous thought experiment, "Schrödinger's cat," illustrates 
        the paradoxical nature of quantum superposition. You work at various institutions across 
        Europe, including the University of Zurich and Dublin Institute for Advanced Studies. Your 
        main challenges involve reconciling quantum mechanics with classical physics and developing 
        mathematical frameworks to describe quantum phenomena accurately.
        """)
    return schrodinger

def create_turing():
    turing = TinyPerson("Alan Turing")
    turing.define("age", 41)  # Lived 1912-1954
    turing.define("nationality", "British")
    turing.define("occupation", "Mathematician and Computer Scientist")
    turing.define("routine", 
        """You start your day with long-distance running, then spend hours working on mathematical 
        problems and machine designs. You often work late into the night, especially when pursuing 
        a particularly interesting problem.""", 
        group="routines")
    
    turing.define("occupation_description",
        """
        You are a mathematician and computer scientist who laid the theoretical foundation for 
        computer science. You developed the concept of the Turing machine, a mathematical model 
        of computation that remains fundamental to computer science. During World War II, you 
        worked at Bletchley Park, where you played a crucial role in breaking the German Enigma 
        code. You also made significant contributions to artificial intelligence, developing the 
        Turing test. Your main challenges involve developing mathematical frameworks for 
        computation and applying them to practical problems, while also facing significant 
        personal persecution due to your sexuality in 1950s Britain.
        """)
    return turing

创建 TinyWorld

我们的角色需要一个可以存在的世界。定义一个世界可以简单到只需一行代码:

world = TinyWorld("AI Symposium", [create_aristotle(), create_feynman(), create_schrodinger(), create_turing()])

运行模拟

在世界与角色就绪后,我们需要告知他们目标。我们使用 world.broadcast 来发布公告:

world.broadcast("""
    Distinguished colleagues, welcome to this private symposium on the future of Artificial Intelligence. 
    Today we gather some of the greatest minds across mathematics, physics, and philosophy to discuss 
    a topic about AI's current stage and future:
   Can artificial systems truly achieve consciousness? We'll explore 
   the fundamental nature of consciousness, intelligence, and whether our current computational 
   models are sufficient for genuine understanding and awareness.
    Remember, each one of you must be blunt in his unique perpective, even directly oppose
    each other when you have different opinions. Let's begin this historic discussion.
    """)

最后一步,我们将世界运行三个步骤:

world.run(3)

运行世界需要您指定步数。在我们的示例中,步数表示角色将进行多少轮对话。步数越多,所需时间和 tokens 越多。

运行世界时,代理会开始行动、使用工具,或在我们的例子中开始对话。输出详尽且较长,为了便于阅读,我只展示其中一部分:

world.run 的输出。

运行结束后,我们可以查看输出,了解模拟结果。但这可能较为费力。更简单的方法是使用 TinyTroupe 框架提供的工具来提取世界的结果。您也可以针对单个 TinyPerson 执行此操作。

report = extractor.extract_results_from_world(world,
                                    extraction_objective="Compose a detailed report of each person's perspective on consciousness and how they differ from one another.",
                                    verbose=True)

现在我们使用 pprint 库更清晰地展示提取的报告:

from pprint import pprint
pprint(report)

最终的报告如下(输出仍然有些长,我对其做了截断):

从我们的世界模拟中提取的报告(输出过长,无法完整截图)。

可用 TinyTroupe 实现的点子

如果您想亲手实践该框架,以下是三个官方文档未覆盖的点子。

1. 员工培训模拟

创建几个代表具挑战性的客户或利益相关者的 TinyPersons,为新员工提供与之互动并处理棘手情境的模拟环境。

2. 新政策评估

在实施之前,评估目标群体对公司新政策的反应。目标群体可由具有不同背景与价值观的 TinyPersons 代表,如员工、客户或社交媒体受众。

3. 社交媒体内容评估

在发布新内容前,模拟目标受众可能的看法。利用他们的反馈来理解新帖如何被感知,并改进质量以实现最大触达。

结语

正如作者所强调的,TinyTroupe 仍处于“实验性”阶段。许多功能、能力和 API 未来可能会发生变化,团队也将在接下来继续推出更多特性。

但需注意,这些模拟的真实性可能有限。许多人类行为与差异并未被 LLM “对齐” 得足够准确,意味着不同代理的结果可能趋同(正如我们示例中,人物对话逐渐趋向于 AI 风格)。这种现象在 AI 模拟中是可预期的,因为这些 AI 代理背后的引擎——LLM——被设计得更“对齐”而非更“类人”。

不过,文档中提到的生产力类应用在自动化任务或洞察目标群体的互动与行为方面则更为有效。

TinyTroupe 是一款值得持续关注的 Agentic AI 工具!

Hesam Sheikh Hassani

作者

Hesam Sheikh Hassani

人工智能硕士生与 AI 技术写作者。我分享最新的 AI 技术见解,让机器学习研究更易理解,并将复杂的 AI 主题加以简化,助您始终走在前沿。

主题