Naive implementation of MemReader.
| 165 | |
| 166 | |
| 167 | class SimpleStructMemReader(BaseMemReader, ABC): |
| 168 | """Naive implementation of MemReader.""" |
| 169 | |
| 170 | def __init__(self, config: SimpleStructMemReaderConfig): |
| 171 | """ |
| 172 | Initialize the NaiveMemReader with configuration. |
| 173 | |
| 174 | Args: |
| 175 | config: Configuration object for the reader |
| 176 | """ |
| 177 | self.config = config |
| 178 | # Main LLM for chat/doc memory extraction (fine-tuned model) |
| 179 | self.llm = LLMFactory.from_config(config.llm) |
| 180 | # General LLM for non-chat/doc tasks (hallucination filter, rewrite, merge, etc.) |
| 181 | # Falls back to main llm if not configured |
| 182 | self.general_llm = ( |
| 183 | LLMFactory.from_config(config.general_llm) |
| 184 | if config.general_llm is not None |
| 185 | else self.llm |
| 186 | ) |
| 187 | preference_extractor_llm_config = getattr(config, "preference_extractor_llm", None) |
| 188 | self.preference_extractor_llm = ( |
| 189 | LLMFactory.from_config(preference_extractor_llm_config) |
| 190 | if preference_extractor_llm_config is not None |
| 191 | else self.general_llm |
| 192 | ) |
| 193 | self.qwen_llm = None |
| 194 | qwen_llm_config = getattr(config, "qwen_llm", None) |
| 195 | if qwen_llm_config: |
| 196 | try: |
| 197 | if isinstance(qwen_llm_config, dict): |
| 198 | qwen_llm_config = LLMConfigFactory.model_validate(qwen_llm_config) |
| 199 | self.qwen_llm = LLMFactory.from_config(qwen_llm_config) |
| 200 | except Exception as e: |
| 201 | logger.warning(f"[LLM] Qwen initialization failed: {e}") |
| 202 | self.embedder = EmbedderFactory.from_config(config.embedder) |
| 203 | self.chunker = ChunkerFactory.from_config(config.chunker) |
| 204 | self.save_rawfile = self.chunker.config.save_rawfile |
| 205 | self.memory_max_length = 8000 |
| 206 | # Use token-based windowing; default to ~5000 tokens if not configured |
| 207 | self.chat_window_max_tokens = getattr(self.config, "chat_window_max_tokens", 1024) |
| 208 | self._count_tokens = count_tokens_text |
| 209 | self.searcher = None |
| 210 | # Initialize graph_db as None, can be set later via set_graph_db for |
| 211 | # recall operations |
| 212 | self.graph_db = None |
| 213 | |
| 214 | def set_graph_db(self, graph_db: "BaseGraphDB | None") -> None: |
| 215 | self.graph_db = graph_db |
| 216 | |
| 217 | def set_searcher(self, searcher: "Searcher | None") -> None: |
| 218 | self.searcher = searcher |
| 219 | |
| 220 | def _make_memory_item( |
| 221 | self, |
| 222 | value: str, |
| 223 | info: dict, |
| 224 | memory_type: str, |
no outgoing calls