Factory class for creating memory configurations.
| 291 | |
| 292 | |
| 293 | class MemoryConfigFactory(BaseConfig): |
| 294 | """Factory class for creating memory configurations.""" |
| 295 | |
| 296 | backend: str = Field("uninitialized", description="Backend for memory") |
| 297 | config: dict[str, Any] = Field({}, description="Configuration for the memory backend") |
| 298 | |
| 299 | backend_to_class: ClassVar[dict[str, Any]] = { |
| 300 | "naive_text": NaiveTextMemoryConfig, |
| 301 | "general_text": GeneralTextMemoryConfig, |
| 302 | "simple_tree_text": SimpleTreeTextMemoryConfig, |
| 303 | "tree_text": TreeTextMemoryConfig, |
| 304 | "pref_text": PreferenceTextMemoryConfig, |
| 305 | "kv_cache": KVCacheMemoryConfig, |
| 306 | "vllm_kv_cache": KVCacheMemoryConfig, # Use same config as kv_cache |
| 307 | "lora": LoRAMemoryConfig, |
| 308 | "uninitialized": UninitializedMemoryConfig, |
| 309 | "mem_feedback": MemFeedbackConfig, |
| 310 | } |
| 311 | |
| 312 | @field_validator("backend") |
| 313 | @classmethod |
| 314 | def validate_backend(cls, backend: str) -> str: |
| 315 | """Validate the backend field.""" |
| 316 | if backend not in cls.backend_to_class: |
| 317 | raise ConfigurationError(f"Invalid backend: {backend}") |
| 318 | return backend |
| 319 | |
| 320 | @model_validator(mode="after") |
| 321 | def create_config(self) -> "MemoryConfigFactory": |
| 322 | config_class = self.backend_to_class[self.backend] |
| 323 | self.config = config_class(**self.config) |
| 324 | return self |