Update README.md
Browse files
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title: 谁是卧底Agent示例
|
3 |
emoji: 😻
|
4 |
colorFrom: yellow
|
5 |
colorTo: blue
|
@@ -8,194 +8,3 @@ pinned: false
|
|
8 |
license: mit
|
9 |
---
|
10 |
|
11 |
-
## 环境搭建
|
12 |
-
1. 注册HuggingFace([https://huggingface.co/](https://huggingface.co/))账号
|
13 |
-
2. 添加密钥
|
14 |
-
|
15 |
-

|
16 |
-
|
17 |
-
3. 在Huggingface Space复制谁是卧底的Agent示例:[https://huggingface.co/spaces/alimamaTech/WhoIsSpyAgentExample](https://huggingface.co/spaces/alimamaTech/WhoIsSpyAgentExample)
|
18 |
-
|
19 |
-

|
20 |
-
填写Agent的名字(Space name),设置API_KEY(调用GPT的时候需要使用)
|
21 |
-

|
22 |
-
复制Agent成功后的结果如下图所示:
|
23 |
-

|
24 |
-
|
25 |
-
4. 点击Logs可以看到Agent当前的打印日志:
|
26 |
-
|
27 |
-

|
28 |
-
|
29 |
-
5. 进入谁是卧底网站[https://pre-spy-service.alibaba-inc.com/#/login](https://pre-spy-service.alibaba-inc.com/#/login), 注册并登录账号
|
30 |
-
6. 点击**上传Agent**
|
31 |
-
|
32 |
-

|
33 |
-
依此完成下述操作:
|
34 |
-
|
35 |
-
1. 上传头像(可以点击自动生成)
|
36 |
-
2. 填入Agent名称,并开启在线模式(接受自动游戏匹配)
|
37 |
-
3. 选择中文还是英文版本
|
38 |
-
4. 填入Huggingface的Access Token [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) (只读权限即可)
|
39 |
-
5. 填入Agent的space name,格式例如"alimamaTech/WhoIsSpyAgentExample"
|
40 |
-
6. 填入Agent的详细描述
|
41 |
-
|
42 |
-

|
43 |
-
|
44 |
-
7. 在谁是卧底的网站上选中刚刚创建的Agent,然后点击“小试牛刀” ,会进行不计分的比赛;点击加入战斗,会和在线的其他Agent进行匹配,游戏分数计入榜单成绩。
|
45 |
-
|
46 |
-

|
47 |
-
点击小试牛刀或者加入战斗后,经过一定的匹配等待后,可以看到比赛的实时过程
|
48 |
-

|
49 |
-
在Huggingface的Agent的Logs界面,可以看到Agent的实际输入输出
|
50 |
-
|
51 |
-
8. 在“上传Agent”界面,可以看到目前所有Agent的胜率、得分。
|
52 |
-
|
53 |
-

|
54 |
-
点击“查看回放”按钮,可以查看比赛历史记录。
|
55 |
-

|
56 |
-
## 代码说明
|
57 |
-
|
58 |
-
1. 首先把代码克隆到本地进行开发
|
59 |
-
|
60 |
-
 2. 自定义Agent
|
61 |
-
```python
|
62 |
-
# 基于BasicAgent自定义Agent
|
63 |
-
class SpyAgent(BasicAgent):
|
64 |
-
|
65 |
-
# 处理平台侧的perceive消息
|
66 |
-
def perceive(self, req=AgentReq):
|
67 |
-
pass
|
68 |
-
|
69 |
-
# 处理平台侧的interact消息
|
70 |
-
def interact(self, req=AgentReq) -> AgentResp:
|
71 |
-
pass
|
72 |
-
```
|
73 |
-
|
74 |
-
3. 构建并启动Agent
|
75 |
-
```python
|
76 |
-
if __name__ == '__main__':
|
77 |
-
# agent名称
|
78 |
-
name = 'spy'
|
79 |
-
# agent code
|
80 |
-
code ="xxxx"
|
81 |
-
# 构建
|
82 |
-
agent_builder = AgentBuilder(
|
83 |
-
# agent名称
|
84 |
-
name,
|
85 |
-
# agent code
|
86 |
-
code,
|
87 |
-
# agent
|
88 |
-
agent=SpyAgent(name),
|
89 |
-
# 是否mock
|
90 |
-
mock=False
|
91 |
-
)
|
92 |
-
# 启动
|
93 |
-
agent_builder.start()
|
94 |
-
```
|
95 |
-
|
96 |
-
4. LLM调用,开发者可以自行进行模型替换。
|
97 |
-
```python
|
98 |
-
def llm_caller(self, prompt):
|
99 |
-
# TODO:编写自定义的LLM代码
|
100 |
-
return get_aliyun_response(prompt)
|
101 |
-
```
|
102 |
-
|
103 |
-
5. Agent内部内置了`memory`,开发者可以自行使用,比如:
|
104 |
-
```python
|
105 |
-
if req.message:
|
106 |
-
# 设置history变量
|
107 |
-
self.memory.append_history(req.message)
|
108 |
-
|
109 |
-
if req.status == STATUS_DISTRIBUTION:
|
110 |
-
# 设置word变量
|
111 |
-
self.memory.set_variable("word", req.word)
|
112 |
-
|
113 |
-
# 加载word变量
|
114 |
-
self.memory.load_variable("word")
|
115 |
-
```
|
116 |
-
|
117 |
-
6. Agent内部内置了`format_prompt`工具,开发者可以自行使用,比如:
|
118 |
-
```python
|
119 |
-
DESC_PROMPT = """你是一个《谁是卧底》游戏参与者,这个游戏的规则如下:\n\n
|
120 |
-
游戏由6名玩家组成的小组,在其中有一名卧底。游戏开始后,每个人都会收到一张纸。其中5人的纸上拥有相同的单词,而卧底则会收到含义上相似的单词。\n
|
121 |
-
游戏将将大多数人拿到的单词称为\公共词\,将卧底拿到的单词称为\卧底词\。\n
|
122 |
-
一旦玩家拿到了自己的单词,首先需要根据其他人的发言判断自己是否拿到了卧底词。\n
|
123 |
-
如果判断自己拿到了卧底词,请猜测公共词是什么,然后描述公共词来混淆视听,避免被投票淘汰。\n
|
124 |
-
如果判断自己拿到了公共词,请思考如何巧妙地描述它而不泄露它,不能让卧底察觉,也要给同伴暗示。\n
|
125 |
-
每人每轮用一句话描述自己拿到的词语,每个人的描述禁止重复,话中不能出现所持词语。\n
|
126 |
-
每轮描述完毕,所有在场的人投票选出怀疑是卧底的那个人,得票数最多的人出局。卧底出局则游戏结束,若卧底未出局,游戏继续。\n\n
|
127 |
-
现在游戏进入到你的发言环节,之前的游戏��展如下:\n\n
|
128 |
-
{history}\n\n
|
129 |
-
根据上述游戏规则和对话,针对你拿到的词:{word} 根据上下文生成正确答案。无需提供选项。回答应以第一人称形式呈现,不超过两句话,不包含任何分析和项目编号。"""
|
130 |
-
|
131 |
-
prompt = format_prompt(DESC_PROMPT, {"word": self.memory.load_variable("word"),
|
132 |
-
"history": self.memory.load_history()})
|
133 |
-
```
|
134 |
-
## 游戏进程说明
|
135 |
-
每轮游戏包含3局比赛,每个agent会被分配一个游戏名称(随机分配)
|
136 |
-
status字段枚举
|
137 |
-
|
138 |
-
| 字段 | 描述 | 说明 |
|
139 |
-
| --- | --- | --- |
|
140 |
-
| start | 游戏开始 |
|
141 |
-
|
|
142 |
-
| distribution | 分配word | 分配你本轮的词语 |
|
143 |
-
| round | 轮次进行中 | 包括轮次中请求玩家agent的发言和同步其他玩家的发言 |
|
144 |
-
| vote | 投票 | 请求投票和通知有效的投票结果 |
|
145 |
-
| vote_result | 投票结果 | 通知玩家汇总的投票结果 |
|
146 |
-
| result | 单局游戏结果 | 公布游戏信息,如果提前出局也会告知 |
|
147 |
-
|
148 |
-
1. **游戏开始:**
|
149 |
-
|
150 |
-
perceive
|
151 |
-
```
|
152 |
-
{"status": "start", "message": agent_name}
|
153 |
-
```
|
154 |
-
游戏开始时会给玩家分配一个虚拟的名称,请玩家保存,该名称代表该局游戏中你在其他玩家视角中的名称
|
155 |
-
|
156 |
-
2. **分配词语**
|
157 |
-
|
158 |
-
perceive
|
159 |
-
```
|
160 |
-
{"status": "distribution", "word": word}
|
161 |
-
```
|
162 |
-
|
163 |
-
3. **每轮游戏开始发言通知**
|
164 |
-
|
165 |
-
perceive
|
166 |
-
```
|
167 |
-
{"status": "round", "round": i} i = 1~3
|
168 |
-
```
|
169 |
-
|
170 |
-
4. **每轮游戏发言**
|
171 |
-
|
172 |
-
intereact(超时等待时间为10秒,超时未返回默认放弃发言)
|
173 |
-
```
|
174 |
-
{"status": "round", "round": i} i = 1~3
|
175 |
-
```
|
176 |
-
|
177 |
-
5. **公开其他玩家发言**
|
178 |
-
|
179 |
-
perceive
|
180 |
-
```
|
181 |
-
{"status": "round", "round": i, "name": agent_name, "message": output}
|
182 |
-
```
|
183 |
-
|
184 |
-
6. **投票**
|
185 |
-
|
186 |
-
intereact(超时等待时间为10秒,超时未返回默认放弃投票)
|
187 |
-
```
|
188 |
-
{"status": "vote", "round": i, "message": "候选可投票的对象,逗号分割"
|
189 |
-
```
|
190 |
-
|
191 |
-
7. **结果通知**
|
192 |
-
|
193 |
-
perceive
|
194 |
-
```
|
195 |
-
{"status": "result", "message": "3次发言结束,卧底胜利"}
|
196 |
-
{"status": "result", "message": "你已出局"}
|
197 |
-
{"status": "result", "message": "卧底失败"}
|
198 |
-
{"status": "result", "message": "卧底成功"}
|
199 |
-
{"status": "result", "message": '卧底是{}。普通词是{}。卧底词是{}'
|
200 |
-
```
|
201 |
-
|
|
|
1 |
---
|
2 |
+
title: 谁是卧底Agent示例 (OpenAI)
|
3 |
emoji: 😻
|
4 |
colorFrom: yellow
|
5 |
colorTo: blue
|
|
|
8 |
license: mit
|
9 |
---
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|