Implement dynamic graph memory update feature for simulations
- Added a new optional parameter `enable_graph_memory_update` to the simulation API, allowing real-time updates of agent activities to the Zep knowledge graph. - Introduced `ZepGraphMemoryUpdater` and `ZepGraphMemoryManager` classes to handle the background processing of activity updates, ensuring efficient API calls and data management. - Updated the README.md to include detailed instructions on the new graph memory update functionality and its configuration. - Enhanced the simulation runner to manage the lifecycle of the graph memory updater, including starting and stopping the updater based on user configuration. - Improved logging to track the status of graph memory updates, providing better insights into the simulation process.
This commit is contained in:
@@ -20,6 +20,7 @@ from queue import Queue
|
||||
|
||||
from ..config import Config
|
||||
from ..utils.logger import get_logger
|
||||
from .zep_graph_memory_updater import ZepGraphMemoryManager
|
||||
|
||||
logger = get_logger('mirofish.simulation_runner')
|
||||
|
||||
@@ -201,6 +202,9 @@ class SimulationRunner:
|
||||
_stdout_files: Dict[str, Any] = {} # 存储 stdout 文件句柄
|
||||
_stderr_files: Dict[str, Any] = {} # 存储 stderr 文件句柄
|
||||
|
||||
# 图谱记忆更新配置
|
||||
_graph_memory_enabled: Dict[str, bool] = {} # simulation_id -> enabled
|
||||
|
||||
@classmethod
|
||||
def get_run_state(cls, simulation_id: str) -> Optional[SimulationRunState]:
|
||||
"""获取运行状态"""
|
||||
@@ -281,7 +285,9 @@ class SimulationRunner:
|
||||
cls,
|
||||
simulation_id: str,
|
||||
platform: str = "parallel", # twitter / reddit / parallel
|
||||
max_rounds: int = None # 最大模拟轮数(可选,用于截断过长的模拟)
|
||||
max_rounds: int = None, # 最大模拟轮数(可选,用于截断过长的模拟)
|
||||
enable_graph_memory_update: bool = False, # 是否将活动更新到Zep图谱
|
||||
graph_id: str = None # Zep图谱ID(启用图谱更新时必需)
|
||||
) -> SimulationRunState:
|
||||
"""
|
||||
启动模拟
|
||||
@@ -290,6 +296,8 @@ class SimulationRunner:
|
||||
simulation_id: 模拟ID
|
||||
platform: 运行平台 (twitter/reddit/parallel)
|
||||
max_rounds: 最大模拟轮数(可选,用于截断过长的模拟)
|
||||
enable_graph_memory_update: 是否将Agent活动动态更新到Zep图谱
|
||||
graph_id: Zep图谱ID(启用图谱更新时必需)
|
||||
|
||||
Returns:
|
||||
SimulationRunState
|
||||
@@ -332,6 +340,21 @@ class SimulationRunner:
|
||||
|
||||
cls._save_run_state(state)
|
||||
|
||||
# 如果启用图谱记忆更新,创建更新器
|
||||
if enable_graph_memory_update:
|
||||
if not graph_id:
|
||||
raise ValueError("启用图谱记忆更新时必须提供 graph_id")
|
||||
|
||||
try:
|
||||
ZepGraphMemoryManager.create_updater(simulation_id, graph_id)
|
||||
cls._graph_memory_enabled[simulation_id] = True
|
||||
logger.info(f"已启用图谱记忆更新: simulation_id={simulation_id}, graph_id={graph_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"创建图谱记忆更新器失败: {e}")
|
||||
cls._graph_memory_enabled[simulation_id] = False
|
||||
else:
|
||||
cls._graph_memory_enabled[simulation_id] = False
|
||||
|
||||
# 确定运行哪个脚本(脚本位于 backend/scripts/ 目录)
|
||||
if platform == "twitter":
|
||||
script_name = "run_twitter_simulation.py"
|
||||
@@ -489,6 +512,15 @@ class SimulationRunner:
|
||||
cls._save_run_state(state)
|
||||
|
||||
finally:
|
||||
# 停止图谱记忆更新器
|
||||
if cls._graph_memory_enabled.get(simulation_id, False):
|
||||
try:
|
||||
ZepGraphMemoryManager.stop_updater(simulation_id)
|
||||
logger.info(f"已停止图谱记忆更新: simulation_id={simulation_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"停止图谱记忆更新器失败: {e}")
|
||||
cls._graph_memory_enabled.pop(simulation_id, None)
|
||||
|
||||
# 清理进程资源
|
||||
cls._processes.pop(simulation_id, None)
|
||||
cls._action_queues.pop(simulation_id, None)
|
||||
@@ -527,6 +559,12 @@ class SimulationRunner:
|
||||
Returns:
|
||||
新的读取位置
|
||||
"""
|
||||
# 检查是否启用了图谱记忆更新
|
||||
graph_memory_enabled = cls._graph_memory_enabled.get(state.simulation_id, False)
|
||||
graph_updater = None
|
||||
if graph_memory_enabled:
|
||||
graph_updater = ZepGraphMemoryManager.get_updater(state.simulation_id)
|
||||
|
||||
try:
|
||||
with open(log_path, 'r', encoding='utf-8') as f:
|
||||
f.seek(position)
|
||||
@@ -557,6 +595,10 @@ class SimulationRunner:
|
||||
if action.round_num and action.round_num > state.current_round:
|
||||
state.current_round = action.round_num
|
||||
|
||||
# 如果启用了图谱记忆更新,将活动发送到Zep
|
||||
if graph_updater:
|
||||
graph_updater.add_activity_from_dict(action_data, platform)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return f.tell()
|
||||
@@ -615,6 +657,15 @@ class SimulationRunner:
|
||||
state.completed_at = datetime.now().isoformat()
|
||||
cls._save_run_state(state)
|
||||
|
||||
# 停止图谱记忆更新器
|
||||
if cls._graph_memory_enabled.get(simulation_id, False):
|
||||
try:
|
||||
ZepGraphMemoryManager.stop_updater(simulation_id)
|
||||
logger.info(f"已停止图谱记忆更新: simulation_id={simulation_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"停止图谱记忆更新器失败: {e}")
|
||||
cls._graph_memory_enabled.pop(simulation_id, None)
|
||||
|
||||
logger.info(f"模拟已停止: {simulation_id}")
|
||||
return state
|
||||
|
||||
@@ -811,6 +862,14 @@ class SimulationRunner:
|
||||
"""
|
||||
logger.info("正在清理所有模拟进程...")
|
||||
|
||||
# 首先停止所有图谱记忆更新器
|
||||
try:
|
||||
ZepGraphMemoryManager.stop_all()
|
||||
logger.info("已停止所有图谱记忆更新器")
|
||||
except Exception as e:
|
||||
logger.error(f"停止图谱记忆更新器失败: {e}")
|
||||
cls._graph_memory_enabled.clear()
|
||||
|
||||
# 复制字典以避免在迭代时修改
|
||||
processes = list(cls._processes.items())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user