Spaces:
Paused
Paused
| import re | |
| import json | |
| import ast | |
| def extract_json(text: str) -> dict: | |
| try: | |
| if isinstance(text, dict) or isinstance(text, list): | |
| return text | |
| filtered_text = "" | |
| filtered_text = re.sub(r'^.*?({|\[)', r'\1', text, flags=re.DOTALL) | |
| filtered_text = re.sub(r'(}|\])(?!.*(\]|\})).*$', r'\1', filtered_text, flags=re.DOTALL) | |
| try: | |
| parsed = json.loads(filtered_text, strict=False) | |
| except json.JSONDecodeError: | |
| try: | |
| clean_text = re.sub(r"\\'", "'", filtered_text) | |
| clean_text = re.sub(r"\\,", ",", clean_text) | |
| parsed = ast.literal_eval(clean_text) | |
| except: | |
| converted_text = re.sub(r"'([^']*)'", r'"\1"', filtered_text) | |
| parsed = json.loads(converted_text, strict=False) | |
| return parsed | |
| except Exception as e: | |
| return {} | |
| def _coerce_to_list(llm_output:str): | |
| if isinstance(llm_output, list): | |
| return llm_output | |
| try: | |
| result = extract_json(llm_output) | |
| if result == {} and not (isinstance(llm_output, str) and ('[' in llm_output and ']' in llm_output)): | |
| raise ValueError("Cannot convert the LLM output to a list value.") | |
| if not isinstance(result, list): | |
| raise ValueError(f"Cannot convert the LLM output to a list value. Got {type(result)}") | |
| return result | |
| except Exception as e: | |
| raise ValueError(f"Cannot convert the LLM output to a list value. Underlying error: {e}") | |
| print("Test 1:", _coerce_to_list("""```json | |
| [ | |
| {"key": "value"} | |
| ] | |
| ```""")) | |