|
|
import re |
|
|
from typing import Dict, List, Any, Optional |
|
|
|
|
|
def parse_conversation(raw: str) -> Dict[str, Any]: |
|
|
""" |
|
|
Parse a single conversation payload. |
|
|
|
|
|
- Everything before the `<dialogue>` tag becomes the system prompt. |
|
|
- Inside <dialogue>…</dialogue>, lines starting with `User:` or `Assistant:` open a new turn. |
|
|
- Lines starting with `<alt>` are stored as alternatives on the *current* turn, under key "alt". |
|
|
- <code>…</code> blocks are embedded into the current turn as a fenced code block (```). |
|
|
|
|
|
Returns: |
|
|
{ |
|
|
"system": str, |
|
|
"messages": [ |
|
|
{"role": "user"|"assistant", "content": str, "alt": [str, ...]?}, |
|
|
... |
|
|
] |
|
|
} |
|
|
""" |
|
|
|
|
|
start = raw.find("<dialogue>") |
|
|
if start == -1: |
|
|
raise ValueError("No <dialogue> tag found.") |
|
|
end = raw.find("</dialogue>", start) |
|
|
if end == -1: |
|
|
|
|
|
end = len(raw) |
|
|
|
|
|
system_prompt = raw[:start].strip() |
|
|
dialogue = raw[start + len("<dialogue>"):end].strip("\n") |
|
|
|
|
|
|
|
|
messages: List[Dict[str, Any]] = [] |
|
|
current_role: Optional[str] = None |
|
|
current_lines: List[str] = [] |
|
|
current_alts: List[str] = [] |
|
|
|
|
|
def flush(): |
|
|
nonlocal current_role, current_lines, current_alts |
|
|
if current_role is not None: |
|
|
msg: Dict[str, Any] = { |
|
|
"role": current_role, |
|
|
"content": "\n".join(current_lines).strip() |
|
|
} |
|
|
if current_alts: |
|
|
msg["alt"] = current_alts[:] |
|
|
messages.append(msg) |
|
|
current_role = None |
|
|
current_lines = [] |
|
|
current_alts = [] |
|
|
|
|
|
|
|
|
i = 0 |
|
|
lines = dialogue.splitlines() |
|
|
while i < len(lines): |
|
|
line = lines[i] |
|
|
|
|
|
|
|
|
m_user = re.match(r'^\s*User:\s*(.*)$', line) |
|
|
m_assistant = re.match(r'^\s*Assistant:\s*(.*)$', line) |
|
|
|
|
|
if m_user or m_assistant: |
|
|
|
|
|
flush() |
|
|
current_role = "user" if m_user else "assistant" |
|
|
first_text = (m_user or m_assistant).group(1) |
|
|
current_lines.append(first_text) |
|
|
i += 1 |
|
|
continue |
|
|
|
|
|
|
|
|
m_alt = re.match(r'^\s*<alt>\s*(.*)$', line) |
|
|
if m_alt: |
|
|
|
|
|
if current_role is not None: |
|
|
current_alts.append(m_alt.group(1)) |
|
|
i += 1 |
|
|
continue |
|
|
|
|
|
|
|
|
if "<code>" in line: |
|
|
|
|
|
code_content_lines: List[str] = [] |
|
|
|
|
|
after_open = line.split("<code>", 1)[1] |
|
|
|
|
|
if "</code>" in after_open: |
|
|
inside, after = after_open.split("</code>", 1) |
|
|
code_content_lines.append(inside) |
|
|
code_text = "\n".join(code_content_lines).strip("\n") |
|
|
current_lines.append("```python\n" + code_text + "\n```") |
|
|
|
|
|
if after.strip(): |
|
|
current_lines.append(after.strip()) |
|
|
i += 1 |
|
|
continue |
|
|
else: |
|
|
|
|
|
code_content_lines.append(after_open) |
|
|
i += 1 |
|
|
found_close = False |
|
|
while i < len(lines): |
|
|
if "</code>" in lines[i]: |
|
|
before_close, after = lines[i].split("</code>", 1) |
|
|
code_content_lines.append(before_close) |
|
|
code_text = "\n".join(code_content_lines).strip("\n") |
|
|
current_lines.append("```python\n" + code_text + "\n```") |
|
|
if after.strip(): |
|
|
current_lines.append(after.strip()) |
|
|
found_close = True |
|
|
i += 1 |
|
|
break |
|
|
else: |
|
|
code_content_lines.append(lines[i]) |
|
|
i += 1 |
|
|
if not found_close: |
|
|
|
|
|
code_text = "\n".join(code_content_lines).strip("\n") |
|
|
current_lines.append("```python\n" + code_text + "\n```") |
|
|
continue |
|
|
|
|
|
|
|
|
if current_role is not None: |
|
|
current_lines.append(line) |
|
|
|
|
|
|
|
|
i += 1 |
|
|
|
|
|
|
|
|
flush() |
|
|
|
|
|
return { |
|
|
"system": system_prompt, |
|
|
"messages": messages |
|
|
} |
|
|
|
|
|
|