Skip to content

Commit 056d788

Browse files
committed
feat: implement md to json conversion
1 parent 421ff3d commit 056d788

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/chat_completion_md/main.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import re
23

34
import yaml
45
from pydantic import ValidationError
@@ -48,4 +49,62 @@ def json_to_md(json_str: str) -> str:
4849
return s
4950

5051

51-
def md_to_json(md_str: str) -> str: ...
52+
def md_to_json(md_str: str) -> str:
53+
"""Convert Markdown to JSON.
54+
55+
Convert a Markdown string (formatted according to the defined
56+
specification) to a JSON string for performing a request to OpenAI's chat
57+
completion API.
58+
59+
Args:
60+
md_str (str): Markdown string.
61+
62+
Returns:
63+
str : JSON string.
64+
65+
Raises:
66+
ValueError: If the Markdown string is not properly formatted or
67+
messages are missing.
68+
KeyError: If the required keys are not found in the front matter.
69+
"""
70+
pattern = r"\A---\n(.*?)\n---\n(.*)"
71+
match = re.search(pattern, md_str, re.DOTALL)
72+
if match is None:
73+
raise ValueError("Cannot parse Markdown string")
74+
75+
yaml_str = match.group(1)
76+
if not yaml_str:
77+
raise ValueError("Front matter is empty")
78+
llm_request_config = yaml.safe_load(yaml_str)
79+
80+
if "model" not in llm_request_config:
81+
raise KeyError("Model key not found in front matter")
82+
83+
msgs_str = match.group(2)
84+
if not msgs_str:
85+
raise ValueError("Content after front matter is empty")
86+
87+
roles = ["system", "user", "assistant", "developer", "tool"]
88+
pattern = (
89+
fr"\n# ({'|'.join(roles)})\n\n"
90+
fr"(.*?)\n\n---(?=(?:\n\n# (?:{'|'.join(roles)})\n\n|\s*\Z))"
91+
)
92+
93+
messages = [
94+
{"role": match.group(1), "content": match.group(2)}
95+
for match in re.finditer(pattern, msgs_str, re.DOTALL)
96+
]
97+
98+
if not messages:
99+
raise ValueError("No messages found")
100+
101+
json_str = json.dumps(
102+
{**llm_request_config, "messages": messages},
103+
indent=2,
104+
sort_keys=True,
105+
)
106+
return json_str
107+
# %%
108+
109+
# %%
110+

0 commit comments

Comments
 (0)