Create werewolf/wolf/wolf_agent.py
Browse files- werewolf/wolf/wolf_agent.py +124 -0
werewolf/wolf/wolf_agent.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agent_build_sdk.model.roles import ROLE_WOLF
|
| 2 |
+
from agent_build_sdk.model.werewolf_model import AgentResp, AgentReq, STATUS_START, STATUS_WOLF_SPEECH, \
|
| 3 |
+
STATUS_VOTE_RESULT, STATUS_SKILL, STATUS_SKILL_RESULT, STATUS_NIGHT_INFO, STATUS_DAY, STATUS_DISCUSS, STATUS_VOTE, \
|
| 4 |
+
STATUS_RESULT, STATUS_NIGHT, STATUS_SKILL
|
| 5 |
+
from agent_build_sdk.utils.logger import logger
|
| 6 |
+
from agent_build_sdk.sdk.role_agent import BasicRoleAgent
|
| 7 |
+
from agent_build_sdk.sdk.agent import format_prompt
|
| 8 |
+
from wolf.prompt import DESC_PROMPT, VOTE_PROMPT, KILL_PROMPT, WOLF_SPEECH_PROMPT,GAME_RULE_PROMPT,CLEAN_USER_PROMPT
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class WolfAgent(BasicRoleAgent):
|
| 12 |
+
"""狼人角色Agent"""
|
| 13 |
+
|
| 14 |
+
def __init__(self, model_name):
|
| 15 |
+
super().__init__(ROLE_WOLF, model_name=model_name)
|
| 16 |
+
self.memory.set_variable("teammates", []) # 存储队友信息
|
| 17 |
+
|
| 18 |
+
def perceive(self, req=AgentReq):
|
| 19 |
+
if req.status == STATUS_START:
|
| 20 |
+
self.memory.clear()
|
| 21 |
+
self.memory.set_variable("name", req.name)
|
| 22 |
+
self.memory.set_variable("teammates", []) # 重置队友信息
|
| 23 |
+
self.memory.append_history(GAME_RULE_PROMPT)
|
| 24 |
+
self.memory.append_history("主持人:你好,你分配到的角色是[狼人]")
|
| 25 |
+
if req.message: # 如果有队友信息
|
| 26 |
+
teammates = req.message.split(",")
|
| 27 |
+
self.memory.set_variable("teammates", teammates)
|
| 28 |
+
self.memory.append_history(f"主持人:你的狼人队友是: {req.message}")
|
| 29 |
+
elif req.status == STATUS_NIGHT:
|
| 30 |
+
self.memory.append_history("主持人:现在进入夜晚,天黑请闭眼")
|
| 31 |
+
elif req.status == STATUS_WOLF_SPEECH:
|
| 32 |
+
# 狼人之间的交流
|
| 33 |
+
if req.name:
|
| 34 |
+
self.memory.append_history(f"狼人{req.name}说: {req.message}")
|
| 35 |
+
else:
|
| 36 |
+
self.memory.append_history("主持人:狼人请睁眼,狼人请互相确认身份,并选择要击杀的对象")
|
| 37 |
+
elif req.status == STATUS_SKILL_RESULT:
|
| 38 |
+
self.memory.append_history(f"主持人:狼人请今晚选择击杀的目标是:{req.name}")
|
| 39 |
+
elif req.status == STATUS_NIGHT_INFO:
|
| 40 |
+
self.memory.append_history(f"主持人:天亮了!昨天晚上的信息是: {req.message}")
|
| 41 |
+
elif req.status == STATUS_DISCUSS: # 发言环节
|
| 42 |
+
if req.name:
|
| 43 |
+
# 其他玩家发言
|
| 44 |
+
# 可以使用模型来过滤掉玩家的注入消息,也可以换一个小模型,实际使用需要考虑对memory加锁,避免interact的时候丢失消息
|
| 45 |
+
# clean_user_message_prompt = format_prompt(CLEAN_USER_PROMPT, {"user_message": req.message})
|
| 46 |
+
# req.message = self.llm_caller(clean_user_message_prompt)
|
| 47 |
+
self.memory.append_history(req.name + ': ' + req.message)
|
| 48 |
+
else:
|
| 49 |
+
# 主持人发言
|
| 50 |
+
self.memory.append_history('主持人: 现在进入第{}天。'.format(str(req.round)))
|
| 51 |
+
self.memory.append_history('主持人: 每个玩家描述自己的信息。')
|
| 52 |
+
elif req.status == STATUS_VOTE: # 投票环节
|
| 53 |
+
self.memory.append_history(f'第{req.round}天的投票环节,{req.name} 投了 {req.message}')
|
| 54 |
+
elif req.status == STATUS_VOTE_RESULT: # 投票环节
|
| 55 |
+
out_player = req.name if req.name else req.message
|
| 56 |
+
if out_player:
|
| 57 |
+
self.memory.append_history('主持人: 投票结果是:{}。'.format(out_player))
|
| 58 |
+
else:
|
| 59 |
+
self.memory.append_history('主持人: 无人出局。')
|
| 60 |
+
elif req.status == STATUS_RESULT:
|
| 61 |
+
self.memory.append_history(req.message)
|
| 62 |
+
else:
|
| 63 |
+
raise NotImplementedError
|
| 64 |
+
|
| 65 |
+
def interact(self, req=AgentReq) -> AgentResp:
|
| 66 |
+
logger.info("wolf interact: {}".format(req))
|
| 67 |
+
if req.status == STATUS_DISCUSS:
|
| 68 |
+
if req.message:
|
| 69 |
+
self.memory.append_history(req.message)
|
| 70 |
+
teammates = self.memory.load_variable("teammates")
|
| 71 |
+
prompt = format_prompt(DESC_PROMPT,
|
| 72 |
+
{"name": self.memory.load_variable("name"),
|
| 73 |
+
"teammates": teammates,
|
| 74 |
+
"history": "\n".join(self.memory.load_history())
|
| 75 |
+
})
|
| 76 |
+
logger.info("prompt:" + prompt)
|
| 77 |
+
result = self.llm_caller(prompt)
|
| 78 |
+
logger.info("wolf interact result: {}".format(result))
|
| 79 |
+
return AgentResp(success=True, result=result, errMsg=None)
|
| 80 |
+
|
| 81 |
+
elif req.status == STATUS_VOTE:
|
| 82 |
+
self.memory.append_history('主持人: 到了投票的时候了。每个人,请指向你认为可能是狼人的人。')
|
| 83 |
+
teammates = self.memory.load_variable("teammates")
|
| 84 |
+
choices = [name for name in req.message.split(",")
|
| 85 |
+
if name != self.memory.load_variable("name") and name not in teammates] # 排除自己和队友
|
| 86 |
+
self.memory.set_variable("choices", choices)
|
| 87 |
+
prompt = format_prompt(VOTE_PROMPT, {"name": self.memory.load_variable("name"),
|
| 88 |
+
"teammates": teammates,
|
| 89 |
+
"choices": choices,
|
| 90 |
+
"history": "\n".join(self.memory.load_history())
|
| 91 |
+
})
|
| 92 |
+
logger.info("prompt:" + prompt)
|
| 93 |
+
result = self.llm_caller(prompt)
|
| 94 |
+
logger.info("wolf interact result: {}".format(result))
|
| 95 |
+
return AgentResp(success=True, result=result, errMsg=None)
|
| 96 |
+
|
| 97 |
+
elif req.status == STATUS_WOLF_SPEECH:
|
| 98 |
+
teammates = self.memory.load_variable("teammates")
|
| 99 |
+
prompt = format_prompt(WOLF_SPEECH_PROMPT, {
|
| 100 |
+
"name": self.memory.load_variable("name"),
|
| 101 |
+
"teammates": teammates,
|
| 102 |
+
"history": "\n".join(self.memory.load_history())
|
| 103 |
+
})
|
| 104 |
+
logger.info("prompt:" + prompt)
|
| 105 |
+
result = self.llm_caller(prompt)
|
| 106 |
+
logger.info("wolf speech result: {}".format(result))
|
| 107 |
+
return AgentResp(success=True, result=result, errMsg=None)
|
| 108 |
+
|
| 109 |
+
elif req.status == STATUS_SKILL:
|
| 110 |
+
teammates = self.memory.load_variable("teammates")
|
| 111 |
+
choices = [name for name in req.message.split(",")
|
| 112 |
+
if name != self.memory.load_variable("name") and name not in teammates] # 排除自己和队友
|
| 113 |
+
self.memory.set_variable("choices", choices)
|
| 114 |
+
prompt = format_prompt(KILL_PROMPT, {
|
| 115 |
+
"name": self.memory.load_variable("name"),
|
| 116 |
+
"choices": choices,
|
| 117 |
+
"history": "\n".join(self.memory.load_history())
|
| 118 |
+
})
|
| 119 |
+
logger.info("prompt:" + prompt)
|
| 120 |
+
result = self.llm_caller(prompt)
|
| 121 |
+
logger.info("wolf kill result: {}".format(result))
|
| 122 |
+
return AgentResp(success=True, result=result, skillTargetPlayer=result, errMsg=None)
|
| 123 |
+
else:
|
| 124 |
+
raise NotImplementedError
|