jiangjunguang1123 commited on
Commit
b0f4c90
·
verified ·
1 Parent(s): 2400427

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from agent_build_sdk.builder import AgentBuilder
2
+ from agent_build_sdk.model.model import AgentResp, AgentReq, STATUS_DISTRIBUTION, STATUS_ROUND, STATUS_VOTE, \
3
+ STATUS_START, STATUS_VOTE_RESULT, STATUS_RESULT
4
+ from agent_build_sdk.sdk.agent import BasicAgent
5
+ from agent_build_sdk.sdk.agent import format_prompt
6
+
7
+ from prompts import DESC_PROMPT, VOTE_PROMPT
8
+ from agent_build_sdk.utils.logger import logger
9
+
10
+ class SpyAgent(BasicAgent):
11
+
12
+ def perceive(self, req=AgentReq):
13
+ logger.info("spy perceive: {}".format(req))
14
+ if req.status == STATUS_START: # 开始新的一局比赛
15
+ self.memory.clear()
16
+ self.memory.set_variable("name", req.message)
17
+ self.memory.append_history(
18
+ '主持人: 女士们先生们,欢迎来到《谁是卧底》游戏!我们有一个由6名玩家组成的小组,在其中有一名卧底。让我们开始吧!每个人都会收到一张纸。其中5人的纸上拥有相同的单词,而卧底则会收到含义上相似的单词。我们将大多数人拿到的单词称为"公共词",将卧底拿到的单词称为"卧底词"。一旦你拿到了你的单词,首先需要根据其他人的发言判断自己是否拿到了卧底词。如果判断自己拿到了卧底词,请猜测公共词是什么,然后描述公共词来混淆视听,避免被投票淘汰。如果判断自己拿到了公共词,请思考如何巧妙地描述它而不泄露它,不能让卧底察觉,也要给同伴暗示。每人每轮用一句话描述自己拿到的词语,每个人的描述禁止重复,话中不能出现所持词语。每轮描述完毕,所有在场的人投票选出怀疑是卧底的那个人,得票数最多的人出局。卧底出局则游戏结束,若卧底未出局,游戏继续。现在游戏开始。')
19
+ elif req.status == STATUS_DISTRIBUTION: # 分配单词
20
+ self.memory.set_variable("word", req.word)
21
+ self.memory.append_history(
22
+ '主持人: 你好,{},你分配到的单词是:{}'.format(self.memory.load_variable("name"), req.word))
23
+ elif req.status == STATUS_ROUND: # 发言环节
24
+ if req.name:
25
+ # 其他玩家发言
26
+ self.memory.append_history(req.name + ': ' + req.message)
27
+ else:
28
+ # 主持人发言
29
+ self.memory.append_history('主持人: 现在进入第{}轮。'.format(str(req.round)))
30
+ self.memory.append_history('主持人: 每个玩家描述自己分配到的单词。')
31
+ elif req.status == STATUS_VOTE: # 投票环节
32
+ self.memory.append_history(req.name + ': ' + req.message)
33
+ elif req.status == STATUS_VOTE_RESULT: # 投票环节
34
+ if req.name:
35
+ self.memory.append_history('主持人: 投票结果是:{}。'.format(req.name))
36
+ else:
37
+ self.memory.append_history('主持人: 无人出局。')
38
+ elif req.status == STATUS_RESULT:
39
+ self.memory.append_history(req.message)
40
+ else:
41
+ raise NotImplementedError
42
+
43
+ def interact(self, req=AgentReq) -> AgentResp:
44
+ logger.info("spy interact: {}".format(req))
45
+ if req.status == STATUS_ROUND:
46
+ prompt = format_prompt(DESC_PROMPT,
47
+ {"name": self.memory.load_variable("name"),
48
+ "word": self.memory.load_variable("word"),
49
+ "history": "\n".join(self.memory.load_history())
50
+ })
51
+ logger.info("prompt:" + prompt)
52
+ result = self.llm_caller(prompt)
53
+ logger.info("spy interact result: {}".format(result))
54
+ return AgentResp(success=True, result=result, errMsg=None)
55
+
56
+ elif req.status == STATUS_VOTE:
57
+ self.memory.append_history('主持人: 到了投票的时候了。每个人,请指向你认为可能是卧底的人。')
58
+ choices = [name for name in req.message.split(",") if name != self.memory.load_variable("name")] # 排除自己
59
+ self.memory.set_variable("choices", choices)
60
+ prompt = format_prompt(VOTE_PROMPT, {"name": self.memory.load_variable("name"),
61
+ "choices": choices,
62
+ "history": "\n".join(self.memory.load_history())
63
+ })
64
+ logger.info("prompt:" + prompt)
65
+ result = self.llm_caller(prompt)
66
+ logger.info("spy interact result: {}".format(result))
67
+ return AgentResp(success=True, result=result, errMsg=None)
68
+ else:
69
+ raise NotImplementedError
70
+
71
+
72
+ if __name__ == '__main__':
73
+ name = 'spy'
74
+ code = "xxxx"
75
+ agent_builder = AgentBuilder(name, code, agent=SpyAgent(name), mock=True)
76
+ agent_builder.start()