Get or create A-Mem system for the given context.
(self, context_id: int)
| 104 | return self._context_id if self._context_id is not None else 0 |
| 105 | |
| 106 | def _get_memory_system(self, context_id: int): |
| 107 | """Get or create A-Mem system for the given context.""" |
| 108 | system = self._amem_systems.get(context_id) |
| 109 | if system is not None: |
| 110 | return system |
| 111 | |
| 112 | # Convert token limit to char limit |
| 113 | # For Chinese text: ~1.5 chars per token (conservative) |
| 114 | # For English text: ~4 chars per token |
| 115 | # Use 1.5 as conservative estimate for mixed content |
| 116 | max_context_chars = int(self.amem_max_context_tokens * 1.5) |
| 117 | |
| 118 | system = self._amem_class( |
| 119 | model_name=self.amem_embedding_model, |
| 120 | llm_backend=self.amem_backend, |
| 121 | llm_model=self.amem_model, |
| 122 | evo_threshold=self.amem_evo_threshold, |
| 123 | api_key=self._amem_api_key, |
| 124 | api_base=self._amem_api_base, |
| 125 | max_tokens=self.amem_max_tokens, |
| 126 | max_context_chars=max_context_chars, |
| 127 | check_connection=False, |
| 128 | usage_tracker=get_usage_tracker(), |
| 129 | ) |
| 130 | self._amem_systems[context_id] = system |
| 131 | logger.info( |
| 132 | "Created A-Mem system for context %d: model=%s, evo_threshold=%d, max_context_chars=%d", |
| 133 | context_id, self.amem_model, self.amem_evo_threshold, max_context_chars |
| 134 | ) |
| 135 | return system |
| 136 | |
| 137 | def _split_text_into_chunks(self, text: str, max_tokens: int) -> List[str]: |
| 138 | """Split text into chunks based on token count. |
no test coverage detected