gpt4free/g4f/client/helper.py

16 lines
379 B
Python
Raw Normal View History

2024-04-06 18:25:28 +00:00
import re
def read_json(text: str) -> dict:
"""
Parses JSON code block from a string.
Args:
text (str): A string containing a JSON code block.
Returns:
dict: A dictionary parsed from the JSON code block.
"""
match = re.search(r"```(json|)\n(?P<code>[\S\s]+?)\n```", text)
if match:
return match.group("code")
return text