Persist the memory index to disk
(self)
| 70 | self.save() |
| 71 | |
| 72 | def save(self) -> None: |
| 73 | """Persist the memory index to disk""" |
| 74 | if not self.index_path: |
| 75 | logger.warning("No index_path specified, skipping save") |
| 76 | return |
| 77 | |
| 78 | # Ensure directory exists |
| 79 | os.makedirs(os.path.dirname(self.index_path), exist_ok=True) |
| 80 | |
| 81 | # Prepare data for serialization |
| 82 | data = { |
| 83 | "file_metadata": self.file_metadata, |
| 84 | "contents": [item.to_dict() for item in self.contents], |
| 85 | "config": { |
| 86 | "chunk_size": self.chunk_size, |
| 87 | "chunk_overlap": self.chunk_overlap, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | # Save to JSON |
| 92 | with open(self.index_path, 'w', encoding='utf-8') as f: |
| 93 | json.dump(data, f, indent=2, ensure_ascii=False) |
| 94 | |
| 95 | logger.info(f"Index saved to {self.index_path} ({len(self.contents)} chunks)") |
| 96 | |
| 97 | def retrieve( |
| 98 | self, |
no test coverage detected