-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_parser.py
More file actions
207 lines (167 loc) · 6.53 KB
/
chat_parser.py
File metadata and controls
207 lines (167 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Chat log parsing utilities for SillyLoresmith."""
import json
import re
from helpers import STATUS_LOG
def parse_sillytavern_jsonl(content):
"""
Parse SillyTavern JSONL chat format.
Line 1 = metadata (chat info)
Lines 2+ = individual messages
Returns (messages, metadata)
"""
lines = content.strip().split('\n')
if not lines:
return [], {}
metadata = {}
messages = []
for i, line in enumerate(lines):
if not line.strip():
continue
try:
data = json.loads(line)
# First line is usually metadata
if i == 0 and "user_name" in data:
metadata = data
continue
# Parse message
msg = {
"name": data.get("name", data.get("sender", "Unknown")),
"text": data.get("mes", data.get("message", data.get("content", ""))),
"is_user": data.get("is_user", data.get("role") == "user"),
"timestamp": data.get("send_date", data.get("timestamp", "")),
}
if msg["text"]:
messages.append(msg)
except json.JSONDecodeError:
# Not valid JSON, skip
continue
STATUS_LOG.info(f"Parsed JSONL: {len(messages)} messages")
return messages, metadata
def parse_sillytavern_json(content):
"""
Parse SillyTavern JSON chat format (array or object with messages).
Returns (messages, metadata)
"""
try:
data = json.loads(content)
except json.JSONDecodeError as e:
STATUS_LOG.error(f"Invalid JSON: {e}")
return [], {}
metadata = {}
messages = []
# Handle different formats
if isinstance(data, list):
# Array of messages
msg_list = data
elif isinstance(data, dict):
# Object with messages array
metadata = {k: v for k, v in data.items() if k not in ("messages", "chat", "data")}
msg_list = data.get("messages", data.get("chat", data.get("data", [])))
if isinstance(msg_list, dict):
msg_list = list(msg_list.values())
else:
return [], {}
for item in msg_list:
if isinstance(item, dict):
msg = {
"name": item.get("name", item.get("sender", item.get("role", "Unknown"))),
"text": item.get("mes", item.get("message", item.get("content", ""))),
"is_user": item.get("is_user", item.get("role") == "user"),
"timestamp": item.get("send_date", item.get("timestamp", "")),
}
if msg["text"]:
messages.append(msg)
elif isinstance(item, str):
# Plain string message
messages.append({"name": "Unknown", "text": item, "is_user": False})
STATUS_LOG.info(f"Parsed JSON: {len(messages)} messages")
return messages, metadata
def parse_plain_text_chat(content):
"""
Parse plain text chat format (Name: Message).
Returns (messages, metadata)
"""
messages = []
# Common patterns for chat messages
patterns = [
# "Name: Message" format
r'^([A-Za-z0-9_\-\s]{1,30}):\s*(.+?)(?=\n[A-Za-z0-9_\-\s]{1,30}:|\Z)',
# "[Name] Message" format
r'^\[([A-Za-z0-9_\-\s]{1,30})\]\s*(.+?)(?=\n\[[A-Za-z0-9_\-\s]{1,30}\]|\Z)',
# "<Name> Message" format
r'^<([A-Za-z0-9_\-\s]{1,30})>\s*(.+?)(?=\n<[A-Za-z0-9_\-\s]{1,30}>|\Z)',
]
for pattern in patterns:
matches = re.findall(pattern, content, re.MULTILINE | re.DOTALL)
if matches and len(matches) > 2: # Found at least a few messages
for name, text in matches:
text = text.strip()
if text:
messages.append({
"name": name.strip(),
"text": text,
"is_user": False, # Can't determine from plain text
"timestamp": "",
})
STATUS_LOG.info(f"Parsed plain text: {len(messages)} messages")
return messages, {}
# Fallback: Split by double newlines and treat as alternating messages
paragraphs = [p.strip() for p in content.split('\n\n') if p.strip()]
for i, para in enumerate(paragraphs):
messages.append({
"name": f"Speaker {i % 2 + 1}",
"text": para,
"is_user": i % 2 == 0,
"timestamp": "",
})
STATUS_LOG.info(f"Parsed as paragraphs: {len(messages)} messages")
return messages, {}
def parse_chat_log(content, format_hint=None):
"""
Auto-detect and parse chat log format.
format_hint: "jsonl", "json", "text", or None for auto-detect
Returns (messages, metadata)
"""
content = content.strip()
if not content:
return [], {}
# Try JSONL first (most specific)
if format_hint == "jsonl" or (format_hint is None and content.startswith('{')):
try:
# Check if it's JSONL (multiple JSON objects per line)
lines = content.split('\n')
if len(lines) > 1:
json.loads(lines[0])
json.loads(lines[1]) if len(lines) > 1 else None
messages, metadata = parse_sillytavern_jsonl(content)
if messages:
return messages, metadata
except Exception:
pass
# Try JSON
if format_hint == "json" or (format_hint is None and content.startswith(('[', '{'))):
try:
messages, metadata = parse_sillytavern_json(content)
if messages:
return messages, metadata
except Exception:
pass
# Fall back to plain text
return parse_plain_text_chat(content)
def extract_character_names(messages):
"""Extract unique character names from messages."""
names = set()
for msg in messages:
name = msg.get("name", "").strip()
if name and name.lower() not in ("unknown", "system", "narrator"):
names.add(name)
return sorted(names)
def filter_messages_by_character(messages, character_name):
"""Filter messages to only those from a specific character."""
return [m for m in messages
if m.get("name", "").lower() == character_name.lower()]
def get_message_context(messages, index, window=3):
"""Get context around a specific message."""
start = max(0, index - window)
end = min(len(messages), index + window + 1)
return messages[start:end]