Update app.py
Browse files
app.py
CHANGED
|
@@ -272,6 +272,48 @@ class SpyAgent(BasicAgent):
|
|
| 272 |
logger.info(f"成功推理出卧底词: '{spy_word}'")
|
| 273 |
return spy_word
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
return None
|
| 276 |
def detect_civilian_word(self):
|
| 277 |
"""分析历史发言,尝试识别平民词"""
|
|
@@ -361,6 +403,26 @@ class SpyAgent(BasicAgent):
|
|
| 361 |
# 在发言前更新对手词
|
| 362 |
civilian_word = self.detect_civilian_word()
|
| 363 |
spy_word = self.detect_spy_word()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
if civilian_word:
|
| 365 |
self.memory.set_variable('sim_word', civilian_word)
|
| 366 |
|
|
|
|
| 272 |
logger.info(f"成功推理出卧底词: '{spy_word}'")
|
| 273 |
return spy_word
|
| 274 |
|
| 275 |
+
return None
|
| 276 |
+
def analyze_similar_words(self, general_concept):
|
| 277 |
+
"""当推断出的平民词和卧底词相同时,进行深度分析尝试细分概念"""
|
| 278 |
+
speak_history = self.memory.load_variable('speak_history')
|
| 279 |
+
if not speak_history:
|
| 280 |
+
return None
|
| 281 |
+
|
| 282 |
+
my_word = self.memory.load_variable("word")
|
| 283 |
+
|
| 284 |
+
prompt = []
|
| 285 |
+
prompt.append(f'在《谁是卧底》游戏中,初步分析显示玩家们可能在描述"{general_concept}"这个概念。')
|
| 286 |
+
prompt.append(f'你的词是"{my_word}"。游戏规则要求5名玩家拿到相同的平民词,1名玩家拿到不同但相关的卧底词。')
|
| 287 |
+
prompt.append('由于平民词和卧底词通常是近义词或同类事物,我需要你进行更精细的分析,找出可能的具体词语。')
|
| 288 |
+
prompt.append('请分析以下玩家发言,尝试识别出更具体的词语类别:\n')
|
| 289 |
+
|
| 290 |
+
# 添加玩家发言供分析
|
| 291 |
+
for name, speaks in speak_history.items():
|
| 292 |
+
if name != self.memory.load_variable('name'):
|
| 293 |
+
for i, speak in enumerate(speaks):
|
| 294 |
+
if speak.strip():
|
| 295 |
+
prompt.append(f"[轮次{i+1}] {name}: {speak}\n")
|
| 296 |
+
|
| 297 |
+
prompt.append(f'\n基于"{general_concept}"这个概念,请分析出两个可能的更具体词语:')
|
| 298 |
+
prompt.append('1. 多数人可能持有的具体词语(平民词)')
|
| 299 |
+
prompt.append('2. 少数人可能持有的相关但不同的词语(卧底词)')
|
| 300 |
+
prompt.append('\n请用JSON格式回答:{"possible_civilian": "具体平民词", "possible_spy": "具体卧底词"}')
|
| 301 |
+
|
| 302 |
+
result = self.llm_caller(''.join(prompt))
|
| 303 |
+
logger.info(f"词语细化分析原始返回: '{result}'")
|
| 304 |
+
|
| 305 |
+
try:
|
| 306 |
+
# 尝试提取JSON内容
|
| 307 |
+
import json
|
| 308 |
+
import re
|
| 309 |
+
|
| 310 |
+
json_match = re.search(r'{.*}', result, re.DOTALL)
|
| 311 |
+
if json_match:
|
| 312 |
+
json_str = json_match.group(0)
|
| 313 |
+
return json.loads(json_str)
|
| 314 |
+
except Exception as e:
|
| 315 |
+
logger.error(f"词语细化分析JSON解析失败: {e}")
|
| 316 |
+
|
| 317 |
return None
|
| 318 |
def detect_civilian_word(self):
|
| 319 |
"""分析历史发言,尝试识别平民词"""
|
|
|
|
| 403 |
# 在发言前更新对手词
|
| 404 |
civilian_word = self.detect_civilian_word()
|
| 405 |
spy_word = self.detect_spy_word()
|
| 406 |
+
# 添加这段处理逻辑
|
| 407 |
+
if civilian_word and spy_word and civilian_word == spy_word:
|
| 408 |
+
# 平民词和卧底词推断结果相同的特殊处理
|
| 409 |
+
logger.info(f"检测到平民词'{civilian_word}'与卧底词'{spy_word}'推断结果相同,执行深度分析")
|
| 410 |
+
|
| 411 |
+
refined_analysis = self.analyze_similar_words(civilian_word)
|
| 412 |
+
if refined_analysis:
|
| 413 |
+
# 尝试获取更精确的词语区分
|
| 414 |
+
possible_civilian = refined_analysis.get("possible_civilian")
|
| 415 |
+
possible_spy = refined_analysis.get("possible_spy")
|
| 416 |
+
|
| 417 |
+
if possible_civilian and possible_civilian != civilian_word:
|
| 418 |
+
logger.info(f"细化分析后的平民词: '{possible_civilian}'")
|
| 419 |
+
self.memory.set_variable('sim_word', possible_civilian)
|
| 420 |
+
civilian_word = possible_civilian
|
| 421 |
+
|
| 422 |
+
if possible_spy and possible_spy != spy_word:
|
| 423 |
+
logger.info(f"细化分析后的卧底词: '{possible_spy}'")
|
| 424 |
+
self.memory.set_variable('spy_word', possible_spy)
|
| 425 |
+
spy_word = possible_spy
|
| 426 |
if civilian_word:
|
| 427 |
self.memory.set_variable('sim_word', civilian_word)
|
| 428 |
|