Store text into LightMem memory system using official turn-by-turn approach. This follows EXACTLY the official LightMem usage pattern from add_locomo.py: 1. Each turn (user + assistant pair) is passed to add_memory separately 2. Only the LAST turn sets force_segment
(self, text: str, **kwargs)
| 959 | content_start = line[3:].strip() # Remove "医生:" prefix |
| 960 | current_content = [content_start] if content_start else [] |
| 961 | |
| 962 | elif line.startswith('[') and line.endswith(']'): |
| 963 | # Skip timestamp lines like [2024-01-05] |
| 964 | continue |
| 965 | elif line.startswith('[') and '健康咨询' in line: |
| 966 | # Skip header lines like [健康咨询记录] or [关于xxx的健康咨询记录] |
| 967 | continue |
| 968 | else: |
| 969 | # Continue current speaker's content |
| 970 | if current_role is not None: |
| 971 | current_content.append(line) |
| 972 | |
| 973 | # Don't forget the last turn |
| 974 | if current_role is not None and current_content: |
| 975 | content = '\n'.join(current_content).strip() |
| 976 | if content: |
| 977 | dialogue_turns.append((current_role, content)) |
| 978 | |
| 979 | # If no dialogue structure found, fall back to treating all text as user message |
| 980 | if not dialogue_turns: |
| 981 | # Remove header/timestamp lines and use remaining content |
| 982 | content_lines = [] |
| 983 | for line in lines: |
| 984 | line = line.strip() |
| 985 | if line and not (line.startswith('[') and (line.endswith(']') or '健康咨询' in line)): |
| 986 | content_lines.append(line) |
| 987 | |
| 988 | if content_lines: |
| 989 | dialogue_turns = [('user', '\n'.join(content_lines))] |
| 990 | |
| 991 | # Convert dialogue turns to LightMem message format |
| 992 | # LightMem expects user+assistant pairs |
| 993 | # |
| 994 | # IMPORTANT: LightMem's SenMemBufferManager uses BERT tokenizer with max_position_embeddings=512. |
| 995 | # If a SINGLE user message exceeds 512 BERT tokens, the segmenter will fail. |
| 996 | # We need to split long messages to ensure each user message is within BERT's limit. |
| 997 | # |
| 998 | # BERT tokenizer for Chinese typically produces ~1.5-2x more tokens than GPT tokenizer, |
| 999 | # so we use a conservative threshold of 250 GPT tokens (≈375-500 BERT tokens). |
| 1000 | MAX_USER_TOKENS = 250 # Conservative threshold for single user message |
| 1001 | |
| 1002 | i = 0 |
| 1003 | while i < len(dialogue_turns): |
| 1004 | role, content = dialogue_turns[i] |
| 1005 | |
| 1006 | if role == 'user': |
| 1007 | # Check if next is assistant |
| 1008 | if i + 1 < len(dialogue_turns) and dialogue_turns[i + 1][0] == 'assistant': |
| 1009 | assistant_content = dialogue_turns[i + 1][1] |
| 1010 | |
| 1011 | # Check if user content needs splitting |
| 1012 | user_tokens = self._llm_client.count_tokens(content) |
| 1013 | if user_tokens > MAX_USER_TOKENS: |
| 1014 | # Split long user message |
| 1015 | user_chunks = self._split_long_message(content, max_tokens=MAX_USER_TOKENS) |
| 1016 | # Pair each user chunk with empty assistant, except last one gets real assistant |
| 1017 | for j, chunk in enumerate(user_chunks): |
| 1018 | messages.append({ |
nothing calls this directly
no test coverage detected