少女祈祷中...

最近哥们儿搞了个好玩的:用 TinyTroupe 做了个 D&D 模拟器!

最近哥们儿在刷开源项目的时候,瞅见了一个叫 TinyTroupe 的玩意儿。这东西原本是个超正经的生产力工具,用来模拟带有个性化特征的多智能体行为。微软的官方文档还列了一堆“正经”用途,比如:

  • 广告分析:用虚拟用户群评估广告效果,省钱!
  • 产品反馈:让“虚拟专家”帮你挑出文档里的问题。
  • 数据生成:生成一堆合成数据喂给你的模型。
  • 脑暴场景:虚拟用户群低成本完成头脑风暴。

一看就很严肃是不是?
但我脑洞大开,觉得这玩意儿不就是一个超适合 RPG 模拟的工具吗!于是就搞了一个 D&D 风格的 全自动冒险模拟器,直接拉满我的快乐值。以下就是我整个项目的全过程!


效果展示

为了让大家感受一下模拟器的效果,先给你们看看模拟中的片段,这里是模拟器中队伍面临哥布林埋伏的场景:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
USER --> Thorin: [CONVERSATION]
> 事件发生:一队哥布林埋伏在前方,你们必须决定战斗还是潜行通过。
请讨论以下解决方案:直接冲锋,与哥布林正面对战。, 由Shade先潜行刺杀领头哥布林,减少敌人数量。,
> 尝试谈判,让哥布林放你们过去。
USER --> Elena: [CONVERSATION]
> 事件发生:一队哥布林埋伏在前方,你们必须决定战斗还是潜行通过。
请讨论以下解决方案:直接冲锋,与哥布林正面对战。, 由Shade先潜行刺杀领头哥布林,减少敌人数量。,
> 尝试谈判,让哥布林放你们过去。
USER --> Shade: [CONVERSATION]
> 事件发生:一队哥布林埋伏在前方,你们必须决定战斗还是潜行通过。
请讨论以下解决方案:直接冲锋,与哥布林正面对战。, 由Shade先潜行刺杀领头哥布林,减少敌人数量。,
> 尝试谈判,让哥布林放你们过去。
──────────────────────────────────────────── Dungeon Adventure step 1 of 4 ─────────────────────────────────────────────
Thorin --> Thorin: [THOUGHT]
> I will now act a bit, and then issue DONE.
Thorin acts: [TALK]
> I understand the urgency of the situation. Let's discuss the proposed plan: 1. Direct
> confrontation with the enemy team led by Shade. 2. We should aim to reduce their
> numbers. 3. We can strategize on how to outmaneuver them. It's crucial that we act
> swiftly and decisively.
Thorin --> Thorin: [THOUGHT]
> I will now act a bit, and then issue DONE.
Thorin acts: [DONE]

Thorin --> Elena: [CONVERSATION]
> I understand the urgency of the situation. Let's discuss the proposed plan: 1. Direct
> confrontation with the enemy team led by Shade. 2. We should aim to reduce their
> numbers. 3. We can strategize on how to outmaneuver them. It's crucial that we act
> swiftly and decisively.
Thorin --> Shade: [CONVERSATION]
> I understand the urgency of the situation. Let's discuss the proposed plan: 1. Direct
> confrontation with the enemy team led by Shade. 2. We should aim to reduce their
> numbers. 3. We can strategize on how to outmaneuver them. It's crucial that we act
> swiftly and decisively.
Elena --> Elena: [THOUGHT]
> I will now act a bit, and then issue DONE.
Elena acts: [TALK]
> I agree with the proposed plan, Thorin. Direct confrontation is necessary, and with
> Shade leading the charge, we can effectively reduce their numbers. Let's also consider
> how we can use the terrain to our advantage to outmaneuver them. We need to ensure
> everyone is ready to act quickly and follow the strategy we outline.

部署你的模拟器

1. 准备环境

  1. 安装 Anaconda,并创建虚拟环境:

    1
    2
    conda create -n tinytroupe python=3.10
    conda activate tinytroupe
  2. 更新 pip:

    1
    python -m pip install --upgrade pip

2. 安装 TinyTroupe

克隆项目并安装依赖:

1
2
3
4
git clone https://github.com/microsoft/tinytroupe.git
cd tinytroupe
pip install -e .
*该情况所用时间会长一些请耐心等待,谢谢你们(我自己安装了很久

3. 配置 API 密钥

设置 OpenAI 或 Azure OpenAI 的密钥:

  • Linux/macOS

    1
    export OPENAI_API_KEY=your_api_key
  • Windows
    *请务必用powershell,cmd不行,这是一次性的环境嘟,所以各位同学and老师请注意一下!

    打开 PowerShell 并输入:

    1
    $env:OPENAI_API_KEY="your_api_key"

4. 添加你的代码

在项目根目录新建文件夹 tiny-test,将以下两个文件放入其中:
*为了看起来不那么冗杂,本文的配置代码放在下面,先下载下来吧!不在本blog里面展示!!!!!

你可以通过以下链接下载配置文件和脚本文件的:

运行冒险模拟器:

1
python tiny-test/dnd.py

部署过程中可能遇到的问题

1. API 密钥设置问题

如果环境变量未正确设置,可能会导致无法访问 OpenAI 的 API。


2. 依赖安装问题

确保在虚拟环境中安装依赖,否则可能会出现冲突。


希望你们玩得开心! 🎲