day1demo / utils /markdown.py
sg-altis's picture
add initial unit and integration tests, makefile, and more codebase guidelines
9b32d94
Raw History Blame Contribute Delete
4.14 kB
from typing import Any
def convert_dict_to_markdown(data: dict[str, Any], include_flags: bool = False) -> str:
"""
Converts a Python dictionary (with a 'details' key containing a list
of key/description/citation dictionaries) into a formatted markdown string.
Args:
data: A dictionary object with the structure
{'details': [{'key': '...', 'description': '...', 'claims': [...], ...}, ...],
'flags': [...]}
include_flags: Whether to include flags in the markdown output (default: False)
Returns:
A formatted markdown string.
"""
if data is None:
return "No data available"
markdown_output = []
# Process details
details_list = data.get("details", [])
if not isinstance(details_list, list):
return "Error: 'details' key is missing or not a list in the input dictionary."
for item in details_list:
key = item.get("key", "Untitled Section")
description = item.get("description", "No description provided.")
claims = item.get("claims", [])
# Collect citations from claims
citation_ids = []
for claim in claims:
for citation in claim.get("citations", []):
if "id" in citation:
citation_ids.append(str(citation["id"]))
citation_string = ""
if citation_ids:
id_list_str = ", ".join(citation_ids)
citation_string = f" [cite: {id_list_str}]"
# Construct the markdown block
markdown_block = f"## {key}\n"
markdown_block += f"{description}{citation_string}"
markdown_output.append(markdown_block)
# Process flags (only if include_flags is True)
if include_flags:
flags_list = data.get("flags", [])
if flags_list:
markdown_output.append("\n---\n## Flags")
for flag in flags_list:
flag_type = flag.get("flag_type", "Unknown")
summary = flag.get("summary", "No summary")
next_step = flag.get("next_step", "")
flag_block = f"**{flag_type}**: {summary}"
if next_step:
flag_block += f"\n - *Next step*: {next_step}"
markdown_output.append(flag_block)
if markdown_output:
return "\n\n".join(markdown_output)
return "No info found"
def convert_key_debates_to_markdown(data: dict[str, Any]) -> str:
"""
Convert KeyDebates response to markdown format.
"""
if data is None:
return "No data available"
markdown_output = []
debates = data.get("debates", [])
for i, debate in enumerate(debates, 1):
title = debate.get("title", f"Debate {i}")
question = debate.get("question", "")
markdown_output.append(f"# {title}")
markdown_output.append(f"*{question}*\n")
# Bull case
bull_case = debate.get("bull_case", {})
bull_details = bull_case.get("details", [])
if bull_details:
markdown_output.append("## Bull Case")
for detail in bull_details:
key = detail.get("key", "")
desc = detail.get("description", "")
markdown_output.append(f"- **{key}**: {desc}")
# Bear case
bear_case = debate.get("bear_case", {})
bear_details = bear_case.get("details", [])
if bear_details:
markdown_output.append("\n## Bear Case")
for detail in bear_details:
key = detail.get("key", "")
desc = detail.get("description", "")
markdown_output.append(f"- **{key}**: {desc}")
# Flags
flags = debate.get("flags", [])
if flags:
markdown_output.append("\n## Flags")
for flag in flags:
flag_type = flag.get("flag_type", "Unknown")
summary = flag.get("summary", "")
markdown_output.append(f"- **{flag_type}**: {summary}")
markdown_output.append("\n---\n")
return "\n".join(markdown_output) if markdown_output else "No debates found"