in src/alpaca_eval/completion_parsers.py [0:0]
def json_parser(completion: str, annotation_key: Optional[str]) -> list[Any]:
r"""Parse the completion by reading it as a JSON and selecting "annotation_key".
Examples
--------
>>> completion = '{"short_explanation": "that is why", "is_incorporated": true}'
>>> json_parser(completion, "is_incorporated")
[True]
>>> completion = '[{"short_explanation": "that is why", "is_incorporated": true}, {"is_incorporated": false}]'
>>> json_parser(completion, "is_incorporated")
[True, False]
>>> completion = 'blah ```json\n{"short_explanation": "that is why", "integer": 1}```'
>>> json_parser(completion, "integer")
[1]
"""
if completion == "":
raise ValueError("The completion is empty.")
# search for a pattern "```json{...}```" and take what is inside the curly brackets
if "```json" in completion:
completion = re.search(r"```json(.*?)```", completion, re.DOTALL).group(1)
json_loaded = json.loads(completion)
if isinstance(json_loaded, dict):
return [json_loaded[annotation_key] if annotation_key is not None else json_loaded]
return [d[annotation_key] if annotation_key is not None else d for d in json.loads(completion)]