ChoCoFly commited on
Commit
2da85e0
·
verified ·
1 Parent(s): 1404a0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -18
app.py CHANGED
@@ -10,7 +10,7 @@ import threading
10
  from concurrent.futures import ThreadPoolExecutor
11
 
12
 
13
-
14
  class SpyAgent(BasicAgent):
15
  def process_speak(self, name, speak):
16
  prompt = []
@@ -195,30 +195,60 @@ class SpyAgent(BasicAgent):
195
  if not speak_history:
196
  return None
197
 
198
- # 收集其他玩家的发言
199
  prompt = []
200
- prompt.append('你现在是《谁是卧底》游戏的一名玩家。你拿到的词是"{}"。'.format(self.memory.load_variable("word")))
201
- prompt.append('请你思考在《谁是卧底》这个游戏中你的词的对手词可能什么。例如:【胶卷机】-【码相机】,【华为】-【小米】,【蔚来】-【特斯拉】。以下是其他玩家的历史发言,请仔细分析,找出你的词游戏中对手是什么:\n')
202
-
 
 
203
  for name, speaks in speak_history.items():
204
  if name != self.memory.load_variable('name'):
205
  for speak in speaks:
206
  prompt.append(f"{name}: {speak}\n")
207
 
208
- prompt.append('\n请仔细分析以上发言,回答在玩家的聊天记录中,可能存在的你的对手词是什么。请只回答这个词,不要解释。如果确实无法确定,请只回答"未知"。')
209
-
210
- # 调用LLM识别平民词
 
 
 
 
211
  civilian_word = self.llm_caller(''.join(prompt))
212
- logger.info(f"检测到可能的对手词: {civilian_word}")
213
- # 更宽松的匹配条件
214
- if civilian_word and len(civilian_word) < 15: # 有效词汇通常很短
215
- # 过滤常见的拒绝回答形式
216
- if "未知" not in civilian_word and "不确定" not in civilian_word and \
217
- "无法" not in civilian_word and "难以" not in civilian_word and \
218
- civilian_word != self.memory.load_variable("word"):
219
- civilian_word = civilian_word.strip() # 去除空格
220
- return civilian_word
221
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  return None
223
  def interact(self, req=AgentReq) -> AgentResp:
224
  logger.info("spy interact: {}".format(req))
 
10
  from concurrent.futures import ThreadPoolExecutor
11
 
12
 
13
+ import re
14
  class SpyAgent(BasicAgent):
15
  def process_speak(self, name, speak):
16
  prompt = []
 
195
  if not speak_history:
196
  return None
197
 
198
+ # 构建推理提示
199
  prompt = []
200
+ prompt.append('你是《谁是卧底》游戏的评审,需要推理出玩家们描述的词。')
201
+ prompt.append(f'已知一名玩家的词是"{self.memory.load_variable("word")}",有理由信大多数其他玩家在描述另一相关但不同的词')
202
+ prompt.append('《谁是卧底》游戏规则:5名玩家拿到相同的平民词,1名玩家拿到不同的卧底词。')
203
+ prompt.append('请仔细分析以下玩家发言,推理出大多数玩家可能在描述的词:\n')
204
+
205
  for name, speaks in speak_history.items():
206
  if name != self.memory.load_variable('name'):
207
  for speak in speaks:
208
  prompt.append(f"{name}: {speak}\n")
209
 
210
+ prompt.append('\n推理步骤:')
211
+ prompt.append('1. 分析每个玩家发言中描述的特征')
212
+ prompt.append('2. 找出发言中的共同特征')
213
+ prompt.append('3. 根据这些特征,推测最可能的词语')
214
+ prompt.append('4. 考虑这个词与已知词"'+self.memory.load_variable("word")+'"的关系,两者应该相似但有明显区别')
215
+ prompt.append('\n请直接回答你推理出的词语(单词)。如果实在无法推理,只回答"未知"。')
216
+ # 调用LLM进行推理
217
  civilian_word = self.llm_caller(''.join(prompt))
218
+ logger.info(f"平民推理原始返回: '{civilian_word}'")
219
+
220
+ # 处理返回结果
221
+ if civilian_word:
222
+ # 清理结果,移除常见的格式标记和说明文本
223
+ cleaned_word = civilian_word
224
+
225
+ # 如果包含多行,尝试提取最后一行作为答案
226
+ if "\n" in cleaned_word:
227
+ lines = cleaned_word.strip().split("\n")
228
+ for line in reversed(lines):
229
+ if line.strip() and len(line.strip()) < 30: # 找到最后一个非空且较短的行
230
+ cleaned_word = line.strip()
231
+ break
232
+
233
+ # 清理常见的格式符号和修饰语
234
+ cleaned_word = cleaned_word.strip().strip('"\'[]()()""「」『』').strip()
235
+
236
+ # 如果结果包含冒号,取冒号后面的部分
237
+ if ":" in cleaned_word or ":" in cleaned_word:
238
+ parts = re.split(r'[::]', cleaned_word)
239
+ cleaned_word = parts[-1].strip()
240
+
241
+ logger.info(f"清理后的平民词推理结果: '{cleaned_word}'")
242
+
243
+ # 验证结果是否有效
244
+ reject_words = ["未知", "不清楚", "无法确定", "不确定", "无法判断", "无法推理"]
245
+ if (not any(reject in cleaned_word for reject in reject_words) and
246
+ len(cleaned_word) < 20 and len(cleaned_word) > 0 and
247
+ cleaned_word != self.memory.load_variable("word")):
248
+
249
+ logger.info(f"成功推理出平民词: '{cleaned_word}'")
250
+ return cleaned_word
251
+
252
  return None
253
  def interact(self, req=AgentReq) -> AgentResp:
254
  logger.info("spy interact: {}".format(req))