Convert response string to JSON, with error handling and fallback to non-standard JSON extraction.
(response: str)
| 142 | |
| 143 | |
| 144 | def convert_response_to_json(response: str) -> dict: |
| 145 | """Convert response string to JSON, with error handling and fallback to non-standard JSON extraction.""" |
| 146 | prediction_json = extract_first_complete_json(response) |
| 147 | |
| 148 | if prediction_json is None: |
| 149 | logger.info("Attempting to extract values from a non-standard JSON string...") |
| 150 | prediction_json = extract_values_from_json(response, allow_no_quotes=True) |
| 151 | |
| 152 | if not prediction_json: |
| 153 | logger.error("Unable to extract meaningful data from the response.") |
| 154 | else: |
| 155 | logger.info("JSON data successfully extracted.") |
| 156 | |
| 157 | return prediction_json |
| 158 | def encode_string_by_tiktoken(content: str, model_name: str = "gpt-4o"): |
| 159 | global ENCODER |
| 160 | if ENCODER is None: |
no test coverage detected