mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-12 01:54:45 +08:00
Implement a memory system that stores user context and conversation history in memory.json, uses LLM to summarize conversations, and injects relevant context into system prompts for personalized responses. Key components: - MemoryConfig for configuration management - MemoryUpdateQueue with debounce for batch processing - MemoryUpdater for LLM-based memory extraction - MemoryMiddleware to queue conversations after agent execution - Memory injection into lead agent system prompt Note: Add memory section to config.yaml to enable (see config.example.yaml) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
"""Configuration for memory mechanism."""
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class MemoryConfig(BaseModel):
|
|
"""Configuration for global memory mechanism."""
|
|
|
|
enabled: bool = Field(
|
|
default=True,
|
|
description="Whether to enable memory mechanism",
|
|
)
|
|
storage_path: str = Field(
|
|
default=".deer-flow/memory.json",
|
|
description="Path to store memory data (relative to backend directory)",
|
|
)
|
|
debounce_seconds: int = Field(
|
|
default=30,
|
|
ge=1,
|
|
le=300,
|
|
description="Seconds to wait before processing queued updates (debounce)",
|
|
)
|
|
model_name: str | None = Field(
|
|
default=None,
|
|
description="Model name to use for memory updates (None = use default model)",
|
|
)
|
|
max_facts: int = Field(
|
|
default=100,
|
|
ge=10,
|
|
le=500,
|
|
description="Maximum number of facts to store",
|
|
)
|
|
fact_confidence_threshold: float = Field(
|
|
default=0.7,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="Minimum confidence threshold for storing facts",
|
|
)
|
|
injection_enabled: bool = Field(
|
|
default=True,
|
|
description="Whether to inject memory into system prompt",
|
|
)
|
|
max_injection_tokens: int = Field(
|
|
default=2000,
|
|
ge=100,
|
|
le=8000,
|
|
description="Maximum tokens to use for memory injection",
|
|
)
|
|
|
|
|
|
# Global configuration instance
|
|
_memory_config: MemoryConfig = MemoryConfig()
|
|
|
|
|
|
def get_memory_config() -> MemoryConfig:
|
|
"""Get the current memory configuration."""
|
|
return _memory_config
|
|
|
|
|
|
def set_memory_config(config: MemoryConfig) -> None:
|
|
"""Set the memory configuration."""
|
|
global _memory_config
|
|
_memory_config = config
|
|
|
|
|
|
def load_memory_config_from_dict(config_dict: dict) -> None:
|
|
"""Load memory configuration from a dictionary."""
|
|
global _memory_config
|
|
_memory_config = MemoryConfig(**config_dict)
|