From 91c6c6b200a6f7f9b9e9a44240ef51e51ea2567e Mon Sep 17 00:00:00 2001 From: yzlin Date: Sat, 30 Sep 2023 23:36:54 +0800 Subject: [PATCH] example of modifying role strategy --- examples/werewolf_game/actions/__init__.py | 3 ++- .../werewolf_game/actions/werewolf_actions.py | 22 +++++++++++++++++++ examples/werewolf_game/roles/moderator.py | 3 +++ examples/werewolf_game/roles/werewolf.py | 5 +++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/werewolf_game/actions/__init__.py b/examples/werewolf_game/actions/__init__.py index cf358e92b..16b9391b3 100644 --- a/examples/werewolf_game/actions/__init__.py +++ b/examples/werewolf_game/actions/__init__.py @@ -1,6 +1,6 @@ from examples.werewolf_game.actions.moderator_actions import InstructSpeak from examples.werewolf_game.actions.common_actions import Speak -from examples.werewolf_game.actions.werewolf_actions import Hunt +from examples.werewolf_game.actions.werewolf_actions import Hunt, Impersonate from examples.werewolf_game.actions.guard_actions import Protect from examples.werewolf_game.actions.seer_actions import Verify from examples.werewolf_game.actions.witch_actions import Save, Poison @@ -12,4 +12,5 @@ ACTIONS = { "Verify": Verify, "Save": Save, "Poison": Poison, + "Impersonate": Impersonate, } diff --git a/examples/werewolf_game/actions/werewolf_actions.py b/examples/werewolf_game/actions/werewolf_actions.py index 0a750a02d..410221cb0 100644 --- a/examples/werewolf_game/actions/werewolf_actions.py +++ b/examples/werewolf_game/actions/werewolf_actions.py @@ -1,4 +1,5 @@ from metagpt.actions import Action +from examples.werewolf_game.actions.common_actions import Speak class Hunt(Action): """Action: choose a villager to kill""" @@ -23,3 +24,24 @@ class Hunt(Action): # rsp = "Kill Player 1" return rsp + +class Impersonate(Speak): + """Action: werewolf impersonating a good guy in daytime speak""" + + PROMPT_TEMPLATE = """ + ## BACKGROUND + It's a Werewolf game, you are {profile}, say whatever possible to increase your chance of win, + ## HISTORY + You have knowledge to the following conversation: + {context} + ## ATTENTION: Try continuously impersonating a role with special ability, such as a Seer or a Witch, in order to mislead + other players, make them trust you, and thus hiding your werewolf identity + ## YOUR TURN + Please follow the moderator's latest instruction, FIGURE OUT if you need to speak your opinion or directly to vote, + 1. If the instruction is to speak, speak in 100 words; + 2. If the instruction is to vote, you MUST vote and ONLY say "I vote to eliminate PlayerX", where X is the player index, DO NOT include any other words. + Your will say: + """ + + def __init__(self, name="Impersonate", context=None, llm=None): + super().__init__(name, context, llm) diff --git a/examples/werewolf_game/roles/moderator.py b/examples/werewolf_game/roles/moderator.py index c02400918..df0b870cd 100644 --- a/examples/werewolf_game/roles/moderator.py +++ b/examples/werewolf_game/roles/moderator.py @@ -1,6 +1,7 @@ import re from collections import Counter +from metagpt.const import WORKSPACE_ROOT from metagpt.roles import Role from metagpt.schema import Message from metagpt.logs import logger @@ -183,6 +184,8 @@ class Moderator(Role): # 进行完一夜一日的循环,打印一次完整发言历史 logger.info("a night and day cycle completed, examine all history") print(self.get_all_memories()) + with open(WORKSPACE_ROOT / 'werewolf_transcript.txt', "w") as f: + f.write(self.get_all_memories()) # 根据_think的结果,执行InstructSpeak还是ParseSpeak, 并将结果返回 if isinstance(todo, InstructSpeak): diff --git a/examples/werewolf_game/roles/werewolf.py b/examples/werewolf_game/roles/werewolf.py index 4a7393d77..5426ca7c4 100644 --- a/examples/werewolf_game/roles/werewolf.py +++ b/examples/werewolf_game/roles/werewolf.py @@ -1,5 +1,5 @@ from examples.werewolf_game.roles.base_player import BasePlayer -from examples.werewolf_game.actions import Speak, Hunt +from examples.werewolf_game.actions import Speak, Hunt, Impersonate from metagpt.schema import Message from metagpt.logs import logger @@ -24,7 +24,8 @@ class Werewolf(BasePlayer): # 根据自己定义的角色Action,对应地去run,run的入参可能不同 if isinstance(todo, Speak): - rsp = await todo.run(profile=self.profile, context=memories) + # rsp = await todo.run(profile=self.profile, context=memories) + rsp = await Impersonate().run(profile=self.profile, context=memories) msg = Message( content=rsp, role=self.profile, sent_from=self.name, cause_by=Speak, send_to="", restricted_to="",