Update app.py
Browse files
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('你
|
| 201 |
-
prompt.append('
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
civilian_word = self.llm_caller(''.join(prompt))
|
| 212 |
-
logger.info(f"
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 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))
|