mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-07-20 16:51:07 +02:00
json to markdown
This commit is contained in:
parent
6b0daa0312
commit
d18a419d4b
5 changed files with 113 additions and 11 deletions
|
|
@ -13,6 +13,7 @@ from metagpt.actions import Action, ActionOutput
|
|||
from metagpt.const import WORKSPACE_ROOT
|
||||
from metagpt.logs import logger
|
||||
from metagpt.utils.common import CodeParser
|
||||
from metagpt.utils.json_to_markdown import json_to_markdown
|
||||
from metagpt.utils.mermaid import mermaid_to_file
|
||||
|
||||
PROMPT_TEMPLATE = """
|
||||
|
|
@ -97,8 +98,9 @@ class WriteDesign(Action):
|
|||
quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"]
|
||||
mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis")
|
||||
|
||||
logger.info(f"Saving PRD to {prd_file}")
|
||||
prd_file.write_text(context[-1].content)
|
||||
if context[-1].instruct_content:
|
||||
logger.info(f"Saving PRD to {prd_file}")
|
||||
prd_file.write_text(json_to_markdown(context[-1].instruct_content.dict()))
|
||||
|
||||
def _save_system_design(self, docs_path, resources_path, system_design):
|
||||
data_api_design = system_design.instruct_content.dict()[
|
||||
|
|
@ -111,7 +113,7 @@ class WriteDesign(Action):
|
|||
mermaid_to_file(seq_flow, resources_path / "seq_flow")
|
||||
system_design_file = docs_path / "system_design.md"
|
||||
logger.info(f"Saving System Designs to {system_design_file}")
|
||||
system_design_file.write_text(system_design.content)
|
||||
system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict())))
|
||||
|
||||
def _save(self, context, system_design):
|
||||
if isinstance(system_design, ActionOutput):
|
||||
|
|
@ -128,10 +130,7 @@ class WriteDesign(Action):
|
|||
self._save_system_design(docs_path, resources_path, system_design)
|
||||
|
||||
async def run(self, context):
|
||||
if isinstance(context, ActionOutput):
|
||||
prompt = PROMPT_TEMPLATE.format(context=context.content, format_example=FORMAT_EXAMPLE)
|
||||
else:
|
||||
prompt = PROMPT_TEMPLATE.format(context=context, format_example=FORMAT_EXAMPLE)
|
||||
prompt = PROMPT_TEMPLATE.format(context=context, format_example=FORMAT_EXAMPLE)
|
||||
# system_design = await self._aask(prompt)
|
||||
system_design = await self._aask_json_v1(prompt, "system_design", OUTPUT_MAPPING)
|
||||
self._save(context, system_design)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from typing import List
|
|||
|
||||
from metagpt.actions.action import Action
|
||||
from metagpt.const import WORKSPACE_ROOT
|
||||
from metagpt.utils.json_to_markdown import json_to_markdown
|
||||
|
||||
PROMPT_TEMPLATE = """
|
||||
# Context
|
||||
|
|
@ -86,7 +87,7 @@ class WriteTasks(Action):
|
|||
"Python package name"
|
||||
] # CodeParser.parse_str(block="Python package name", text=context[-1].content)
|
||||
file_path = WORKSPACE_ROOT / ws_name / "docs/api_spec_and_tasks.md"
|
||||
file_path.write_text(rsp.content)
|
||||
file_path.write_text(json_to_markdown(rsp.instruct_content.dict()))
|
||||
|
||||
# Write requirements.txt
|
||||
requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt"
|
||||
|
|
|
|||
42
metagpt/utils/json_to_markdown.py
Normal file
42
metagpt/utils/json_to_markdown.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/9/11 11:50
|
||||
@Author : femto Zheng
|
||||
@File : json_to_markdown.py
|
||||
"""
|
||||
|
||||
|
||||
# since we original write docs/*.md in markdown format, so I convert json back to markdown
|
||||
def json_to_markdown(data, depth=2):
|
||||
"""
|
||||
Convert a JSON object to Markdown with headings for keys and lists for arrays, supporting nested objects.
|
||||
|
||||
Args:
|
||||
data: JSON object (dictionary) or value.
|
||||
depth (int): Current depth level for Markdown headings.
|
||||
|
||||
Returns:
|
||||
str: Markdown representation of the JSON data.
|
||||
"""
|
||||
markdown = ""
|
||||
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
if isinstance(value, list):
|
||||
# Handle JSON arrays
|
||||
markdown += "#" * depth + f" {key}\n\n"
|
||||
items = [str(item) for item in value]
|
||||
markdown += "- " + "\n- ".join(items) + "\n\n"
|
||||
elif isinstance(value, dict):
|
||||
# Handle nested JSON objects
|
||||
markdown += "#" * depth + f" {key}\n\n"
|
||||
markdown += json_to_markdown(value, depth + 1)
|
||||
else:
|
||||
# Handle other values
|
||||
markdown += "#" * depth + f" {key}\n\n{value}\n\n"
|
||||
else:
|
||||
# Handle non-dictionary JSON data
|
||||
markdown = str(data)
|
||||
|
||||
return markdown
|
||||
Loading…
Add table
Add a link
Reference in a new issue