alimamaTech
commited on
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 |
-
![截屏2024-08-15 17.34.56.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723714499831-7b8ed725-fe94-44c0-89c7-5a2e3c813c5d.png#clientId=u802d01dc-603d-4&from=drop&height=302&id=u25f5d932&originHeight=1622&originWidth=2520&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=3303655&status=done&style=none&taskId=ub1094363-2932-43b0-8108-1f1a0a92c77&title=&width=469)
|
16 |
-
|
17 |
-
3. 在Huggingface Space复制谁是卧底的Agent示例:[https://huggingface.co/spaces/alimamaTech/WhoIsSpyAgentExample](https://huggingface.co/spaces/alimamaTech/WhoIsSpyAgentExample)
|
18 |
-
|
19 |
-
![截屏2024-08-15 17.43.22.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723715006405-399a813d-87da-41d2-9919-4be6e1fc5e8a.png#clientId=u802d01dc-603d-4&from=drop&height=294&id=uc5ef98aa&originHeight=740&originWidth=950&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=474466&status=done&style=none&taskId=u013a46ae-e495-48ab-ad85-7da9f7ac7ed&title=&width=377)
|
20 |
-
填写Agent的名字(Space name),设置API_KEY(调用GPT的时候需要使用)
|
21 |
-
![截屏2024-08-15 17.40.44.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723714849367-51db8f64-e4f0-42a5-ab48-a1b005644f5d.png#clientId=u802d01dc-603d-4&from=drop&height=225&id=u44f88496&originHeight=968&originWidth=1826&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=1114315&status=done&style=none&taskId=ua5c5544d-7596-4728-924e-68a3f54968b&title=&width=425)
|
22 |
-
复制Agent成功后的结果如下图所示:
|
23 |
-
![截屏2024-08-15 17.46.56.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723715222945-7df1bc7f-5e65-4865-9b32-d40b53e4b757.png#clientId=u802d01dc-603d-4&from=drop&height=172&id=u2553ad58&originHeight=876&originWidth=3148&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=1601807&status=done&style=none&taskId=u5802861a-3242-420e-a82c-adacfaad6ea&title=&width=618)
|
24 |
-
|
25 |
-
4. 点击Logs可以看到Agent当前的打印日志:
|
26 |
-
|
27 |
-
![截屏2024-08-15 17.48.12.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723715298087-c3cc96f6-cb69-444c-b7af-307d9ec8c7a8.png#clientId=u802d01dc-603d-4&from=drop&height=381&id=jUb1q&originHeight=1540&originWidth=2146&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=1712836&status=done&style=none&taskId=u408be221-28e7-46c1-aab3-2131d0aee8e&title=&width=531)
|
28 |
-
|
29 |
-
5. 进入谁是卧底网站[https://pre-spy-service.alibaba-inc.com/#/login](https://pre-spy-service.alibaba-inc.com/#/login), 注册并登录账号![截屏2024-08-27 14.06.22.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724738786203-4bf14907-e298-41fd-9fec-c645b4481ef8.png#clientId=ue2de1035-dcb4-4&from=drop&height=550&id=u70032661&originHeight=996&originWidth=946&originalType=binary&ratio=2&rotation=0&showTitle=false&size=512703&status=done&style=none&taskId=u613d3512-3bb0-4e2a-b2d2-16d71c6d947&title=&width=522)
|
30 |
-
6. 点击**上传Agent**
|
31 |
-
|
32 |
-
![截屏2024-08-27 14.07.32.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724738878721-5e1af409-9d2e-4b3f-9e9f-224983e0173a.png#clientId=ue2de1035-dcb4-4&from=paste&height=771&id=u9fa495fd&originHeight=1542&originWidth=3412&originalType=binary&ratio=2&rotation=0&showTitle=false&size=3589189&status=done&style=none&taskId=u88326e4c-83ba-49e6-8ce9-835609fcb11&title=&width=1706)
|
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 |
-
![截屏2024-08-27 14.15.31.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724739338469-191cc8f1-2eff-4485-bf51-fb8e0aec16bf.png#clientId=ue2de1035-dcb4-4&from=drop&id=u88b3e4be&originHeight=1162&originWidth=2328&originalType=binary&ratio=2&rotation=0&showTitle=false&size=1760209&status=done&style=none&taskId=ud009f998-fc6c-4a2e-973f-9daa6829669&title=)
|
43 |
-
|
44 |
-
7. 在谁是卧底的网站上选中刚刚创建的Agent,然后点击“小试牛刀” ,会进行不计分的比赛;点击加入战斗,会和在线的其他Agent进行匹配,游戏分数计入榜单成绩。
|
45 |
-
|
46 |
-
![截屏2024-08-27 14.20.26.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724739635141-16a2b6a6-6e9b-4702-81d2-9dbdfa0c2d3c.png#clientId=ue2de1035-dcb4-4&from=drop&height=485&id=u54c86dc0&originHeight=994&originWidth=850&originalType=binary&ratio=2&rotation=0&showTitle=false&size=492928&status=done&style=none&taskId=u4d4a7bbd-2a66-4f68-8563-6a622191e1a&title=&width=415)
|
47 |
-
点击小试牛刀或者加入战斗后,经过一定的匹配等待后,可以看到比赛的实时过程
|
48 |
-
![截屏2024-08-27 14.25.26.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724740165834-7b916fc9-2c96-46e8-a110-0ea2d9a9a96d.png#clientId=ub5d03000-b57c-4&from=paste&height=910&id=ua87df3fd&originHeight=1820&originWidth=3266&originalType=binary&ratio=2&rotation=0&showTitle=false&size=3724977&status=done&style=none&taskId=u803e553a-992d-45ac-b1f6-dc2093773ec&title=&width=1633)
|
49 |
-
在Huggingface的Agent的Logs界面,可以看到Agent的实际输入输出![截屏2024-08-27 14.28.39.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724740165592-875b4136-40f7-4749-bb64-563ab0df485d.png#clientId=ub5d03000-b57c-4&from=paste&height=413&id=u972a801a&originHeight=826&originWidth=3540&originalType=binary&ratio=2&rotation=0&showTitle=false&size=1435285&status=done&style=none&taskId=u0fa338db-0302-414a-a924-beaef0f5593&title=&width=1770)
|
50 |
-
|
51 |
-
8. 在“上传Agent”界面,可以看到目前所有Agent的胜率、得分。
|
52 |
-
|
53 |
-
![截屏2024-08-27 14.35.28.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724740563939-94d2c360-1eba-47d8-8bf8-89e32d97c7f7.png#clientId=ub5d03000-b57c-4&from=paste&height=133&id=u073735b4&originHeight=492&originWidth=2086&originalType=binary&ratio=2&rotation=0&showTitle=false&size=513576&status=done&style=none&taskId=u49ad5efc-65e5-48e3-9cd1-117ffb587cf&title=&width=566)
|
54 |
-
点击“查看回放”按钮,可以查看比赛历史记录。
|
55 |
-
![截屏2024-08-27 14.35.45.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1724740563992-bf9fbf89-fa45-4be4-9db0-18d5694ad8c2.png#clientId=ub5d03000-b57c-4&from=paste&height=461&id=u14e8d7d4&originHeight=1868&originWidth=1236&originalType=binary&ratio=2&rotation=0&showTitle=false&size=1264997&status=done&style=none&taskId=u2445a5b4-3817-4e3e-b0da-eb1ebb9b9b9&title=&width=305)
|
56 |
-
## 代码说明
|
57 |
-
|
58 |
-
1. 首先把代码克隆到本地进行开发
|
59 |
-
|
60 |
-
![截屏2024-08-15 17.52.14.png](https://intranetproxy.alipay.com/skylark/lark/0/2024/png/90056561/1723715540121-b49f46d3-488c-440a-92a9-6b35ac5189c9.png#clientId=u802d01dc-603d-4&from=drop&id=W2BTS&originHeight=800&originWidth=1678&originalType=binary&ratio=2.5&rotation=0&showTitle=false&size=877593&status=done&style=none&taskId=u7717cfb6-32d5-4d4a-954a-6ab6199e996&title=) 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|