Update app.py
Browse files46 unmodified lines47484950515253545556575859606162636465666768697071727374757677787944 unmodified lines12412512612712812913013113213313446 unmodified linesself.last_filtered_index = 0 # 记录上次过滤到的位置def filter_new_messages(self):"""只过滤新添加的消息,而不是整个历史记录"""raw_history = self.memory.load_history()# 如果没有新消息,直接返回现有的过滤历史if self.last_filtered_index >= len(raw_history):return "\n".join(self.filtered_history)# 获取新消息new_messages = raw_history[self.last_filtered_index:]# 如果新消息很少,且都是带##标记的裁判消息,可以直接添加if all(("##" in msg and "主持人:" in msg) for msg in new_messages):self.filtered_history.extend(new_messages)self.last_filtered_index = len(raw_history)return "\n".join(self.filtered_history)# 否则,使用LLM过滤新消息new_raw = "\n".join(new_messages)prompt = format_prompt(HISTORY_FILTER_PROMPT, {"raw_history": new_raw})filtered_new = self.llm_caller(prompt)# 更新过滤历史和索引if filtered_new:self.filtered_history.extend(filtered_new.split("\n"))self.last_filtered_index = len(raw_history)# 返回完整的过滤历史return "\n".join(self.filtered_history)def clear_filtered_history(self):"""清空过滤历史(在新游戏开始时调用)"""self.filtered_history = []44 unmodified lineselse:self.memory.append_history('主持人: 无人出局。##')elif req.status == STATUS_RESULT:# 修复错误:将结果消息格式化为主持人发言try:self.memory.append_history('主持人: {}##'.format(req.message))except Exception as e:logger.error("Error appending result message: {}".format(e))else:raise NotImplementedError46 unmodified lines4748495051525354555657585960616263646566676869707172737475767778798081828344 unmodified lines12812913013113213313413513646 unmodified linesself.last_filtered_index = 0 # 记录上次过滤到的位置def filter_new_messages(self):"""只过滤新添加的消息,而不是整个历史记录"""try:raw_history = self.memory.load_history()# 如果没有新消息,直接返回现有的过滤历史if self.last_filtered_index >= len(raw_history):return "\n".join(self.filtered_history)# 获取新消息new_messages = raw_history[self.last_filtered_index:]# 如果新消息很少,且都是带##标记的裁判消息,可以直接添加if all(("##" in msg and "主持人:" in msg) for msg in new_messages):self.filtered_history.extend(new_messages)self.last_filtered_index = len(raw_history)return "\n".join(self.filtered_history)# 否则,使用LLM过滤新消息new_raw = "\n".join(new_messages)prompt = format_prompt(HISTORY_FILTER_PROMPT, {"raw_history": new_raw})filtered_new = self.llm_caller(prompt)# 更新过滤历史和索引if filtered_new:self.filtered_history.extend(filtered_new.split("\n"))self.last_filtered_index = len(raw_history)# 返回完整的过滤历史return "\n".join(self.filtered_history)except AttributeError as e:logger.error(f"Filter error: {e}, returning empty history")return ""def clear_filtered_history(self):"""清空过滤历史(在新游戏开始时调用)"""self.filtered_history = []44 unmodified lineselse:self.memory.append_history('主持人: 无人出局。##')elif req.status == STATUS_RESULT:# 修复问题:确保message是字符串result_message = str(req.message) if req.message else "游戏结束"self.memory.append_history(f'主持人: {result_message}##')else:raise NotImplementedError
app.py+32-30