Download code/graph/parsing.py from frontal-labs/graph-reasoner-typed-compositional: direct link, hf CLI and curl.
- Browser
- Download file 877 Bytes
-
https://huggingface.co/frontal-labs/graph-reasoner-typed-compositional/resolve/main/code/graph/parsing.py
- Command line
-
hf download hf://frontal-labs/graph-reasoner-typed-compositional/code/graph/parsing.py
-
curl -L -o parsing.py https://huggingface.co/frontal-labs/graph-reasoner-typed-compositional/resolve/main/code/graph/parsing.py
877 Bytes
| """Parse structured values produced by a language model, without executing them.""" | |
| from __future__ import annotations | |
| import ast | |
| import json | |
| from typing import Any | |
| def parse_literal(text: str) -> Any: | |
| """Parse JSON, or a Python literal, from ``text``; never evaluate code. | |
| JSON is tried first because it is what the models are asked for; it also | |
| accepts ``null``/``true``/``false``, which Python literals do not. A Python | |
| literal (single quotes, for instance) is the fallback. Anything else raises | |
| ``ValueError``, so callers' existing handlers apply unchanged. | |
| """ | |
| try: | |
| return json.loads(text) | |
| except ValueError: | |
| pass | |
| try: | |
| return ast.literal_eval(text) | |
| except (ValueError, SyntaxError, TypeError, MemoryError, RecursionError) as error: | |
| raise ValueError(f"not a literal value: {error}") from error | |