Langchain を使用してエージェントを記述し、複数人が龍と地下都市をプレイするシミュレーションを行う

《龙与地下城》(英语:Dungeons & Dragons,简称D&D或DnD)是一款奇幻背景的角色扮演游戏。

D&D 是一种结构化但开放式的设定,通常在室内进行,参与者围坐在桌子周围。典型的游戏中,一个玩家担任地下城主(Dungeon Master,简称DM),而其他玩家各自控制一个角色,代表一个虚构世界中的个体。

Langchain 有一个实例,即使用 LLM Agents 来玩这个游戏,主要代码如下:https://python.langchain.com/docs/use_cases/more/agents/agent_simulations/multi_player_dnd

之前的设置的代码就可以看原链接,我就把演示开始玩的部分打出来。

打印叙述者和角色的描述

print("Storyteller Description:")
print(storyteller_description)
for character_name, character_description in zip(
character_names, character_descriptions
):
print(f"{character_name} Description:")
print(character_description)

使任务更具体化

quest_specifier_prompt = [
SystemMessage(content="You can make a task more specific."),
HumanMessage(
content=f"""{game_description}

You are the storyteller, {storyteller_name}.
Please make the quest more specific. Be creative and imaginative.
Please reply with the specified quest in {word_limit} words or less.
Speak directly to the characters: {*character_names,}.
Do not add anything else."""

),
]
specified_quest = ChatOpenAI(temperature=1.0)(quest_specifier_prompt).content

print(f"Original quest:\n{quest}\n")
print(f"Detailed quest:\n{specified_quest}\n")

Original quest:

Find all of Lord Voldemort's seven horcruxes.

Detailed quest:

Harry Potter, Ron Weasley, Hermione Granger, and Argus Filch, your mission awaits. In the hidden depths of the Forbidden Forest lies a forgotten vault. Guarded by ancient creatures bewitched by Lord Voldemort himself, it holds a vital clue regarding his horcruxes. Explore the vault, overcome its treacherous trials, and unravel Lord Voldemort's seven secrets.

开始对话模拟

max_iters = 20
n = 0

simulator = DialogueSimulator(
agents=[storyteller] + characters, selection_function=select_next_speaker
)
simulator.reset()
simulator.inject(storyteller_name, specified_quest)
print(f"({storyteller_name}): {specified_quest}")
print("\n")

while n < max_iters:
name, message = simulator.step()
print(f"({name}): {message}")
print("\n")
n += 1

开始运行之后,这几个 agent 大哥就聊起来了