|
1 | 1 | import json
|
| 2 | +import re |
2 | 3 |
|
3 | 4 | import yaml
|
4 | 5 | from pydantic import ValidationError
|
@@ -48,4 +49,62 @@ def json_to_md(json_str: str) -> str:
|
48 | 49 | return s
|
49 | 50 |
|
50 | 51 |
|
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