Locate the JSON string body from a string
(content: str)
| 46 | |
| 47 | |
| 48 | def locate_json_string_body_from_string(content: str) -> Union[str, None]: |
| 49 | """Locate the JSON string body from a string""" |
| 50 | try: |
| 51 | maybe_json_str = re.search(r"{.*}", content, re.DOTALL) |
| 52 | if maybe_json_str is not None: |
| 53 | maybe_json_str = maybe_json_str.group(0) |
| 54 | maybe_json_str = maybe_json_str.replace("\\n", "") |
| 55 | maybe_json_str = maybe_json_str.replace("\n", "") |
| 56 | maybe_json_str = maybe_json_str.replace("'", '"') |
| 57 | json.loads(maybe_json_str) |
| 58 | return maybe_json_str |
| 59 | except Exception: |
| 60 | pass |
| 61 | # try: |
| 62 | # content = ( |
| 63 | # content.replace(kw_prompt[:-1], "") |
| 64 | # .replace("user", "") |
| 65 | # .replace("model", "") |
| 66 | # .strip() |
| 67 | # ) |
| 68 | # maybe_json_str = "{" + content.split("{")[1].split("}")[0] + "}" |
| 69 | # json.loads(maybe_json_str) |
| 70 | |
| 71 | return None |
| 72 | |
| 73 | |
| 74 | def convert_response_to_json(response: str) -> dict: |
no outgoing calls
no test coverage detected