tags
"""
def wrap_text(text: str, config: VisualizationConfig) -> str:
"""Wrap text to fit within box constraints"""
text = text.replace('\n', ' ').replace('"', "'")
wrapped_lines = textwrap.wrap(text, width=config.max_chars_per_line)
if len(wrapped_lines) > config.max_lines:
# Option 1: Simply truncate and add ellipsis to the last line
wrapped_lines = wrapped_lines[:config.max_lines]
wrapped_lines[-1] = wrapped_lines[-1][:config.max_chars_per_line-3] + "..."
# Option 2 (alternative): Include part of the next line to show continuity
# original_next_line = wrapped_lines[config.max_lines] if len(wrapped_lines) > config.max_lines else ""
# wrapped_lines = wrapped_lines[:config.max_lines-1]
# wrapped_lines.append(original_next_line[:config.max_chars_per_line-3] + "...")
return "
".join(wrapped_lines)
def parse_cot_response(response_text: str, question: str) -> CoTResponse:
"""
Parse CoT response text to extract steps and final answer.
Args:
response_text: The raw response from the API
question: The original question
Returns:
CoTResponse object containing question, steps, and answer
"""
# Extract all steps
step_pattern = r'\s*(.*?)\s*'
steps = []
for match in re.finditer(step_pattern, response_text, re.DOTALL):
number = int(match.group(1))
content = match.group(2).strip()
steps.append(CoTStep(number=number, content=content))
# Extract answer
answer_pattern = r'\s*(.*?)\s*'
answer_match = re.search(answer_pattern, response_text, re.DOTALL)
answer = answer_match.group(1).strip() if answer_match else None
# Sort steps by number
steps.sort(key=lambda x: x.number)
return CoTResponse(question=question, steps=steps, answer=answer)
def create_mermaid_diagram(cot_response: CoTResponse, config: VisualizationConfig) -> str:
"""
Convert CoT steps to Mermaid diagram with improved text wrapping.
Args:
cot_response: CoTResponse object containing the reasoning steps
config: VisualizationConfig for text formatting
Returns:
Mermaid diagram markup as a string
"""
diagram = ['', 'graph TD']
# Add question node
question_content = wrap_text(cot_response.question, config)
diagram.append(f' Q["{question_content}"]')
# Add steps with wrapped text and connect them
if cot_response.steps:
# Connect question to first step
diagram.append(f' Q --> S{cot_response.steps[0].number}')
# Add all steps
for i, step in enumerate(cot_response.steps):
content = wrap_text(step.content, config)
node_id = f'S{step.number}'
diagram.append(f' {node_id}["{content}"]')
# Connect steps sequentially
if i < len(cot_response.steps) - 1:
next_id = f'S{cot_response.steps[i + 1].number}'
diagram.append(f' {node_id} --> {next_id}')
# Add final answer node
if cot_response.answer:
answer = wrap_text(cot_response.answer, config)
diagram.append(f' A["{answer}"]')
if cot_response.steps:
diagram.append(f' S{cot_response.steps[-1].number} --> A')
else:
diagram.append(' Q --> A')
# Add styles for better visualization
diagram.extend([
' classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px;',
' classDef question fill:#e3f2fd,stroke:#1976d2,stroke-width:2px;',
' classDef answer fill:#d4edda,stroke:#28a745,stroke-width:2px;',
' class Q question;',
' class A answer;',
' linkStyle default stroke:#666,stroke-width:2px;'
])
diagram.append('
')
return '\n'.join(diagram)